From 836e9e6a346572fcb6a875af6c1444b1b65f14d1 Mon Sep 17 00:00:00 2001 From: nocturn9x Date: Mon, 13 May 2024 18:31:23 +0200 Subject: [PATCH] Time limits are updated when the engine stops pondering --- Chess/nimfish/nimfishpkg/search.nim | 16 +++++++++++----- Chess/nimfish/nimfishpkg/uci.nim | 1 + 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Chess/nimfish/nimfishpkg/search.nim b/Chess/nimfish/nimfishpkg/search.nim index adb3c45..7d4890c 100644 --- a/Chess/nimfish/nimfishpkg/search.nim +++ b/Chess/nimfish/nimfishpkg/search.nim @@ -121,6 +121,7 @@ type transpositionTable: ptr TTable history: ptr HistoryTable killers: ptr KillersTable + maxSearchTime: int64 # We keep one extra entry so we don't need any special casing # inside the search function when constructing pv lines pvMoves: array[MAX_DEPTH + 1, array[MAX_DEPTH + 1, Move]] @@ -248,11 +249,17 @@ proc reorderMoves(self: SearchManager, moves: var MoveList, ply: int) = proc timedOut(self: SearchManager): bool = getMonoTime() >= self.hardLimit proc isPondering*(self: var SearchManager): bool = self.pondering.load() -proc stopPondering*(self: var SearchManager) = self.pondering.store(false) proc cancelled(self: var SearchManager): bool = self.stop.load() proc elapsedTime(self: SearchManager): int64 = (getMonoTime() - self.searchStart).inMilliseconds() +proc stopPondering*(self: var SearchManager) = + self.pondering.store(false) + let t = getMonoTime() + self.hardLimit = t + initDuration(milliseconds=self.maxSearchTime) + self.softLimit = t + initDuration(milliseconds=self.maxSearchTime div 3) + + proc log(self: SearchManager, depth: int) = let elapsedMsec = self.elapsedTime().uint64 @@ -641,16 +648,15 @@ proc findBestLine*(self: var SearchManager, timeRemaining, increment: int64, max ## variation instead of the best move # Apparently negative remaining time is a thing. Welp - let - maxSearchTime = if not timePerMove: max(1, (timeRemaining div 10) + ((increment div 3) * 2)) else: timeRemaining - softLimit = if not timePerMove: maxSearchTime div 3 else: maxSearchTime + self.maxSearchTime = if not timePerMove: max(1, (timeRemaining div 10) + ((increment div 3) * 2)) else: timeRemaining + let softLimit = if not timePerMove: self.maxSearchTime div 3 else: self.maxSearchTime result = @[] var pv: array[256, Move] self.maxNodes = maxNodes self.pondering.store(ponder) self.searchMoves = searchMoves self.searchStart = getMonoTime() - self.hardLimit = self.searchStart + initDuration(milliseconds=maxSearchTime) + self.hardLimit = self.searchStart + initDuration(milliseconds=self.maxSearchTime) self.softLimit = self.searchStart + initDuration(milliseconds=softLimit) var maxDepth = maxDepth if maxDepth == -1: diff --git a/Chess/nimfish/nimfishpkg/uci.nim b/Chess/nimfish/nimfishpkg/uci.nim index bc4bd54..cda6164 100644 --- a/Chess/nimfish/nimfishpkg/uci.nim +++ b/Chess/nimfish/nimfishpkg/uci.nim @@ -353,6 +353,7 @@ proc startUCISession* = echo "id name Nimfish 0.1" echo "id author Nocturn9x & Contributors (see LICENSE)" echo "option name Hash type spin default 64 min 1 max 33554432" + echo "option name Threads type spin default 1 min 1 max 1" echo "uciok" var cmd: UCICommand