Skip to content

Commit

Permalink
test(bot): make competency test non-flakey by fixing the testing seed
Browse files Browse the repository at this point in the history
  • Loading branch information
DecentM committed Jan 12, 2024
1 parent c2303e2 commit 6872ca9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 27 deletions.
10 changes: 3 additions & 7 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@
"default": {
"runner": "nx/tasks-runners/default",
"options": {
"cacheableOperations": [
"build",
"lint",
"test",
"e2e"
]
"cacheableOperations": ["build", "lint", "test", "e2e"]
}
}
},
Expand All @@ -27,6 +22,7 @@
"inputs": [
"default",
"^default",
"{workspaceRoot}/jest.preset.js",
"{workspaceRoot}/jest.preset.js"
]
},
Expand All @@ -47,4 +43,4 @@
"e2eTestRunner": "cypress"
}
}
}
}
5 changes: 4 additions & 1 deletion packages/bot/src/engine/competency.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import { Bot } from '.'
test('saves the knight', (t) => {
t.timeout(Number.POSITIVE_INFINITY)

const bot = new Bot()
const bot = new Bot('0.5834624105388075')

bot.board.importAFEN(
'rnbqkbnr/ppppp3/8/5p>p1/6N1/7p/PPPPPP1P/RNBQKR>R1 w Qkq - 1 8'
)

const result = bot.findBestMove(3)

t.log(result)
t.log(bot.board.dump())

t.true(result.path?.split(',')[0].startsWith('Ng4'))
})
24 changes: 7 additions & 17 deletions packages/bot/src/engine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,19 @@ export const getBoardScore = (board: Board) => {
type SearchResult = {
score: number
index: number
seed: string
path?: string
}

type TranspositionTable = Map<string, SearchResult>

const TRANSPOSITION_TABLE_LIMIT = 10240
const TRANSPOSITION_TABLE_LIMIT = 1024000

export class Bot {
private transpositionTable: TranspositionTable = new Map()

public readonly board: Board

constructor() {
constructor(private seed: string = String(Math.random())) {
this.board = new Board()
this.board.importAFEN(AfenPreset.VanillaDefault)
}
Expand All @@ -93,18 +92,16 @@ export class Bot {

public findBestMove(
depth: number,
seed = String(Math.random()),
rng = seedrandom(seed),
rng = seedrandom(this.seed),
maxDepth = depth
) {
return this._findBestMove(this.board, depth, seed, rng, maxDepth)
return this._findBestMove(this.board, depth, rng, maxDepth)
}

private _findBestMove = (
board: Board,
depth: number,
seed = String(Math.random()),
rng = seedrandom(seed),
rng = seedrandom(this.seed),
maxDepth = depth
): SearchResult => {
const moves = board.getValidMoves()
Expand Down Expand Up @@ -145,7 +142,7 @@ export class Bot {
virtualBoard.executeNode(move)

// Default jitter to avoid playing the same moves every game
let score = rng() - 0.5
let score = (rng() - 0.5) / 5

// Prefer to play known openings
// if (depth === maxDepth) {
Expand Down Expand Up @@ -182,13 +179,7 @@ export class Bot {
let subResult = this.transpositionTable.get(virtualBoardAFEN)

if (!subResult) {
subResult = this._findBestMove(
virtualBoard,
depth - 1,
seed,
rng,
maxDepth
)
subResult = this._findBestMove(virtualBoard, depth - 1, rng, maxDepth)

this.transpositionTable.set(virtualBoardAFEN, subResult)
}
Expand All @@ -215,7 +206,6 @@ export class Bot {
const result: SearchResult = {
score,
index,
seed,
path,
}

Expand Down
4 changes: 2 additions & 2 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"resolveJsonModule": true,
"baseUrl": ".",
"paths": {
"@decentm/allegiance-chess-core": ["packages/core/src/index.ts"],
"@decentm/allegiance-chess-bot": ["packages/bot/src/index.ts"]
"@decentm/allegiance-chess-bot": ["packages/bot/src/index.ts"],
"@decentm/allegiance-chess-core": ["packages/core/src/index.ts"]
}
},
"exclude": ["node_modules", "tmp"]
Expand Down

0 comments on commit 6872ca9

Please sign in to comment.