diff --git a/lib/rl/draughts.js b/lib/rl/draughts.js index e3ad515c..ccbc6767 100644 --- a/lib/rl/draughts.js +++ b/lib/rl/draughts.js @@ -16,7 +16,7 @@ export default class DraughtsRLEnvironment extends RLEnvironmentBase { this._size = [8, 8] - this._board = new DraughtsBoard(this._size, this._evaluation) + this._board = new DraughtsBoard(this._size) this._reward = { win: 1, @@ -106,16 +106,6 @@ export default class DraughtsRLEnvironment extends RLEnvironmentBase { return s } - set evaluation(func) { - if (func) { - this._board._evaluator = this._evaluation = (board, turn) => { - return func(this._makeState(board, turn, this._turn)) - } - } else { - this._board._evaluator = this._evaluation = null - } - } - _makeState(board, agentturn, gameturn) { const s = [gameturn] for (let i = 0; i < this._size[0]; i++) { @@ -137,7 +127,7 @@ export default class DraughtsRLEnvironment extends RLEnvironmentBase { } _state2board(state, turn) { - const board = new DraughtsBoard(this._size, this._evaluation) + const board = new DraughtsBoard(this._size) const opturn = turn === RED ? WHITE : RED for (let i = 0, p = 1; i < this._size[0]; i++) { 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 { } class DraughtsBoard { - constructor(size, evaluator) { - this._evaluator = evaluator + constructor(size) { this._size = size this._lines = 3 @@ -302,7 +291,7 @@ class DraughtsBoard { } copy() { - const cp = new DraughtsBoard(this._size, this._evaluator) + const cp = new DraughtsBoard(this._size) for (let i = 0; i < this._size[0]; i++) { cp._board[i] = this._board[i].concat() } @@ -310,9 +299,6 @@ class DraughtsBoard { } score(turn) { - if (this._evaluator) { - return this._evaluator(this, turn) - } const count = this.count if (turn === RED) { return count.red + count.redking * 2 - count.white - count.whiteking * 4 diff --git a/lib/rl/gem_puzzle.js b/lib/rl/gem_puzzle.js index 3e672bbd..486db32f 100644 --- a/lib/rl/gem_puzzle.js +++ b/lib/rl/gem_puzzle.js @@ -1,8 +1,7 @@ import { RLEnvironmentBase } from './base.js' class GemPuzzleBoard { - constructor(size, evaluator) { - this._evaluator = evaluator + constructor(size) { this._size = size this.reset() @@ -57,7 +56,7 @@ class GemPuzzleBoard { } copy() { - const cp = new GemPuzzleBoard(this._size, this._evaluator) + const cp = new GemPuzzleBoard(this._size) for (let i = 0; i < this._size[0]; i++) { for (let j = 0; j < this._size[1]; j++) { cp._board[i][j] = this._board[i][j] @@ -67,9 +66,6 @@ class GemPuzzleBoard { } score() { - if (this._evaluator) { - return this._evaluator(this) - } let s = 0 for (let i = 0; i < this._size[0]; i++) { for (let j = 0; j < this._size[1]; j++) { @@ -489,7 +485,7 @@ export default class GemPuzzleRLEnvironment extends RLEnvironmentBase { this._size = [4, 4] - this._board = new GemPuzzleBoard(this._size, this._evaluation) + this._board = new GemPuzzleBoard(this._size) this._reward = { win: 10, @@ -524,16 +520,6 @@ export default class GemPuzzleRLEnvironment extends RLEnvironmentBase { return s } - set evaluation(func) { - if (func) { - this._board._evaluator = this._evaluation = board => { - return func(this._makeState(board)) - } - } else { - this._board._evaluator = this._evaluation = null - } - } - _makeState(board) { const s = [] for (let i = 0; i < this._size[0]; i++) { @@ -546,7 +532,7 @@ export default class GemPuzzleRLEnvironment extends RLEnvironmentBase { } _state2board(state) { - const board = new GemPuzzleBoard(this._size, this._evaluation) + const board = new GemPuzzleBoard(this._size) for (let i = 0, p = 0; i < this._size[0]; i++) { for (let j = 0; j < this._size[1]; j++, p++) { board._board[i][j] = state[p] === -1 ? null : state[p] diff --git a/lib/rl/gomoku.js b/lib/rl/gomoku.js index 9b2ead00..d490b89c 100644 --- a/lib/rl/gomoku.js +++ b/lib/rl/gomoku.js @@ -13,7 +13,7 @@ export default class GomokuRLEnvironment extends RLEnvironmentBase { this._size = [8, 8] - this._board = new GomokuBoard(this._size, this._evaluation) + this._board = new GomokuBoard(this._size) this._reward = { win: 1, @@ -50,16 +50,6 @@ export default class GomokuRLEnvironment extends RLEnvironmentBase { return s } - set evaluation(func) { - if (func) { - this._board._evaluator = this._evaluation = (board, turn) => { - return func(this._makeState(board, turn, this._turn)) - } - } else { - this._board._evaluator = this._evaluation = null - } - } - _makeState(board, agentturn, gameturn) { const s = [gameturn] for (let i = 0; i < this._size[0]; i++) { @@ -72,7 +62,7 @@ export default class GomokuRLEnvironment extends RLEnvironmentBase { } _state2board(state, turn) { - const board = new GomokuBoard(this._size, this._evaluation) + const board = new GomokuBoard(this._size) const opturn = board.nextTurn(turn) for (let i = 0, p = 1; i < this._size[0]; i++) { for (let j = 0; j < this._size[1]; j++, p++) { @@ -164,8 +154,7 @@ export default class GomokuRLEnvironment extends RLEnvironmentBase { } class GomokuBoard { - constructor(size, evaluator) { - this._evaluator = evaluator + constructor(size) { this._size = size this._a = 5 this._count = 0 @@ -215,7 +204,7 @@ class GomokuBoard { } copy() { - const cp = new GomokuBoard(this._size, this._evaluator) + const cp = new GomokuBoard(this._size) for (let i = 0; i < this._size[0]; i++) { for (let j = 0; j < this._size[1]; j++) { cp._board[i][j] = this._board[i][j] @@ -226,9 +215,6 @@ class GomokuBoard { } score(turn) { - if (this._evaluator) { - return this._evaluator(this, turn) - } const winner = this.winner const nt = this.nextTurn(turn) if (winner === turn) return this._size[0] * this._size[1] * 100 - this._count diff --git a/lib/rl/reversi.js b/lib/rl/reversi.js index 700b95ae..3f8b989a 100644 --- a/lib/rl/reversi.js +++ b/lib/rl/reversi.js @@ -22,7 +22,7 @@ export default class ReversiRLEnvironment extends RLEnvironmentBase { this._size = [8, 8] - this._board = new ReversiBoard(this._size, this._evaluation) + this._board = new ReversiBoard(this._size) this._turn = BLACK this._reward = { @@ -60,16 +60,6 @@ export default class ReversiRLEnvironment extends RLEnvironmentBase { return s } - set evaluation(func) { - if (func) { - this._board._evaluator = this._evaluation = (board, turn) => { - return func(this._makeState(board, turn, this._turn)) - } - } else { - this._board._evaluator = this._evaluation = null - } - } - _makeState(board, agentturn, gameturn) { const s = [gameturn] for (let i = 0; i < this._size[0]; i++) { @@ -82,7 +72,7 @@ export default class ReversiRLEnvironment extends RLEnvironmentBase { } _state2board(state, turn) { - const board = new ReversiBoard(this._size, this._evaluation) + const board = new ReversiBoard(this._size) const opturn = flipPiece(turn) for (let i = 0, p = 1; i < this._size[0]; i++) { for (let j = 0; j < this._size[1]; j++, p++) { @@ -186,8 +176,7 @@ export default class ReversiRLEnvironment extends RLEnvironmentBase { } class ReversiBoard { - constructor(size, evaluator) { - this._evaluator = evaluator + constructor(size) { this._size = size this.reset() @@ -257,7 +246,7 @@ class ReversiBoard { } copy() { - const cp = new ReversiBoard(this._size, this._evaluator) + const cp = new ReversiBoard(this._size) for (let i = 0; i < this._size[0]; i++) { for (let j = 0; j < this._size[1]; j++) { cp._board[i][j] = this._board[i][j] @@ -267,9 +256,6 @@ class ReversiBoard { } score(turn) { - if (this._evaluator) { - return this._evaluator(this, turn) - } const count = this.count if (turn === BLACK) { return count.black - count.white diff --git a/tests/lib/rl/draughts.test.js b/tests/lib/rl/draughts.test.js index 483e6d67..f705ea4c 100644 --- a/tests/lib/rl/draughts.test.js +++ b/tests/lib/rl/draughts.test.js @@ -18,27 +18,6 @@ describe('env', () => { expect(env.states).toHaveLength(1 + 8 * 4) }) - describe('evaluation', () => { - test('set', () => { - const env = new DraughtsRLEnvironment() - env.evaluation = state => { - expect(state).toHaveLength(1 + 8 * 4) - return 1 - } - - const score = env._board.score() - expect(score).toBe(1) - }) - - test('clear', () => { - const env = new DraughtsRLEnvironment() - env.evaluation = null - - const score = env._board.score() - expect(score).toBe(0) - }) - }) - describe('reset', () => { test('success', () => { const env = new DraughtsRLEnvironment() diff --git a/tests/lib/rl/gem_puzzle.test.js b/tests/lib/rl/gem_puzzle.test.js index da51cb39..75a21c4c 100644 --- a/tests/lib/rl/gem_puzzle.test.js +++ b/tests/lib/rl/gem_puzzle.test.js @@ -16,29 +16,6 @@ describe('env', () => { } }) - describe('evaluation', () => { - test('set', () => { - const env = new GemPuzzleRLEnvironment() - const n = env._size[0] * env._size[1] - env.evaluation = state => { - expect(state).toHaveLength(n) - return 1 - } - - const score = env._board.score() - expect(score).toBe(1) - }) - - test('clear', () => { - const env = new GemPuzzleRLEnvironment() - const orgScore = env._board.score() - env.evaluation = null - - const score = env._board.score() - expect(score).toBe(orgScore) - }) - }) - test('reset', () => { const env = new GemPuzzleRLEnvironment() const n = env._size[0] * env._size[1] @@ -228,17 +205,6 @@ describe('board', () => { expect(score).toBe(0) }) - - test('evaluator', () => { - const env = new GemPuzzleRLEnvironment() - const board = env._board - board._evaluator = () => { - return 1 - } - const score = board.score() - - expect(score).toBe(1) - }) }) test.todo('at') diff --git a/tests/lib/rl/gomoku.test.js b/tests/lib/rl/gomoku.test.js index 9e0c84b5..0e092a2e 100644 --- a/tests/lib/rl/gomoku.test.js +++ b/tests/lib/rl/gomoku.test.js @@ -18,27 +18,6 @@ describe('env', () => { expect(env.states).toHaveLength(1 + 8 * 8) }) - describe('evaluation', () => { - test('set', () => { - const env = new GomokuRLEnvironment() - env.evaluation = state => { - expect(state).toHaveLength(1 + 8 * 8) - return 1 - } - - const score = env._board.score() - expect(score).toBe(1) - }) - - test('clear', () => { - const env = new GomokuRLEnvironment() - env.evaluation = null - - const score = env._board.score() - expect(score).toBe(0) - }) - }) - describe('reset', () => { test('success', () => { const env = new GomokuRLEnvironment() @@ -288,6 +267,13 @@ describe('board', () => { }) describe('score', () => { + test('0', () => { + const env = new GomokuRLEnvironment() + const board = env._board + + expect(board.score(GomokuRLEnvironment.BLACK)).toBe(0) + }) + test('win', () => { const env = new GomokuRLEnvironment() const board = env._board diff --git a/tests/lib/rl/reversi.test.js b/tests/lib/rl/reversi.test.js index 76e1d5ca..19059d87 100644 --- a/tests/lib/rl/reversi.test.js +++ b/tests/lib/rl/reversi.test.js @@ -18,27 +18,6 @@ describe('env', () => { expect(env.states).toHaveLength(1 + 8 * 8) }) - describe('evaluation', () => { - test('set', () => { - const env = new ReversiRLEnvironment() - env.evaluation = state => { - expect(state).toHaveLength(1 + 8 * 8) - return 1 - } - - const score = env._board.score() - expect(score).toBe(1) - }) - - test('clear', () => { - const env = new ReversiRLEnvironment() - env.evaluation = null - - const score = env._board.score() - expect(score).toBe(0) - }) - }) - describe('reset', () => { test('success', () => { const env = new ReversiRLEnvironment()