methods get and set
Publicado por psosmol (1 intervención) el 04/04/2019 19:25:11
Hola, tengo cuatro ficheros: AllOnesGA.cpp, GeneticAlgorithm.h, Population.h, Individual.h
Y no se por qué individual.getFitness() da -1y no 2que es el último valor que se le asigna con el método setFitness()
Gracias
En main...AllOnesGA.cpp
Pero no confundir population de AllOnesGA.cpp y el vector population de Population.h
Y no se por qué individual.getFitness() da -1y no 2que es el último valor que se le asigna con el método setFitness()
Gracias
En main...AllOnesGA.cpp
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
int main()
{
GeneticAlgorithm ga(100);
Population population = ga.initPopulation(50);
ga.evalPopulation(population);
ga.isTerminationConditionMet(population);
...
Edit & Run
GeneticAlgorithm.h
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;
Population.h es:
...
vector <Individual> getIndividual()
{
return this->population;
}
...
private:
vector <Individual> population;
Pero no confundir population de AllOnesGA.cpp y el vector population de Population.h
Valora esta pregunta


0