samedi 29 avril 2017

c++ function with random always returns identical object

I am working on code for a genetic algorithm (understanding of GA not required). I have a StackingChromosome class which contains a member variable called 'solution' which is a vector of vectors of floats (a vector of 3 coordinates; x, y and z). I have a ChromosomePopulation class which is a vector of StackingChromosomes. Creating new StackingChromsomes is handled by a ChromosomeFactory class. I am trying to test it with this code (more details after the code block):

cout << "Testing the chromosomes are different: " << endl;

std::vector<std::vector<float>> loopChromosome;
// I have used the name firstgene2 because i have already used firstgene
std::vector<float> firstGene2;
float firstGenePos;

for (int i = 0; i < outputPopulation.getPopulationSize(); i++)
{
    loopChromosome = outputSolutions[i].getSolution();
    for (int j = 0; j < 3; j++)
    {
        firstGene2 = loopChromosome[0];
        firstGenePos = firstGene2[j];
        cout << "chromosome " << to_string(i) << " has at gene position " << to_string(j) << " the value " << to_string(firstGenePos) << endl;
    }
}

outputSolutions if defined by:

ChromosomePopulation outputPopulation = ChromosomePopulation(10);
std::vector<StackingChromosome> outputSolutions = outputPopulation.getPopulation();

ChromosomePopulation sets the population variable with this code in the constructor:

ChromosomePopulation::ChromosomePopulation(unsigned int initialPopulationSize)
{
    populationSize = initialPopulationSize;
    cFactory = ChromosomeFactory();
    for (unsigned int i = 0; i < initialPopulationSize; i++)
    {
        StackingChromosome newChromosome = cFactory.createNewChromosome();
        population.push_back(newChromosome);
    }
}

ChromosomeFactory funciton createNewChromosome is defined as:

StackingChromosome ChromosomeFactory::createNewChromosome()
{
    StackingChromosome newChromosome = StackingChromosome();
    std::vector<std::vector<float>> newSolution = std::vector<std::vector<float>>();
    for (int i = 0; i < chromosomeMaxLength; i++)
    {
        std::vector<float> gene = generateRandomGene();
        newSolution.push_back(gene);
    }
    newChromosome.setSolution(newSolution);
    return newChromosome;
}

and the generateRandomGene() function is defined as:

std::vector<float> ChromosomeFactory::generateRandomGene()
{
    //creates an empty gene
    std::vector<float> gene = std::vector<float>();
    //repeats for every space the gene needs filled, 3 in this case. 
    for (int i = 0; i < geneMaxLength; i++)
    {
        //initialise random seed
        srand(time(NULL));
        //generate random number in the range 0-100 inclusive
        float randomWhole = rand() % 101;
        //turns the random number into a percentage
        float randomPercent = randomWhole / 100;
        //the 2 vectors have the max and min values of the x, y and z coordinates. 
        //range is the difference between the max and the min.
        //for example, if the max value is 3 and the min is 1 the range is 2
        float range = geneMaxValues[i] - geneMinValues[i];
        //muliplies the percentage generated randomly ealier with the range of the values
        float randRange = randomPercent * range;
        //adds the generate random amount within the range to the minimum value, giving a number between the max and min values
        float randPoint = geneMinValues[i] + randRange;
        //add the random point to the gene
        gene.push_back(randPoint);
    }
    return gene;
}

My problem is that in the vector of StackingChromosomes that is created, all the chromosomes have the same values. When you run the first code block you will get an output like:

Chromosome 0 has at gene position 0 the value 7
Chromosome 0 has at gene position 1 the value 4
Chromosome 0 has at gene position 2 the value 9
Chromosome 1 has at gene position 0 the value 7
Chromosome 1 has at gene position 1 the value 4
Chromosome 1 has at gene position 2 the value 9
Chromosome 2 has at gene position 0 the value 7
Chromosome 2 has at gene position 1 the value 4
Chromosome 2 has at gene position 2 the value 9

As you can see, the chromosomes all have the same values. How can I make it so the chromosomes have unique values?




Aucun commentaire:

Enregistrer un commentaire