-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
130 lines (102 loc) · 3.7 KB
/
app.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
const canvas = document.getElementById('myCanvas')
canvas.width = 300; //highway canvas width
const networkCanvas = document.getElementById('networkCanvas')
networkCanvas.width = 300; //highway canvas width
const saveBestCar = document.getElementById('saveTheBestCar')
const discardBestTraining = document.getElementById('discardGen')
const ctx = canvas.getContext("2d");
const networkCtx = networkCanvas.getContext("2d");
const road = new Road(canvas.width/2, canvas.width*0.9);
const counterEntityNumber = document.querySelector('.entityCounter')
const bestShow = document.querySelector('.bestCarShow')
let pause = false;
const N = 0 //numbers to generated
const cars = generateCars(N)
let bestCar = cars[0];//best car in gen
counterEntityNumber.innerText = 'Quantidade de carros gerados: ' + N
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.1);
}
}
}
const traffic =[
new Car(road.getLaneCenter(2),-200, 30, 50,"DUMMY", 2),
new Car(road.getLaneCenter(1),-100,30,50,"DUMMY",2),
new Car(road.getLaneCenter(0),-300,30,50,"DUMMY",2),
new Car(road.getLaneCenter(4),-300,30,50,"DUMMY",2),
new Car(road.getLaneCenter(0),-500,30,50,"DUMMY",2),
new Car(road.getLaneCenter(1),-500,30,50,"DUMMY",2),
new Car(road.getLaneCenter(2),-200,30,50,"DUMMY",2),
new Car(road.getLaneCenter(3),-400,30,50,"DUMMY",2),
new Car(road.getLaneCenter(2),-550, 30, 50,"DUMMY", 2),
new Car(road.getLaneCenter(1),-800,30,50,"DUMMY",2),
new Car(road.getLaneCenter(0),-835,30,50,"DUMMY",2),
new Car(road.getLaneCenter(1),-700,30,50,"DUMMY",2),
new Car(road.getLaneCenter(3),-700,30,50,"DUMMY",2),
new Car(road.getLaneCenter(2),-750, 30, 50,"DUMMY", 2),
new Car(road.getLaneCenter(1),-800,30,50,"DUMMY",2),
new Car(road.getLaneCenter(0),-835,30,50,"DUMMY",2),
new Car(road.getLaneCenter(2),-845,30,50,"DUMMY",2),
new Car(road.getLaneCenter(4),-950,30,50,"DUMMY",2),
];
const animate = (time) =>{
for(let i =0;i<traffic.length;i++){
traffic[i].update(road.borders,[]);
}
for(let i =0;i<cars.length;i++){
cars[i].update(road.borders,traffic)
}
bestCar = cars.find(
c=>c.y==Math.min(
...cars.map(c=>c.y)
)
)
canvas.height=window.innerHeight;
networkCanvas.height=window.innerHeight;
ctx.save();
ctx.translate(0,-bestCar.y+canvas.height*0.7)
road.draw(ctx);
for(let i = 0; i<traffic.length;i++){
traffic[i].draw(ctx, "red")
}
ctx.globalAlpha = 0.2
for(let i =0;i<cars.length;i++){
cars[i].draw(ctx, "blue");
}
ctx.globalAlpha = 1
bestCar.draw(ctx,"blue", true);
ctx.restore();
networkCtx.lineDashOffset=-time/25
Visualizer.drawNetwork(networkCtx, bestCar.brain);
requestAnimationFrame(animate)
}
function generateCars(N){
const cars = [];
for(let i = 0; i<=N;i++){
cars.push(new Car(road.getLaneCenter(2),100,30,50,"AI"))
}
return cars
}
function save(){
localStorage.setItem("bestBrain",
JSON.stringify(bestCar.brain));
saveBestCar.innerText="Check your console for see the best car brain in this generation"
console.log(bestCar.brain);
bestShow.innerText = 'Best Output: ' + bestCar.brain.levels[0].outputs
}
function discard(){
localStorage.removeItem("bestBrain");
discardBestTraining.innerText = "All training was discarded"
console.log('discard')
}
function reinitialize(){
window.location.reload()
}
function changeAccelerationCars(){
return animate()
}
animate()