-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
219 lines (186 loc) · 6.94 KB
/
main.js
File metadata and controls
219 lines (186 loc) · 6.94 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
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
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const nnCanvas = document.getElementById('nnCanvas');
const nnCtx = nnCanvas.getContext('2d');
const btnRun = document.getElementById('btn-run');
const btnNext = document.getElementById('btn-next');
const speedSlider = document.getElementById('speed-slider');
const speedVal = document.getElementById('speed-val');
const statGen = document.getElementById('stat-gen');
const statAlive = document.getElementById('stat-alive');
const statFitness = document.getElementById('stat-fitness');
const statGlobalFitness = document.getElementById('stat-global-fitness');
const inputPop = document.getElementById('input-pop');
const inputMut = document.getElementById('input-mut');
const inputHidden = document.getElementById('input-hidden');
const selectCrossover = document.getElementById('select-crossover');
const btnRestart = document.getElementById('btn-restart');
const checkOnlyBest = document.getElementById('check-only-best');
let popSize = parseInt(inputPop.value) || 100;
let mutationRate = (parseFloat(inputMut.value) || 5) / 100;
window.hiddenLayerNodes = parseInt(inputHidden.value) || 16;
window.crossoverType = selectCrossover ? selectCrossover.value : 'weight';
let population = new Population(popSize, mutationRate);
let running = false;
let simulationSpeed = 1;
btnRun.addEventListener('click', () => {
running = !running;
btnRun.innerText = running ? 'Pause' : 'Run';
if (running) {
requestAnimationFrame(loop);
}
});
// btnNext.addEventListener('click', () => {
// // Force next generation
// population.naturalSelection();
// updateStats();
// });
speedSlider.addEventListener('input', (e) => {
simulationSpeed = parseInt(e.target.value);
speedVal.innerText = simulationSpeed + 'x';
});
btnRestart.addEventListener('click', () => {
popSize = parseInt(inputPop.value);
mutationRate = parseFloat(inputMut.value) / 100;
window.hiddenLayerNodes = parseInt(inputHidden.value);
window.crossoverType = selectCrossover ? selectCrossover.value : 'weight';
population = new Population(popSize, mutationRate);
updateStats();
if (!running) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawGrid();
population.show(ctx, checkOnlyBest.checked);
}
});
function updateStats() {
statGen.innerText = population.generation;
statAlive.innerText = `${population.getAliveCount()} / ${popSize}`;
statFitness.innerText = Math.floor(population.bestFitness);
statGlobalFitness.innerText = Math.floor(population.globalBestFitness);
}
function drawGrid() {
ctx.strokeStyle = '#1e293b';
ctx.lineWidth = 1;
for (let x = 0; x <= canvas.width; x += GRID_SIZE) {
ctx.beginPath();
ctx.moveTo(x, 0);
ctx.lineTo(x, canvas.height);
ctx.stroke();
}
for (let y = 0; y <= canvas.height; y += GRID_SIZE) {
ctx.beginPath();
ctx.moveTo(0, y);
ctx.lineTo(canvas.width, y);
ctx.stroke();
}
}
let it_count = 0;
function loop() {
if (!running) return;
it_count++;
// Run simulation updates
for (let i = 0; i < simulationSpeed; i++) {
if (!population.allDead()) {
population.update();
} else {
population.naturalSelection();
}
}
// Skip rendering frames for better performance
if (simulationSpeed > 3 && it_count % 3 != 0) {
requestAnimationFrame(loop);
return;
}
// Render
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawGrid();
population.show(ctx, checkOnlyBest.checked);
updateStats();
let bestAlive = population.getBestAliveSnake();
if (bestAlive && bestAlive.brain) {
drawNetwork(bestAlive.brain, nnCtx);
}
requestAnimationFrame(loop);
}
// Initial draw
drawGrid();
updateStats();
function drawNetwork(brain, ctx) {
ctx.clearRect(0, 0, nnCanvas.width, nnCanvas.height);
if (!brain) return;
let inputs = brain.lastInputs || [];
let hidden = brain.lastHidden || [];
let hidden2 = brain.lastHidden2 || [];
let outputs = brain.lastOutputs || [];
let inputNodes = brain.inputNodes;
let hiddenNodes = brain.hiddenNodes;
let outputNodes = brain.outputNodes;
let w = nnCanvas.width;
let h = nnCanvas.height;
let nodeRadius = 3;
let xInput = w * 0.15;
let xHidden1 = w * 0.38;
let xHidden2 = w * 0.62;
let xOutput = w * 0.85;
function getY(index, total) {
let spacing = (h * 0.9) / Math.max(total, 1);
let offset = (h - (spacing * (total - 1))) / 2;
return offset + (index * spacing);
}
for (let i = 0; i < hiddenNodes; i++) {
for (let j = 0; j < inputNodes; j++) {
let weight = brain.weights_ih.data[i][j];
if (Math.abs(weight) > 0.5) {
ctx.beginPath();
ctx.moveTo(xInput, getY(j, inputNodes));
ctx.lineTo(xHidden1, getY(i, hiddenNodes));
ctx.lineWidth = Math.abs(weight);
ctx.strokeStyle = weight > 0 ? 'rgba(59, 130, 246, 0.4)' : 'rgba(239, 68, 68, 0.4)';
ctx.stroke();
}
}
}
for (let i = 0; i < hiddenNodes; i++) {
for (let j = 0; j < hiddenNodes; j++) {
let weight = brain.weights_hh.data[i][j];
if (Math.abs(weight) > 0.5) {
ctx.beginPath();
ctx.moveTo(xHidden1, getY(j, hiddenNodes));
ctx.lineTo(xHidden2, getY(i, hiddenNodes));
ctx.lineWidth = Math.abs(weight);
ctx.strokeStyle = weight > 0 ? 'rgba(59, 130, 246, 0.4)' : 'rgba(239, 68, 68, 0.4)';
ctx.stroke();
}
}
}
for (let i = 0; i < outputNodes; i++) {
for (let j = 0; j < hiddenNodes; j++) {
let weight = brain.weights_ho.data[i][j];
if (Math.abs(weight) > 0.5) {
ctx.beginPath();
ctx.moveTo(xHidden2, getY(j, hiddenNodes));
ctx.lineTo(xOutput, getY(i, outputNodes));
ctx.lineWidth = Math.abs(weight);
ctx.strokeStyle = weight > 0 ? 'rgba(59, 130, 246, 0.4)' : 'rgba(239, 68, 68, 0.4)';
ctx.stroke();
}
}
}
function drawNodeLayer(x, count, activations) {
for (let i = 0; i < count; i++) {
let val = (activations[i] !== undefined) ? activations[i] : 0;
let brightness = Math.floor(val * 255);
ctx.beginPath();
ctx.arc(x, getY(i, count), nodeRadius, 0, Math.PI * 2);
ctx.fillStyle = `rgb(${brightness}, ${brightness}, ${brightness})`;
ctx.fill();
ctx.strokeStyle = '#cbd5e1';
ctx.lineWidth = 1;
ctx.stroke();
}
}
drawNodeLayer(xInput, inputNodes, inputs);
drawNodeLayer(xHidden1, hiddenNodes, hidden);
drawNodeLayer(xHidden2, hiddenNodes, hidden2);
drawNodeLayer(xOutput, outputNodes, outputs);
}