This repository has been archived by the owner on Feb 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleAG.cpp
252 lines (208 loc) · 5.73 KB
/
SimpleAG.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
/*
Simple AG
Author: Bruno Vieira - github.com/brunovieira97
Artificial Intelligence - Unisinos - 2018/1
*/
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#define POPULATION_SIZE 5
#define MAXIMUM_GENERATIONS 5
double mutation = 0.2;
double range = 100.0;
double rangeOffset = 50.0;
/*
0, 1: function variables
2: fitness storage
*/
float population[POPULATION_SIZE][3];
void CreatePopulation() {
for (int i = 0; i < POPULATION_SIZE; i++) {
population[i][0] = (((float) rand() / (float) RAND_MAX) * range) - rangeOffset;
population[i][1] = (((float) rand() / (float) RAND_MAX) * range) - rangeOffset;
}
}
void ShowPopulation() {
for (int i = 0; i < POPULATION_SIZE; i++) {
printf("%d %.3f %.3f\n", i, population[i][0], population[i][1]);
}
}
void ShowFitness() {
for (int i = 0; i < POPULATION_SIZE; i++) {
printf("%.3f\n", population[i][2]);
}
}
void ShowPopulationWithFitness() {
for (int i = 0; i < POPULATION_SIZE; i++) {
printf("%d %.3f %.3f fit: %.3f\n", i, population[i][0], population[i][1], population[i][2]);
}
}
/* deprecated
void CalculateFitness() {
for (int i = 0; i < POPULATION_SIZE; i++) {
population[i][2] = (population[i][0] * population[i][0]) + (population[i][1] * population[i][1]);
}
}
*/
// New Fitness function, provided by Gustavo Pessin
void CalculateFitness() {
for (int i = 0; i < POPULATION_SIZE; i++) {
population[i][2] = (
(
sin(pow(population[i][0], 3))
+ sin(pow(population[i][0], 2))
+ sin(population[i][1])
) * 3
) + (
sqrt((
pow(population[i][0], 2)
+ pow(population[i][1], 2)
))
);
}
}
int FindMinor() {
float minor = (float) RAND_MAX;
int minorIndex = 0;
for (int i = 0; i < POPULATION_SIZE; i++) {
if (minor > population[i][2]) {
minor = population[i][2];
minorIndex = i;
}
}
return minorIndex;
}
int FindMinorForTournament(int indexes[3]) {
float minor = (float) RAND_MAX;
int minorIndex = 0;
for (int i = 0; i < 3; i++) {
if (minor > population[indexes[i]][2]) {
minor = population[indexes[i]][2];
minorIndex = indexes[i];
}
}
return minorIndex;
}
// deprecated
void Crossover(int minorIndex) {
for (int i = 0; i < POPULATION_SIZE; i++) {
population[i][0] = (population[i][0] + population[minorIndex][0]) / 2.0;
population[i][1] = (population[i][1] + population[minorIndex][1]) / 2.0;
}
}
void UniformMutationWithoutElitism() {
for (int i = 0; i < POPULATION_SIZE; i++) {
for (int j = 0; j < 2; j++) {
float r = rand() / (float) RAND_MAX;
if (r < mutation) {
population[i][j] = (((float) rand() / (float) RAND_MAX) * range) - rangeOffset;
}
}
}
}
void UniformMutationWithElitism(int minorIndex) {
for (int i = 0; i < POPULATION_SIZE; i++) {
if (i != minorIndex) {
for (int j = 0; j < 2; j++) {
float r = rand() / (float) RAND_MAX;
if (r < mutation) {
population[i][j] = (((float) rand() / (float) RAND_MAX) * range) - rangeOffset;
}
}
}
}
}
void GaussMutationWithoutElitism() {
for (int i = 0; i < POPULATION_SIZE; i++) {
for (int j = 0; j < 2;j++) {
float r = rand() / (float) RAND_MAX;
if (r < mutation) {
float q;
q = rand() / (float) RAND_MAX; // 0 to 1
q = (q * 2.0) - 1.0; // -1 to 1
q = q / 4.0; // -0.25 to 0.25
q = 1.0 + q; // 0.75 to 1.25
population[i][j] = population[i][j] * q;
}
}
}
}
void GaussMutationWithElitism(int minorIndex) {
for (int i = 0; i < POPULATION_SIZE; i++) {
if (i != minorIndex) {
for (int j = 0; j < 2; j++) {
float r = rand() / (float) RAND_MAX;
if (r < mutation) {
float q;
q = rand() / (float) RAND_MAX; // valor entre 0 e 1
q = (q * 2.0) - 1.0; // valor entre -1 e 1
q = q / 4.0; // valor entre + 0.25 e -0.25
q = 1.0 + q; // valor entre 0.75 e 1.25
population[i][j] = population[i][j] * q;
}
}
}
}
}
void OverwritePopulation(float new_population[][3]) {
for (int i = 0; i < POPULATION_SIZE; i++) {
population[i][0] = new_population[i][0];
population[i][1] = new_population[i][1];
}
}
void TournamentSelectionWithCrossover() {
float tempPopulation[POPULATION_SIZE][3];
for (int i = 0; i < POPULATION_SIZE; i++) {
int firstIndexes[3] = {
(rand() % POPULATION_SIZE),
(rand() % POPULATION_SIZE),
(rand() % POPULATION_SIZE)
};
int secondIndexes[3] = {
(rand() % POPULATION_SIZE),
(rand() % POPULATION_SIZE),
(rand() % POPULATION_SIZE)
};
auto firstElement = FindMinorForTournament(firstIndexes);
auto secondElement = FindMinorForTournament(secondIndexes);
// crossover
tempPopulation[i][0] = (population[firstElement][0] + population[secondElement][0]) / 2.0;
tempPopulation[i][1] = (population[firstElement][1] + population[secondElement][1]) / 2.0;
}
OverwritePopulation(tempPopulation);
}
int main() {
srand(time(NULL));
CreatePopulation();
CalculateFitness();
printf("Created population with fitness:\n");
ShowPopulationWithFitness();
getchar();
int currentGeneration = 1;
while (currentGeneration < MAXIMUM_GENERATIONS) {
int minorIndex = FindMinor();
TournamentSelectionWithCrossover();
// Mutation
/*
UniformMutationWithElitism(minorIndex);
UniformMutationWithoutElitism();
GaussMutationWithElitism(minorIndex);
*/
GaussMutationWithoutElitism();
CalculateFitness();
printf("Generation: %d \n\n", currentGeneration);
ShowPopulationWithFitness();
// View only
minorIndex = FindMinor();
printf("\nBest: %d %.3f\n", minorIndex, population[minorIndex][2]);
getchar();
currentGeneration++;
}
printf("Final population:\n");
ShowPopulation();
//ShowFitness();
printf("\nThe end.\n");
getchar();
}