-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenAlg.c
59 lines (47 loc) · 1.29 KB
/
GenAlg.c
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "Board.h"
#define TRIMMED_ALPHABET "acdegilmnoprst"
#define POP_SIZE 2000
#define GENERATIONS 200
int compareBoards(const void *b1, const void *b2)
{
return BoardScore(*(Board **) b2) - BoardScore(*(Board **) b1);
}
int main()
{
Trie *trie = TrieScanWordCode(stdin, &WordScore);
BoardSolver *bs = BoardSolverInit(trie, TRIMMED_ALPHABET);
Board *pop[POP_SIZE];
int i, j, mom, dad, gen, best = 0;
srand(time(NULL));
for (i = 0; i < POP_SIZE; i++) {
pop[i] = BoardRandom(bs);
}
for (gen = 0; gen < GENERATIONS; gen++) {
qsort(pop, POP_SIZE, sizeof(Board *), compareBoards);
if (BoardScore(pop[0]) > best) {
best = BoardScore(pop[0]);
BoardPrintWithStats(stdout, pop[0]);
}
i = 0;
j = POP_SIZE / 2;
while (j < POP_SIZE) {
mom = i++;
dad = i++;
BoardRecycle(pop[j]);
pop[j++] = BoardBreed(bs, pop[mom], pop[dad]);
BoardRecycle(pop[j]);
pop[j++] = BoardBreed(bs, pop[mom], pop[dad]);
}
}
for (i = 0; i < POP_SIZE; i++) {
BoardRecycle(pop[i]);
}
BoardGarbageCollect();
TrieDestroy(trie);
BoardSolverDestroy(bs);
return 0;
}