-
Notifications
You must be signed in to change notification settings - Fork 1
/
ga.c
48 lines (39 loc) · 1.2 KB
/
ga.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
#include "utils.h"
#include "genetics.h"
#include "opt.h"
#define quarter_pop_size 25
#define half_pop_size 2*quarter_pop_size
#define pop_size 2*half_pop_size//must be even
#define num_iters 1000
int main(){
SETRNG(123);
Creature best;//we could actually just save it allways in pop[0]...
Creature pop[pop_size];
for(int i=0;i<pop_size;i++){
pop[i]=RandomCreature();
}
qsort(pop,pop_size,sizeof(Creature),compareCreatures);
best=cloneCreature(pop[0]);
for(int iter=0;iter<num_iters;iter++){
//new
for(int i=half_pop_size+quarter_pop_size;i<pop_size;i++){
pop[i]=RandomCreature();
}
//children
for (int i = half_pop_size; i < pop_size-quarter_pop_size; i++) {
int parent1 = RNG() % quarter_pop_size; // Select parent1 from the top quarter
int parent2 = RNG() % quarter_pop_size; // Select parent2 from the top quarter
pop[i] = MakeChild(pop[parent1], pop[parent2]);
}
//small mutations
for(int i=quarter_pop_size;i<half_pop_size;i++){
pop[i]=MutateCreature(pop[i]);
}
qsort(pop,pop_size,sizeof(Creature),compareCreatures);
if(best.score<pop[0].score){
best=cloneCreature(pop[0]);
}
}
OutputCreature(best);
return 0;
}