0

Hello I have four files: AllOnesGA.cpp, GeneticAlgorithm.h, Population.h, Individual.h And I don't know why individual.getFitness() give me -1 and not 2 that is the last value that I give it with the method setFitness

I simplified my code

int main()
{
   GeneticAlgorithm ga(100);
   Population population = ga.initPopulation(50);  
   ga.evalPopulation(population);
   ga.isTerminationConditionMet(population);
   ...

In geneticAlgorithm

void evalPopulation(Population population)
{
    double populationFitness = 0;
    for (Individual individual : population.getIndividual())
    {
        individual.setFitness(2);       
    }
}
bool isTerminationConditionMet(Population population)
{   
    for(Individual individual :population.getIndividual())
    {
        cout<<individual.getFitness()<<endl; //this gives -1 and not 2
    }
}

and in Individual.h

class Individual{
public:
    Individual(vector<int> chromosome2)
    :chromosome(chromosome2),chromosomeLength(chromosome2.size())
    {}

    Individual(int chromosomeLength)
    :chromosomeLength(chromosomeLength)
    {
        for(int gene=0;gene<chromosomeLength;gene++)
        {
            chromosome.push_back(gene);
        }
    }

    int getChromosomeLength()
    {
        return chromosomeLength;
    }

    vector<int> getChromosome()
    {
        return chromosome;
    }

    int getGene(int offset)
    {
        return chromosome[offset];
    }

    void setFitness(double fitness)
    {
        this->fitness=fitness;
    }

    double getFitness()
    {
        return fitness;
    }

private:
    vector<int> chromosome;
    double fitness=-1.0;
    int chromosomeLength;

from Population.h

...
vector <Individual> getIndividual()
{
    return this->population;
}
...
private:
    vector <Individual> population;

But don't confuse the object population from AllOnesGA.cpp and the population object from Population.h that is a vector. Any recomendation?

kalehmann
  • 4,821
  • 6
  • 26
  • 36
  • 2
    In C++, returning an object from a function, or passing it as an argument to a function, creates a copy of that object. If you haven't already done so, check out [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Apr 05 '19 at 08:28
  • 2
    You need to learn how to use references. (1) The loop that calls `setFitness()` works by value. `population.getIndividual()` returns by value, so returns a copy of the member of `population`. It needs to return a reference. (2) The loop also iterates by value, so the call of `setFitness()` affect a copy of the elements of the copied vector. The loop needs to use a reference. Both of these fixes are necessary (doing only one of them will not fix the problem). In future, read up on how to provide a [mcve] - your code is incomplete and that reduces the chances of anyone being able to help – Peter Apr 05 '19 at 08:31
  • 2
    In `for (Individual individual : population.getIndividual())` loop you modify the `individual` which is a copy of object stored in `population`. You should use `for (Individual &individual : population.getIndividual())` instead – vahancho Apr 05 '19 at 08:32

0 Answers0