version 1.4.63b
[polyglot.git] / book.c
1
2 // book.c
3
4 // includes
5
6 #include <errno.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10
11 #include "board.h"
12 #include "book.h"
13 #include "move.h"
14 #include "move_legal.h"
15 #include "san.h"
16 #include "util.h"
17 #include "option.h"
18
19 // types
20
21 typedef struct {
22    uint64 key;
23    uint16 move;
24    uint16 count;
25    uint16 n;
26    uint16 sum;
27 } entry_t;
28
29 // variables
30
31 static FILE * BookFile;
32 static int BookSize;
33
34 // prototypes
35
36 static int    find_pos      (uint64 key);
37
38 static void   read_entry    (entry_t * entry, int n);
39 static void   write_entry   (const entry_t * entry, int n);
40
41 static uint64 read_integer  (FILE * file, int size);
42 static void   write_integer (FILE * file, int size, uint64 n);
43
44 // functions
45
46 // book_clear()
47
48 void book_clear() {
49
50    BookFile = NULL;
51    BookSize = 0;
52 }
53
54 bool book_is_open(){
55     return BookFile!=NULL;
56 }
57
58 // book_open()
59
60 void book_open(const char file_name[]) {
61
62    ASSERT(file_name!=NULL);
63    if(option_get_bool(Option,"BookLearn")){
64        BookFile = fopen(file_name,"rb+");
65    }else{
66        BookFile = fopen(file_name,"rb");
67    }
68       
69 //   if (BookFile == NULL) my_fatal("book_open(): can't open file \"%s\": %s\n",file_name,strerror(errno));
70    if (BookFile == NULL)  return; 
71
72    if (fseek(BookFile,0,SEEK_END) == -1) {
73       my_fatal("book_open(): fseek(): %s\n",strerror(errno));
74    }
75
76    BookSize = ftell(BookFile) / 16;
77 //   if (BookSize == 0) my_fatal("book_open(): empty file\n");
78    if (BookSize == 0) {
79       book_close();
80       book_clear(); 
81    };
82 }
83
84 // book_close()
85
86 void book_close() {
87
88    if(BookFile==NULL) return;
89
90    if (fclose(BookFile) == EOF) {
91       my_fatal("book_close(): fclose(): %s\n",strerror(errno));
92    }
93 }
94
95 // is_in_book()
96
97 bool is_in_book(const board_t * board) {
98
99    int pos;
100    entry_t entry[1];
101
102    if(BookFile==NULL) return FALSE;
103
104    ASSERT(board!=NULL);
105
106    for (pos = find_pos(board->key); pos < BookSize; pos++) {
107       read_entry(entry,pos);
108       if (entry->key == board->key) return TRUE;
109    }
110
111    return FALSE;
112 }
113
114 // book_move()
115
116 int book_move(const board_t * board, bool random) {
117
118    int best_move;
119    int best_score;
120    int pos;
121    entry_t entry[1];
122    int move;
123    int score;
124    list_t list[1];
125    int i;
126
127    if(BookFile==NULL) return MoveNone;
128
129    ASSERT(board!=NULL);
130    ASSERT(random==TRUE||random==FALSE);
131
132    // init
133    
134    list_clear(list);
135
136    book_moves(list,board);
137
138    best_move = MoveNone;
139    best_score = 0;
140    for(i=0; i<list_size(list); i++){
141
142       move = list->move[i];
143       score = list->value[i];
144
145       if (move != MoveNone &&
146           move_is_legal(move,board) &&
147           score>10*option_get_int(Option,"BookTreshold")) {
148
149          // pick this move?
150
151          ASSERT(score>0);
152
153          if (random) {
154             best_score += score;
155             if (my_random_int(best_score) < score) best_move = move;
156          } else {
157             if (score > best_score) {
158                best_move = move;
159                best_score = score;
160             }
161          }
162
163       } else {
164
165          ASSERT(FALSE);
166       }
167    }
168
169    return best_move;
170 }
171
172 // book_moves()
173
174 void book_moves(list_t * list, const board_t * board) {
175
176    int first_pos;
177    int sum;
178    int pos;
179    entry_t entry[1];
180    int move;
181    int score;
182    char move_string[256];
183
184    ASSERT(board!=NULL);
185    ASSERT(list!=NULL);
186
187    if(BookFile==NULL) return;
188
189    // init
190
191    list_clear(list);
192
193    first_pos = find_pos(board->key);
194
195    // sum
196
197    sum = 0;
198
199    for (pos = first_pos; pos < BookSize; pos++) {
200
201       read_entry(entry,pos);
202       if (entry->key != board->key) break;
203
204       sum += entry->count;
205    }
206
207    // disp
208
209    for (pos = first_pos; pos < BookSize; pos++) {
210
211       read_entry(entry,pos);
212       if (entry->key != board->key) break;
213
214       move = entry->move;
215       score = (((uint32)entry->count)*((uint32)10000))/sum;  // 32 bit safe!
216
217       if (move != MoveNone && move_is_legal(move,board)) {
218               list_add_ex(list,move,score);
219       }
220    }
221
222 }
223
224
225 // book_disp()
226
227 void book_disp(const board_t * board) {
228
229    char move_string[256];
230    list_t list[1];
231    int i;
232    int treshold=option_get_int(Option,"BookTreshold");
233
234    ASSERT(board!=NULL);
235
236    if(BookFile==NULL) return;
237
238    book_moves(list,board);
239    
240    for(i=0; i<list_size(list); i++){
241        move_to_san(list->move[i],board,move_string,256);
242        if(list->value[i]>10*treshold){
243            printf(" %6s %5.2f%%\n",move_string,list->value[i]/100.0);
244        }else{
245            printf(" %6s %5.2f%% (below treshold %4.2f%%)\n",
246                   move_string,list->value[i]/100.0,treshold/10.0);
247        }
248    }
249    // this is necessary by the xboard protocol
250    printf("\n");
251 }
252
253 // book_learn_move()
254
255 void book_learn_move(const board_t * board, int move, int result) {
256
257    int pos;
258    entry_t entry[1];
259
260    if(BookFile==NULL) return;
261
262    ASSERT(board!=NULL);
263    ASSERT(move_is_ok(move));
264    ASSERT(result>=-1&&result<=+1);
265
266    ASSERT(move_is_legal(move,board));
267
268    for (pos = find_pos(board->key); pos < BookSize; pos++) {
269
270       read_entry(entry,pos);
271       if (entry->key != board->key) break;
272
273       if (entry->move == move) {
274
275          entry->n++;
276          entry->sum += result+1;
277
278          write_entry(entry,pos);
279
280          break;
281       }
282    }
283 }
284
285 // book_flush()
286
287 void book_flush() {
288
289    if(BookFile==NULL) return;
290
291    if (fflush(BookFile) == EOF) {
292       my_fatal("book_flush(): fflush(): %s\n",strerror(errno));
293    }
294 }
295
296 // find_pos()
297
298 static int find_pos(uint64 key) {
299
300    int left, right, mid;
301    entry_t entry[1];
302
303    // binary search (finds the leftmost entry)
304
305    left = 0;
306    right = BookSize-1;
307
308    ASSERT(left<=right);
309
310    while (left < right) {
311
312       mid = (left + right) / 2;
313       ASSERT(mid>=left&&mid<right);
314
315       read_entry(entry,mid);
316
317       if (key <= entry->key) {
318          right = mid;
319       } else {
320          left = mid+1;
321       }
322    }
323
324    ASSERT(left==right);
325
326    read_entry(entry,left);
327
328    return (entry->key == key) ? left : BookSize;
329 }
330
331 // read_entry()
332
333 static void read_entry(entry_t * entry, int n) {
334
335    ASSERT(entry!=NULL);
336    ASSERT(n>=0&&n<BookSize);
337
338    if (fseek(BookFile,n*16,SEEK_SET) == -1) {
339       my_fatal("read_entry(): fseek(): %s\n",strerror(errno));
340    }
341
342    entry->key   = read_integer(BookFile,8);
343    entry->move  = read_integer(BookFile,2);
344    entry->count = read_integer(BookFile,2);
345    entry->n     = read_integer(BookFile,2);
346    entry->sum   = read_integer(BookFile,2);
347 }
348
349 // write_entry()
350
351 static void write_entry(const entry_t * entry, int n) {
352
353    ASSERT(entry!=NULL);
354    ASSERT(n>=0&&n<BookSize);
355
356    if (fseek(BookFile,n*16,SEEK_SET) == -1) {
357       my_fatal("write_entry(): fseek(): %s\n",strerror(errno));
358    }
359
360    write_integer(BookFile,8,entry->key);
361    write_integer(BookFile,2,entry->move);
362    write_integer(BookFile,2,entry->count);
363    write_integer(BookFile,2,entry->n);
364    write_integer(BookFile,2,entry->sum);
365 }
366
367 // read_integer()
368
369 static uint64 read_integer(FILE * file, int size) {
370
371    uint64 n;
372    int i;
373    int b;
374
375    ASSERT(file!=NULL);
376    ASSERT(size>0&&size<=8);
377
378    n = 0;
379
380    for (i = 0; i < size; i++) {
381
382       b = fgetc(file);
383
384       if (b == EOF) {
385          if (feof(file)) {
386             my_fatal("read_integer(): fgetc(): EOF reached\n");
387          } else { // error
388             my_fatal("read_integer(): fgetc(): %s\n",strerror(errno));
389          }
390       }
391
392       ASSERT(b>=0&&b<256);
393       n = (n << 8) | b;
394    }
395
396    return n;
397 }
398
399 // write_integer()
400
401 static void write_integer(FILE * file, int size, uint64 n) {
402
403    int i;
404    int b;
405
406    ASSERT(file!=NULL);
407    ASSERT(size>0&&size<=8);
408    ASSERT(size==8||n>>(size*8)==0);
409
410    for (i = size-1; i >= 0; i--) {
411
412       b = (n >> (i*8)) & 0xFF;
413       ASSERT(b>=0&&b<256);
414
415       if (fputc(b,file) == EOF) {
416          my_fatal("write_integer(): fputc(): %s\n",strerror(errno));
417       }
418    }
419 }
420
421 // end of book.cpp
422