-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoSnake.pde
194 lines (177 loc) · 3.97 KB
/
autoSnake.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
int w=15;
int h=15;
int size=20;
int popSize=500;
long pFrame=0;
int frameLim=1800;
int[] brainDim={6,5,3};
game[] gams=new game[popSize];
int growth=1;
int prize=30;
int mutRate=20;
float mutMult=1;
int best=0;
long frameC=0;
int fast;
int bestNext;
int genC=0;
int at_fit=0;
int alpha=20;
void settings(){
size(w*size,h*size+40);
}
void setup(){
for(int i=0; i<popSize; i++){
gams[i]=new game();
}
}
void draw(){
if(mousePressed){
fast=1;
}else{
fast=100;
}
background(0);
for(int i=0; i<fast; i++){
run();
frameC++;
}
if(keyPressed&&key==' '){
gams[0].display();
}else{
for(int i=0; i<popSize; i++){
gams[i].display();
}
}
}
void findBest(){
int bestF=0;
for(int i=0; i<popSize; i++){
if(gams[i].snek.fitness>bestF){
bestF=gams[i].snek.fitness;
best=i;
}
}
if(genC%5==0){
gams[best].snek.brain.toTxt("genFiles/gen"+genC+".txt");
}
if(bestF>at_fit){
gams[best].snek.brain.toTxt("backup.txt");
println("backup");
at_fit=bestF;
println(at_fit+" "+gams[best].snek.l);
}
}
void run(){
boolean going=false;
for(int i=0; i<popSize; i++){
gams[i].core();
if(gams[i].snek.dead==false){
going=true;
}
}
if(going==false||frameC==pFrame+frameLim){
pFrame=frameC;
findBest();
nNet[] doneBrains=nextGen();
for(int i=0; i<popSize; i++){
gams[i].restart();
gams[i].snek.brain=doneBrains[i];
}
genC++;
}
}
nNet[] nextGen(){
nNet[] gen=new nNet[popSize];
IntList lotto=pool();
for(int i=0; i<popSize; i++){
lotto.shuffle();
gen[i]=mutate(gams[lotto.get(0)].snek.brain);
if(lotto.get(0)==best){
bestNext=i;
}
}
return gen;
}
nNet mutate(nNet mutee){
nNet mutated=new nNet(brainDim[0],brainDim[1],brainDim[2]);
for(int i=0; i<mutee.w_ih.data.length; i++){
for(int j=0; j<mutee.w_ih.data[i].length; j++){
if(random(100)>mutRate){
mutated.w_ih.data[i][j]=mutee.w_ih.data[i][j]+randomGaussian()*mutMult/10.0;
}
}
}
for(int i=0; i<mutee.w_ho.data.length; i++){
for(int j=0; j<mutee.w_ho.data[i].length; j++){
if(random(100)>mutRate){
mutated.w_ho.data[i][j]=mutee.w_ho.data[i][j]+randomGaussian()*mutMult/10.0;
}
}
}
for(int i=0; i<mutee.b_h.data.length; i++){
for(int j=0; j<mutee.b_h.data[i].length; j++){
if(random(100)>mutRate){
mutated.b_h.data[i][j]=mutee.b_h.data[i][j]+randomGaussian()*mutMult/10.0;
}
}
}
for(int i=0; i<mutee.b_o.data.length; i++){
for(int j=0; j<mutee.b_o.data[i].length; j++){
if(random(100)>mutRate){
mutated.b_o.data[i][j]=mutee.b_o.data[i][j]+randomGaussian()*mutMult/10.0;
}
}
}
return(mutated);
}
IntList pool(){
IntList genePool=new IntList();
for(int i=0; i<popSize; i++){
for(int j=0; j<gams[i].snek.fitness; j++){
genePool.append(i);
}
}
return genePool;
}
void keyPressed(){
if(key=='s'){
saveBest();
}
}
void saveBest(){
game[] testGams=new game[popSize];
for(int i=0; i<popSize; i++){
testGams[i]=new game();
}
int[] totFit=new int[popSize];
for(int i=0; i<10; i++){
boolean done=false;
int count=0;
for(int j=0; j<popSize; j++){
testGams[j].snek.brain=gams[j].snek.brain;
}
while(done==false&&count<frameLim){
count++;
for(int j=0; j<popSize; j++){
testGams[j].core();
if(testGams[j].snek.dead==true){
done=true;
}
}
}
for(int j=0; j<popSize; j++){
totFit[j]+=testGams[j].snek.fitness;
testGams[j].restart();
}
}
int maxFit=max(totFit);
int saveBest=0;
for(int i=0; i<popSize; i++){
if(totFit[i]==maxFit){
saveBest=i;
}
}
testGams[saveBest].snek.brain.toTxt("weights.txt");
println("save "+maxFit/10.0);
}