-
Notifications
You must be signed in to change notification settings - Fork 28
/
Population.pde
113 lines (98 loc) · 3.31 KB
/
Population.pde
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
class Population {
Tetris[] tetris_pop;
Tetris best_tetris;
int gen = 1;
int same_best = 0;
float best_score = 0;
float fitness_sum = 0;
Population(int size) {
tetris_pop = new Tetris[size];
for(int i = 0; i < tetris_pop.length; i++) {
tetris_pop[i] = new Tetris(i+1);
}
best_tetris = tetris_pop[0];
best_tetris.is_best = true;
}
boolean is_done() { //check if all the tetris_pop in the population are dead
for(int i = 0; i < tetris_pop.length; i++) {
if(!tetris_pop[i].dead)
return false;
}
return true;
}
void update() { //update all the tetris_pop in the generation
if(best_tetris.dead) {
best_tetris.is_best = false;
best_tetris = find_best_tetris_alive();
best_tetris.is_best = true;
}
for(int i = 0; i < tetris_pop.length; i++) {
if(!tetris_pop[i].dead) {
tetris_pop[i].update();
}
}
}
void show() { //show either the best Tetris or all the tetris_pop
best_tetris.show_game();
best_tetris.show_next();
best_tetris.brain.show(100,570,250,250,new float[5], new float[1]);
}
Tetris find_best_tetris_alive() { //set the best Tetris of the generation
float max = 0;
int max_index = 0;
for(int i = 0; i < tetris_pop.length; i++) {
if(!tetris_pop[i].dead && tetris_pop[i].fitness() > max) {
max = tetris_pop[i].fitness();
max_index = i;
}
}
return tetris_pop[max_index];
}
Tetris find_best_tetris() { //set the best Tetris of the generation
float max = 0;
int max_index = 0;
for(int i = 0; i < tetris_pop.length; i++) {
if(tetris_pop[i].fitness() > max) {
max = tetris_pop[i].fitness();
max_index = i;
}
}
return tetris_pop[max_index];
}
Tetris select_parent() { //selects a random number in range of the fitnesssum and if a Tetris falls in that range then select it
float rand = random(fitness_sum);
float summation = 0;
for(int i = 0; i < tetris_pop.length; i++) {
summation += tetris_pop[i].fitness();
if(summation > rand) {
return tetris_pop[i];
}
}
return tetris_pop[0];
}
void natural_selection() {
Tetris[] newtetris_pop = new Tetris[tetris_pop.length];
best_tetris = find_best_tetris();
best_tetris.is_best = true;
calculate_fitness_sum();
newtetris_pop[0] = best_tetris.clone(); //add the best Tetris of the prior generation into the new generation
for(int i = 1; i < tetris_pop.length; i++) {
Tetris child = select_parent().crossover(select_parent());
child.mutate();
newtetris_pop[i] = child;
}
tetris_pop = newtetris_pop.clone();
gen+=1;
}
void mutate() {
for(int i = 1; i < tetris_pop.length; i++) { //start from 1 as to not override the best Tetris placed in index 0
tetris_pop[i].mutate();
}
}
void calculate_fitness_sum() { //calculate the sum of all the tetris_pop fitnesses
fitness_sum = 0;
for(int i = 0; i < tetris_pop.length; i++) {
fitness_sum += tetris_pop[i].fitness();
}
}
}