-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser-interface.js
More file actions
370 lines (322 loc) · 11.5 KB
/
user-interface.js
File metadata and controls
370 lines (322 loc) · 11.5 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
var UI = (() => {
var snakeSize = 20;
var width = 10;
var height = 10;
var fps = 1000;
var learningAlgorithm = QLearning;
var iterations;
var r1;
var r2;
var r3;
var r4;
var learningRate;
var discountFactor;
var epsilon;
var npm;
var npo;
var irap;
var ifdtw;
var idrctn;
const score = document.getElementById('score');
const hscore = document.getElementById('highscore');
const ratio = document.getElementById('ratio');
const deaths = document.getElementById('deaths');
const qTableDownload = document.getElementById('qTableDownload');
const qTableExt = document.getElementById('qTableExt');
const qTableDownloadBtn = document.getElementById('qTableDownloadBtn');
const testReportDownload = document.getElementById('testReportDownload');
const testReportExt = document.getElementById('testReportExt');
const testReportDownloadBtn = document.getElementById('testReportDownloadBtn');
const trainType = document.getElementById('trainType');
const train = document.getElementById('train');
const test = document.getElementById('test');
const pauseTest = document.getElementById('pauseTest');
const newFPS = document.getElementById('newFPS');
const fpsBtn = document.getElementById('fpsBtn');
const unstick = document.getElementById('unstick');
const resetTest = document.getElementById('resetTest');
const iters = document.getElementById('iters');
const rewardWall = document.getElementById('rewardWall');
const rewardBody = document.getElementById('rewardBody');
const rewardApple = document.getElementById('rewardApple');
const rewardNothing = document.getElementById('rewardNothing');
const lr = document.getElementById('lr');
const df = document.getElementById('df');
const ep = document.getElementById('ep');
const widt = document.getElementById('widt');
const heigh = document.getElementById('heigh');
const nbpMain = document.getElementById('nbpMain');
const ortpMain = document.getElementById('ortpMain');
const arbpMain = document.getElementById('arbpMain');
const nbpOther = document.getElementById('nbpOther');
const ortpOther = document.getElementById('ortpOther');
const arbpOther = document.getElementById('arbpOther');
const rap = document.getElementById('rap');
const fdtw = document.getElementById('fdtw');
const rdtw = document.getElementById('rdtw');
const ldtw = document.getElementById('ldtw');
const drctn = document.getElementById('drctn');
var testPaused = false;
var timer;
var numUnsticks = 0;
qTableDownloadBtn.addEventListener("click", () => {
var downloadName = qTableDownload.value ? qTableDownload.value : 'snakeQTable';
downloadQTable(QLearning.getQTableSmallDecimals(), 'QTable ' + downloadName + qTableExt.value);
});
testReportDownloadBtn.addEventListener("click", () => {
var downloadName = testReportDownload.value ? testReportDownload.value : 'snakeTestReport';
downloadTestReport(Game.getReport(), 'Report ' + downloadName + testReportExt.value);
});
train.addEventListener("click", () => {
if (trainType.value == 'custom') {
train.innerHTML = 'Trained 0%';
train.disabled = true;
iterations = parseInt(iters.value.split(',').join(''));
r1 = parseFloat(rewardWall.value);
r2 = parseFloat(rewardBody.value);
r3 = parseFloat(rewardApple.value);
r4 = parseFloat(rewardNothing.value);
learningRate = parseFloat(lr.value);
discountFactor = parseFloat(df.value);
epsilon = parseFloat(ep.value);
width = parseInt(widt.value);
height = parseInt(heigh.value);
othersnaks = parseInt(othersnaks.value);
appls = parseInt(appls.value);
npm = nbpMain.checked ? 0 : (ortpMain.checked ? 1 : 2);
npo = nbpOther.checked ? 0 : (ortpOther.checked ? 1 : 2);
irap = rap.checked;
ifdtw = fdtw.checked;
irdtw = rdtw.checked;
ildtw = ldtw.checked;
idrctn = drctn.checked;
Game.init(QLearning, snakeSize, width, height, othersnaks, appls, iterations, r1, r2, r3, r4, npm, npo, irap, ifdtw, irdtw, ildtw, idrctn);
learningAlgorithm.init({}, learningRate, discountFactor, epsilon);
setTimeout(trainLoop, 5)
} else {
train.innerHTML = 'Trained 0%';
train.disabled = true;
iters.value = '50,000,000';
iterations = parseInt(iters.value.split(',').join(''));
rewardWall.value = -1;
rewardBody.value = -1;
rewardApple.value = 1;
rewardNothing.value = -.075;
r1 = parseFloat(rewardWall.value);
r2 = parseFloat(rewardBody.value);
r3 = parseFloat(rewardApple.value);
r4 = parseFloat(rewardNothing.value);
lr.value = .85;
df.value = .9;
ep.value = .5;
learningRate = lr.value;
discountFactor = df.value;
epsilon = ep.value;
widt.value = 10;
heigh.value = 10;
othersnaks.value = 0;
appls.value = 1;
width = widt.value;
height = heigh.value;
othersnaks = parseInt(othersnaks.value);
appls = parseInt(appls.value);
ortpMain.checked = true;
nbpOther.checked = true;
rap.checked = true;
fdtw.checked = true;
rdtw.checked = false;
ldtw.checked = false;
drctn.checked = true;
npm = nbpMain.checked ? 0 : (ortpMain.checked ? 1 : 2);
npo = nbpOther.checked ? 0 : (ortpOther.checked ? 1 : 2);
irap = rap.checked;
ifdtw = fdtw.checked;
irdtw = rdtw.checked;
ildtw = ldtw.checked;
idrctn = drctn.checked;
Game.init(QLearning, snakeSize, width, height, othersnaks, appls, iterations, r1, r2, r3, r4, npm, npo, irap, ifdtw, irdtw, ildtw, idrctn);
addScript('./preloaded-q-table.json', () => {
learningAlgorithm.init(preloadedQTable, learningRate, discountFactor, epsilon);
train.innerHTML = 'Trained';
test.disabled = false;
fpsBtn.disabled = false;
unstick.disabled = false;
resetTest.disabled = false;
qTableDownloadBtn.disabled = false;
});
}
});
test.addEventListener("click", () => {
test.disabled = true;
pauseTest.disabled = false;
highscore = 0;
score.style.visibility = 'visible';
hscore.style.visibility = 'visible';
ratio.style.visibility = 'visible';
deaths.style.visibility = 'visible';
Game.init(QLearning, snakeSize, width, height, othersnaks, appls, iterations, r1, r2, r3, r4, npm, npo, irap, ifdtw, irdtw, ildtw, idrctn);
learningAlgorithm.changeLR(9999);
learningAlgorithm.changeDF(9999);
learningAlgorithm.changeEpsilon(9999);
testLoop();
});
pauseTest.addEventListener("click", () => {
if (testPaused) {
testPaused = false;
pauseTest.innerHTML = "Pause Test"
testReportDownloadBtn.disabled = true;
testLoop();
} else {
testPaused = true;
pauseTest.innerHTML = "Unpause Test"
testReportDownloadBtn.disabled = false;
}
});
fpsBtn.addEventListener("click", () => {
setFPS(newFPS.value);
});
unstick.addEventListener("click", () => {
Game.reset();
numUnsticks++;
});
resetTest.addEventListener("click", () => {
Game.resetTest();
numUnsticks = 0;
});
function myStringify(myObj) {
var final = '';
var firstIter = true;
for (let prop in myObj) {
final += JSON.stringify(prop) + ':';
final += JSON.stringify(myObj[prop]);
break;
}
for (let prop in myObj) {
if (!firstIter) {
final += ',' + JSON.stringify(prop) + ':';
final += JSON.stringify(myObj[prop]);
}
firstIter = false;
}
return final;
}
function downloadQTable(obj, filename) {
var blob = new Blob(['{{' + JSON.stringify(obj) + '}'], {type: "application/json;charset=utf-8"}).slice(2,-1);
var url = URL.createObjectURL(blob);
var elem = document.createElement("a");
elem.href = url;
elem.download = filename;
document.body.appendChild(elem);
elem.click();
document.body.removeChild(elem);
}
function downloadTestReport(obj, filename) {
var blob = new Blob(['{{' + JSON.stringify(obj, null, 2) + '}'], {type: "application/json;charset=utf-8"}).slice(2,-1);
var url = URL.createObjectURL(blob);
var elem = document.createElement("a");
elem.href = url;
elem.download = filename;
document.body.appendChild(elem);
elem.click();
document.body.removeChild(elem);
}
// Include script file
function addScript(filename, callback) {
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.src = filename;
script.type = 'text/javascript';
function scriptCallback() {
callback();
script.removeEventListener("load", scriptCallback);
}
head.append(script);
script.addEventListener('load', scriptCallback);
}
function trainLoop() {
train.innerHTML = 'Trained 0%'
var numCheckpoints = 5;
var timesLooped = 0;
function microTrain() {
for (let j = 0; j < iterations / numCheckpoints; ++j) {
Game.trainLoop();
QLearning.changeLR(learningRate / iterations);
QLearning.changeDF(discountFactor / iterations);
QLearning.changeEpsilon(epsilon / iterations);
}
train.innerHTML = 'Trained ' + 100 / numCheckpoints * (++timesLooped) + '%';
if (timesLooped < numCheckpoints) {
setTimeout(microTrain, 5);
} else {
test.disabled = false;
fpsBtn.disabled = false;
unstick.disabled = false;
resetTest.disabled = false;
qTableDownloadBtn.disabled = false;
}
}
setTimeout(microTrain, 5);
}
function testLoop() {
if (testPaused)
return;
Game.testLoop();
score.innerHTML = 'Score: ' + Game.getScore();
hscore.innerHTML = 'Highscore: ' + Game.getHighscore();
ratio.innerHTML = 'Apples/Death: ' + (Game.getDeaths() == 0 ? Game.getApplesEaten() + '.000' : (Game.getApplesEaten() / Game.getDeaths()).toFixed(3));
deaths.innerHTML = 'Deaths: ' + Game.getDeaths() + '    Unsticks: ' + getNumUnsticks();
if (!testPaused) {
timer = setTimeout(testLoop, 1000 / fps);
}
}
function setFPS(f) {
fps = f;
}
var getInitialLearningRate = () => {
return learningRate;
};
var getInitialDiscountFactor = () => {
return discountFactor;
};
var getInitialEpsilon = () => {
return epsilon;
};
var getNumUnsticks = () => {
return numUnsticks;
};
var getIncludeMainSnakeBody = () => {
return nbpMain.checked ? nbpMain.value : (ortpMain.checked ? ortpMain.value : arbpMain.value);
};
var getIncludeOtherSnakesBody = () => {
return nbpOther.checked ? nbpOther.value : (ortpOther.checked ? ortpOther.value : arbpOther.value);
};
var getIncludeRelApplePos = () => {
return rap.checked;
};
var getIncludeDistForward = () => {
return fdtw.checked;
};
var getIncludeDistRight = () => {
return rdtw.checked;
};
var getIncludeDistLeft = () => {
return ldtw.checked;
};
var getIncludeDirection = () => {
return drctn.checked;
};
// Make public methods accessible to QLearning
return {
getInitialLearningRate: getInitialLearningRate,
getInitialDiscountFactor: getInitialDiscountFactor,
getInitialEpsilon: getInitialEpsilon,
getNumUnsticks: getNumUnsticks,
getIncludeMainSnakeBody: getIncludeMainSnakeBody,
getIncludeOtherSnakesBody: getIncludeOtherSnakesBody,
getIncludeRelApplePos: getIncludeRelApplePos,
getIncludeDistForward: getIncludeDistForward,
getIncludeDistRight: getIncludeDistRight,
getIncludeDistLeft: getIncludeDistLeft,
getIncludeDirection: getIncludeDirection
};
})();