-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpnjesse.js
462 lines (395 loc) · 16.9 KB
/
cpnjesse.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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// Here all the magic happens
class cpnJesseQLearner {
// Setting it all up
constructor(context) {
this.imgPath = '.\\images\\';
this.cpnjesseImg = this.imgPath + 'cpnjesse.png';
this.enemyImg = this.imgPath + 'enemy.png';
this.fastforwardImg = this.imgPath + 'fast-forward.png';
this.flagImg = this.imgPath + 'flag.png';
this.pauseImg = this.imgPath + 'pause.png';
this.playImg = this.imgPath + 'play.png';
this.treasureImg = this.imgPath + 'treasure.png';
this.context = context;
let defaultBoard = [
'S------',
'-------',
'-------',
'-------',
'-------',
'-------',
'Q-----E'
];
this.board = defaultBoard;
this.resetQTable();
this.numberOfColumns = this.board[0].length;
this.numerOfRows = this.board.length;
this.maxWidth = 300;
this.maxHeight = 300;
this.context.lineWidth = 2;
this.context.strokeStyle = '#000000';
this.drawBoard();
this.resetButtons();
}
// Resets/empties the QTable
resetQTable() {
this.qTable = new Map();
for (let y=0; y<this.board.length; y++) {
for (let x=0; x<this.board[y].length; x++) {
this.qTable.set(x + '-' + y, [0, 0, 0, 0]);
if (this.board[y].charAt(x) == 'S') {
this.startingState = x + '-' + y;
}
if (this.board[y].charAt(x) == 'E') {
this.endingState = x + '-' + y;
}
}
}
}
// Returns char of the given position on the board
getBoard(x,y) {
return this.board[y].charAt(x);
}
// Set someting on a position on the board & redraw
setBoard(x, y, char) {
this.board[ y ] = this.board[ y ].substring(0, x) + char + this.board[ y ].substring(x+1);
this.drawBoard();
}
// Completely (re)draw the board
drawBoard() {
// Draw the grid
this.context.clearRect(0, 0, this.maxWidth, this.maxHeight);
this.context.beginPath();
this.context.strokeStyle = '#000000';
// Vertical lines
for (let x=this.context.lineWidth/2; x<=this.maxWidth+this.context.lineWidth; x=x+(this.maxWidth/this.numberOfColumns) ) {
this.context.moveTo(x, 0);
this.context.lineTo(x, this.maxHeight);
}
// Horizontal lines
for (let y=this.context.lineWidth/2; y<=this.maxHeight+this.context.lineWidth; y=y+(this.maxHeight/this.numerOfRows) ) {
this.context.moveTo(0, y);
this.context.lineTo(this.maxWidth+this.context.lineWidth, y);
}
this.context.stroke();
this.context.closePath();
// 'Fill' the cells
for (let y=0; y<this.board.length; y++) {
for (let x=0; x<this.board[y].length; x++) {
this.drawCell(x, y);
}
}
}
// Draw a cell:
// 1. Draw the icon
// 2. Draw arrow
drawCell(x, y) {
// End/Treasure
if (this.board[y].charAt(x)=='E') {
this.drawIcon(this.treasureImg, x, y);
// Enemy
} else if (this.board[y].charAt(x)=='Q') {
this.drawIcon(this.enemyImg, x, y);
// Beginning/Flag
} else if (this.board[y].charAt(x)=='S') {
this.drawIcon(this.flagImg, x, y);
// Nothing special
} else {
this.drawIcon('', x, y);
}
this.drawArrow(x, y);
}
// Draw an icon on a position on the board
drawIcon(icon, x, y) {
let image = new Image();
let width = this.getCellWidth();
let height = this.getCellHeight();
let xPos = this.getX(x);
let yPos = this.getY(y);
if (icon != '') {
image.src = icon;
image.onload = function() {
context.drawImage(image, xPos, yPos, width, height);
};
} else {
// No icon to draw, must be an 'empty' field
this.context.clearRect(xPos, yPos, width, height);
}
}
// Draws the arrow on a position on the board
drawArrow(x, y) {
let arrows = this.qTable.get(x + '-' + y);
// Only draw an arrow if there is at least one positive value
if (Math.max(...arrows) > 0 || Math.min(...arrows) < 0 ) {
// Make sure the arrow is in the middle of de cell
let xPos = -this.getCellWidth()/4;
let yPos = 0
let xPosNew = this.getCellWidth()/4
let yPosNew = yPos;
// Relative positioning & rotating
this.context.save();
this.context.translate( this.getX(x) + this.getCellWidth()/2, this.getY(y) + this.getCellHeight()/2 );
if (Math.max(...arrows)>0) {
this.context.rotate( 2*Math.PI / 4 * (arrows.indexOf(Math.max(...arrows))-1) );
} else {
this.context.rotate( 2*Math.PI / 4 * (arrows.indexOf(Math.min(...arrows))-1) );
}
// Draw the arrow
this.context.beginPath();
this.context.moveTo(xPos, yPos);
this.context.lineTo(xPosNew, yPosNew);
this.context.lineTo(xPosNew-10, yPosNew-10);
this.context.moveTo(xPosNew, yPosNew);
this.context.lineTo(xPosNew-10, yPosNew+10);
// Red or green arrow?
if (Math.max(...arrows)>0) {
this.context.strokeStyle = 'rgb(0,' + Math.floor((255/100)* arrows[arrows.indexOf(Math.max(...arrows))]) + ',0)';
} else {
this.context.strokeStyle = 'rgb(' + -1 * Math.floor((255/100)* arrows[arrows.indexOf(Math.min(...arrows))]) + ',0,0)';
}
this.context.stroke();
this.context.restore();
}
}
// Helper functions
getCellWidth() { return this.maxWidth / this.numberOfColumns - this.context.lineWidth; }
getCellHeight() { return this.maxHeight / this.numerOfRows - this.context.lineWidth; }
getX(position) { return this.maxWidth / this.numberOfColumns * position + this.context.lineWidth; }
getY(position) { return this.maxHeight / this.numerOfRows * position + this.context.lineWidth; }
// Draw a button
drawButton(icon, position, selected) {
let image = new Image();
let xPos = 350;
let yPos = 50*(position-1);
let width = 50;
let height = 50;
// Green if selected, black otherwise
if (selected) {
this.context.strokeStyle = '#00FF00';
} else {
this.context.strokeStyle = '#000000';
}
this.context.beginPath();
this.context.rect(xPos, yPos, width,height);
this.context.stroke();
if (icon=='') {
// For the 'empty' icon
} else {
image.src = icon;
image.onload = function() {
context.clearRect(xPos+5, yPos+5, width-10, height-10);
context.drawImage(image, xPos+5, yPos+5, width-10, height-10);
};
}
}
// Set all buttons to 'not selected'
resetButtons(onlyTheFirstFour) {
this.drawButton(this.flagImg, 1, false);
this.drawButton(this.enemyImg, 2, false);
this.drawButton(this.treasureImg, 3, false);
this.drawButton('', 4, false);
if (!onlyTheFirstFour) {
this.drawButton(this.playImg, 6, false);
speed = 0;
}
}
// Move Cap'n Jesse from one cell to the other
moveCpnJesse(oldX, oldY, x, y) {
if (this.board[y].charAt(x)!='E') {
this.drawIcon(this.cpnjesseImg, x, y);
}
this.drawCell(parseInt(oldX,10), parseInt(oldY,10));
}
// Helper function to wait for N ms
waitSomeTime(ms) {
return new Promise(resolve => {
setTimeout(() => {
resolve('resolved');
}, ms);
});
}
// The q-learning magic...
async train(epsilon, discount) {
// Keep training until stopped
while(speed > 0) {
let currentState = this.startingState;
let nextState;
let value;
let action;
let endIteration = false;
// Keep going until iteration is ended (teasure is found)
while(!endIteration) {
let arr = currentState.split('-');
let x = arr[0];
let y = arr[1];
let oldX = x;
let oldY = y;
// Choose next action based on E-greedy:
// - If random < epsilon setting:
// - If QTable has values other than zero: get best actions
// - If QTable has only zeroes: choose random action
// - If random >= epsilon setting: choose random action
if( Math.random() < epsilon ) {
if (Math.max(...this.qTable.get(currentState)) == 0) {
do {
action = Math.floor((Math.random() * 4 ));
} while ( this.qTable.get(currentState)[action] != 0 )
} else {
action = this.qTable.get(currentState).indexOf(Math.max(...this.qTable.get(currentState))) ;
}
} else {
action = Math.floor((Math.random() * 4 ));
}
// Move based on action
switch (action) {
case 0: y--; break; // up
case 1: x++; break; // right
case 2: y++; break; // down
case 3: x--; break; // left
}
nextState = x + '-' + y;
let tableValues = this.qTable.get(currentState);
if (this.qTable.get(nextState) != undefined) {
this.moveCpnJesse(oldX, oldY, x, y);
await this.waitSomeTime(100/Math.pow(speed,3));
// Assign value based on new state
let values = Array.from(tableValues);
switch (this.board[y].charAt(x)) {
case '-' : value = 0; break;
case 'Q' : value = -100; break;
case 'E' : value = 100; endIteration = true; break;
default : value = 0; break;
}
// Q function...
let max = Math.max.apply(null, this.qTable.get(nextState));
if (max < 0) { max = 0 }
values[ action ] =
values[ action ] +
(1 * // learning rate
(value +
discount * max -
values[ action ]));
this.qTable.set(currentState, values)
currentState = x + '-' + y;
} else {
// No state found, off the map :). Try again for another action.
}
}
}
}
printQTable() {
console.log(this.qTable);
}
}
// Here all the user input is handled
document.onreadystatechange = function () {
if (document.readyState === 'complete') {
speed = 0;
choice = 0;
canvas = document.getElementById('cpnjessecanvas');
context = canvas.getContext('2d');
cpnJesseQLearner = new cpnJesseQLearner(context);
canvas.addEventListener('click', (e) => {
let x = e.offsetX;
let y = e.offsetY;
// Secret button :)
if (x>350 && x<400 && y>200 && y<250) {
cpnJesseQLearner.printQTable();
}
// Play / Fast forward / Pause button clicked?
if (x>350 && x<400 && y>250 && y<300) {
cpnJesseQLearner.resetButtons(true);
choice = 0;
if (speed == 0) {
cpnJesseQLearner.drawButton(cpnJesseQLearner.fastforwardImg, 6, true);
speed = 1;
cpnJesseQLearner.train(
0.5, // e-greedy
0.9, // discount
);
} else if (speed == 1) {
speed = 2;
cpnJesseQLearner.drawButton(cpnJesseQLearner.pauseImg, 6, true);
} else if (speed == 2 ) {
speed = 0;
cpnJesseQLearner.drawButton(cpnJesseQLearner.playImg, 6, false);
}
}
// Flag button clicked?
if (x>350 && x<400 && y>0 && y<50) {
cpnJesseQLearner.resetButtons();
cpnJesseQLearner.drawButton(cpnJesseQLearner.flagImg, 1, true);
choice = 1;
}
// Enemy button clicked?
if (x>350 && x<400 && y>50 && y<100) {
cpnJesseQLearner.resetButtons();
cpnJesseQLearner.drawButton(cpnJesseQLearner.enemyImg, 2, true);
choice = 2;
}
// Treasure button clicked?
if (x>350 && x<400 && y>100 && y<150) {
cpnJesseQLearner.resetButtons();
cpnJesseQLearner.drawButton(cpnJesseQLearner.treasureImg, 3, true);
choice = 3;
}
// Empty button clicked?
if (x>350 && x<400 && y>150 && y<200) {
cpnJesseQLearner.resetButtons();
cpnJesseQLearner.drawButton('', 4, true);
choice = 4;
}
// Clicked on the grid?
if (x>0 && x<300 && y>0 && y<300) {
boardX = Math.floor( x / (cpnJesseQLearner.maxWidth / cpnJesseQLearner.numberOfColumns) );
boardY = Math.floor( y / (cpnJesseQLearner.maxHeight / cpnJesseQLearner.numerOfRows) );
// One of the first four buttons? Reset the QTable
if (choice>0 && choice<5) {
cpnJesseQLearner.resetQTable();
}
// Flag
if (choice==1) {
// Only set the flag if it doesn't cover the end
if ( cpnJesseQLearner.getBoard(boardX, boardY) != 'E' ) {
// Remove flag from original position
let arr = cpnJesseQLearner.startingState.split('-');
let oldX = arr[0];
let oldY = arr[1];
cpnJesseQLearner.setBoard( parseInt(oldX,10), parseInt(oldY,10), '-' );
// Set flag on new position
cpnJesseQLearner.setBoard( boardX, boardY, 'S' );
cpnJesseQLearner.startingState = boardX + '-' + boardY;
}
}
// Enemy
if (choice==2) {
// Only set the enemy if it doesn't cover the flag or end
if ( cpnJesseQLearner.getBoard(boardX, boardY) != 'E' && cpnJesseQLearner.getBoard(boardX, boardY) != 'S' ) {
cpnJesseQLearner.setBoard( boardX, boardY, 'Q' );
}
}
// Treasure
if (choice==3) {
// Only set the treasure if it doesn't cover the flag
if ( cpnJesseQLearner.getBoard(boardX, boardY) != 'S' ) {
// Remove treasure from original position
let arr = cpnJesseQLearner.endingState.split('-');
let oldX = arr[0];
let oldY = arr[1];
cpnJesseQLearner.setBoard( parseInt(oldX,10), parseInt(oldY,10), '-' );
// Set treasure on new position
cpnJesseQLearner.setBoard( boardX, boardY, 'E' );
cpnJesseQLearner.endingState = boardX + '-' + boardY;
}
}
// Empty
if (choice==4) {
// Only set it empty if it doesn't cover the flag or end
if ( cpnJesseQLearner.getBoard(boardX, boardY) != 'E' && cpnJesseQLearner.getBoard(boardX, boardY) != 'S' ) {
cpnJesseQLearner.setBoard( boardX, boardY, '-' );
}
}
}
});
}
}