-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtarget-ga.c
More file actions
127 lines (106 loc) · 3.29 KB
/
target-ga.c
File metadata and controls
127 lines (106 loc) · 3.29 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
#include <ncurses.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#define TARGET "Hello, world!"
#define POP_SIZE 200
#define MUTATION_RATE 0.01
#define CHARSET "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz,.!? "
#define INTERVAL_USEC 50000
const int geneLength = sizeof(TARGET) - 1;
char randomChar() {
return CHARSET[rand() % strlen(CHARSET)];
}
void randomGene(char *gene) {
for (int i = 0; i < geneLength; i++)
gene[i] = randomChar();
gene[geneLength] = '\0';
}
double fitness(const char *gene) {
int score = 0;
for (int i = 0; i < geneLength; i++) {
if (gene[i] == TARGET[i]) score++;
}
return (double)score / geneLength;
}
void mutate(char *gene) {
for (int i = 0; i < geneLength; i++) {
if ((double)rand() / RAND_MAX < MUTATION_RATE) {
gene[i] = randomChar();
}
}
}
void crossover(const char *a, const char *b, char *child) {
int midpoint = rand() % geneLength;
for (int i = 0; i < geneLength; i++) {
child[i] = i < midpoint ? a[i] : b[i];
}
child[geneLength] = '\0';
mutate(child);
}
int compare(const void *a, const void *b) {
double fa = ((double *)a)[0];
double fb = ((double *)b)[0];
return fb > fa ? 1 : -1;
}
int main() {
initscr();
noecho();
curs_set(FALSE);
srand(time(NULL));
char population[POP_SIZE][geneLength + 1];
double fitnessScores[POP_SIZE];
int generation = 0;
for (int i = 0; i < POP_SIZE; i++)
randomGene(population[i]);
while (1) {
generation++;
// Evaluate fitness
for (int i = 0; i < POP_SIZE; i++)
fitnessScores[i] = fitness(population[i]);
// Find best
int bestIdx = 0;
for (int i = 1; i < POP_SIZE; i++) {
if (fitnessScores[i] > fitnessScores[bestIdx])
bestIdx = i;
}
// Display
clear();
mvprintw(1, 2, "Target: %s", TARGET);
mvprintw(2, 2, "Generation: %d", generation);
mvprintw(3, 2, "Best Match: %s", population[bestIdx]);
mvprintw(4, 2, "Fitness: %.2f%%", fitnessScores[bestIdx] * 100);
refresh();
if (strcmp(population[bestIdx], TARGET) == 0) {
break;
}
// Selection (top 20%)
int parentsCount = POP_SIZE * 0.2;
int indices[POP_SIZE];
for (int i = 0; i < POP_SIZE; i++) indices[i] = i;
for (int i = 0; i < POP_SIZE - 1; i++) {
for (int j = i + 1; j < POP_SIZE; j++) {
if (fitnessScores[indices[j]] > fitnessScores[indices[i]]) {
int tmp = indices[i];
indices[i] = indices[j];
indices[j] = tmp;
}
}
}
// Reproduce
char newPopulation[POP_SIZE][geneLength + 1];
for (int i = 0; i < POP_SIZE; i++) {
char *a = population[indices[rand() % parentsCount]];
char *b = population[indices[rand() % parentsCount]];
crossover(a, b, newPopulation[i]);
}
memcpy(population, newPopulation, sizeof(population));
usleep(INTERVAL_USEC);
}
mvprintw(6, 2, "Target reached in %d generations! Press any key to exit...", generation);
refresh();
getch();
endwin();
return 0;
}