Skip to content

Commit 68ac85c

Browse files
authored
Refactor game environments to remove evaluator parameter from board constructors (#1002)
* Refactor game environments to remove evaluator parameter from board constructors * Add score test for GomokuRLEnvironment to verify initial score is zero
1 parent 86dfc8c commit 68ac85c

File tree

8 files changed

+23
-169
lines changed

8 files changed

+23
-169
lines changed

lib/rl/draughts.js

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default class DraughtsRLEnvironment extends RLEnvironmentBase {
1616

1717
this._size = [8, 8]
1818

19-
this._board = new DraughtsBoard(this._size, this._evaluation)
19+
this._board = new DraughtsBoard(this._size)
2020

2121
this._reward = {
2222
win: 1,
@@ -106,16 +106,6 @@ export default class DraughtsRLEnvironment extends RLEnvironmentBase {
106106
return s
107107
}
108108

109-
set evaluation(func) {
110-
if (func) {
111-
this._board._evaluator = this._evaluation = (board, turn) => {
112-
return func(this._makeState(board, turn, this._turn))
113-
}
114-
} else {
115-
this._board._evaluator = this._evaluation = null
116-
}
117-
}
118-
119109
_makeState(board, agentturn, gameturn) {
120110
const s = [gameturn]
121111
for (let i = 0; i < this._size[0]; i++) {
@@ -137,7 +127,7 @@ export default class DraughtsRLEnvironment extends RLEnvironmentBase {
137127
}
138128

139129
_state2board(state, turn) {
140-
const board = new DraughtsBoard(this._size, this._evaluation)
130+
const board = new DraughtsBoard(this._size)
141131
const opturn = turn === RED ? WHITE : RED
142132
for (let i = 0, p = 1; i < this._size[0]; i++) {
143133
for (let j = i % 2 === 0 ? 1 : 0; j < this._size[1]; j += 2, p++) {
@@ -234,8 +224,7 @@ export default class DraughtsRLEnvironment extends RLEnvironmentBase {
234224
}
235225

236226
class DraughtsBoard {
237-
constructor(size, evaluator) {
238-
this._evaluator = evaluator
227+
constructor(size) {
239228
this._size = size
240229
this._lines = 3
241230

@@ -302,17 +291,14 @@ class DraughtsBoard {
302291
}
303292

304293
copy() {
305-
const cp = new DraughtsBoard(this._size, this._evaluator)
294+
const cp = new DraughtsBoard(this._size)
306295
for (let i = 0; i < this._size[0]; i++) {
307296
cp._board[i] = this._board[i].concat()
308297
}
309298
return cp
310299
}
311300

312301
score(turn) {
313-
if (this._evaluator) {
314-
return this._evaluator(this, turn)
315-
}
316302
const count = this.count
317303
if (turn === RED) {
318304
return count.red + count.redking * 2 - count.white - count.whiteking * 4

lib/rl/gem_puzzle.js

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { RLEnvironmentBase } from './base.js'
22

33
class GemPuzzleBoard {
4-
constructor(size, evaluator) {
5-
this._evaluator = evaluator
4+
constructor(size) {
65
this._size = size
76

87
this.reset()
@@ -57,7 +56,7 @@ class GemPuzzleBoard {
5756
}
5857

5958
copy() {
60-
const cp = new GemPuzzleBoard(this._size, this._evaluator)
59+
const cp = new GemPuzzleBoard(this._size)
6160
for (let i = 0; i < this._size[0]; i++) {
6261
for (let j = 0; j < this._size[1]; j++) {
6362
cp._board[i][j] = this._board[i][j]
@@ -67,9 +66,6 @@ class GemPuzzleBoard {
6766
}
6867

6968
score() {
70-
if (this._evaluator) {
71-
return this._evaluator(this)
72-
}
7369
let s = 0
7470
for (let i = 0; i < this._size[0]; i++) {
7571
for (let j = 0; j < this._size[1]; j++) {
@@ -489,7 +485,7 @@ export default class GemPuzzleRLEnvironment extends RLEnvironmentBase {
489485

490486
this._size = [4, 4]
491487

492-
this._board = new GemPuzzleBoard(this._size, this._evaluation)
488+
this._board = new GemPuzzleBoard(this._size)
493489

494490
this._reward = {
495491
win: 10,
@@ -524,16 +520,6 @@ export default class GemPuzzleRLEnvironment extends RLEnvironmentBase {
524520
return s
525521
}
526522

527-
set evaluation(func) {
528-
if (func) {
529-
this._board._evaluator = this._evaluation = board => {
530-
return func(this._makeState(board))
531-
}
532-
} else {
533-
this._board._evaluator = this._evaluation = null
534-
}
535-
}
536-
537523
_makeState(board) {
538524
const s = []
539525
for (let i = 0; i < this._size[0]; i++) {
@@ -546,7 +532,7 @@ export default class GemPuzzleRLEnvironment extends RLEnvironmentBase {
546532
}
547533

548534
_state2board(state) {
549-
const board = new GemPuzzleBoard(this._size, this._evaluation)
535+
const board = new GemPuzzleBoard(this._size)
550536
for (let i = 0, p = 0; i < this._size[0]; i++) {
551537
for (let j = 0; j < this._size[1]; j++, p++) {
552538
board._board[i][j] = state[p] === -1 ? null : state[p]

lib/rl/gomoku.js

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default class GomokuRLEnvironment extends RLEnvironmentBase {
1313

1414
this._size = [8, 8]
1515

16-
this._board = new GomokuBoard(this._size, this._evaluation)
16+
this._board = new GomokuBoard(this._size)
1717

1818
this._reward = {
1919
win: 1,
@@ -50,16 +50,6 @@ export default class GomokuRLEnvironment extends RLEnvironmentBase {
5050
return s
5151
}
5252

53-
set evaluation(func) {
54-
if (func) {
55-
this._board._evaluator = this._evaluation = (board, turn) => {
56-
return func(this._makeState(board, turn, this._turn))
57-
}
58-
} else {
59-
this._board._evaluator = this._evaluation = null
60-
}
61-
}
62-
6353
_makeState(board, agentturn, gameturn) {
6454
const s = [gameturn]
6555
for (let i = 0; i < this._size[0]; i++) {
@@ -72,7 +62,7 @@ export default class GomokuRLEnvironment extends RLEnvironmentBase {
7262
}
7363

7464
_state2board(state, turn) {
75-
const board = new GomokuBoard(this._size, this._evaluation)
65+
const board = new GomokuBoard(this._size)
7666
const opturn = board.nextTurn(turn)
7767
for (let i = 0, p = 1; i < this._size[0]; i++) {
7868
for (let j = 0; j < this._size[1]; j++, p++) {
@@ -164,8 +154,7 @@ export default class GomokuRLEnvironment extends RLEnvironmentBase {
164154
}
165155

166156
class GomokuBoard {
167-
constructor(size, evaluator) {
168-
this._evaluator = evaluator
157+
constructor(size) {
169158
this._size = size
170159
this._a = 5
171160
this._count = 0
@@ -215,7 +204,7 @@ class GomokuBoard {
215204
}
216205

217206
copy() {
218-
const cp = new GomokuBoard(this._size, this._evaluator)
207+
const cp = new GomokuBoard(this._size)
219208
for (let i = 0; i < this._size[0]; i++) {
220209
for (let j = 0; j < this._size[1]; j++) {
221210
cp._board[i][j] = this._board[i][j]
@@ -226,9 +215,6 @@ class GomokuBoard {
226215
}
227216

228217
score(turn) {
229-
if (this._evaluator) {
230-
return this._evaluator(this, turn)
231-
}
232218
const winner = this.winner
233219
const nt = this.nextTurn(turn)
234220
if (winner === turn) return this._size[0] * this._size[1] * 100 - this._count

lib/rl/reversi.js

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default class ReversiRLEnvironment extends RLEnvironmentBase {
2222

2323
this._size = [8, 8]
2424

25-
this._board = new ReversiBoard(this._size, this._evaluation)
25+
this._board = new ReversiBoard(this._size)
2626
this._turn = BLACK
2727

2828
this._reward = {
@@ -60,16 +60,6 @@ export default class ReversiRLEnvironment extends RLEnvironmentBase {
6060
return s
6161
}
6262

63-
set evaluation(func) {
64-
if (func) {
65-
this._board._evaluator = this._evaluation = (board, turn) => {
66-
return func(this._makeState(board, turn, this._turn))
67-
}
68-
} else {
69-
this._board._evaluator = this._evaluation = null
70-
}
71-
}
72-
7363
_makeState(board, agentturn, gameturn) {
7464
const s = [gameturn]
7565
for (let i = 0; i < this._size[0]; i++) {
@@ -82,7 +72,7 @@ export default class ReversiRLEnvironment extends RLEnvironmentBase {
8272
}
8373

8474
_state2board(state, turn) {
85-
const board = new ReversiBoard(this._size, this._evaluation)
75+
const board = new ReversiBoard(this._size)
8676
const opturn = flipPiece(turn)
8777
for (let i = 0, p = 1; i < this._size[0]; i++) {
8878
for (let j = 0; j < this._size[1]; j++, p++) {
@@ -186,8 +176,7 @@ export default class ReversiRLEnvironment extends RLEnvironmentBase {
186176
}
187177

188178
class ReversiBoard {
189-
constructor(size, evaluator) {
190-
this._evaluator = evaluator
179+
constructor(size) {
191180
this._size = size
192181

193182
this.reset()
@@ -257,7 +246,7 @@ class ReversiBoard {
257246
}
258247

259248
copy() {
260-
const cp = new ReversiBoard(this._size, this._evaluator)
249+
const cp = new ReversiBoard(this._size)
261250
for (let i = 0; i < this._size[0]; i++) {
262251
for (let j = 0; j < this._size[1]; j++) {
263252
cp._board[i][j] = this._board[i][j]
@@ -267,9 +256,6 @@ class ReversiBoard {
267256
}
268257

269258
score(turn) {
270-
if (this._evaluator) {
271-
return this._evaluator(this, turn)
272-
}
273259
const count = this.count
274260
if (turn === BLACK) {
275261
return count.black - count.white

tests/lib/rl/draughts.test.js

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,6 @@ describe('env', () => {
1818
expect(env.states).toHaveLength(1 + 8 * 4)
1919
})
2020

21-
describe('evaluation', () => {
22-
test('set', () => {
23-
const env = new DraughtsRLEnvironment()
24-
env.evaluation = state => {
25-
expect(state).toHaveLength(1 + 8 * 4)
26-
return 1
27-
}
28-
29-
const score = env._board.score()
30-
expect(score).toBe(1)
31-
})
32-
33-
test('clear', () => {
34-
const env = new DraughtsRLEnvironment()
35-
env.evaluation = null
36-
37-
const score = env._board.score()
38-
expect(score).toBe(0)
39-
})
40-
})
41-
4221
describe('reset', () => {
4322
test('success', () => {
4423
const env = new DraughtsRLEnvironment()

tests/lib/rl/gem_puzzle.test.js

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,6 @@ describe('env', () => {
1616
}
1717
})
1818

19-
describe('evaluation', () => {
20-
test('set', () => {
21-
const env = new GemPuzzleRLEnvironment()
22-
const n = env._size[0] * env._size[1]
23-
env.evaluation = state => {
24-
expect(state).toHaveLength(n)
25-
return 1
26-
}
27-
28-
const score = env._board.score()
29-
expect(score).toBe(1)
30-
})
31-
32-
test('clear', () => {
33-
const env = new GemPuzzleRLEnvironment()
34-
const orgScore = env._board.score()
35-
env.evaluation = null
36-
37-
const score = env._board.score()
38-
expect(score).toBe(orgScore)
39-
})
40-
})
41-
4219
test('reset', () => {
4320
const env = new GemPuzzleRLEnvironment()
4421
const n = env._size[0] * env._size[1]
@@ -228,17 +205,6 @@ describe('board', () => {
228205

229206
expect(score).toBe(0)
230207
})
231-
232-
test('evaluator', () => {
233-
const env = new GemPuzzleRLEnvironment()
234-
const board = env._board
235-
board._evaluator = () => {
236-
return 1
237-
}
238-
const score = board.score()
239-
240-
expect(score).toBe(1)
241-
})
242208
})
243209

244210
test.todo('at')

tests/lib/rl/gomoku.test.js

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,6 @@ describe('env', () => {
1818
expect(env.states).toHaveLength(1 + 8 * 8)
1919
})
2020

21-
describe('evaluation', () => {
22-
test('set', () => {
23-
const env = new GomokuRLEnvironment()
24-
env.evaluation = state => {
25-
expect(state).toHaveLength(1 + 8 * 8)
26-
return 1
27-
}
28-
29-
const score = env._board.score()
30-
expect(score).toBe(1)
31-
})
32-
33-
test('clear', () => {
34-
const env = new GomokuRLEnvironment()
35-
env.evaluation = null
36-
37-
const score = env._board.score()
38-
expect(score).toBe(0)
39-
})
40-
})
41-
4221
describe('reset', () => {
4322
test('success', () => {
4423
const env = new GomokuRLEnvironment()
@@ -288,6 +267,13 @@ describe('board', () => {
288267
})
289268

290269
describe('score', () => {
270+
test('0', () => {
271+
const env = new GomokuRLEnvironment()
272+
const board = env._board
273+
274+
expect(board.score(GomokuRLEnvironment.BLACK)).toBe(0)
275+
})
276+
291277
test('win', () => {
292278
const env = new GomokuRLEnvironment()
293279
const board = env._board

0 commit comments

Comments
 (0)