This repository was archived by the owner on Nov 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcrossover.c
More file actions
262 lines (232 loc) · 6.7 KB
/
crossover.c
File metadata and controls
262 lines (232 loc) · 6.7 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
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
#include "crossover.h"
#include "GA.h"
void gameEngine(char *problem, ANSWER_T *userguess) {
userguess->black = 0;
userguess->white = 0;
int problemUsed[NUM_PROBLEM];
int guessUsed[NUM_PROBLEM];
memset(problemUsed, 0, sizeof(problemUsed));
memset(guessUsed, 0, sizeof(guessUsed));
// Check for correct positions
for (int i = 0; i < NUM_PROBLEM; i++) {
if (userguess->ans[i] == problem[i]) {
userguess->black++;
problemUsed[i] = 1;
guessUsed[i] = 1;
}
}
for (int i = 0; i < NUM_PROBLEM; i++) {
if (guessUsed[i] == 0) {
for (int j = 0; j < NUM_PROBLEM; j++) {
if (problemUsed[j] == 0 && userguess->ans[i] == problem[j]) {
userguess->white++;
problemUsed[j] = 1;
break;
}
}
}
}
}
void initializePopulation(ANSWER_T *populations) {
for (int i = 0; i < POPULATION_SIZE; i++) {
generateRandomGuess(populations[i].ans);
populations[i].black = 0;
populations[i].white = 0;
populations[i].fitness = 0;
}
}
void generateRandomGuess(char *str) {
for (int i = 0; i < NUM_PROBLEM; i++) {
str[i] = 'A' + (rand() % NUM_COLORS);
}
str[NUM_PROBLEM] = '\0';
}
// รับค่าปูชณียบุคคลที่เจ๋งที่สุดตลอดกาลเข้ามา
// Developer note : theGOAT = theGreatestOfAllTime is the member that has the
// most satisfy fitness value in the "hallOfFame" array
double calculateNewFitness(int round, ANSWER_T *theGOAT, ANSWER_T guess) {
double retVal = 0;
int difBlack = 0;
int difWhite = 0;
for (int i = 0; i < round; i++) {
int b, w;
b = theGOAT[i].black;
w = theGOAT[i].white;
gameEngine(theGOAT[i].ans, &guess);
difBlack += abs(guess.black - b);
difWhite += abs(guess.white - w);
}
// printf("\n%d %d\n", difBlack, difWhite);
retVal = (difBlack * FITNESS_BLACK_VALUE) +
(difWhite * FITNESS_WHITE_VALUE); // + NUM_PROBLEM * (round - 1);
return retVal;
}
int addEntryToEHat(ANSWER_T *eHat, int *eHatElemNum, ANSWER_T elem) {
if (*eHatElemNum == 0) {
eHat[*eHatElemNum] = elem;
*eHatElemNum += 1;
return 0;
} else {
for (int i = 0; i < *eHatElemNum; i++) {
if (strcmp((eHat[i]).ans, elem.ans) == 0) {
return -1;
}
}
eHat[*eHatElemNum] = elem;
*eHatElemNum += 1;
}
return 0;
}
int addEntryToChild(ANSWER_T *eHat, int *eHatElemNum, char *child) {
if (*eHatElemNum == 0) {
for (int i = 0; i <= NUM_PROBLEM; i++) {
eHat[*eHatElemNum].ans[i] = child[i];
}
*eHatElemNum += 1;
return 0;
} else {
for (int i = 0; i < *eHatElemNum; i++) {
if (strcmp((eHat[i]).ans, child) == 0) {
return -1;
}
}
for (int i = 0; i <= NUM_PROBLEM; i++) {
eHat[*eHatElemNum].ans[i] = child[i];
}
*eHatElemNum += 1;
}
return 0;
}
void TournamentSelection(ANSWER_T *populations, char *parentA, char *parentB) {
int tournamentSize = TOURNAMENTSIZE; // Adjust as needed
int randIndexA, randIndexB;
// Perform tournament selection for parent A
randIndexA = rand() % POPULATION_SIZE;
strcpy(parentA, populations[randIndexA].ans);
for (int i = 1; i < tournamentSize; i++) {
int currentIndex = rand() % POPULATION_SIZE;
if (populations[currentIndex].fitness <= populations[randIndexA].fitness) {
strcpy(parentA, populations[currentIndex].ans);
randIndexA = currentIndex;
}
}
// Perform tournament selection for parent B (ensuring diversity)
randIndexB = (randIndexA + tournamentSize / 2) % POPULATION_SIZE;
while (strcmp(parentA, populations[randIndexB].ans) == 0) {
randIndexB = (randIndexB + 1) % POPULATION_SIZE;
}
strcpy(parentB, populations[randIndexB].ans);
}
void PropotionalRouletteWheelSelection(ANSWER_T *populations, char *parentA,
char *parentB) {
double totalFitness = 0.0;
// Calculate the total fitness of the population
for (int i = 0; i < POPULATION_SIZE; ++i) {
totalFitness += populations[i].fitness;
}
// Generate random values for selection
double randomValue1 = (double)rand() / RAND_MAX * totalFitness;
double randomValue2;
do {
randomValue2 = (double)rand() / RAND_MAX * totalFitness;
} while (randomValue2 == randomValue1);
// Perform the first selection
double partialSum = 0.0;
int parentIndex1 = 0;
for (int i = 0; i < POPULATION_SIZE; ++i) {
partialSum += populations[i].fitness;
if (partialSum >= randomValue1) {
parentIndex1 = i;
strcpy(parentA, populations[parentIndex1].ans);
break;
}
}
// Perform the second selection
partialSum = 0.0;
int parentIndex2 = 0;
for (int i = 0; i < POPULATION_SIZE; ++i) {
partialSum += populations[i].fitness;
if (partialSum >= randomValue2) {
parentIndex2 = i;
strcpy(parentB, populations[parentIndex2].ans);
break;
}
}
}
void crossingOver(char *parentA, char *parentB) {
int rate = rand() % 100;
if (rate < CROSSOVER_RATE) {
singlePointCrossOver(parentA, parentB);
} else {
twoPointCrossOver(parentA, parentB);
}
}
void singlePointCrossOver(char *parentA, char *parentB) {
int p1 = rand() % NUM_PROBLEM;
for (int i = p1; i < NUM_PROBLEM; i++) {
char temp = parentA[i];
parentA[i] = parentB[i];
parentB[i] = temp;
}
parentA[NUM_PROBLEM] = '\0';
parentB[NUM_PROBLEM] = '\0';
}
void twoPointCrossOver(char *parentA, char *parentB) {
int p1 = rand() % NUM_PROBLEM;
int p2 = rand() % NUM_PROBLEM;
if (p1 > p2) {
int temp1 = p1;
p1 = p2;
p2 = temp1;
}
for (int i = p1; i <= p2; i++) {
char temp2 = parentA[i];
parentA[i] = parentB[i];
parentB[i] = temp2;
}
parentA[NUM_PROBLEM] = '\0';
parentB[NUM_PROBLEM] = '\0';
}
void mutate(char *guess) {
int rate = rand() % 100;
if (rate < MUTATION_RATE) {
guess[rand() % NUM_PROBLEM] = 'A' + (rand() % NUM_COLORS);
guess[NUM_PROBLEM] = '\0';
}
}
void inversion(char *code) {
int rate = rand() % 100;
if (rate < INVERSION_RATE) {
int start = rand() % NUM_PROBLEM;
int end = rand() % NUM_PROBLEM;
if (start > end) {
int temp = start;
start = end;
end = temp;
}
while (start == end) {
end = rand() % NUM_PROBLEM;
}
while (start < end) {
char temp = code[start];
code[start] = code[end];
code[end] = temp;
start++;
end--;
}
code[NUM_PROBLEM] = '\0';
}
}
void mutateswap(char *code) {
int rate = rand() % 100;
if (rate < PERMUTATION_RATE) {
int point1 = rand() % NUM_PROBLEM;
int point2 = rand() % NUM_PROBLEM;
while (point1 == point2)
point2 = rand() % NUM_PROBLEM;
char temp = code[point1];
code[point1] = code[point2];
code[point2] = temp;
code[NUM_PROBLEM] = '\0';
}
}