-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
242 lines (198 loc) · 4.71 KB
/
index.js
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
let car;
const A = 65;
const D = 68;
const S = 83;
const W = 87;
let sim;
let memSize = 20;
let layerSpecs = [
{
count: 10 + memSize,
rand: () => 0
},
{
count: 20,
rand: () => random(-1, 1)
},
{
count: 20,
rand: () => random(-1, 1)
},
{
count: 2 + memSize,
rand: () => random(-1, 1)
}];
let mutateStrength = 0.1;
let keepRateRecip = 5;
let simLen = 10;
let simStep = 1/30;
let simMap = {
width: 1000,
height: 1000,
walls: []
};
let statMarginSize = 300;
let demoStep = 0;
let demoModel;
let demoMem = Array(memSize).fill(0);
let demoTarget;
let currGen = [];
let topScores = [];
let bestScore = 0;
let doTraining = false;
let doUserCar = false;
let trainingGen;
function setup() {
createCanvas(simMap.width + statMarginSize, simMap.height);
car = makeCar();
for(let i = 0; i <= 5; i++) {
simMap.walls.push({
x1: simMap.width / 5 * i - simMap.width / 2,
y1: (i % 2 == 0 ? 0 : 600) - simMap.height / 2,
x2: simMap.width / 5 * i - simMap.width / 2,
y2: simMap.height / 2 - (i % 2 == 0 ? 600 : 0)
});
}
//simMap.walls.push({
// x1: -100,
// y1: -100,
// x2: -100,
// y2: 100
//});
let models = [];
for(let i = 0; i < 300; i++)
models.push(NNMutate.random(layerSpecs, Math.tanh, mutateStrength));
sim = new CarSim(models);
currGen = models.map(m => ({ model: m }));
trainingGen = sim.trainGeneration(makeCar, simLen, simStep,
simMap, keepRateRecip);
demoModel = models[0];
demoTarget = CarSim.randomTarget(simMap.width, simMap.height);
frameRate(30);
}
function draw() {
background(51);
if(doTraining) {
let startMillis = millis();
let count = 0;
while(millis() < startMillis + 100) {
let done;
({ value, done } = trainingGen.next());
count++;
if(done) {
currGen = value;
topScores = currGen.slice(0, 20).map(s => int(s.score));
if(topScores[0] > bestScore)
bestScore = topScores[0];
trainingGen = sim.trainGeneration(makeCar, simLen, simStep,
simMap, keepRateRecip);
}
}
}
drawStats(sim, topScores, bestScore, doTraining);
translate(statMarginSize + simMap.width / 2, simMap.height / 2);
drawWalls(simMap.walls);
if(doUserCar)
drawCar(car, updateUserCar(car), car.getVision(simMap.walls));
else {
const mv = CarSim.updateCar(car, demoModel, simStep,
demoTarget, car.getVision(simMap.walls),
demoMem);
demoMem = mv.mem;
drawCar(car, mv, car.getVision(simMap.walls));
stroke(255, 0, 0);
strokeWeight(15);
point(demoTarget);
demoStep++;
if(simMap.walls.some(w => car.intersectsWall(w)))
resetDemo(currGen[0].model);
if(demoTarget.copy().sub(car.x, car.y).magSq() < 400)
demoTarget = CarSim.randomTarget(simMap.width, simMap.height);
if(demoStep * simStep > simLen)
resetDemo(currGen[0].model);
}
}
function resetDemo(model) {
demoModel = model;
car = makeCar();
demoStep = 0;
demoMem = Array(memSize).fill(0);
}
function keyPressed() {
if(key === " ") {
doTraining = !doTraining;
}
}
function mousePressed() {
if(statMarginSize < mouseX && mouseX < width
&& 0 < mouseY && mouseY < height)
demoTarget = createVector(mouseX - simMap.width / 2 - statMarginSize,
mouseY - simMap.height / 2);
}
function makeCar() {
return new Car(20, 40, PI/4, 30, PI * 7/4, 8);
}
function drawWalls(walls) {
stroke(200);
strokeWeight(3);
for(let w of walls)
line(w.x1, w.y1, w.x2, w.y2);
}
function drawStats(sim, scores, bestScore, isTraining) {
noStroke();
fill(40, 40, 60);
rect(0, 0, statMarginSize, height);
stroke(0);
strokeWeight(3);
fill(255);
textSize(30);
text("Generation: " + sim.generation, 15, 15, 300);
textSize(20);
if(isTraining)
fill(100, 255, 100);
else
fill(255, 50, 50);
text("Training: " + isTraining, 15, 50, 300);
fill(255);
text("Best score: "+ bestScore, 15, 75, 300);
text("Top scores:\n" + scores.join(", "), 15, 100, 300);
}
function updateUserCar(c) {
let fd = int(keyIsDown(W)) - int(keyIsDown(S));
let rt = int(keyIsDown(D)) - int(keyIsDown(A));
let dt = 1 / (frameRate() || 1);
c.update(fd, rt, dt);
return { fd, rt };
}
function drawCar(c, { fd, rt }, ds) {
push();
rectMode(CENTER);
fill(0, 255, 0);
noStroke();
translate(c.x, c.y);
rotate(c.angle - HALF_PI);
rect(0, 0, c.width, c.height);
drawLines(c, ds);
translate(0, c.height / 4);
rotate(c.turnAngle * rt);
stroke(255, 0, 0);
strokeWeight(3);
line(0, 0, 0, c.height / 2 * fd);
pop();
}
function drawLines(c, ds) {
stroke(0, 0, 255);
const n = c.eyeCount;
let startAngle = PI/2;
let angleStep = c.pov / (n - 1);
if(n == 1) {
startAngle = 0;
angleStep = 0;
}
for(let i = 0; i < n; i++) {
const a = startAngle + (i - (n-1) / 2) * angleStep;
const x2 = Math.cos(a);
const y2 = Math.sin(a);
line(0, 0, cos(a) * ds[i], sin(a) * ds[i]);
}
}