forked from tensorflow/tfjs-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake_game_test.js
439 lines (391 loc) · 14.7 KB
/
snake_game_test.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
/**
* @license
* Copyright 2019 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
import * as tf from '@tensorflow/tfjs';
import {ACTION_GO_STRAIGHT, ACTION_TURN_LEFT, ACTION_TURN_RIGHT, FRUIT_REWARD, getRandomAction, getStateTensor, NO_FRUIT_REWARD, SnakeGame, DEATH_REWARD} from "./snake_game";
import {expectArraysClose} from "@tensorflow/tfjs-core/dist/test_util";
describe('getRandomAction', () => {
it('getRandomAction', () => {
for (let i = 0; i < 40; ++i) {
const action = getRandomAction();
expect(
[ACTION_GO_STRAIGHT, ACTION_TURN_LEFT, ACTION_TURN_RIGHT].indexOf(action))
.not.toEqual(-1);
}
});
});
function manhanttanDistance(xy1, xy2) {
return Math.abs(xy1[0] - xy2[0]) + Math.abs(xy1[1] - xy2[1]);
}
describe('SnakeGame', () => {
it('SnakeGame constructor: no-arg', () => {
const game = new SnakeGame();
expect(game.height).toEqual(16);
expect(game.width).toEqual(16);
});
it('SnakeGame constructor: with args', () => {
const game = new SnakeGame({height: 12, width: 14});
expect(game.height).toEqual(12);
expect(game.width).toEqual(14);
});
it('Invalid height and/or width leads to Error', () => {
expect(() => new SnakeGame({height: -12})).toThrowError(
/Expected height to be a positive number/);
expect(() => new SnakeGame({height: 1.2})).toThrowError(
/Expected height to be an integer/);
expect(() => new SnakeGame({width: -12})).toThrowError(
/Expected width to be a positive number/);
expect(() => new SnakeGame({width: 1.2})).toThrowError(
/Expected width to be an integer/);
});
it('getState: 1 fruit', async () => {
// Since the board generation process is random, run the testing
// for multiple iterations to increase the likelihood of finding
// nondeterministic testing failures.
for (let i = 0; i < 40; ++i) {
const game = new SnakeGame({height: 4, width: 4, initLen: 2});
const {s, f} = game.getState();
expect(s).toEqual(game.snakeSquares_);
expect(f).toEqual(game.fruitSquares_);
expect(s.length).toBeGreaterThanOrEqual(2);
for (let i = 0; i < s.length; ++i) {
expect(s[i].length).toEqual(2);
const [y, x] = s[i];
expect(Number.isInteger(y));
expect(y).toBeGreaterThanOrEqual(0);
expect(y).toBeLessThan(4);
expect(Number.isInteger(x));
expect(x).toBeGreaterThanOrEqual(0);
expect(x).toBeLessThan(4);
// Verify that the snake squares are consecutive (i.e.,
// with a Manhattan distance of 1.
if (i > 0) {
expect(manhanttanDistance(s[i - 1], s[i])).toEqual(1);
}
}
expect(f.length).toEqual(1);
f.forEach(item => {
expect(item.length).toEqual(2);
});
// Check that the snake and fruit are non-overlapping.
const sIndices = s.map(yx => yx[0] * 4 + yx[1]);
const fIndices = f.map(yx => yx[0] * 4 + yx[1]);
expect(sIndices.indexOf(fIndices[0])).toEqual(-1);
}
});
it('getState: 2 fruits', async () => {
for (let i = 0; i < 40; ++i) {
const game = new SnakeGame({
height: 5,
width: 5,
initLen: 2,
numFruits: 2
});
const {s, f} = game.getState();
expect(s).toEqual(game.snakeSquares_);
expect(f).toEqual(game.fruitSquares_);
expect(s.length).toBeGreaterThanOrEqual(2);
for (let i = 0; i < s.length; ++i) {
expect(s[i].length).toEqual(2);
const [y, x] = s[i];
expect(Number.isInteger(y));
expect(y).toBeGreaterThanOrEqual(0);
expect(y).toBeLessThan(5);
expect(Number.isInteger(x));
expect(x).toBeGreaterThanOrEqual(0);
expect(x).toBeLessThan(5);
// Verify that the snake squares are consecutive (i.e.,
// with a Manhattan distance of 1.
if (i > 0) {
expect(manhanttanDistance(s[i - 1], s[i])).toEqual(1);
}
}
expect(f.length).toEqual(2);
f.forEach(item => expect(item.length).toEqual(2));
// Check that the snake and fruit are non-overlapping.
const sIndices = s.map(yx => yx[0] * 5 + yx[1]);
const fIndices = f.map(yx => yx[0] * 5 + yx[1]);
for (let i = 0; i < fIndices.length; ++i) {
expect(sIndices.indexOf(fIndices[i])).toEqual(-1);
}
// Check that the two fruits are non-overlapping.
expect(sIndices[0] !== sIndices[1]).toEqual(true);
}
});
it('step: not done, no fruit eaten', () => {
const game = new SnakeGame({height: 5, width: 5, initLen: 2});
// Manually set the positions of the snake and the fruit for testing.
game.snakeSquares_ = [[4, 1], [4, 0]];
game.fruitSquares_ = [[0, 4]];
let out = game.step(ACTION_GO_STRAIGHT);
let reward = out.reward;
let done = out.done;
let fruitEaten = out.fruitEaten;
expect(game.snakeDirection).toEqual('r');
expect(reward).toEqual(NO_FRUIT_REWARD);
expect(done).toEqual(false);
expect(fruitEaten).toEqual(false);
expect(game.snakeSquares_).toEqual([[4, 2], [4, 1]]);
expect(game.fruitSquares_).toEqual([[0, 4]]);
out = game.step(ACTION_TURN_LEFT);
reward = out.reward;
done = out.done;
fruitEaten = out.fruitEaten;
expect(game.snakeDirection).toEqual('u');
expect(reward).toEqual(NO_FRUIT_REWARD);
expect(done).toEqual(false);
expect(fruitEaten).toEqual(false);
expect(game.snakeSquares_).toEqual([[3, 2], [4, 2]]);
expect(game.fruitSquares_).toEqual([[0, 4]]);
out = game.step(ACTION_TURN_RIGHT);
reward = out.reward;
done = out.done;
fruitEaten = out.fruitEaten;
expect(reward).toEqual(NO_FRUIT_REWARD);
expect(game.snakeDirection).toEqual('r');
expect(done).toEqual(false);
expect(fruitEaten).toEqual(false);
expect(game.snakeSquares_).toEqual([[3, 3], [3, 2]]);
expect(game.fruitSquares_).toEqual([[0, 4]]);
out = game.step(ACTION_TURN_RIGHT);
reward = out.reward;
done = out.done;
fruitEaten = out.fruitEaten;
expect(game.snakeDirection).toEqual('d');
expect(reward).toEqual(NO_FRUIT_REWARD);
expect(done).toEqual(false);
expect(fruitEaten).toEqual(false);
expect(game.snakeSquares_).toEqual([[4, 3], [3, 3]]);
expect(game.fruitSquares_).toEqual([[0, 4]]);
});
it('step: goes off left edge of board', () => {
const game = new SnakeGame({height: 5, width: 5, initLen: 2});
// Manually set the positions of the snake and the fruit for testing.
game.snakeSquares_ = [[4, 0], [4, 1]];
game.fruitSquares_ = [[0, 4]];
const {reward, done} = game.step(ACTION_GO_STRAIGHT);
expect(reward).toEqual(DEATH_REWARD);
expect(done).toEqual(true);
});
it('step: goes off top edge of board', () => {
const game = new SnakeGame({height: 5, width: 5, initLen: 2});
game.snakeSquares_ = [[0, 0], [1, 0]];
game.snakeDirection_ = 'u';
game.fruitSquares_ = [[0, 4]];
const {reward, done} = game.step(ACTION_GO_STRAIGHT);
expect(reward).toEqual(DEATH_REWARD);
expect(done).toEqual(true);
});
it('step: goes off right edge of board', () => {
const game = new SnakeGame({height: 5, width: 5, initLen: 2});
game.snakeSquares_ = [[3, 4], [3, 3]];
game.fruitSquares_ = [[0, 4]];
const {reward, done} = game.step(ACTION_GO_STRAIGHT);
expect(reward).toEqual(DEATH_REWARD);
expect(done).toEqual(true);
});
it('step: goes off bottom edge of board', () => {
const game = new SnakeGame({height: 5, width: 5, initLen: 2});
game.snakeSquares_ = [[4, 2], [4, 3]];
game.snakeDirection_ = 'l';
game.fruitSquares_ = [[0, 4]];
const {reward, done} = game.step(ACTION_TURN_LEFT);
expect(reward).toEqual(DEATH_REWARD);
expect(done).toEqual(true);
});
it('step: bumps into own body 1', () => {
const game = new SnakeGame({height: 5, width: 5, initLen: 4});
game.snakeSquares_ = [[2, 3], [3, 3], [3, 4], [2, 4]];
game.snakeDirection_ = 'u';
game.fruitSquares_ = [[0, 4]];
const {reward, done} = game.step(ACTION_TURN_RIGHT);
expect(game.snakeDirection).toEqual('r');
expect(reward).toEqual(DEATH_REWARD);
expect(done).toEqual(true);
});
it('step: fruit eaten', () => {
const game = new SnakeGame({height: 5, width: 5, initLen: 4});
game.snakeSquares_ = [[2, 3], [3, 3], [3, 4], [2, 4]];
game.snakeDirection_ = 'u';
game.fruitSquares_ = [[1, 3]];
const {reward, done, fruitEaten} = game.step(ACTION_GO_STRAIGHT);
expect(game.snakeDirection).toEqual('u');
expect(reward).toEqual(FRUIT_REWARD);
expect(fruitEaten).toEqual(true);
expect(done).toEqual(false);
// The snake ate a fruit. Its length should grow from 4 to 5.
const expectedSnakeSquares = [[1, 3], [2, 3], [3, 3], [3, 4], [2, 4]];
expect(game.snakeSquares_).toEqual(expectedSnakeSquares);
// Check that a new fruit has been generated.
expect(game.fruitSquares_.length).toEqual(1);
expect(game.fruitSquares_[0].length).toEqual(2);
const [fruitY, fruitX] = game.fruitSquares_[0];
expectedSnakeSquares.forEach(snakeYX => {
expect(snakeYX[0] === fruitY && snakeYX[1] === fruitX).toEqual(false);
});
});
it('reset after game over', () => {
const game = new SnakeGame({height: 5, width: 5, initLen: 4});
game.snakeSquares_ = [[2, 3], [3, 3], [3, 4], [2, 4]];
game.snakeDirection_ = 'u';
game.fruitSquares_ = [[0, 4]];
const {reward, done} = game.step(ACTION_TURN_RIGHT);
expect(game.snakeDirection).toEqual('r');
expect(reward).toEqual(DEATH_REWARD);
expect(done).toEqual(true);
const {s, f} = game.reset();
expect(s.length).toEqual(4);
expect(f.length).toEqual(1);
const [headY, headX] = s[0];
// Find a valid direction to step in.
// This works because the snake currently always starts from
// a straight horizontal pose.
const action = headY > 0 ? ACTION_TURN_LEFT : ACTION_TURN_RIGHT;
const out = game.step(action);
expect(out.done).toEqual(false);
});
it('reset after eating fruits and then dies', () => {
const game = new SnakeGame({height: 5, width: 5, initLen: 3});
game.snakeSquares_ = [[1, 3], [1, 2], [1, 1]];
game.fruitSquares_ = [[1, 4]];
let out = game.step(ACTION_GO_STRAIGHT);
expect(game.snakeDirection).toEqual('r');
expect(out.reward).toEqual(FRUIT_REWARD);
expect(out.done).toEqual(false);
expect(game.snakeSquares_).toEqual([[1, 4], [1, 3], [1, 2], [1, 1]]);
expect(game.fruitSquares_.length).toEqual(1);
game.fruitSquares_ = [[0, 4]];
out = game.step(ACTION_TURN_LEFT);
expect(game.snakeDirection).toEqual('u');
expect(out.reward).toEqual(FRUIT_REWARD);
expect(out.done).toEqual(false);
expect(game.snakeSquares_).toEqual(
[[0, 4], [1, 4], [1, 3], [1, 2], [1, 1]]);
expect(game.fruitSquares_.length).toEqual(1);
out = game.step(ACTION_GO_STRAIGHT);
expect(game.snakeDirection).toEqual('u');
expect(out.reward).toEqual(DEATH_REWARD);
expect(out.done).toEqual(true);
const {s, f} = game.reset();
expect(s.length).toEqual(3);
expect(f.length).toEqual(1);
});
});
describe('getStateTensor', () => {
it('1 frame, 1 fruit', () => {
const h = 4;
const w = 4;
const state = {
s: [[0, 0], [1, 0], [2, 0], [3, 0], [3, 1], [2, 1]],
f: [[2, 2]]
};
const tensor = getStateTensor(state, h, w);
expect(tensor.shape).toEqual([1, 4, 4, 2]);
expect(tensor.dtype).toEqual('float32');
const [snakeTensor, fruitTensor] = tensor.squeeze(0).unstack(-1);
expectArraysClose(snakeTensor, tf.tensor2d(
[[2, 0, 0, 0],
[1, 0, 0, 0],
[1, 1, 0, 0],
[1, 1, 0, 0]]));
expectArraysClose(fruitTensor, tf.tensor2d(
[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 0]]));
});
it('2 fruits', () => {
const h = 5;
const w = 5;
const state = {
s: [[4, 2], [4, 1], [4, 0], [3, 0], [2, 0], [2, 1], [2, 2]],
f: [[4, 4], [0, 0]]
};
const tensor = getStateTensor([state], h, w);
expect(tensor.shape).toEqual([1, 5, 5, 2]);
expect(tensor.dtype).toEqual('float32');
const [snakeTensor, fruitTensor] = tensor.squeeze(0).unstack(-1);
expectArraysClose(snakeTensor, tf.tensor2d(
[[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[1, 1, 1, 0, 0],
[1, 0, 0, 0, 0],
[1, 1, 2, 0, 0]]));
expectArraysClose(fruitTensor, tf.tensor2d(
[[1, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 1]]));
});
it('2 examples, both defined', () => {
const h = 4;
const w = 4;
const state1 = {
s: [[0, 0], [1, 0], [2, 0], [3, 0], [3, 1], [2, 1]],
f: [[2, 2]]
};
const state2 = {
s: [[0, 0], [1, 0], [2, 0], [3, 0], [3, 1]],
f: [[3, 3]]
};
const tensor = getStateTensor([state1, state2], h, w);
expect(tensor.shape).toEqual([2, 4, 4, 2]);
expectArraysClose(tensor.gather(0).gather(0, -1), tf.tensor2d(
[[2, 0, 0, 0],
[1, 0, 0, 0],
[1, 1, 0, 0],
[1, 1, 0, 0]]));
expectArraysClose(tensor.gather(0).gather(1, -1), tf.tensor2d(
[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 0]]));
expectArraysClose(tensor.gather(1).gather(0, -1), tf.tensor2d(
[[2, 0, 0, 0],
[1, 0, 0, 0],
[1, 0, 0, 0],
[1, 1, 0, 0]]));
expectArraysClose(tensor.gather(1).gather(1, -1), tf.tensor2d(
[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 1]]));
});
it('2 examples, one undefined', () => {
const h = 4;
const w = 4;
const state1 = undefined;
const state2 = {
s: [[0, 0], [1, 0], [2, 0], [3, 0], [3, 1]],
f: [[3, 3]]
};
const tensor = getStateTensor([state1, state2], h, w);
expect(tensor.shape).toEqual([2, 4, 4, 2]);
expectArraysClose(tensor.gather(0).gather(0, -1), tf.zeros([4, 4]));
expectArraysClose(tensor.gather(0).gather(1, -1), tf.zeros([4, 4]));
expectArraysClose(tensor.gather(1).gather(0, -1), tf.tensor2d(
[[2, 0, 0, 0],
[1, 0, 0, 0],
[1, 0, 0, 0],
[1, 1, 0, 0]]));
expectArraysClose(tensor.gather(1).gather(1, -1), tf.tensor2d(
[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 1]]));
});
});