Repository navigation
Expand file tree
/
Copy pathga.cpp
More file actions
215 lines (177 loc) 路 6.35 KB
/
Copy pathga.cpp
File metadata and controls
215 lines (177 loc) 路 6.35 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
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
#include <vector>
#include <iostream>
#include "hsl.h"
#include "algos.h"
#include "raylib.h"
#include "ga.h"
// *--------------------------------------------
// * GA CLASS IMPLEMENTATION
// *--------------------------------------------
// ! CONSTRUCTORS
GA::GA(std::size_t row_count, std::size_t col_count, double mutation_rate, HSLColor background)
{
this->ROW_COUNT = row_count;
this->COL_COUNT = col_count;
this->BACKGROUND = background;
this->MUTATION_RATE = mutation_rate;
this->POPULATION_SIZE = row_count * col_count;
// ! INITIALIZE THE VECTORS
this->population = PopulationVector(this->POPULATION_SIZE);
this->fitnessScores = SizeTVector(this->POPULATION_SIZE);
this->sortedFitnessScoresIndices = SizeTVector(this->POPULATION_SIZE);
// ! INITIALIZE RANDOM POPULATION
this->generateRandomPopulation();
}
// ! GENERATION
void GA::generateRandomPopulation()
{
std::vector<HSLColor> population(this->POPULATION_SIZE);
for (std::size_t index = 0; index < this->POPULATION_SIZE; ++index)
{
// ! POPULATION WILL HAVE DIFFERENT HUE ONLY
// ! BUT SAME SATURATION AND LIGHTNESS, TO KEEP THINGS SIMPLE
population.at(index) = HSLColor{
GenerateRandomValue(0, 360),
0.5,
0.5,
};
}
this->population = population;
}
// ! FITNESS CALCULATION
int GA::calculateHueDifference(int h1, int h2)
{
int deltaH = std::abs(h1 - h2);
return std::min(deltaH, 360 - deltaH);
}
void GA::calculatePopulationFitness()
{
SizeTVector fitnessScores;
fitnessScores.reserve(population.size());
for (const HSLColor &individual : population)
{
fitnessScores.push_back(360 - calculateHueDifference(this->BACKGROUND.h, individual.h));
}
this->fitnessScores = fitnessScores;
}
void GA::calculateSortedFitnessScoresIndices()
{
this->sortedFitnessScoresIndices = argsort(this->fitnessScores);
}
// ! CROSSOVER
HSLColor GA::breed(const HSLColor &elite, const HSLColor &normie, const std::size_t eliteFitnessScore, const std::size_t normieFitnessScore)
{
double totalFitness = eliteFitnessScore + normieFitnessScore;
double w1 = eliteFitnessScore / totalFitness;
double w2 = normieFitnessScore / totalFitness;
int newHue = (elite.h * w1 + normie.h * w2);
if (rand() % 100 < this->MUTATION_RATE * 100)
{
int mutationValue;
int prob = rand() % 100;
if (prob < 1)
{
mutationValue = GenerateRandomValue(-360, 360);
}
else if (prob < 20)
{
mutationValue = GenerateRandomValue(-240, 240);
}
else if (prob < 40)
{
mutationValue = GenerateRandomValue(-180, 180);
}
else if (prob < 60)
{
mutationValue = GenerateRandomValue(-120, 120);
}
else if (prob < 80)
{
mutationValue = GenerateRandomValue(-60, 60);
}
else if (prob < 85)
{
mutationValue = GenerateRandomValue(-30, 30);
}
else
{
mutationValue = GenerateRandomValue(-10, 10);
}
newHue = abs((newHue + mutationValue)) % 360;
}
return HSLColor{
newHue % 360,
0.5,
0.5,
};
}
void GA::getNextGeneration()
{
std::size_t maxEliteIndex = static_cast<std::size_t>(this->ELITE_RATIO * this->sortedFitnessScoresIndices.size());
PopulationVector newPopulation(population.size());
// ! ADDING ELITES TO THE NEW POPULATION
for (std::size_t index = 0; index < maxEliteIndex; ++index)
{
newPopulation.at(index) = (this->population.at(this->sortedFitnessScoresIndices.at(index)));
}
for (std::size_t index = maxEliteIndex; index < population.size(); ++index)
{
const std::size_t eliteIndex = GenerateRandomValue(0, maxEliteIndex - 1);
const std::size_t normieIndex = GenerateRandomValue(maxEliteIndex, this->sortedFitnessScoresIndices.size() - 1);
const HSLColor elite = population.at(this->sortedFitnessScoresIndices.at(eliteIndex));
const HSLColor normie = population.at(this->sortedFitnessScoresIndices.at(normieIndex));
const std::size_t eliteFitnessScore = this->fitnessScores.at(this->sortedFitnessScoresIndices.at(eliteIndex));
const std::size_t normieFitnessScore = this->fitnessScores.at(this->sortedFitnessScoresIndices.at(normieIndex));
const HSLColor child = this->breed(elite, normie, eliteFitnessScore, normieFitnessScore);
newPopulation.at(index) = child;
}
this->population = newPopulation;
}
// ! RENDERING
void GA::Update()
{
this->calculatePopulationFitness();
this->calculateSortedFitnessScoresIndices();
this->getNextGeneration();
++this->GENERATION;
}
void GA::Render(int xPos, int yPos, int width, int height)
{
std::size_t pos = 0;
const std::size_t GRID_WIDTH = width / this->COL_COUNT;
const std::size_t GRID_HEIGHT = height / this->ROW_COUNT;
for (std::size_t row = 0; row < this->ROW_COUNT; ++row)
{
const int y = row * GRID_HEIGHT + yPos;
for (std::size_t col = 0; col < this->COL_COUNT; ++col)
{
const int x = col * GRID_WIDTH + xPos;
const int index = (this->COL_COUNT * row + col);
const HSLColor &hsl = this->population.at(index);
const RGBColor &rgb = HSLToRGB(hsl.h, hsl.s, hsl.l);
DrawRectangle(x, y, GRID_WIDTH, GRID_HEIGHT, Color{(unsigned char)rgb.r, (unsigned char)rgb.g, (unsigned char)rgb.b, 255});
pos += 3;
}
}
if (this->border)
{
DrawRectangleLinesEx(Rectangle{(float)xPos - 1, (float)yPos - 1, (float)GRID_WIDTH * this->COL_COUNT + 1, (float)GRID_HEIGHT * this->ROW_COUNT + 1}, 1, WHITE);
}
}
// ! STATS
Stats GA::Stat()
{
Stats s;
s.BACKGROUND = this->BACKGROUND;
s.GENERATION = this->GENERATION;
s.ELITE_RATIO = this->ELITE_RATIO;
s.MUTATION_RATE = this->MUTATION_RATE;
s.TOP_FITNESS_SCORE = this->fitnessScores.at(this->sortedFitnessScoresIndices.at(0));
s.MEDIAN_FITNESS_SCORE = this->fitnessScores.at(this->sortedFitnessScoresIndices.at(this->sortedFitnessScoresIndices.size() / 2));
s.WORST_FITNESS_SCORE = this->fitnessScores.at(this->sortedFitnessScoresIndices.at(this->sortedFitnessScoresIndices.size() - 1));
return s;
}
void GA::SetBackgroundHue(int hue)
{
this->BACKGROUND.h = hue;
}