+ Files owl_SOURCES - add headers
[owl.git] / epd.c
blobbb6335a174af07ee721da3a78af00b9afad925b2
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 #include "common.h"
17 #include "board.h"
18 #include "engine.h"
19 #include "move.h"
20 #include "search.h"
21 #include "trans.h"
23 /* TODO: better EPD knowlege */
24 enum EPD_TASKS { EPD_BEST_MOVE, EPD_AVOID_MOVE };
26 /* load EPD positions from file and solve them */
27 void *run_epd_test(void *args)
29 int move;
30 FILE *epd_file;
31 struct epd_info_t *ei;
32 char dummy[MAX_STRING];
33 char target_move1[MAX_STRING];
34 char target_move2[MAX_STRING];
35 char epd_line[MAX_STRING];
36 char epd_type[MAX_STRING];
37 int epd_count, epd_processed, epd_solved, epd_task;
39 /* args - struct epd_info_t */
40 ei = (struct epd_info_t *)args;
41 epd_file = fopen(ei->filename, "r");
43 if (!epd_file) {
44 fprintf(stderr, "EPD error: %s\n", strerror(errno));
46 /* free memory */
47 free(ei->filename);
48 free(ei);
49 return (void *)0;
52 epd_count = 0;
53 epd_processed = 0;
54 epd_solved = 0;
56 set_engine_value(&e.thinking, TRUE);
57 set_engine_value(&e.fixed_time, TRUE);
59 while (fgets(epd_line, MAX_STRING, epd_file)) {
60 fprintf(stdout, "EPD: %s", epd_line);
62 /* seek for first test */
63 if (++epd_count < ei->first_test)
64 continue;
66 if (!setup_board(epd_line)) {
67 /* handle incorrect lines */
68 fprintf(stderr, "Invalid EPD!\n");
69 continue;
72 int target_moves = sscanf(epd_line, "%s %s %s %s %s %[1-8a-hNBRQKx+#]"\
73 " %[1-8a-hNBRQKx+#]", dummy, dummy, dummy, dummy, epd_type, \
74 target_move1, target_move2) - 5;
76 /* invalid epd position */
77 if (target_moves < 1)
78 continue;
80 if (!strncmp(epd_type, "bm", 2))
81 epd_task = EPD_BEST_MOVE;
82 else if ((target_moves > 1) && !strncmp(epd_type, "am", 2))
83 epd_task = EPD_AVOID_MOVE;
84 else
85 continue;
87 epd_processed++;
89 /* set maximum search depth and convert time from seconds */
90 set_engine_value(&e.max_depth, 64);
91 set_engine_value(&e.max_time, ei->think_time * 100);
93 /* clear transposition table and start search */
94 clear_main_hashtable();
95 clear_pawn_hashtable();
97 move = iterate();
99 if (move) {
100 san_move(move, dummy);
102 /* did we found the move? */
103 if (!strncmp(dummy, target_move1, 6)) {
104 if (epd_task == EPD_BEST_MOVE)
105 epd_solved++;
106 } else if ((target_moves > 1) && !strncmp(dummy, target_move2, 6)) {
107 if (epd_task == EPD_BEST_MOVE)
108 epd_solved++;
112 fprintf(stdout, "\n* Tested EPD's = %d, solved = %d, percent = %.2f%%, " \
113 "total = %d.\n\n", epd_processed, epd_solved, \
114 ((float)epd_solved / epd_processed) * 100, epd_count);
116 if (epd_count >= ei->max_tests)
117 break;
120 set_engine_value(&e.fixed_time, FALSE);
121 set_engine_value(&e.thinking, FALSE);
123 /* cleanup */
124 fclose(epd_file);
125 free(ei->filename);
126 free(ei);
128 return (void *)0;