-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneticAlgorithm.cpp
More file actions
138 lines (104 loc) · 2.61 KB
/
GeneticAlgorithm.cpp
File metadata and controls
138 lines (104 loc) · 2.61 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "GeneticAlgorithm.h"
#include <sstream>
#include <iostream>
using namespace std;
using namespace NGroupingChallenge;
GeneticAlgorithm::GeneticAlgorithm(int popSize, double crossProb, double mutProb, CGroupingEvaluator& evaluator)
: crossProb(crossProb), mutProb(mutProb), evaluator(evaluator)
{
if (popSize % 2 != 0) this->popSize = popSize - 1;
genotypeLen = evaluator.iGetNumberOfPoints();
maxValue = evaluator.iGetUpperBound();
population = generatePop(popSize, maxValue);
}
//GeneticAlgorithm::~GeneticAlgorithm()
//{
// for (int i = 0; i < genotypeLen; i++)
// {
// delete population[i];
// }
// //population.clear();
//}
vector<Individual*> GeneticAlgorithm::generatePop(int popSize, int maxValue)
{
vector<Individual*> pop;
for (int i = 0; i < popSize; i++)
{
pop.push_back(new Individual(genotypeLen, maxValue));
}
return pop;
}
void GeneticAlgorithm::evaluateFitnesses()
{
for (int i = 0; i < popSize; i++)
{
population[i]->evaluateFitness(evaluator);
}
}
void GeneticAlgorithm::crossPop()
{
vector<Individual*> newPop;
uniform_real_distribution<> dist(0, 1);
for (int i = 0; i < popSize/2; i++)
{
if (newPop.size() < popSize) {
Individual* fstToCross = chooseInd();
Individual* sndToCross = chooseInd();
double crossRand = dist(gen);
if (crossRand < crossProb) {
vector<Individual*> afterCross = fstToCross->cross(*sndToCross, NUM_OF_CHILDREN);
int iter = 0;
while (newPop.size() < popSize) {
newPop.push_back(afterCross[i]);
iter++;
}
// ---BEFORE MODIFICATION---
/*newPop.push_back(afterCross.first);
newPop.push_back(afterCross.second);*/
}
else
{
newPop.push_back(new Individual(*fstToCross));
newPop.push_back(new Individual(*sndToCross));
}
}
}
population = newPop;
}
void GeneticAlgorithm::mutatePop()
{
for (int i = 0; i < popSize; i++)
{
population[i]->mutate(mutProb, maxValue);
}
}
Individual* GeneticAlgorithm::chooseInd()
{
uniform_int_distribution<> dist(0, popSize-1);
int first = dist(gen);
int second = dist(gen);
if (population[first]->getFitness() < population[second]->getFitness()) { return population[first]; }
else { return population[second]; }
}
string GeneticAlgorithm::popStr()
{
ostringstream oss;
for (int i = 0; i < popSize; ++i) {
oss << "Ind" << i << population[i]->genotypeString() << "\n";
}
string result = oss.str();
return result;
}
double GeneticAlgorithm::getOverallBestFit()
{
return evaluator.getBestFit();
}
void GeneticAlgorithm::start(int numOfIters)
{
for (int i = 0; i < numOfIters; i++)
{
evaluateFitnesses();
crossPop();
mutatePop();
}
}