-
Notifications
You must be signed in to change notification settings - Fork 1
/
genealgomanager.cpp
191 lines (165 loc) · 4.52 KB
/
genealgomanager.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "genealgomanager.h"
GeneAlgoManager::GeneAlgoManager(int _n, int seed)
: n(_n), geneScore(_n), geneFitScore(_n), geneFitness(_n), fitSum(0), rm(seed), gm(seed) {
for (int i = 0; i < _n; i++) {
Gene tempGene(rm);
geneArr.push_back(tempGene);
}
}
void GeneAlgoManager::CalculateScore() {
for (int i = 0; i < n; i++) {
geneScore[i] = gm.EvaluateMax(geneArr[i]);
geneFitScore[i] = gm.EvaluateIgnoreOne(geneArr[i]);
}
}
double GeneAlgoManager::CalculateFitness() {
double fitSum = 0;
double maxScore = *max_element(geneFitScore.begin(), geneFitScore.end());
double minScore = *min_element(geneFitScore.begin(), geneFitScore.end());
for (int i = 0; i < n; i++) {
geneFitness[i] = (geneFitScore[i] - minScore) + (maxScore - minScore) / (fitness_k - 1);
fitSum += geneFitness[i];
}
return fitSum;
}
int GeneAlgoManager::SelectParent(double fitSum) {
double pt = rm.RandomDouble(fitSum);
double timeSum = 0;
int sel = -1;
for (int i = 0; i < n; i++) {
timeSum += geneFitness[i];
if (pt <= timeSum) {
sel = i;
break;
}
}
if (sel == -1) return n - 1;
else return sel;
}
void GeneAlgoManager::IndividualCross(Gene& parGeneA, Gene& parGeneB, Gene& childGene) {
int parA[8][14], parB[8][14], child[8][14];
parGeneA.GetGene(parA);
parGeneB.GetGene(parB);
childGene.GetGene(child);
pair<Pos, Pos> cr_pos = rm.TwoRandomPosition();
Pos posA = cr_pos.first, posB = cr_pos.second;
if (posA.x > posB.x) swap(posA, posB);
if (posA.y > posB.y) swap(posA.y, posB.y);
for (int i = 0; i < 8; i++)
for (int j = 0; j < 14; j++)
child[i][j] = parA[i][j];
for (int i = posA.x; i < posB.x; i++)
for (int j = posA.y; j < posB.y; j++)
child[i][j] = parB[i][j];
childGene.InitGene(child);
}
void GeneAlgoManager::Cross() { //need optimization
vector<Gene> newGene(n);
int parA = SelectParent(fitSum);
int parB = SelectParent(fitSum);
typedef pair<int, int> pii;
priority_queue<pii, vector<pii>, greater<pii>> pq;
for (int i = 0; i < n; i++) {
pq.emplace(geneFitScore[i], i);
while (pq.size() > elite) pq.pop();
}
for (int i = 0; i < elite; i++) {
int idx = pq.top().second;
pq.pop();
newGene[i] = geneArr[idx];
}
for (int i = elite; i < n; i++) {
if (parA == parB) {
newGene[i] = geneArr[parA];
continue;
}
IndividualCross(geneArr[parA], geneArr[parB], newGene[i]);
parA = SelectParent(fitSum);
parB = SelectParent(fitSum);
}
geneArr = newGene;
}
void GeneAlgoManager::Mutate() {
for (int i = 0; i < n; i++) {
if (rm.RandomDouble01() < all_init) {
Gene newGene(rm);
geneArr[i] = newGene;
}
if (rm.RandomDouble01() < gene_mutate_prob)
gm.ProbMutate(geneArr[i], mutate_prob, rm);
}
}
void GeneAlgoManager::Optimize() {
for (int i = 0; i < n; i++) {
if (rm.RandomDouble(10.0) <= 1) gm.Optimizer(geneArr[i], max_iter, 1);
else gm.Optimizer(geneArr[i], max_iter, 0);
}
}
void GeneAlgoManager::SaveBestGene() {
int ma = -1, mai = -1;
for (int i = 0; i < n; i++) {
if (geneScore[i] > ma) {
ma = geneScore[i];
mai = i;
}
}
if (ma < gm.EvaluateMax(bestGene)) return;
bestGene = geneArr[mai];
}
void GeneAlgoManager::NextGeneration()
{
CalculateScore();
SaveBestGene();
fitSum = CalculateFitness();
Cross();
Mutate();
Optimize();
}
void GeneAlgoManager::PrintBestGene() {
int hGene[8][14];
bestGene.GetGene(hGene);
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 14; j++)
cout << hGene[i][j];
cout << endl;
}
}
void GeneAlgoManager::SaveAllGene(ofstream& out) {
int hGene[8][14];
for (int t = 0; t < n; t++) {
geneArr[t].GetGene(hGene);
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 14; j++)
out << hGene[i][j];
out << endl;
}
}
}
void GeneAlgoManager::LoadAllGene(ifstream& in) {
int hGene[8][14];
for (int i = 0; i < n; i++) {
for (int j = 0; j < 8; j++) {
string s; in >> s;
for (int k = 0; k < 14; k++)
hGene[j][k] = s[k] - '0';
}
geneArr[i].InitGene(hGene);
}
}
int GeneAlgoManager::BestGeneScore() {
return gm.EvaluateMax(bestGene);
}
double GeneAlgoManager::returnAverageScore(){
double timeSum = 0;
for (int i = 0; i < n; i++) {
timeSum += geneScore[i];
}
return timeSum / n;
}
vector<int> GeneAlgoManager::top10Score() {
vector<int> compareVec = geneScore;
sort(compareVec.begin(), compareVec.end(), greater<int>());
vector<int> returnVec(10);
for (int i = 0; i < 10; i++) returnVec[i] = compareVec[i];
return returnVec;
}