18 #include "move_legal.h"
\r
27 static const bool UseDebug = false;
\r
28 static const bool UseTrace = false;
\r
30 static const int StringSize = 4096;
\r
34 static int MinDepth;
\r
35 static int MaxDepth;
\r
37 static double MaxTime;
\r
38 static double MinTime;
\r
40 static int DepthDelta;
\r
42 static int FirstMove;
\r
43 static int FirstDepth;
\r
44 static int FirstSelDepth;
\r
45 static int FirstScore;
\r
46 static double FirstTime;
\r
47 static uint64 FirstNodeNb;
\r
48 static move_t FirstPV[LineSize];
\r
50 static int LastMove;
\r
51 static int LastDepth;
\r
52 static int LastSelDepth;
\r
53 static int LastScore;
\r
54 static double LastTime;
\r
55 static uint64 LastNodeNb;
\r
56 static move_t LastPV[LineSize];
\r
58 static my_timer_t Timer[1];
\r
62 static void epd_test_file (const char file_name[]);
\r
64 static bool is_solution (int move, const board_t * board, const char bm[], const char am[]);
\r
65 static bool string_contain (const char string[], const char substring[]);
\r
67 static bool engine_step ();
\r
73 void epd_test(int argc, char * argv[]) {
\r
76 const char * epd_file;
\r
79 my_string_set(&epd_file,"wac.epd");
\r
89 for (i = 1; i < argc; i++) {
\r
93 } else if (my_string_equal(argv[i],"epd-test")) {
\r
97 } else if (my_string_equal(argv[i],"-epd")) {
\r
100 if (argv[i] == NULL) my_fatal("epd_test(): missing argument\n");
\r
102 my_string_set(&epd_file,argv[i]);
\r
104 } else if (my_string_equal(argv[i],"-min-depth")) {
\r
107 if (argv[i] == NULL) my_fatal("epd_test(): missing argument\n");
\r
109 MinDepth = atoi(argv[i]);
\r
111 } else if (my_string_equal(argv[i],"-max-depth")) {
\r
114 if (argv[i] == NULL) my_fatal("epd_test(): missing argument\n");
\r
116 MaxDepth = atoi(argv[i]);
\r
118 } else if (my_string_equal(argv[i],"-min-time")) {
\r
121 if (argv[i] == NULL) my_fatal("epd_test(): missing argument\n");
\r
123 MinTime = atof(argv[i]);
\r
125 } else if (my_string_equal(argv[i],"-max-time")) {
\r
128 if (argv[i] == NULL) my_fatal("epd_test(): missing argument\n");
\r
130 MaxTime = atof(argv[i]);
\r
132 } else if (my_string_equal(argv[i],"-depth-delta")) {
\r
135 if (argv[i] == NULL) my_fatal("epd_test(): missing argument\n");
\r
137 DepthDelta = atoi(argv[i]);
\r
141 my_fatal("epd_test(): unknown option \"%s\"\n",argv[i]);
\r
145 if(MinTime>MaxTime){
\r
149 epd_test_file(epd_file);
\r
154 static void epd_test_file(const char file_name[]) {
\r
158 char epd[StringSize];
\r
159 char am[StringSize], bm[StringSize], id[StringSize];
\r
161 char string[StringSize];
\r
163 char pv_string[StringSize];
\r
165 double depth_tot, time_tot, node_tot;
\r
168 ASSERT(file_name!=NULL);
\r
172 file = fopen(file_name,"r");
\r
173 if (file == NULL) my_fatal("epd_test_file(): can't open file \"%s\": %s\n",file_name,strerror(errno));
\r
182 printf("\nEngineName=%s\n",option_get_string("EngineName"));
\r
184 printf("\n[Search parameters: MaxDepth=%d MaxTime=%.1f DepthDelta=%d MinDepth=%d MinTime=%.1f]\n\n",MaxDepth,MaxTime,DepthDelta,MinDepth,MinTime);
\r
188 while (my_file_read_line(file,epd,StringSize)) {
\r
190 if(my_string_whitespace(epd)) continue;
\r
191 if (UseTrace) printf("%s\n",epd);
\r
193 if (!epd_get_op(epd,"am",am,StringSize)) strcpy(am,"");
\r
194 if (!epd_get_op(epd,"bm",bm,StringSize)) strcpy(bm,"");
\r
195 if (!epd_get_op(epd,"id",id,StringSize)) strcpy(id,"");
\r
197 if (my_string_empty(am) && my_string_empty(bm)) {
\r
198 my_fatal("epd_test(): no am or bm field at line %d\n",line);
\r
203 uci_send_ucinewgame(Uci);
\r
204 uci_send_isready_sync(Uci);
\r
206 ASSERT(!Uci->searching);
\r
209 if (!board_from_fen(board,epd)) ASSERT(false);
\r
210 if (!board_to_fen(board,string,StringSize)) ASSERT(false);
\r
212 engine_send(Engine,"position fen %s",string);
\r
216 my_timer_start(Timer); // also resets
\r
218 // which ones of the next two alternatives is best?
\r
219 engine_send(Engine,"go movetime %.0f depth %d",MaxTime*1000.0,MaxDepth);
\r
220 //engine_send(Engine,"go infinite");
\r
224 board_copy(Uci->board,board);
\r
227 Uci->searching = true;
\r
230 FirstMove = MoveNone;
\r
236 line_clear(FirstPV);
\r
238 LastMove = MoveNone;
\r
244 line_clear(LastPV);
\r
246 // parse engine output
\r
248 while (!engine_eof(Engine) && engine_step()) {
\r
252 // printf("Uci->time=%.2f time=%.2f\n",Uci->time,my_timer_elapsed_real(Timer));
\r
253 if (Uci->depth > MaxDepth){
\r
254 my_log("POLYGLOT Maximum depth %d reached\n",MaxDepth);
\r
256 }else if(my_timer_elapsed_real(Timer) >= MaxTime){
\r
257 my_log("POLYGLOT Maximum search time %.2fs reached\n",MaxTime);
\r
259 }else if(Uci->depth - FirstDepth >= DepthDelta){
\r
260 if(Uci->depth > MinDepth){
\r
261 if(Uci->time >= MinTime){
\r
262 if(is_solution(FirstMove,board,bm,am)){
\r
263 my_log("POLYGLOT Solution found\n",MaxTime);
\r
270 my_log("POLYGLOT Stopping engine\n");
\r
271 engine_send(Engine,"stop");
\r
277 correct = is_solution(move,board,bm,am);
\r
279 if (correct) hit++;
\r
283 depth_tot += double(FirstDepth);
\r
284 time_tot += FirstTime;
\r
285 node_tot += double(sint64(FirstNodeNb));
\r
288 printf("%2d: %-15s %s %4d",tot,id,correct?"OK":"--",hit);
\r
290 if (!line_to_san(LastPV,Uci->board,pv_string,StringSize)) ASSERT(false);
\r
291 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
294 printf("\nscore=%d/%d",hit,tot);
\r
298 depth_tot /= double(hit);
\r
299 time_tot /= double(hit);
\r
300 node_tot /= double(hit);
\r
302 printf(" [averages on correct positions: depth=%.1f time=%.2f nodes=%.0f]",depth_tot,time_tot,node_tot);
\r
313 static bool is_solution(int move, const board_t * board, const char bm[], const char am[]) {
\r
315 char move_string[256];
\r
318 ASSERT(move!=MoveNone);
\r
322 if (!move_is_legal(move,board)) {
\r
324 move_disp(move,board);
\r
328 ASSERT(move_is_legal(move,board));
\r
330 if (!move_to_san(move,board,move_string,256)) ASSERT(false);
\r
333 if (!my_string_empty(bm)) {
\r
334 correct = string_contain(bm,move_string);
\r
335 } else if (!my_string_empty(am)) {
\r
336 correct = !string_contain(am,move_string);
\r
346 bool epd_get_op(const char record[], const char opcode[], char string[], int size) {
\r
350 const char *p_start, *p_end;
\r
352 ASSERT(record!=NULL);
\r
353 ASSERT(opcode!=NULL);
\r
354 ASSERT(string!=NULL);
\r
359 sprintf(op," %s ",opcode);
\r
361 p_start = strstr(record,op);
\r
362 if (p_start == NULL){
\r
363 sprintf(op,";%s ",opcode);
\r
364 p_start = strstr(record,op);
\r
365 if (p_start == NULL){
\r
372 p_start += strlen(op);
\r
375 p_end = strchr(p_start,';');
\r
376 if (p_end == NULL) return false;
\r
378 // calculate the length
\r
380 len = p_end - p_start;
\r
381 if (size < len+1) my_fatal("epd_get_op(): size < len+1\n");
\r
383 strncpy(string,p_start,len);
\r
384 string[len] = '\0';
\r
389 // string_contain()
\r
391 static bool string_contain(const char string[], const char substring[]) {
\r
393 char new_string[StringSize], *p;
\r
395 strcpy(new_string,string); // HACK
\r
397 for (p = strtok(new_string," "); p != NULL; p = strtok(NULL," ")) {
\r
398 if (my_string_equal(p,substring)) return true;
\r
406 static bool engine_step() {
\r
408 char string[StringSize];
\r
411 engine_get(Engine,string,StringSize);
\r
412 event = uci_parse(Uci,string);
\r
414 if ((event & EVENT_MOVE) != 0) {
\r
419 if ((event & EVENT_PV) != 0) {
\r
421 LastMove = Uci->best_pv[0];
\r
422 LastDepth = Uci->best_depth;
\r
423 LastSelDepth = Uci->best_sel_depth;
\r
424 LastScore = Uci->best_score;
\r
425 LastTime = Uci->time;
\r
426 LastNodeNb = Uci->node_nb;
\r
427 line_copy(LastPV,Uci->best_pv);
\r
429 if (LastMove != FirstMove) {
\r
430 FirstMove = LastMove;
\r
431 FirstDepth = LastDepth;
\r
432 FirstSelDepth = LastSelDepth;
\r
433 FirstScore = LastScore;
\r
434 FirstTime = LastTime;
\r
435 FirstNodeNb = LastNodeNb;
\r
436 line_copy(FirstPV,LastPV);
\r