Version 0.10: null move, hash table and tsume
authorH.G. Muller <h.g.muller@hccnet.nl>
Fri, 27 Sep 2013 11:57:46 +0000 (13:57 +0200)
committerH.G. Muller <h.g.muller@hccnet.nl>
Mon, 21 Oct 2013 08:40:26 +0000 (10:40 +0200)
The hash table and null move are switched on and debugged.
A tsume option is added, where one side only searches checking moves.

hachu.c

diff --git a/hachu.c b/hachu.c
index 3ba7ff0..f1c7ff9 100644 (file)
--- a/hachu.c
+++ b/hachu.c
 // promotions by pieces with Lion power stepping in & out the zone in same turn\r
 // promotion on capture\r
 \r
-#define VERSION "0.9beta"\r
+#define VERSION "0.10 null"\r
 \r
-#define PATH level==0 /*|| path[0] == 0x57097 &&  (level==1 || path[1] == 0x3f081 && (level == 2 || path[2] == 0x6f0ac && (level == 3 || path[3] == 0x3e865 && (level == 4 || path[4] == 0x4b865 && (level == 5)))))*/\r
+#define PATH level==0 || path[0] == 0x848f1 &&  (level==1 /*|| path[1] == 0x3f081 && (level == 2 || path[2] == 0x6f0ac && (level == 3 || path[3] == 0x3e865 && (level == 4 || path[4] == 0x4b865 && (level == 5))))*/)\r
 //define PATH 0\r
 \r
-#define XHASH\r
+#define HASH\r
 #define XKILLERS\r
-#define XNULLMOVE\r
+#define NULLMOVE\r
 \r
 #include <stdio.h>\r
 #include <stdlib.h>\r
@@ -129,8 +129,8 @@ typedef struct {
 } UndoInfo;\r
 \r
 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 bWidth, bHeight, bsize, zone, currentVariant, chuFlag, tenFlag, chessFlag, repDraws, tsume;\r
+int stm, xstm, hashKeyH=1, hashKeyL=1, framePtr, msp, nonCapts, rootEval, retMSP, retFirst, retDep, pvPtr, level, cnt50, mobilityScore;\r
 int nodes, startTime, lastRootMove, lastRootIter, 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
@@ -558,6 +558,7 @@ typedef struct {
     int postThinking;\r
     int resign;         // engine-defined option\r
     int contemptFactor; // likewise\r
+    int seed;\r
 \r
 int squareKey[BSIZE];\r
 \r
@@ -1602,7 +1603,7 @@ int
 Search (int alpha, int beta, int difEval, int depth, int oldPromo, int promoSuppress, int threshold)\r
 {\r
   int i, j, k, phase, king, nextVictim, to, defer, autoFail=0;\r
-  int start, firstMove, oldMSP = msp, curMove, sorted, bad, dubious, bestMoveNr;\r
+  int firstMove, oldMSP = msp, curMove, sorted, bad, dubious, bestMoveNr;\r
   int resDep, iterDep, ext;\r
   int myPV = pvPtr;\r
   int score, bestScore, oldBest, curEval, iterAlpha;\r
@@ -1614,6 +1615,16 @@ Search (int alpha, int beta, int difEval, int depth, int oldPromo, int promoSupp
 if(PATH) /*pboard(board),pmap(attacks, BLACK),*/printf("search(%d) {%d,%d} eval=%d, stm=%d (flag=%d)\n",depth,alpha,beta,difEval,stm,abortFlag),fflush(stdout);\r
   xstm = stm ^ WHITE;\r
 //printf("map made\n");fflush(stdout);\r
+\r
+  // TSUME filter\r
+  if(tsume && tsume & stm+1) {\r
+    k = p[king=royal[stm]].pos;\r
+//    if( k == ABSENT) k = p[king + 2].pos;\r
+    if( k != ABSENT && !attacks[2*k + xstm]) {\r
+      retDep = 60; return INF; // we win when not in check\r
+    }\r
+  }\r
+\r
   // KING CAPTURE\r
   k = p[king=royal[xstm]].pos;\r
   if( k != ABSENT) {\r
@@ -1634,22 +1645,27 @@ if(PATH) /*pboard(board),pmap(attacks, BLACK),*/printf("search(%d) {%d,%d} eval=
   pv[pvPtr++] = 0; // start empty PV, directly behind PV of parent\r
 \r
 \r
-  firstMove = curMove = sorted = start = msp += 50; // leave 50 empty slots in front of move list\r
+  firstMove = curMove = sorted = msp += 50; // leave 50 empty slots in front of move list\r
   iterDep = -(depth == 0); tb.fireMask = phase = 0;\r
 \r
 #ifdef HASH\r
-  index = hashKeyL & hashMask; nr = hashKeyL >> 30; hit = -1;\r
+  index = (hashKeyL ^ 327*stm) & hashMask; nr = (hashKeyL >> 30) & 3; hit = -1;\r
   if(hashTable[index].lock[nr] == hashKeyH) hit = nr; else\r
   if(hashTable[index].lock[4]  == hashKeyH) hit = 4;\r
+if(PATH) printf("# probe hash index=%x hit=%d\n", index, hit),fflush(stdout);\r
   if(hit >= 0) {\r
     bestScore = hashTable[index].score[hit];\r
+    hashMove = hashTable[index].move[hit];\r
     if((bestScore <= alpha || hashTable[index].flag[hit] & H_LOWER) &&\r
        (bestScore >= beta  || hashTable[index].flag[hit] & H_UPPER)   ) {\r
-      iterDep = resDep = hashTable[index].depth[hit];\r
+      iterDep = resDep = hashTable[index].depth[hit]; bestMoveNr = 0;\r
+      if(!level) iterDep = 0; // no hash cutoff in root\r
     }\r
   } else { // decide on replacement\r
     if(depth >= hashTable[index].depth[nr]) hit = nr; else hit = 4;\r
+    hashMove = 0;\r
   }\r
+if(PATH) printf("# iterDep = %d score = %d move = %s\n",iterDep,bestScore,MoveToText(hashMove,0)),fflush(stdout);\r
 #endif\r
 \r
   if(depth > QSdepth && iterDep < QSdepth) iterDep = QSdepth; // full-width: start at least from 1-ply\r
@@ -1675,7 +1691,7 @@ if(PATH)printf("new moves, phase=%d\n", phase);
        switch(phase) {\r
          case 0: // null move\r
 #ifdef NULLMOVE\r
-           else if(curEval >= beta) {\r
+           if(depth > QSdepth && curEval >= beta) {\r
               int nullDep = depth - 3;\r
              stm ^= WHITE;\r
              score = -Search(-beta, 1-beta, -difEval, nullDep<QSdepth ? QSdepth : nullDep, promoSuppress & SQUARE, ABSENT, INF);\r
@@ -1687,13 +1703,14 @@ if(PATH)printf("new moves, phase=%d\n", phase);
 //if(PATH) printf("mask=%x\n",tb.fireMask),pbytes(fireBoard);\r
            phase = 1;\r
          case 1: // hash move\r
+           phase = 2;\r
 #ifdef HASH\r
-           if(hashMove) {\r
+           if(hashMove && (depth > QSdepth || // must be capture in QS\r
+                (hashMove & SQUARE) >= SPECIAL || board[hashMove & SQUARE] != EMPTY)) {\r
              moveStack[sorted = msp++] = hashMove;\r
              goto extractMove;\r
            }\r
 #endif\r
-           phase = 2;\r
          case 2: // capture-gen init\r
            nextVictim = xstm; autoFail = (depth == 0);\r
            phase = 3;\r
@@ -1767,7 +1784,7 @@ if(PATH) printf("# autofail end (%d-%d)\n", firstMove, msp);
 \r
       // MOVE EXTRACTION\r
     extractMove:\r
-if(flag & depth >= 0 || (PATH)) printf("%2d:%d extract %d/%d\n", depth, iterDep, curMove, msp);\r
+//if(flag & depth >= 0 || (PATH)) printf("%2d:%d extract %d/%d\n", depth, iterDep, curMove, msp);\r
       if(curMove > sorted) {\r
        move = moveStack[sorted=j=curMove];\r
        for(i=curMove+1; i<msp; i++)\r
@@ -1785,6 +1802,8 @@ if(flag & depth >= 0) printf("%2d:%d found %d/%d %08x %s\n", depth, iterDep, cur
       defer = MakeMove(move, &tb);\r
       ext = (depth == 0); // when out of depth we extend captures if there was no auto-fail-hi\r
 \r
+//      if(level == 1 && randomize) tb.booty += (hashKeyH * seed >> 24 & 31) - 20;\r
+\r
       if(autoFail) {\r
        UnMake(&tb); // never search moves during auto-fail phase\r
        xstm = stm; stm ^= WHITE;\r
@@ -1900,6 +1919,7 @@ if(PATH) printf("%d:%2d:%d %3d %6x %-10s %6d %6d  (%d)\n", level, depth, iterDep
     // hash store\r
     hashTable[index].lock[hit]  = hashKeyH;\r
     hashTable[index].depth[hit] = iterDep;\r
+    hashTable[index].score[hit] = bestScore;\r
     hashTable[index].flag[hit]  = (bestScore < beta) * H_UPPER;\r
     if(bestScore > alpha) {\r
       hashTable[index].flag[hit] |= H_LOWER;\r
@@ -1990,7 +2010,7 @@ pmoves(int start, int end)
 \r
     // some parameter of your engine\r
     #define MAXMOVES 2000 /* maximum game length  */\r
-    #define MAXPLY   20   /* maximum search depth */\r
+    #define MAXPLY   30   /* maximum search depth */\r
 \r
     #define OFF 0\r
     #define ON  1\r
@@ -2087,7 +2107,8 @@ Setup2 (char *fen)
   }\r
   SetUp(array, currentVariant);\r
   sup0 = sup1 = sup2 = ABSENT;\r
-  rootEval = cnt50 = hashKeyH = hashKeyL = moveNr = 0;\r
+  rootEval = cnt50 = moveNr = 0;\r
+  hashKeyH = hashKeyL = 87620895*currentVariant;\r
   return stm;\r
 }\r
 \r
@@ -2096,15 +2117,14 @@ SetMemorySize (int n)
 {\r
 #ifdef HASH\r
   static HashBucket *realHash;\r
-  static int oldSize;\r
-  int m = 1;\r
-  intptr_t l;\r
-  if(n != oldSize) {\r
+  static intptr_t oldSize;\r
+  intptr_t l, m = 1;\r
+  while(m*sizeof(HashBucket) <= n*512UL) m <<= 1; // take largest power-of-2 that fits\r
+  if(m != oldSize) {\r
     if(oldSize) free(realHash);\r
-    while(m*sizeof(HashBucket) <= n*512) m <<= 1;\r
-    m *= 1024; hashMask = m - 1;\r
-    realHash = calloc(m + 1, sizeof(HashBucket));\r
-    l = (intptr_t) realHash; hashTable = (HashBucket*) (l & ~63); // align with cache line\r
+    hashMask = m*1024 - 1; oldSize = m;\r
+    realHash = malloc(m*1024*sizeof(HashBucket) + 64);\r
+    l = (intptr_t) realHash; hashTable = (HashBucket*) (l & ~63UL); // align with cache line\r
   }\r
 #endif\r
 }\r
@@ -2371,12 +2391,17 @@ printf("# ponder hit\n");
       int i, score, curVarNr;\r
 \r
   Init(V_CHU); // Chu\r
+      seed = GetTickCount(); moveNr = 0; // initialize random\r
 \r
       while(1) { // infinite loop\r
 \r
         fflush(stdout);                 // make sure everything is printed before we do something that might take time\r
-        *inBuf = 0;\r
+        *inBuf = 0; if(moveNr >= 20) randomize = OFF;\r
+//if(moveNr >20) printf("resign\n");\r
 \r
+#ifdef HASH\r
+       if(hashMask)\r
+#endif\r
         if(listEnd == 0) ListMoves();   // always maintain a list of legal moves in root position\r
         abortFlag = -(ponder && WHITE+BLACK-stm == engineSide && moveNr); // pondering and opponent on move\r
         if(stm == engineSide || abortFlag && ponderMove) {      // if it is the engine's turn to move, set it thinking, and let it move\r
@@ -2447,12 +2472,19 @@ pboard(board);
           printf("feature myname=\"HaChu " VERSION "\" highlight=1\n");\r
           printf("feature option=\"Resign -check 0\"\n");           // example of an engine-defined option\r
           printf("feature option=\"Contempt -spin 0 -200 200\"\n"); // and another one\r
+          printf("feature option=\"Tsume -combo no /// White Mates /// Black mates\"\n");\r
           printf("feature done=1\n");\r
           continue;\r
         }\r
         if(!strcmp(command, "option")) { // setting of engine-define option; find out which\r
           if(sscanf(inBuf+7, "Resign=%d",   &resign)         == 1) continue;\r
           if(sscanf(inBuf+7, "Contempt=%d", &contemptFactor) == 1) continue;\r
+          if(sscanf(inBuf+7, "Tsume=%s", command) == 1) {\r
+           if(!strcmp(command, "no"))    tsume = 0; else\r
+           if(!strcmp(command, "White")) tsume = 1; else\r
+           if(!strcmp(command, "Black")) tsume = 2;\r
+           continue;\r
+         }\r
           continue;\r
         }\r
         if(!strcmp(command, "sd"))      { sscanf(inBuf, "sd %d", &maxDepth);    continue; }\r