-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArkaBrain.pde
111 lines (83 loc) · 2.25 KB
/
ArkaBrain.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
//SETTINGS
int SCALINGFACTOR = 2;
int FIRSTGENERATION = 8000;
int OTHERGENERATION = 600;
float MUTATIONRATE = 0.25;
Table tavolo = new Table(200/SCALINGFACTOR,100/SCALINGFACTOR,2700/SCALINGFACTOR,1500/SCALINGFACTOR);
ArrayList<Bar> pool = new ArrayList();
ArrayList<Bar> deathCollector = new ArrayList();
Bar best;
int generationCounter = 0;
float scoreCounter = 0;
float maxScore = 0;
public void settings() {
size((int)3100/SCALINGFACTOR,(int)1800/SCALINGFACTOR);
}
void setup(){
spawnGeneration(FIRSTGENERATION);
}
void draw(){
background(255);
tavolo.update();
for(Bar barra: pool){
barra.update();
if(!barra.isAlive()){
deathCollector.add(barra);
}
}
cleanUp();
if(pool.size()==0){
spawnGeneration(OTHERGENERATION,best,MUTATIONRATE);
if(scoreCounter>maxScore){
maxScore = scoreCounter;
}
scoreCounter = 0;
generationCounter++;
}
scoreCounter+=0.1;
String status = ("generation :"+generationCounter+'\n'+"score: "+(int)scoreCounter+'\n'+"maxScore: "+(int)maxScore+'\n');
fill(255,255,255);
textSize(200/SCALINGFACTOR);
text(status, 200/SCALINGFACTOR, 300/SCALINGFACTOR);
}
/*void keyPressed(){
switch(keyCode){
case LEFT:
barra.moveL();
break;
case RIGHT:
barra.moveR();
break;
}
}*/
void spawnGeneration(int n){
for(int i=0; i<=n; i++){
pool.add(new Bar((int)tavolo.topLeftCorner.x+tavolo.lengthX/2,
(int)tavolo.topLeftCorner.y+tavolo.lengthY-50/SCALINGFACTOR,
200/SCALINGFACTOR, 50/SCALINGFACTOR,
0, 255, 0,
tavolo));
}
}
void spawnGeneration(int n, Bar best, float mutationRate){
pool.add(best);
for(int i=0; i<=n; i++){
NeuralNetwork bestBrain = best.brain.copy();
bestBrain.mutate(mutationRate);
Bar nuovo = new Bar((int)tavolo.topLeftCorner.x+tavolo.lengthX/2,
(int)tavolo.topLeftCorner.y+tavolo.lengthY-50/SCALINGFACTOR,
200/SCALINGFACTOR, 50/SCALINGFACTOR,
0, 255, 0,
tavolo);
nuovo.mountBrain(bestBrain);
pool.add(nuovo);
}
}
void cleanUp(){
for(Bar b:deathCollector){
if(pool.size()==1){
best = b;
}
pool.remove(b);
}
}