Enable book learning
authorH.G. Muller <h.g.muller@hccnet.nl>
Thu, 9 Jun 2011 10:05:54 +0000 (12:05 +0200)
committerH.G. Muller <h.g.muller@hccnet.nl>
Thu, 9 Jun 2011 10:05:54 +0000 (12:05 +0200)
Also use the learn info during probing.

book.c
xboard2uci.c

diff --git a/book.c b/book.c
index 9c7e04f..2a939f9 100644 (file)
--- a/book.c
+++ b/book.c
@@ -60,7 +60,7 @@ bool book_is_open(){
 void book_open(const char file_name[]) {
 
    ASSERT(file_name!=NULL);
-   if(FALSE && option_get_bool(Option,"BookLearn")){
+   if(option_get_bool(Option,"BookLearn")){
        BookFile = fopen(file_name,"rb+");
    }else{
        BookFile = fopen(file_name,"rb");
@@ -174,11 +174,12 @@ int book_move(const board_t * board, bool random) {
 void book_moves(list_t * list, const board_t * board) {
 
    int first_pos;
-   int sum;
+   double sum;
    int pos;
    entry_t entry[1];
    int move;
    int score;
+   uint32 weight[1000]; // [HGM] assumes not more than 1000 book moves per position!
    char move_string[256];
 
    ASSERT(board!=NULL);
@@ -201,7 +202,10 @@ void book_moves(list_t * list, const board_t * board) {
       read_entry(entry,pos);
       if (entry->key != board->key) break;
 
-      sum += entry->count;
+      weight[pos - first_pos] = 1000 * (uint32)entry->count;
+      if(option_get_bool(Option,"BookLearn")) // [HGM] improvised use of learn info
+          weight[pos - first_pos] *= ((uint32)entry->n + 10.) /((uint32)entry->sum + 1.);
+      sum += weight[pos - first_pos];
    }
 
    // disp
@@ -212,7 +216,7 @@ void book_moves(list_t * list, const board_t * board) {
       if (entry->key != board->key) break;
 
       move = entry->move;
-      score = (((uint32)entry->count)*((uint32)10000))/sum;  // 32 bit safe!
+      score = (10000.*weight[pos-first_pos])/sum;
 
       if (move != MoveNone && move_is_legal(move,board)) {
               list_add_ex(list,move,score);
index 762f1c8..fcc1812 100644 (file)
@@ -43,6 +43,7 @@ static const bool DelayPong = FALSE;
 typedef struct {
    int state;
    bool computer[ColourNb];
+   bool playedAllMoves[ColourNb];
    int exp_move;
    int hint_move;
    int resign_nb;
@@ -327,6 +328,8 @@ void xboard2uci_gui_step(char string[]) {
                        } else {
                                State->computer[White] = FALSE;
                                State->computer[Black] = TRUE;
+                               State->playedAllMoves[White] = TRUE; // [HGM]
+                               State->playedAllMoves[Black] = TRUE;
                        }
 
                        XB->new_hack = TRUE;
@@ -443,7 +446,7 @@ void xboard2uci_gui_step(char string[]) {
 
                                // book learning
 
-                               if (FALSE && option_get_bool(Option,"Book") &&
+                               if (option_get_bool(Option,"Book") &&
                     option_get_bool(Option,"BookLearn")) {
 
                                        if (FALSE) {
@@ -616,6 +619,9 @@ void xboard2uci_gui_step(char string[]) {
                                ASSERT(!XB->result);
                                XB->result = FALSE;
 
+                               // [HGM] externally supplied move means we did not fully play the current stm
+                               State->playedAllMoves[colour_is_white(game_turn(Game)) ? White : Black] = FALSE;
+
                                move_step(move);
                                no_mess(move);
 
@@ -846,6 +852,19 @@ void format_xboard_option_line(char * option_line, option_t *opt){
     }
 }
 
+// disarm() // [HGM] cleanse a string of offending double-quotes
+
+static char*disarm(const char *s){
+    static char buf[25];
+    char *p = buf, *q;
+    strncpy(buf, s, 24);
+    q = buf + strlen(buf) - 1;
+    while(*q == '"') *q-- = '\0';          // strip trailing quotes
+    while(*p == '"') p++;                  // strip leading quotes
+    while((q = strchr(p, '"'))) *q = '\''; // replace internal quotes
+    return p;
+}
+
 // send_xboard_options()
 
 static void send_xboard_options(){
@@ -860,7 +879,7 @@ static void send_xboard_options(){
     gui_send(GUI,"feature draw=1");
     gui_send(GUI,"feature ics=1");
     gui_send(GUI,"feature myname=\"%s\"",
-             option_get_string(Option,"EngineName"));
+             disarm(option_get_string(Option,"EngineName")));
     gui_send(GUI,"feature name=1");
     gui_send(GUI,"feature pause=0");
     gui_send(GUI,"feature ping=1");
@@ -1699,20 +1718,20 @@ static void learn(int result) {
    ASSERT(result>=-1&&result<=+1);
 
    ASSERT(XB->result);
-   ASSERT(State->computer[White]||State->computer[Black]);
+//   ASSERT(State->computer[White]||State->computer[Black]);
 
    // init
 
    pos = 0;
 
-   if (FALSE) {
-   } else if (State->computer[White]) {
+   // [HGM] does not account for the hypothetical possibility we played both sides!
+   if (State->playedAllMoves[White]) {
       pos = 0;
-   } else if (State->computer[Black]) {
+   } else if (State->playedAllMoves[Black]) {
       pos = 1;
       result = -result;
    } else {
-      my_fatal("learn(): unknown side\n");
+      return; // [HGM] if we did not play all moves for some side, do not learn, but don't make a fuss!
    }
 
    if (FALSE) {