no overwriting object

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

Thank you very much


In main...AllOnesGA.cpp

1
2
3
4
5
6
7
    int main()
{
        GeneticAlgorithm ga(100);
	Population population = ga.initPopulation(50);	
	ga.evalPopulation(population);
	ga.isTerminationConditionMet(population);
	...

In GeneticAlgorithm.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
	        }
        }


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

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;

	


For population.h we have:

1
2
3
4
5
6
7
8
...
	vector <Individual> getIndividual()
	{
		return this->population;
	}
   ...
private:
	vector <Individual> population;

but don't cofuse the object population from AllOnesGA.cpp and the population object from Population.h that is a vector.
Any recomendation?
Topic archived. No new replies allowed.