-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
217 lines (179 loc) · 5.98 KB
/
main.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
/**
* This program demonstrates the genetic algorithm to maximize our function f(x)= x^2.
* We do this in a series of 5 steps:
*
* (1) Randomly generate candidates
* (2) Apply fitness function/evaluate & sort
* (3) Crossover
* (4) Mutate
* (5) Repeat
*
* These steps give us the ability to find optimized solutions to difficult problems when we don’t know the exact answer.
* This program uses bitstrings as candidates. Of course we can know by looking that a candidate bitstring of 11111
* (given a max bitstring length of 5) would maximize our fitness function f(x) = x^2, because 11111 (31 in decimal) is f(31) = 961.
* However this program is meant to show the procedure of the GA and provide any reference necessary.
*/
#include <iostream>
#include <vector>
#include <cmath>
#include <ctime>
int candidates = 6;
int candidateSize = 5;
float keep = 0.5;
float mutateRate = 0.2;
struct sample {
std::string code;
int value;
int eval;
};
/**
* Convert the string bitstr, a binary string, to an integer and return its value
* (i.e. if bitstr = "01000", returns 8
* @param bitstr
* @return
*/
int bitStr_toInt(std::string bitstr) {
int value = 0;
int power = 0;
for (int i = (int) (bitstr.size() - 1); i >= 0; i--)
value += (pow(2, power++) * (bitstr[i] - '0')); // char # - '0' gives its integer representation. See ASCII table
return value;
}
/**
* Perform fitness evaluation of the candidate, using fitness function (in this case, f(x) = x^2).
* @param value
* @return
*/
int evaluate(int value) {
return (int) pow(value, 2);
}
/**
* selection sort samples from largest to smallest
* @param vector
*/
void selection_sort(std::vector<sample> &vec) {
for (int i = 0; i < vec.size() - 1; i++) {
for (int j = i; j < vec.size(); j++) {
if (vec[i].eval < vec[j].eval) {
sample tmp = vec[i];
vec[i] = vec[j];
vec[j] = tmp;
}
}
}
}
/**
* Take best candidates and cross corresponding values at
* each end of the bitstring to simulate genetic offspring.
* @param vec
*/
void crossover(std::vector<sample> &vec) {
std::vector<sample> results;
// 6 candidates total. we loop on half since we modify two at a time.
for (int i = 0; i < (candidates * keep); i++) {
// only choose samples from the top 'keep' candidates.
// they are top since we sorted the best at the top (beginning of vector).
sample one = vec[(rand() % candidates) * keep];
sample two = vec[(rand() % candidates) * keep];
// build new candidates, initialize values
sample r1;
sample r2;
r1.code = "";
r1.value = 0;
r1.eval = -1;
r2.code = "";
r2.value = 0;
r2.eval = -1;
// choose cut point for crossing
int cut = rand() % candidateSize;
// loop for crossover
for (int j = 0; j < candidateSize; j++) {
if (j < cut) { // normal lateral movement
r1.code += one.code[j];
r2.code += two.code[j];
}
else { // now crossover
r1.code += two.code[j];
r2.code += one.code[j];
}
}
// convert the new bitstrings to integers and evaluate to finish up the new results
r1.value = bitStr_toInt(r1.code);
r2.value = bitStr_toInt(r2.code);
r1.eval = evaluate(r1.value);
r2.eval = evaluate(r2.value);
results.push_back(r1);
results.push_back(r2);
}
// store results of results vector into original vector
for (int i = 0; i < results.size(); i++)
vec[i] = results[i];
}
/**
* Perform mutation by flipping a random bit while making sure it conforms
* to the mutation rate (x < mutationRate)
* @param vec
*/
void mutate(std::vector<sample> &vec) {
for (int i = 0; i < vec.size(); i++) {
// should we mutate?
if ( ((rand() % 10) / (float) 10 ) < mutateRate) {
// which element to mutate
int mutateIndex = rand() % candidateSize;
// mutate
vec[i].code[mutateIndex] = (vec[i].code[mutateIndex] == '0') ? '1' : '0';
}
}
}
/**
* Print our samples
* @param vec
*/
void showSamples(std::vector<sample> &vec) {
for(unsigned int i = 0; i < vec.size(); i++){
std::cout << "Sample-" << i << " : "<< vec[i].code <<" \t|\t";
std::cout << "Value-" << i << " : "<< vec[i].value << " \t|\t";
std::cout << "Eval-" << i << " : "<< vec[i].eval << std::endl;
}
std::cout << '\n';
}
int main() {
srand((unsigned int) time(NULL));
std::cout << "Genetic Algorithm" << std::endl;
std::vector<sample> sample_vector;
//generate sample population
for (int i = 0; i < candidates; i++) {
sample tempSample;
tempSample.code = ""; tempSample.value = 0; tempSample.eval = -1;
for (int j = 0; j < candidateSize; j++) {
int chance = rand() % 100;
if (chance >= 50)
tempSample.code += "1";
else
tempSample.code += "0";
}
sample_vector.push_back(tempSample);
}
// convert to decimal
for (unsigned int i = 0; i < sample_vector.size(); i++)
sample_vector[i].value = bitStr_toInt(sample_vector[i].code);
// evaluate samples using fitness function.
for (unsigned int i = 0; i < sample_vector.size(); i++)
sample_vector[i].eval = evaluate(sample_vector[i].value);
// output the samples
std::cout << "\nSamples GENERATED..." << '\n';
showSamples(sample_vector);
// sort
selection_sort(sample_vector);
std::cout << "After samples are SORTED..." << '\n';
showSamples(sample_vector);
// crossover
crossover(sample_vector);
std::cout << "After CROSSOVER..." << '\n';
showSamples(sample_vector);
// mutate
mutate(sample_vector);
std::cout << "After MUTATION..." << '\n';
showSamples(sample_vector);
return 0;
}