-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
206 lines (178 loc) · 4.14 KB
/
main.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
/**
* @note About the canvas coordinates:
* - 0, 0 point starts at the top left corner of the canvas;
* - The X dimension increases to the right and decreases to the left (as normal in a cartesian plane)
* - The Y dimension INCREASES to the bottom and DECREASES to the top (inverse of a cartesian plane)
*/
const carCanvas = document.getElementById("carCanvas");
carCanvas.width = 200;
const networkCanvas = document.getElementById("networkCanvas");
networkCanvas.width = 400;
const carCtx = carCanvas.getContext("2d");
const networkCtx = networkCanvas.getContext("2d");
const road = new Road({
x: carCanvas.width / 2,
width: carCanvas.width * 0.9,
});
const N = 300;
const cars = generateCars(N);
let bestCar = cars[0];
if (localStorage.getItem("bestBrain")) {
for (let i = 0; i < cars.length; i++) {
cars[i].brain = JSON.parse(localStorage.getItem("bestBrain"));
if (i != 0) {
NeuralNetwork.mutate(cars[i].brain, 0.15);
}
}
}
const traffic = generateTraffic();
function generateTraffic() {
const width = 30;
const height = 50;
const controlType = "dummy";
const maxSpeed = 2;
return [
new Car({
x: road.getLaneCenter(1),
y: -100,
width,
height,
controlType,
maxSpeed,
color: getRandomColor(),
}),
new Car({
x: road.getLaneCenter(0),
y: -300,
width,
height,
controlType,
maxSpeed,
color: getRandomColor(),
}),
new Car({
x: road.getLaneCenter(2),
y: -300,
width,
height,
controlType,
maxSpeed,
color: getRandomColor(),
}),
new Car({
x: road.getLaneCenter(0),
y: -500,
width,
height,
controlType,
maxSpeed,
color: getRandomColor(),
}),
new Car({
x: road.getLaneCenter(1),
y: -500,
width,
height,
controlType,
maxSpeed,
color: getRandomColor(),
}),
new Car({
x: road.getLaneCenter(1),
y: -750,
width,
height,
controlType,
maxSpeed,
color: getRandomColor(),
}),
new Car({
x: road.getLaneCenter(2),
y: -700,
width,
height,
controlType,
maxSpeed,
color: getRandomColor(),
}),
new Car({
x: road.getLaneCenter(1),
y: -900,
width,
height,
controlType,
maxSpeed,
color: getRandomColor(),
}),
new Car({
x: road.getLaneCenter(0),
y: -1100,
width,
height,
controlType,
maxSpeed,
color: getRandomColor(),
}),
new Car({
x: road.getLaneCenter(2),
y: -1100,
width,
height,
controlType,
maxSpeed,
color: getRandomColor(),
}),
];
}
function generateCars(N) {
const cars = [];
for (let i = 0; i < N; i++) {
cars.push(
new Car({
x: road.getLaneCenter(1),
y: 100,
width: 30,
height: 50,
controlType: "ai",
})
);
}
return cars;
}
/**
* @param {number | undefined} time Passed by `requestAnimationFrame`
*/
function animate(time) {
carCanvas.height = window.innerHeight;
networkCanvas.height = window.innerHeight;
carCtx.save();
// The car with the minimun `y` of all
bestCar = cars.find((c) => c.y === Math.min(...cars.map((c) => c.y)));
const cartStartingPosition = -bestCar.y + carCanvas.height * 0.7;
// This is what makes the road move under the car instead of the car over the road
carCtx.translate(0, cartStartingPosition);
road.draw(carCtx);
for (let i = 0; i < traffic.length; i++) {
const trafficCar = traffic[i];
trafficCar.update(road.borders, []);
trafficCar.draw(carCtx, "red");
}
carCtx.globalAlpha = 0.2;
cars.forEach((car) => {
car.update(road.borders, traffic);
car.draw(carCtx);
});
carCtx.globalAlpha = 1;
bestCar.draw(carCtx, true);
carCtx.restore();
networkCtx.lineDashOffset = -time / 50;
Visualizer.drawNetwork(networkCtx, bestCar.brain);
requestAnimationFrame(animate);
}
function save() {
localStorage.setItem("bestBrain", JSON.stringify(bestCar.brain));
}
function discard() {
localStorage.removeItem("bestBrain");
}
animate();