mardi 16 octobre 2018

How to generate random numbers with uniform distribution in Java?

So, i'm having trouble generating random numbers with uniform distribution in java, given the maximum and the minimun value of some attributes in some data set (Iris from UCI for machine learning). What i have is iris dataset, in some 2-d-array called samples. I put the random values according to the maximun and the minimun value of each attribute in iris data set (without the class attribute) in a 2-d-array called gworms (which has some extra fields for some other values of the algorithm).

So far, the full algorithm is not working properly, and my thoughts are in the fact that maybe the gworms (the points in 4-d space) are not generating correctly or with a good randomness. I think that the points are to close to each other (this i think because of some results obtained later whose code is not shown here). So, i'm asking for your help to validate this code in which i implement "uniform distribution" for gworms (for de first 4 positions):

/*
 * To change this license header, choose License Headers in Project 
 Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
 package glowworms;

 import java.lang.Math;
 import java.util.ArrayList;
 import java.util.Random;
 import weka.core.AttributeStats;

 import weka.core.Instances;

/**
*
* @author oscareduardo937
*/

  public class GSO {

/* ************ Initializing parameters of CGSO algorithm ******************** */
int swarmSize = 1000; // Swarm size m
int maxIte = 200;
double stepSize = 0.03; // Step size for the movements
double luciferin = 5.0; // Initial luciferin level
double rho = 0.4; // Luciferin decay parameter
double gamma = 0.6; // Luciferin reinforcement parameter

double rs = 0.38; // Initial radial sensor range. This parameter depends on the data set and needs to be found by running experiments

double gworms[][] = null; // Glowworms of the swarm. 

/* ************ Initializing parameters of clustering problem and data set ******************** */
int numAtt; // Dimension of the position vector
int numClasses; // Number of classes
int total_data; //Number of instances
int threshold = 5;
int runtime = 1;
/*Algorithm can be run many times in order to see its robustness*/

double minValuesAtts[] = new double[this.numAtt]; // Minimum values for all attributes
double maxValuesAtts[] = new double[this.numAtt]; // Maximum values for all attributes

double samples[][] = new double[this.total_data][this.numAtt]; //Samples of the selected dataset.

ArrayList<Integer> candidateList;
double r;

/*a random number in the range [0,1)*/

/* *********** Method to put the instances in a matrix and get max and min values for attributes ******************* */

public void instancesToSamples(Instances data) {
    this.numAtt = data.numAttributes();
    System.out.println("********* NumAttributes: " + this.numAtt);
    AttributeStats attStats = new AttributeStats();
    if (data.classIndex() == -1) {
      //System.out.println("reset index...");
      data.setClassIndex(data.numAttributes() - 1);
    }

    this.numClasses = data.numClasses();
    this.minValuesAtts = new double[this.numAtt];
    this.maxValuesAtts = new double[this.numAtt];

    System.out.println("********* NumClasses: " + this.numClasses);
    this.total_data = data.numInstances();
    samples = new double[this.total_data][this.numAtt];

    double[] values = new double[this.total_data];

    for (int j = 0; j < this.numAtt; j++) {
        values = data.attributeToDoubleArray(j);

        for (int i = 0; i < this.total_data; i++) {
            samples[i][j] = values[i];
        }

    }

    for(int j=0; j<this.numAtt-1; j++){

        attStats = data.attributeStats(j);

        this.maxValuesAtts[j] = attStats.numericStats.max;
        this.minValuesAtts[j] = attStats.numericStats.min;

        //System.out.println("** Min Value Attribute " + j + ": " + this.minValuesAtts[j]);
        //System.out.println("** Max Value Attribute " + j + ": " + this.maxValuesAtts[j]);
    }

    //Checking
    /*for(int i=0; i<this.total_data; i++){
    for(int j=0; j<this.numAtt; j++){
        System.out.print(samples[i][j] + "** ");
      }
            System.out.println();
 }*/ 

} // End of method InstancesToSamples


public void initializeSwarm(Instances data) {
    this.gworms = new double[this.swarmSize][this.numAtt + 2]; // D-dimensional vector plus luciferin, fitness and intradistance.
    double intraDistance = 0;
    Random r = new Random(); //Random r;

    for (int i = 0; i < this.swarmSize; i++) {

        for (int j = 0; j < this.numAtt - 1; j++) {
            //Uniform randomization of d-dimensional position vector
            this.gworms[i][j] = this.minValuesAtts[j] + (this.maxValuesAtts[j] - this.minValuesAtts[j]) * r.nextDouble();   

        }

        this.gworms[i][this.numAtt - 1] = this.luciferin; // Initial luciferin level for all swarm
        this.gworms[i][this.numAtt] = 0; // Initial fitness for all swarm
        this.gworms[i][this.numAtt + 1] = intraDistance; // Intra-distance for gworm i
    }


    //Checking gworms
    /*for(int i=0; i<this.swarmSize; i++){
    for(int j=0; j<this.numAtt+2; j++){
        System.out.print(gworms[i][j] + "** ");
      }
            System.out.println();
 }*/

} // End of method initializeSwarm
    }

The main class is this one:

package uniformrandomization;

 /**
 *
 * @author oscareduardo937
 */

 import java.io.BufferedReader;
 import java.io.FileReader;
 import java.io.FileNotFoundException;

 import weka.core.Instances;
 import glowworms.GSO;


 public class UniformRandomization {

public UniformRandomization(){
    super();
}

//Loading the data from the filename file to the program. It can be .arff or .csv
public static BufferedReader readDataFile(String filename) {
    BufferedReader inputReader = null;

    try {
        inputReader = new BufferedReader(new FileReader(filename));
    } catch (FileNotFoundException ex) {
        System.err.println("File not found: " + filename);
    }

    return inputReader;
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws Exception {
    // TODO code application logic here

    BufferedReader datafile1 = readDataFile("src/data/iris.arff");
    Instances data = new Instances(datafile1);

GSO gso = new GSO();
    gso.instancesToSamples(data);
    gso.initializeSwarm(data);

    System.out.println("Fin...");
}

 }

So i want to know if with this code, the numbers of the position ij of the gworms are generating within the range of max value and min value for attribute j.

Thanks so much in advanced.




Aucun commentaire:

Enregistrer un commentaire