methods get and set

Apr 4, 2019 at 2:01pm
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);
	...


I GeneticAlgorithm.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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

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;
Last edited on Apr 4, 2019 at 2:04pm
Apr 4, 2019 at 2:07pm
You are setting values in COPIES, which are then thrown away.

1
2
3
4
for (Individual individual : population.getIndividual())
{
		   individual.setFitness(2); // Set a value in the COPY of the Individual
}


Try this:
1
2
3
4
for (Individual& individual : population.getIndividual())
{
		   individual.setFitness(2);
}
Last edited on Apr 4, 2019 at 2:07pm
Apr 4, 2019 at 2:13pm
I have the same problem :(
Apr 4, 2019 at 2:17pm
Oh, so you do.

This: population.getIndividual()

This makes no sense at all. This gives you a COPY of a single Individual? Anything you do to that copy will be lost. population should be a container (for example, vector<Individual> ) and the loop would look like this:

1
2
3
4
for (Individual& individual : population)
{
		   individual.setFitness(2);
}
Last edited on Apr 4, 2019 at 2:17pm
Apr 4, 2019 at 2:27pm
from Population.h

1
2
3
4
5
6
7
8
   ...
	vector <Individual> getIndividual()
	{
		return this->population;
	}
   ...
private:
	vector <Individual> population;
Apr 4, 2019 at 2:45pm
but don't cofuse the object population from AllOnesGA.cpp and the population object from Population.h that is a vector.
Any recomendation?
Apr 4, 2019 at 4:51pm
The OP has restarted the thread by the very first question:
http://www.cplusplus.com/forum/beginner/251860/
Apr 4, 2019 at 6:34pm
vector <Individual> getIndividual()
you are returning a copy of your member variable
Apr 4, 2019 at 7:00pm
And what can I do?
Apr 4, 2019 at 7:41pm
You have a class, I'm guessing of type Population.

Classes should take responsibility for themselves. They should not be handing out parts of themselves to other objects so that those objects can alter them. This breaks encapsulation. It's just bad design for something so simple.

If the Population object needs to change, it should have a class function that does the change, and you should call that function.
Apr 4, 2019 at 7:58pm
but this code works in Java, can you say me exactly what must I change?
Thanks
Apr 4, 2019 at 8:15pm
Do you know what a reference is?
Apr 5, 2019 at 6:02am
I know the difference in passing by value or by reference in a method
but I don't know enought c++ to trnslate thisk chunk of code in Java to c++
thanks
Apr 5, 2019 at 8:42am
Sounds about right. Java tends to pass by reference, by default. C++ by value.

You think you're passing by reference in your code; you're not.

You think you're passing around a single object; you're not, you're making copies, changing them, and then throwing them away, leaving the original unchanged.

If you come back to C++, learn about pass-by-value and pass-by-reference.
Topic archived. No new replies allowed.