-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGA.cpp
319 lines (269 loc) · 11.1 KB
/
GA.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// GAGS
// Developed by Andrew R Wood
#include "GA.h"
#include "StringOperations.h"
#include "Calculator.h"
#include <cstdlib>
#include <stdlib.h>
#include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>
using namespace std;
void InitializeSolutions(int n, vector<string>& s, vector<vector<string> >& p, map<string,int>& e) {
// solutions will be a hash containing arrays of subject IDs.
// We will score these later by referencing ID to phenotype value
// Note that N in each solution will be the total N across the groups in which to find specific distributions for.
// We will break each solution up into the relevant subsets for scoring and returning.
map<string, int> idsSelected;
int randomIndex;
for (int i = 0; i < p.size(); ++i) {
for (int j = 0; j < n; ++j) {
do {
randomIndex = rand() % s.size(); // 0 to vector-size -1
} while (!((idsSelected.find(s[randomIndex]) == idsSelected.end()) && (e.find(s[randomIndex]) == e.end())));
p[i].push_back(s[randomIndex]);
idsSelected[s[randomIndex]] = 0;
}
idsSelected.clear();
}
}
double ScoreSolution(vector<string>& c, map<string, double>& p, vector<int>& n, vector<double>& m, vector<double>& s) {
vector<double> phenotype;
int index = 0;
double score = 0;
for (int i = 0; i < n.size(); ++i) {
int count = 0;
while (count < n[i]) {
phenotype.push_back(p[c[index]]);
index++;
count++;
}
double mean = CalcMean(phenotype);
double sd = sqrt(CalcVariance(phenotype));
if (fabs(m[i] - mean) >= 0) {
score += fabs(m[i] - mean) * 2;
}
if (fabs(s[i] - sd) >= 0) {
score += fabs(s[i] - sd);
}
phenotype.clear();
}
return score;
}
void DetermineBestSolution(std::vector<double>& v, double& s, int& x) {
double bestScore = 999999999;
int bestScoreIndex;
for (int i = 0; i < v.size(); ++i) {
if (v[i] < bestScore) {
bestScore = v[i];
bestScoreIndex = i;
}
}
s = bestScore;
x = bestScoreIndex;
}
bool TestBestSolution(vector<string>& c, map<string, double>& p, vector<int>& n, vector<double>& m, vector<double>& s, vector<int>& mp, vector<int>& sp) {
vector<double> values;
int index = 0;
bool solutionFound;
for (int i = 0; i < n.size(); ++i) {
int count = 0;
while (count < n[i]) {
values.push_back(p[c[index]]);
index++;
count++;
}
double mean = roundf(CalcMean(values) * pow(10,mp[i])) / pow(10,mp[i]);
double sd = roundf(sqrt(CalcVariance(values)) * pow(10,sp[i])) / pow(10,sp[i]);
if (doubleEqual(mean, m[i], 0.00001) && doubleEqual(sd, s[i], 0.00001)) {
solutionFound = true;
}
else {
solutionFound = false;
break;
}
values.clear();
}
return solutionFound;
}
void GenerateRouletteWheel(vector<double>& r, vector<double>& s) {
vector<double>s2;
vector<double>::iterator it = max_element(s.begin(), s.end());
double worstScore = *it;
double reqSum = 0;
double prevProb = 0;
for (int i = 0; i < s.size(); ++i) {
s2.push_back(worstScore-s[i]+1);
reqSum += (worstScore-s[i]+1);
}
for (int i = 0; i < s2.size(); ++i) {
r.push_back(prevProb + (s2[i] / reqSum));
prevProb += s2[i] / reqSum;
}
}
int SpinWheel(vector<double>& r) {
int i;
double randomNumber = rand() / (double)RAND_MAX;
for (i = 0; i < r.size(); ++i) {
if (randomNumber < r[i]) {
break;
}
}
return i;
}
void DetermineWorstSolution(vector<double>& v, double& s, int& x) {
double worstScore = 0;
int worstScoreIndex;
for (int i = 0; i < v.size(); ++i) {
if (v[i] > worstScore) {
worstScore = v[i];
worstScoreIndex = i;
}
}
s = worstScore;
x = worstScoreIndex;
}
void CrossoverPMX(vector<vector<string> >& p, vector<int>& r, vector<string>& s, vector<vector<string> >& chrXY, map<string, int>& chrXAlleles, map<string, int>& chrYAlleles) {
// define crossover points
int xoverA = rand() % p[r[0]].size();
int xoverB = rand() % p[r[0]].size();
if (xoverA > xoverB) {
int t = xoverA;
xoverA = xoverB;
xoverB = t;
}
// Record elements in crossover region in maps: id -> index and map IDs across chromosomes within crossover region
map<string,string> chrXxOverAlleleMap;
map<string,string> chrYxOverAlleleMap;
map<string,string>::iterator it;
for (int i = xoverA; i <= xoverB; ++i) {
chrXxOverAlleleMap[p[r[1]][i]] = p[r[0]][i];
chrYxOverAlleleMap[p[r[0]][i]] = p[r[1]][i];
}
// Perform the PMX crossover
for (int i = 0; i < p[r[0]].size(); ++i) {
if (i < xoverA || i > xoverB) {
// if element not in chromosomal crossover region associated with orginal chr Y
it = chrXxOverAlleleMap.find(p[r[0]][i]);
if (it == chrXxOverAlleleMap.end()) {
chrXY[0].push_back(p[r[0]][i]);
chrXAlleles[p[r[0]][i]] = 0;
}
else {
string newAllele = chrXxOverAlleleMap[p[r[0]][i]];
it = chrXxOverAlleleMap.find(newAllele);
while (it != chrXxOverAlleleMap.end()) {
newAllele = chrXxOverAlleleMap[newAllele];
it = chrXxOverAlleleMap.find(newAllele);
}
chrXY[0].push_back(newAllele);
chrXAlleles[newAllele] = 0;
}
// if element not in chromosomal crossover region associated with orginal chr X
it = chrYxOverAlleleMap.find(p[r[1]][i]);
if (it == chrYxOverAlleleMap.end()) {
chrXY[1].push_back(p[r[1]][i]);
chrYAlleles[p[r[1]][i]] = 0;
}
else {
string newAllele = chrYxOverAlleleMap[p[r[1]][i]];
it = chrYxOverAlleleMap.find(newAllele);
while (it != chrYxOverAlleleMap.end()) {
newAllele = chrYxOverAlleleMap[newAllele];
it = chrYxOverAlleleMap.find(newAllele);
}
chrXY[1].push_back(newAllele);
chrYAlleles[newAllele] = 0;
}
}
else {
//CROSSOVER REGION
chrXY[0].push_back(p[r[1]][i]);
chrXY[1].push_back(p[r[0]][i]);
chrXAlleles[p[r[1]][i]] = 0;
chrYAlleles[p[r[0]][i]] = 0;
}
}
}
void Mutate(vector<vector<string> >& chrXY, vector<int>& n, double m, vector<string>& s, map<string,double>& p, map<string, int>& chrXAlleles, map<string, int>& chrYAlleles, map<string, int>& e) {
double min = 1/(double)chrXY[0].size();
if (min <= m) {
int mutationIndex1;
string mutationIndex1Allele;
int mutationIndex2;
string mutationIndex2Allele;
string newAllele;
map<string,int>::iterator it;
double random = min + ((double)rand() / RAND_MAX) * (m - min);
int mutations = round((double)chrXY[0].size() * random);
// for each mutation to be performed // mutate each chromosome in turn
for (int j = 0; j < mutations; ++j) {
// CHROMOSOME X (0)
// pick indicies at random in which alleles are to be swapper
mutationIndex1 = rand() % chrXY[0].size();
mutationIndex1Allele = chrXY[0][mutationIndex1];
// pick and allele at random to place into this from the full set of IDs. This will help get round local maxima
do {
mutationIndex2 = rand() % s.size();
mutationIndex2Allele = s[mutationIndex2];
} while (e.find(mutationIndex2Allele) != e.end());
// Determine if this is a new allele in the chromosome or whether this will need to be a swap if in chromosome
it = chrXAlleles.find(mutationIndex2Allele);
// if not found then insert new allele into preselected mutation site - this should be the default first check
if (it == chrXAlleles.end()) {
chrXY[0][mutationIndex1] = mutationIndex2Allele;
chrXAlleles.erase(mutationIndex1Allele);
chrXAlleles[mutationIndex2Allele] = 0;
}
// else mutate by performing simple swap but dont put contraint on index range to chose from as below
else {
mutationIndex2 = rand() % chrXY[0].size();
mutationIndex2Allele = chrXY[0][mutationIndex2];
chrXY[0][mutationIndex1] = mutationIndex2Allele;
chrXY[0][mutationIndex2] = mutationIndex1Allele;
}
// CHROMOSOME Y (1)
// pick indicies at random in which alleles are to be swapper
mutationIndex1 = rand() % chrXY[1].size();
mutationIndex1Allele = chrXY[1][mutationIndex1];
// pick and allele at random to place into this from the full set of IDs. This will help get round local maxima
do {
mutationIndex2 = rand() % s.size();
mutationIndex2Allele = s[mutationIndex2];
} while (e.find(mutationIndex2Allele) != e.end());
// Determine if this is a new allele in the chromosome or whether this will need to be a swap if in chromosome
it = chrYAlleles.find(mutationIndex2Allele);
// if not found then insert new allele into preselected mutation site
if (it == chrYAlleles.end()) {
chrXY[1][mutationIndex1] = mutationIndex2Allele;
chrYAlleles.erase(mutationIndex1Allele);
chrYAlleles[mutationIndex2Allele] = 0;
}
// else mutate similar to below performing simple swap but dont put contraint on index range to chose from as below
else {
mutationIndex2 = rand() % chrXY[1].size();
mutationIndex2Allele = chrXY[1][mutationIndex2];
chrXY[1][mutationIndex1] = mutationIndex2Allele;
chrXY[1][mutationIndex2] = mutationIndex1Allele;
}
}
}
else {
cout << "Cannot mutate at user rate - not enough samples in chromosome" << endl;
}
}
void DetermineX2LeastFit(vector<int>& c, std::vector<double> s) {
int index;
vector<double>::iterator it;
it = max_element(s.begin(), s.end());
it = find(s.begin(), s.end(), *it);
index = distance(s.begin(), it);
c.push_back(index);
s[index] = -1;
it = max_element(s.begin(), s.end());
it = find(s.begin(), s.end(), *it);
index = distance(s.begin(), it);
c.push_back(index);
}