Skip to content

Commit

Permalink
Minor hash optimization (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenphuminh authored Jul 31, 2024
1 parent 0a59d3e commit 1d6d69c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion catto.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
// Current version to show in UCI
version: "v0.12.0",
version: "v0.12.1",
// Late move reduction config
lmrFullDepth: 4, // Number of moves to be searched in full depth
lmrMaxReduction: 3, // Only apply LMR above this depth
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "catto",
"version": "0.12.0",
"version": "0.12.1",
"description": "The Catto chess engine",
"main": "index.js",
"scripts": {
Expand Down
28 changes: 14 additions & 14 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,8 @@ export class Engine {
// Tranposition table
public hashTable: Record<string, HashEntry> = {};

recordHash(score: number, depth: number, hashFlag: HashFlag, move: Move) {
// if (this.stable) return;

const hash = genZobristKey(this.chess).toString();
recordHash(score: number, depth: number, hashFlag: HashFlag, move: Move, customHash?: string) {
const hash = customHash || genZobristKey(this.chess).toString();
// const hash = this.chess.fen();

if (score < -48000) score -= this.ply;
Expand All @@ -115,8 +113,8 @@ export class Engine {
}
}

probeHash(alpha: number, beta: number, depth: number): number {
const hash = genZobristKey(this.chess).toString();
probeHash(alpha: number, beta: number, depth: number, customHash?: string): number {
const hash = customHash || genZobristKey(this.chess).toString();
// const hash = this.chess.fen();

const hashEntry = this.hashTable[hash];
Expand Down Expand Up @@ -200,8 +198,8 @@ export class Engine {
return priority;
}

sortMoves(moves: Move[]) {
const currentBoardHash = genZobristKey(this.chess).toString();
sortMoves(moves: Move[], customHash?: string) {
const currentBoardHash = customHash || genZobristKey(this.chess).toString();

return moves
.map(move => ({ move, priority: this.getMovePrio(move, currentBoardHash) }))
Expand All @@ -211,7 +209,7 @@ export class Engine {

// Quiescence search
quiescence(alpha: number, beta: number): number {
// increment nodes count
// Increment nodes count
this.nodes++;

const evaluation = evaluateBoard(this.chess);
Expand Down Expand Up @@ -282,19 +280,21 @@ export class Engine {
// Init
const inCheck = this.chess.inCheck();
this.pvLength[this.ply] = this.ply;
// Hash onced to prevent duplications
const currentHash = genZobristKey(this.chess).toString();

this.nodes++;

let hashFlag = HashFlag.alpha;

// Detecting 3-fold repetition
if (this.ply && this.chess.isThreefoldRepetition()) {
this.recordHash(0, depth, HashFlag.exact, this.prevMove!);
this.recordHash(0, depth, HashFlag.exact, this.prevMove!, currentHash);
return 0;
}

// Check if position exists in transposition table
let score = this.probeHash(alpha, beta, depth);
let score = this.probeHash(alpha, beta, depth, currentHash);

if (this.ply && score !== 99999)
return score;
Expand Down Expand Up @@ -361,7 +361,7 @@ export class Engine {
// Enable PV move scoring
this.enablePVScoring(possibleMoves);

possibleMoves = this.sortMoves(possibleMoves);
possibleMoves = this.sortMoves(possibleMoves, currentHash);
let searchedMoves = 0, bestMoveSoFar: Move;

// Razoring, skipping an entire subtree
Expand Down Expand Up @@ -455,7 +455,7 @@ export class Engine {
// Fail-hard beta cutoff
if (score >= beta) {
// Store move in the case of a fail-hard beta cutoff
this.recordHash(beta, depth, HashFlag.beta, move);
this.recordHash(beta, depth, HashFlag.beta, move, currentHash);

if (!move.captured) { // Only quiet moves
// Store killer moves
Expand Down Expand Up @@ -497,7 +497,7 @@ export class Engine {
}
}

this.recordHash(alpha, depth, hashFlag, bestMoveSoFar!);
this.recordHash(alpha, depth, hashFlag, bestMoveSoFar!, currentHash);

// Node fails low
return alpha;
Expand Down

0 comments on commit 1d6d69c

Please sign in to comment.