From 6872ca9d36b5d99582111604f5908c5c0f205dbc Mon Sep 17 00:00:00 2001 From: DecentM Date: Fri, 12 Jan 2024 09:54:17 +0200 Subject: [PATCH] test(bot): make competency test non-flakey by fixing the testing seed --- nx.json | 10 +++------ packages/bot/src/engine/competency.spec.ts | 5 ++++- packages/bot/src/engine/index.ts | 24 +++++++--------------- tsconfig.base.json | 4 ++-- 4 files changed, 16 insertions(+), 27 deletions(-) diff --git a/nx.json b/nx.json index e1929c0..c8701e0 100644 --- a/nx.json +++ b/nx.json @@ -3,12 +3,7 @@ "default": { "runner": "nx/tasks-runners/default", "options": { - "cacheableOperations": [ - "build", - "lint", - "test", - "e2e" - ] + "cacheableOperations": ["build", "lint", "test", "e2e"] } } }, @@ -27,6 +22,7 @@ "inputs": [ "default", "^default", + "{workspaceRoot}/jest.preset.js", "{workspaceRoot}/jest.preset.js" ] }, @@ -47,4 +43,4 @@ "e2eTestRunner": "cypress" } } -} \ No newline at end of file +} diff --git a/packages/bot/src/engine/competency.spec.ts b/packages/bot/src/engine/competency.spec.ts index eb463e5..e800147 100644 --- a/packages/bot/src/engine/competency.spec.ts +++ b/packages/bot/src/engine/competency.spec.ts @@ -5,7 +5,7 @@ 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' @@ -13,5 +13,8 @@ test('saves the knight', (t) => { const result = bot.findBestMove(3) + t.log(result) + t.log(bot.board.dump()) + t.true(result.path?.split(',')[0].startsWith('Ng4')) }) diff --git a/packages/bot/src/engine/index.ts b/packages/bot/src/engine/index.ts index 4180a08..8b6d2ac 100644 --- a/packages/bot/src/engine/index.ts +++ b/packages/bot/src/engine/index.ts @@ -67,20 +67,19 @@ export const getBoardScore = (board: Board) => { type SearchResult = { score: number index: number - seed: string path?: string } type TranspositionTable = Map -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) } @@ -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() @@ -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) { @@ -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) } @@ -215,7 +206,6 @@ export class Bot { const result: SearchResult = { score, index, - seed, path, } diff --git a/tsconfig.base.json b/tsconfig.base.json index 25b2a83..d90ba55 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -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"]