Implement abort of search on long thinking time
[hachu.git] / hachu.c
diff --git a/hachu.c b/hachu.c
index a926e65..478a20b 100644 (file)
--- a/hachu.c
+++ b/hachu.c
@@ -114,7 +114,8 @@ typedef struct {
 char *array, fenArray[4000], *reason;\r
 int bWidth, bHeight, bsize, zone, currentVariant, chuFlag, tenFlag, chessFlag, repDraws;\r
 int stm, xstm, hashKeyH, hashKeyL, framePtr, msp, nonCapts, rootEval, retMSP, retFirst, retDep, pvPtr, level, cnt50, mobilityScore;\r
-int nodes, startTime, tlim1, tlim2, repCnt, comp;\r
+int nodes, startTime, tlim1, tlim2, tlim3, repCnt, comp, abortFlag;\r
+Move ponderMove;\r
 Move retMove, moveStack[10000], path[100], repStack[300], pv[1000], repeatMove[300], killer[100][2];\r
 \r
       int maxDepth;                            // used by search\r
@@ -1565,6 +1566,8 @@ FireSet (UndoInfo *tb)
     if(p[i].pos != ABSENT) tb->fireMask |= fireFlags[i-2];\r
 }\r
 \r
+void TerminationCheck();\r
+\r
 #define QSdepth 0\r
 \r
 int\r
@@ -1573,7 +1576,7 @@ Search (int alpha, int beta, int difEval, int depth, int oldPromo, int promoSupp
   int i, j, k, firstMove, oldMSP = msp, curMove, sorted, bad, phase, king, iterDep, replyDep, nextVictim, to, defer, dubious, bestMoveNr;\r
   int resDep;\r
   int myPV = pvPtr;\r
-  int score, bestScore, curEval, iterAlpha;\r
+  int score, bestScore, oldBest, curEval, iterAlpha;\r
   Move move, nullMove;\r
   UndoInfo tb;\r
 #ifdef HASH\r
@@ -1598,7 +1601,7 @@ if(PATH) /*pboard(board),pmap(attacks, BLACK),*/printf("search(%d) {%d,%d} eval=
   alpha -= (alpha < curEval);\r
   beta  -= (beta <= curEval);\r
 \r
-  nodes++;\r
+  if(!(nodes++ & 4095)) TerminationCheck();\r
   pv[pvPtr++] = 0; // start empty PV, directly behind PV of parent\r
 \r
 \r
@@ -1779,6 +1782,11 @@ level--;
     repetition:\r
       UnMake(&tb);\r
       xstm = stm; stm ^= WHITE;\r
+      if(abortFlag > 0) { // unwind search\r
+printf("# abort (%d) @ %d\n", abortFlag, level);\r
+        if(curMove == firstMove) bestScore = oldBest, bestMoveNr = firstMove; // none searched yet\r
+        goto leave;\r
+      }\r
 #if 1\r
 if(PATH) printf("%d:%2d:%d %3d %6x %-10s %6d %6d\n", level, depth, iterDep, curMove, moveStack[curMove], MoveToText(moveStack[curMove], 0), score, bestScore);\r
 \r
@@ -1842,6 +1850,7 @@ if(PATH) printf("%d:%2d:%d %3d %6x %-10s %6d %6d\n", level, depth, iterDep, curM
     } else hashTable[index].move[hit] = 0;\r
 #endif\r
   } // next depth\r
+leave:\r
   retMSP = msp;\r
   retFirst = firstMove;\r
   msp = oldMSP; // pop move list\r
@@ -2088,7 +2097,7 @@ ListMoves ()
   int i;\r
   for(i=0; i< BSIZE; i++) boardCopy[i] = !!board[i];\r
 MapFromScratch(attacks);\r
-  postThinking--; repCnt = 0; tlim1 = tlim2 = 1e8; msp = 0;\r
+  postThinking--; repCnt = 0; tlim1 = tlim2 = tlim3 = 1e8; abortFlag = msp = 0;\r
   Search(-INF-1, INF+1, 0, 1, sup1 & ~PROMOTE, sup2);\r
   postThinking++;\r
   listStart = retFirst; listEnd = msp = retMSP;\r
@@ -2214,6 +2223,7 @@ SetSearchTimes (int timeLeft)
   if(timePerMove > 0) targetTime = 0.5*timeLeft, movesLeft = 1;\r
   tlim1 = 0.2*targetTime;\r
   tlim2 = 1.9*targetTime;\r
+  tlim3 = 5*timeLeft / (movesLeft + 4.1);\r
 }\r
 \r
 int\r
@@ -2274,6 +2284,12 @@ printf("# setup done");fflush(stdout);
       }\r
     }\r
 \r
+    void\r
+    TerminationCheck()\r
+    {\r
+        if(GetTickCount() - startTime > tlim3) abortFlag = 2;\r
+    }\r
+\r
     main()\r
     {\r
       int engineSide=NONE;                     // side played by engine\r
@@ -2288,6 +2304,7 @@ printf("# setup done");fflush(stdout);
 \r
         if(listEnd == 0) ListMoves();   // always maintain a list of legal moves in root position\r
 \r
+        abortFlag = 0;\r
         if(stm == engineSide) {         // if it is the engine's turn to move, set it thinking, and let it move\r
      \r
 pboard(board);\r