-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
141 lines (114 loc) · 3.82 KB
/
Copy pathmain.js
File metadata and controls
141 lines (114 loc) · 3.82 KB
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
// Main Canvas from HTML
const mainCanvas = document.getElementById('mainCanvas');
mainCanvas.height = window.innerHeight;
mainCanvas.width = 300;
// Netowrk Canvas from HTML
const networkCanvas = document.getElementById('networkCanvas');
networkCanvas.height = window.innerHeight;
networkCanvas.width = 400;
// Get Main Canvas Context
const mainContext = mainCanvas.getContext('2d');
// Get Network Canvas Context
const networkContext = networkCanvas.getContext('2d');
// Call Road Class
const road = new Road(mainCanvas.width/2, mainCanvas.width*0.9, 3);
// Training Car Class
const N = 1000;
const cars = generateTrainingCars(N);
let bestCar = cars[0];
//// Load Saved Model from Local Storage (JSON)
// If there is a saved best car model in Local Storage
if(localStorage.getItem("bestBrain")){
// Load the model into the first parallel car
for(let i=0; i<cars.length; i++){
cars[i].brain = JSON.parse(
localStorage.getItem("bestBrain"));
// Mutate the models of all parallel cars
// that is not first car
if(i!=0){
// mutate(network, gamma="percentage")
NeuralNetwork.mutate(cars[i].brain, 0.2);
}
}
}
// Call Traffic Class for Dummy Cars
// Car(x,y,width,height,controlType, maxSpeed=[default:6])
const traffic = [
new Car(road.getLaneCenter(0),-330,30,50,"DUMMY",4),
new Car(road.getLaneCenter(1),-100,30,50,"DUMMY",4),
new Car(road.getLaneCenter(2),-270,30,50,"DUMMY",4),
new Car(road.getLaneCenter(0),-730,30,50,"DUMMY",4),
new Car(road.getLaneCenter(1),-500,30,50,"DUMMY",4),
new Car(road.getLaneCenter(2),-770,30,50,"DUMMY",4),
]
// Animate the Canvas
animate();
//// Local Storage (JSON) Functions
// Save the best car model into Local Storage (JSON)
function save(){
localStorage.setItem("bestBrain",
JSON.stringify(bestCar.brain));
}
// Discard the best car model from Local Storage (JSON)
function discard(){
localStorage.removeItem("bestBrain");
}
// Debugger in console
// localStorage
// Generate Multiple Training Cars to speed up training
// (Parallelization)
function generateTrainingCars(N){
const cars = [];
for(let i=0; i<N; i++){
cars.push(new Car(road.getLaneCenter(1),150,30,50,"AI"));
}
return cars;
}
// Animation Function from the Canvas
function animate(time){
// Dummy Cars Generation
for(let i=0; i<traffic.length; i++){
traffic[i].update(road.borders,[]);
}
// All Training Cars Generation
for(let i=0; i<cars.length; i++){
cars[i].update(road.borders, traffic);
}
// Find the best training car
const bestCar = cars.find(
// Find the car with minimum y value
// among the parallel cars
c => c.y == Math.min(
// Create new array with only the y values
...cars.map(c => c.y)
)
);
// Canvas Refresh
mainCanvas.height=window.innerHeight;
networkCanvas.height=window.innerHeight;
// Locate Training Car Position on the Road
mainContext.save();
mainContext.translate(0,-bestCar.y+(mainCanvas.height*0.7));
// Draw Road
road.draw(mainContext);
//// Draw Dummy Cars
for(let i=0; i<traffic.length; i++){
traffic[i].draw(mainContext);
}
// Make other parallel cars half transparent
mainContext.globalAlpha = 0.2;
//// Draw Training Cars
for(let i=0; i<cars.length; i++){
cars[i].draw(mainContext);
}
// Make the best car fully opaque
mainContext.globalAlpha = 1;
bestCar.draw(mainContext, true); // True = Show Sensor
mainContext.restore();
// Animate Dashes in Network Canvas
networkContext.lineDashOffset = -time/60;
// Draw Network
Visualizer.drawNetwork(networkContext, bestCar.brain);
// Animation Func callbacks for multiple times per second
requestAnimationFrame(animate);
}