18 #include "move_legal.h"
\r
28 #define StringSize 4096
\r
32 static const bool UseDebug = FALSE;
\r
33 static const bool UseTrace = FALSE;
\r
37 static int MinDepth;
\r
38 static int MaxDepth;
\r
40 static double MaxTime;
\r
41 static double MinTime;
\r
43 static int DepthDelta;
\r
45 static int FirstMove;
\r
46 static int FirstDepth;
\r
47 static int FirstSelDepth;
\r
48 static int FirstScore;
\r
49 static double FirstTime;
\r
50 static uint64 FirstNodeNb;
\r
51 static move_t FirstPV[LineSize];
\r
53 static int LastMove;
\r
54 static int LastDepth;
\r
55 static int LastSelDepth;
\r
56 static int LastScore;
\r
57 static double LastTime;
\r
58 static uint64 LastNodeNb;
\r
59 static move_t LastPV[LineSize];
\r
61 static my_timer_t Timer[1];
\r
65 static void epd_test_file (const char file_name[]);
\r
67 static bool is_solution (int move, const board_t * board, const char bm[], const char am[]);
\r
68 static bool string_contain (const char string[], const char substring[]);
\r
70 static bool engine_step ();
\r
76 void epd_test(int argc, char * argv[]) {
\r
79 const char * epd_file;
\r
82 my_string_set(&epd_file,"wac.epd");
\r
92 for (i = 1; i < argc; i++) {
\r
96 } else if (my_string_equal(argv[i],"epd-test")) {
\r
100 } else if (my_string_equal(argv[i],"-epd")) {
\r
103 if (argv[i] == NULL) my_fatal("epd_test(): missing argument\n");
\r
105 my_string_set(&epd_file,argv[i]);
\r
107 } else if (my_string_equal(argv[i],"-min-depth")) {
\r
110 if (argv[i] == NULL) my_fatal("epd_test(): missing argument\n");
\r
112 MinDepth = atoi(argv[i]);
\r
114 } else if (my_string_equal(argv[i],"-max-depth")) {
\r
117 if (argv[i] == NULL) my_fatal("epd_test(): missing argument\n");
\r
119 MaxDepth = atoi(argv[i]);
\r
121 } else if (my_string_equal(argv[i],"-min-time")) {
\r
124 if (argv[i] == NULL) my_fatal("epd_test(): missing argument\n");
\r
126 MinTime = atof(argv[i]);
\r
128 } else if (my_string_equal(argv[i],"-max-time")) {
\r
131 if (argv[i] == NULL) my_fatal("epd_test(): missing argument\n");
\r
133 MaxTime = atof(argv[i]);
\r
135 } else if (my_string_equal(argv[i],"-depth-delta")) {
\r
138 if (argv[i] == NULL) my_fatal("epd_test(): missing argument\n");
\r
140 DepthDelta = atoi(argv[i]);
\r
144 my_fatal("epd_test(): unknown option \"%s\"\n",argv[i]);
\r
148 if(MinTime>MaxTime){
\r
152 epd_test_file(epd_file);
\r
157 static void epd_test_file(const char file_name[]) {
\r
161 char epd[StringSize];
\r
162 char am[StringSize], bm[StringSize], id[StringSize];
\r
164 char string[StringSize];
\r
166 char pv_string[StringSize];
\r
168 double depth_tot, time_tot, node_tot;
\r
171 ASSERT(file_name!=NULL);
\r
175 file = fopen(file_name,"r");
\r
176 if (file == NULL) my_fatal("epd_test_file(): can't open file \"%s\": %s\n",file_name,strerror(errno));
\r
185 printf("\nEngineName=%s\n",option_get_string(Option,"EngineName"));
\r
187 printf("\n[Search parameters: MaxDepth=%d MaxTime=%.1f DepthDelta=%d MinDepth=%d MinTime=%.1f]\n\n",MaxDepth,MaxTime,DepthDelta,MinDepth,MinTime);
\r
191 while (my_file_read_line(file,epd,StringSize)) {
\r
193 if(my_string_whitespace(epd)) continue;
\r
194 if (UseTrace) printf("%s\n",epd);
\r
196 if (!epd_get_op(epd,"am",am,StringSize)) strcpy(am,"");
\r
197 if (!epd_get_op(epd,"bm",bm,StringSize)) strcpy(bm,"");
\r
198 if (!epd_get_op(epd,"id",id,StringSize)) strcpy(id,"");
\r
200 if (my_string_empty(am) && my_string_empty(bm)) {
\r
201 my_fatal("epd_test(): no am or bm field at line %d\n",line);
\r
206 uci_send_ucinewgame(Uci);
\r
207 uci_send_isready_sync(Uci);
\r
209 ASSERT(!Uci->searching);
\r
212 if (!board_from_fen(board,epd)) ASSERT(FALSE);
\r
213 if (!board_to_fen(board,string,StringSize)) ASSERT(FALSE);
\r
215 engine_send(Engine,"position fen %s");
\r
219 my_timer_start(Timer); // also resets
\r
221 // which ones of the next two alternatives is best?
\r
222 engine_send(Engine,"go movetime %.0f depth %d",MaxTime*1000.0,MaxDepth);
\r
223 //engine_send(Engine,"go infinite");
\r
227 board_copy(Uci->board,board);
\r
230 Uci->searching = TRUE;
\r
233 FirstMove = MoveNone;
\r
239 line_clear(FirstPV);
\r
241 LastMove = MoveNone;
\r
247 line_clear(LastPV);
\r
249 // parse engine output
\r
251 while (!engine_eof(Engine) && engine_step()) {
\r
255 // printf("Uci->time=%.2f time=%.2f\n",Uci->time,my_timer_elapsed_real(Timer));
\r
256 if (Uci->depth > MaxDepth){
\r
257 my_log("POLYGLOT Maximum depth %d reached\n",MaxDepth);
\r
259 }else if(my_timer_elapsed_real(Timer) >= MaxTime){
\r
260 my_log("POLYGLOT Maximum search time %.2fs reached\n",MaxTime);
\r
262 }else if(Uci->depth - FirstDepth >= DepthDelta){
\r
263 if(Uci->depth > MinDepth){
\r
264 if(Uci->time >= MinTime){
\r
265 if(is_solution(FirstMove,board,bm,am)){
\r
266 my_log("POLYGLOT Solution found\n",MaxTime);
\r
273 my_log("POLYGLOT Stopping engine\n");
\r
274 engine_send(Engine,"stop");
\r
280 correct = is_solution(move,board,bm,am);
\r
282 if (correct) hit++;
\r
286 depth_tot += ((double)FirstDepth);
\r
287 time_tot += FirstTime;
\r
288 node_tot += ((double)((sint64)FirstNodeNb));
\r
291 printf("%2d: %-15s %s %4d",tot,id,correct?"OK":"--",hit);
\r
293 if (!line_to_san(LastPV,Uci->board,pv_string,StringSize)) ASSERT(FALSE);
\r
294 printf(" score=%+6.2f pv [D=%2d, T=%7.2fs, N=%6dk] =%s\n",((double)LastScore)/100.0,FirstDepth,FirstTime,(int)FirstNodeNb/1000,pv_string);
\r
297 printf("\nscore=%d/%d",hit,tot);
\r
301 depth_tot /= ((double)hit);
\r
302 time_tot /= ((double)hit);
\r
303 node_tot /= ((double)hit);
\r
305 printf(" [averages on correct positions: depth=%.1f time=%.2f nodes=%.0f]",depth_tot,time_tot,node_tot);
\r
316 static bool is_solution(int move, const board_t * board, const char bm[], const char am[]) {
\r
318 char move_string[256];
\r
321 ASSERT(move!=MoveNone);
\r
325 if (!move_is_legal(move,board)) {
\r
327 move_disp(move,board);
\r
331 ASSERT(move_is_legal(move,board));
\r
333 if (!move_to_san(move,board,move_string,256)) ASSERT(FALSE);
\r
336 if (!my_string_empty(bm)) {
\r
337 correct = string_contain(bm,move_string);
\r
338 } else if (!my_string_empty(am)) {
\r
339 correct = !string_contain(am,move_string);
\r
349 bool epd_get_op(const char record[], const char opcode[], char string[], int size) {
\r
353 const char *p_start, *p_end;
\r
355 ASSERT(record!=NULL);
\r
356 ASSERT(opcode!=NULL);
\r
357 ASSERT(string!=NULL);
\r
362 sprintf(op," %s ",opcode);
\r
364 p_start = strstr(record,op);
\r
365 if (p_start == NULL){
\r
366 sprintf(op,";%s ",opcode);
\r
367 p_start = strstr(record,op);
\r
368 if (p_start == NULL){
\r
375 p_start += strlen(op);
\r
378 p_end = strchr(p_start,';');
\r
379 if (p_end == NULL) return FALSE;
\r
381 // calculate the length
\r
383 len = p_end - p_start;
\r
384 if (size < len+1) my_fatal("epd_get_op(): size < len+1\n");
\r
386 strncpy(string,p_start,len);
\r
387 string[len] = '\0';
\r
392 // string_contain()
\r
394 static bool string_contain(const char string[], const char substring[]) {
\r
396 char new_string[StringSize], *p;
\r
398 strcpy(new_string,string); // HACK
\r
400 for (p = strtok(new_string," "); p != NULL; p = strtok(NULL," ")) {
\r
401 if (my_string_equal(p,substring)) return TRUE;
\r
409 static bool engine_step() {
\r
411 char string[StringSize];
\r
414 engine_get(Engine,string);
\r
415 event = uci_parse(Uci,string);
\r
417 if ((event & EVENT_MOVE) != 0) {
\r
422 if ((event & EVENT_PV) != 0) {
\r
424 LastMove = Uci->best_pv[0];
\r
425 LastDepth = Uci->best_depth;
\r
426 LastSelDepth = Uci->best_sel_depth;
\r
427 LastScore = Uci->best_score;
\r
428 LastTime = Uci->time;
\r
429 LastNodeNb = Uci->node_nb;
\r
430 line_copy(LastPV,Uci->best_pv);
\r
432 if (LastMove != FirstMove) {
\r
433 FirstMove = LastMove;
\r
434 FirstDepth = LastDepth;
\r
435 FirstSelDepth = LastSelDepth;
\r
436 FirstScore = LastScore;
\r
437 FirstTime = LastTime;
\r
438 FirstNodeNb = LastNodeNb;
\r
439 line_copy(FirstPV,LastPV);
\r