Debian Codegraph @

tbprobe.c

raw symbols SCIP
1 /*
2 This Software is distributed with the following X11 License,
3 sometimes also known as MIT license.
4
5 Copyright (c) 2010 Miguel A. Ballicora
6
7 Permission is hereby granted, free of charge, to any person
8 obtaining a copy of this software and associated documentation
9 files (the "Software"), to deal in the Software without
10 restriction, including without limitation the rights to use,
11 copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the
13 Software is furnished to do so, subject to the following
14 conditions:
15
16 The above copyright notice and this permission notice shall be
17 included in all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
21 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 OTHER DEALINGS IN THE SOFTWARE.
27 */
28
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include "gtb-probe.h"
33
34 /* local prototypes */
35 static void dtm_print (unsigned stm, int tb_available, unsigned info, unsigned pliestomate);
36 static void wdl_print (unsigned stm, int tb_available, unsigned info);
37
38 /*
39 | paths to TBs, generally provided by the user
40 | Two styles are accepted: One path at a time, or
41 | multiple paths separated by ';'
42 | The example in this file uses both styles simultaneoulsy
43 */
44 const char *path1 = "gtb/gtb4";
45 const char *path2 = "gtb/gtb3;gtb/gtb2";
46 const char *path3 = "gtb/gtb1";
47
48 int main (int argc, char *argv[])
49 {
50 /*--------------------------------------*\
51 | Probing info to be provided
52 \*--------------------------------------*/
53
54 unsigned int stm; /* side to move */
55 unsigned int epsquare; /* target square for an en passant capture */
56 unsigned int castling; /* castling availability, 0 => no castles */
57 unsigned int ws[17]; /* list of squares for white */
58 unsigned int bs[17]; /* list of squares for black */
59 unsigned char wp[17]; /* what white pieces are on those squares */
60 unsigned char bp[17]; /* what black pieces are on those squares */
61
62 /*--------------------------------------*\
63 | Probing info to be requested
64 \*--------------------------------------*/
65
66 char *initinfo; /* NULL if verbosity=0, initialization info if verbosity=1*/
67 int tb_available; /* 0 => FALSE, 1 => TRUE */
68 unsigned info = tb_UNKNOWN; /* default, no tbvalue */
69 unsigned pliestomate;
70
71 /*--------------------------------------*\
72 | Initialization info to be provided
73 \*--------------------------------------*/
74
75 int verbosity = 1; /* initialization 0 = non-verbose, 1 = verbose */
76 int scheme = tb_CP4; /* compression scheme to be used */
77 const char ** paths; /* paths where files will be searched */
78 size_t cache_size = 32*1024*1024; /* 32 MiB in this example */
79
80 /* wdl_fraction:
81 fraction, over 128, that will be dedicated to wdl information.
82 In other words, 96 means 3/4 of the cache will be dedicated to
83 win-draw-loss info, and 1/4 dedicated to distance to mate
84 information.
85 */
86 int wdl_fraction = 96;
87
88 /*----------------------------------*\
89 | Return version of this demo
90 \*----------------------------------*/
91
92 #include "version.h"
93 #include "progname.h"
94 if (argc > 1 && 0==strcmp(argv[1],"-v")) {
95 printf ("%s %s\n",PROGRAM_NAME,VERSION);
96 return 0;
97 }
98
99 /*--------------------------------------*\
100 | Initialization:
101 | Include something like this at
102 | the beginning of the program.
103 \*--------------------------------------*/
104
105 /* the number of paths that can be added is only limited by memory */
106 paths = tbpaths_init();
107 if (NULL == paths) printf ("Error here... %d\n",__LINE__);
108 paths = tbpaths_add (paths, path1);
109 if (NULL == paths) printf ("Error here... %d\n",__LINE__);
110 paths = tbpaths_add (paths, path2);
111 if (NULL == paths) printf ("Error here... %d\n",__LINE__);
112 paths = tbpaths_add (paths, path3);
113 if (NULL == paths) printf ("Error here... %d\n",__LINE__);
114
115 /* init probing code, indexes, paths, etc. */
116 initinfo = tb_init (verbosity, scheme, paths);
117
118 /* init cache */
119 tbcache_init(cache_size, wdl_fraction);
120
121 tbstats_reset();
122
123 /* information to be output for the user, or to be saved in logs etc.*/
124 if (initinfo != NULL)
125 printf ("%s",initinfo);
126
127 /*--------------------------------------*\
128 |
129 | ASSIGNING POSITIONAL VALUES for
130 | one probing example
131 |
132 \*--------------------------------------*/
133
134 #if 1
135
136 /* needs 3-pc installed */
137 /* FEN: 8/8/8/4k3/8/8/8/KR6 w - - 0 1 */
138
139 stm = tb_WHITE_TO_MOVE;/* 0 = white to move, 1 = black to move */
140 epsquare = tb_NOSQUARE; /* no ep available */
141 castling = tb_NOCASTLE; /* no castling available, otherwise combine all
142 the castling possibilities with '|', for instance
143 white could castle both sides, black can't:
144 castling = tb_WOO | tb_WOOO;
145 both could castle on the king side:
146 castling = tb_WOO | tb_WOO;
147 etc.
148 */
149
150 ws[0] = tb_A1;
151 ws[1] = tb_B1;
152 ws[2] = tb_NOSQUARE; /* it marks the end of list */
153
154 wp[0] = tb_KING;
155 wp[1] = tb_ROOK;
156 wp[2] = tb_NOPIECE; /* it marks the end of list */
157
158 bs[0] = tb_E5;
159 bs[1] = tb_NOSQUARE; /* it marks the end of list */
160
161 bp[0] = tb_KING;
162 bp[1] = tb_NOPIECE; /* it marks the end of list */
163
164 #else
165
166 /* needs 4-pc installed */
167 /* FEN: 8/8/6p1/4K3/7b/8/8/2k5 w - - 0 76 */
168
169 stm = tb_WHITE_TO_MOVE;/* 0 = white to move, 1 = black to move */
170 epsquare = tb_NOSQUARE; /* no ep available */
171 castling = tb_NOCASTLE; /* no castling available, otherwise combine all
172 the castling possibilities with '|', for instance
173 white could castle both sides, black can't:
174 castling = tb_WOO | tb_WOOO;
175 both could castle on the king side:
176 castling = tb_WOO | tb_WOO;
177 etc.
178 */
179
180 ws[0] = tb_E5;
181 ws[1] = tb_NOSQUARE; /* it marks the end of list */
182
183 wp[0] = tb_KING;
184 wp[1] = tb_NOPIECE; /* it marks the end of list */
185
186 bs[0] = tb_H4;
187 bs[1] = tb_G6;
188 bs[2] = tb_C1;
189 bs[3] = tb_NOSQUARE; /* it marks the end of list */
190
191 bp[0] = tb_BISHOP;
192 bp[1] = tb_PAWN;
193 bp[2] = tb_KING;
194 bp[3] = tb_NOPIECE; /* it marks the end of list */
195
196 #endif
197
198 /*--------------------------------------*\
199 |
200 | PROBING TBs #1 (HARD)
201 |
202 \*--------------------------------------*/
203
204 /*
205 probing hard will go to the cache first, if the info is not found there,
206 it will finally go to the Hard Drive to find it
207 */
208
209 tb_available = tb_probe_hard (stm, epsquare, castling, ws, bs, wp, bp, &info, &pliestomate);
210
211 /* print info */
212 dtm_print (stm, tb_available, info, pliestomate);
213
214 /*--------------------------------------*\
215 |
216 | ASSIGNING POSITIONAL VALUES for
217 | another example
218 |
219 \*--------------------------------------*/
220
221 /* only the rook position is different, the rest is the same */
222 ws[1] = tb_B6;
223
224 /*--------------------------------------*\
225 |
226 | PROBING TBs #2 (SOFT)
227 |
228 \*--------------------------------------*/
229
230 /*
231 probing soft goes to cache, if info not found there, it returns FALSE
232 It will **NEVER** go to the Hard Drive
233 If info is found, it is because the previous probe #1 filled up
234 the cache with the info needed for probe #2
235 */
236
237 tb_available = tb_probe_soft (stm, epsquare, castling, ws, bs, wp, bp, &info, &pliestomate);
238
239 /* print info */
240 dtm_print (stm, tb_available, info, pliestomate);
241
242 /*--------------------------------------*\
243 |
244 | PROBING TBs #3 (SOFT)
245 | An example of what happens
246 | after tbcache_flush()
247 | which may be used to clear it
248 | for epd tests, etc.
249 |
250 \*--------------------------------------*/
251
252 /*
253 cache is flushed, so probing soft with the same position as #2
254 will surely return FALSE
255 */
256
257 tbcache_flush();
258
259 /* same as #2 */
260 tb_available = tb_probe_soft (stm, epsquare, castling, ws, bs, wp, bp, &info, &pliestomate);
261
262 /* print info */
263 dtm_print (stm, tb_available, info, pliestomate);
264
265 /*--------------------------------------*\
266 |
267 | PROBING TBs #4
268 | (HARD, only win, draw, lose)
269 |
270 \*--------------------------------------*/
271
272 /*
273 Probing with the WDL versions of the probing functions
274 will return only the info needed to know whether a position
275 is a win, draw, or a loss.
276 The Gaviota tablebase library will try to return this info
277 with the best performance possible. If the only info needed for
278 a position is WDL, this function should be used rather
279 than the regular tb_probe_hard() function.
280 This function would be the "equivalent" of one that probes a bitbase.
281 */
282
283 tb_available = tb_probe_WDL_hard (stm, epsquare, castling, ws, bs, wp, bp, &info);
284
285 /* print info */
286 wdl_print (stm, tb_available, info);
287
288
289 /*--------------------------------------*\
290 |
291 | RESTART?
292 | What if the user changes
293 | the conditions during run?
294 |
295 \*--------------------------------------*/
296
297 /*
298 | NEW INFO BY THE USER, example
299 \*---------------------------------------------*/
300 scheme = tb_CP2; /* compression scheme changes */
301 path1 = "gtb/gtb2";
302 path2 = "gtb/gtb1";
303 cache_size = 16*1024*1024; /* 16 MiB is the new cache size */
304 wdl_fraction = 104; /* more cache for wdl info than before */
305
306 /*
307 | RESTART PROCESS
308 \*---------------------------------------------*/
309
310 /* cleanup old paths */
311 paths = tbpaths_done(paths);
312
313 /* init new paths */
314 paths = tbpaths_init();
315 paths = tbpaths_add (paths, path1);
316 paths = tbpaths_add (paths, path2);
317
318 /* restart */
319 initinfo = tb_restart (verbosity, scheme, paths);
320 tbcache_restart(cache_size, wdl_fraction);
321
322 /* information to be output for the user, or to be saved in logs etc.*/
323 if (initinfo != NULL)
324 printf ("%s",initinfo);
325
326 /*
327 | Just to show, It is possible to know what TBs are installed.
328 | But, I can only find out after tb_init or tb_restart
329 \*----------------------------------------------------------------------------------------*/
330
331 {
332 unsigned av = tb_availability();
333
334 if (0 != (av& 1)) printf ("Some 3-pc TBs available\n"); else printf ("No 3-pc TBs available\n");
335 if (0 != (av& 2)) printf ("3-pc TBs complete\n");
336 if (0 != (av& 4)) printf ("Some 4-pc TBs available\n"); else printf ("No 4-pc TBs available\n");
337 if (0 != (av& 8)) printf ("4-pc TBs complete\n");
338 if (0 != (av&16)) printf ("Some 5-pc TBs available\n"); else printf ("No 5-pc TBs available\n");
339 if (0 != (av&32)) printf ("5-pc TBs complete\n");
340 printf ("\n");
341 }
342
343 /*
344 | Now that TBs have been restarted, we probe once again (HARD)
345 \*----------------------------------------------------------------------------------------*/
346 tb_available = tb_probe_hard (stm, epsquare, castling, ws, bs, wp, bp, &info, &pliestomate);
347
348 /* print info */
349 dtm_print (stm, tb_available, info, pliestomate);
350
351
352 /*--------------------------------------*\
353 |
354 | Clean up at the end of the program
355 |
356 \*--------------------------------------*/
357
358 tbcache_done();
359
360 tb_done();
361
362 paths = tbpaths_done(paths);
363
364 /*--------------------------------------*\
365 |
366 | Return
367 |
368 \*--------------------------------------*/
369
370 if (tb_available)
371 return EXIT_SUCCESS;
372 else
373 return EXIT_FAILURE;
374 }
375
376
377 /*----------------------------------------------------------------------*\
378 | These are local functions that just print the results after probing
379 \*----------------------------------------------------------------------*/
380
381 static void
382 dtm_print (unsigned stm, int tb_available, unsigned info, unsigned pliestomate)
383 {
384 if (tb_available) {
385
386 if (info == tb_DRAW)
387 printf ("Draw\n");
388 else if (info == tb_WMATE && stm == tb_WHITE_TO_MOVE)
389 printf ("White mates, plies=%u\n", pliestomate);
390 else if (info == tb_BMATE && stm == tb_BLACK_TO_MOVE)
391 printf ("Black mates, plies=%u\n", pliestomate);
392 else if (info == tb_WMATE && stm == tb_BLACK_TO_MOVE)
393 printf ("Black is mated, plies=%u\n", pliestomate);
394 else if (info == tb_BMATE && stm == tb_WHITE_TO_MOVE)
395 printf ("White is mated, plies=%u\n", pliestomate);
396 else {
397 printf ("FATAL ERROR, This should never be reached\n");
398 exit(EXIT_FAILURE);
399 }
400 printf ("\n");
401 } else {
402 printf ("Tablebase info not available\n\n");
403 }
404 }
405
406 static void
407 wdl_print (unsigned stm, int tb_available, unsigned info)
408 {
409 if (tb_available) {
410
411 if (info == tb_DRAW)
412 printf ("Draw\n");
413 else if (info == tb_WMATE && stm == tb_WHITE_TO_MOVE)
414 printf ("White mates\n");
415 else if (info == tb_BMATE && stm == tb_BLACK_TO_MOVE)
416 printf ("Black mates\n");
417 else if (info == tb_WMATE && stm == tb_BLACK_TO_MOVE)
418 printf ("Black is mated\n");
419 else if (info == tb_BMATE && stm == tb_WHITE_TO_MOVE)
420 printf ("White is mated\n");
421 else {
422 printf ("FATAL ERROR, This should never be reached\n");
423 exit(EXIT_FAILURE);
424 }
425 printf ("\n");
426 } else {
427 printf ("Tablebase info not available\n\n");
428 }
429 }
430
431
432