Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 4 additions & 18 deletions lib/rl/draughts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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++) {
Expand All @@ -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++) {
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -302,17 +291,14 @@ 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()
}
return cp
}

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
Expand Down
22 changes: 4 additions & 18 deletions lib/rl/gem_puzzle.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { RLEnvironmentBase } from './base.js'

class GemPuzzleBoard {
constructor(size, evaluator) {
this._evaluator = evaluator
constructor(size) {
this._size = size

this.reset()
Expand Down Expand Up @@ -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]
Expand All @@ -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++) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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++) {
Expand All @@ -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]
Expand Down
22 changes: 4 additions & 18 deletions lib/rl/gomoku.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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++) {
Expand All @@ -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++) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand All @@ -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
Expand Down
22 changes: 4 additions & 18 deletions lib/rl/reversi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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++) {
Expand All @@ -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++) {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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]
Expand All @@ -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
Expand Down
21 changes: 0 additions & 21 deletions tests/lib/rl/draughts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
34 changes: 0 additions & 34 deletions tests/lib/rl/gem_puzzle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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')
Expand Down
28 changes: 7 additions & 21 deletions tests/lib/rl/gomoku.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
Loading