My Project
options.c
Go to the documentation of this file.
1
11#include "options.h"
12#include "stats.h"
13#include "util.h"
14#include "search.h"
15
16#include <string.h>
17#include <ctype.h>
18#include <stdlib.h>
19#include <limits.h>
20
23 21, // hash table size
24
25 {0,-2,-3}, // inc_sort_depth
26
27 1, // n_task (will be set to system available cpus at run-time)
28 false, // cpu_affinity
29
30 1, // verbosity
31 0, // noise
32 80, // width
33 false, // echo
34 false, // info
35 false, // debug cassio
36 true, // transgress cassio
37
38 21, // max level
39 TIME_MAX, // infinite time
40 EDAX_FIXED_LEVEL, // play-type
41 true, // can ponder
42 -1, // depth
43 -1, // selectivity
44
45 3, // mode
46
47 10000000, // speed (default = 10e6)
48 0, // nps (default = 0)
49
50 SCORE_MIN, // alpha
51 SCORE_MAX, // beta
52
53 false, // all_best
54
55 NULL, // evaluation function's weights file.
56
57 NULL, // book file
58 true, // book usage allowed
59 0, // book randomness
60
61 NULL, // ggs host name
62 NULL, // ggs login name
63 NULL, // ggs password
64 NULL, // port
65 1, // open
66
67 0.25, // probcut depth reduction (/2)
68 false, // pv debug
69 false, // pv check
70 false, // pv guess
71
72 NULL, // game file.
73
74 NULL, // search log file.
75 NULL, // ui log file.
76 NULL, // ggs log file.
77
78 NULL, // edax name
79
80 false, //auto start
81 false, //auto store
82 false, //auto learn
83 false, //auto quit
84 0, //repeat
85};
86
90void options_usage(void)
91{
92 fprintf(stderr, "\nCommon options:\n");
93 fprintf(stderr, " -?|help show this message.\n");
94 fprintf(stderr, " -o|option-file read options from this file.\n");
95 fprintf(stderr, " -v|version display the version number.\n");
96 fprintf(stderr, " -name <string> set Edax name to <string>.\n");
97 fprintf(stderr, " -verbose <n> verbosity level.\n");
98 fprintf(stderr, " -q silent mode (eq. -verbose 0).\n");
99 fprintf(stderr, " -vv very verbose mode (eq. -verbose 2).\n");
100 fprintf(stderr, " -noise <n> noise level (print search output from ply <n>).\n");
101 fprintf(stderr, " -width <n> line width.\n");
102 fprintf(stderr, " -h|hash-table-size <nbits> hash table size.\n");
103 fprintf(stderr, " -n|n-tasks <n> search in parallel using n tasks.\n");
104 fprintf(stderr, " -cpu search using 1 cpu/thread.\n");
105#ifdef __APPLE__
106 fprintf(stderr, "\nCassio protocol options:\n");
107 fprintf(stderr, " -debug-cassio print extra-information in cassio.\n");
108 fprintf(stderr, " -follow-cassio follow more closely cassio requests.\n");
109 fprintf(stderr, "\nOptions unavailable to Cassio protocol\n:");
110#endif
111 fprintf(stderr, " -l|level <n> search using limited depth.\n");
112 fprintf(stderr, " -t|game-time <n> search using limited time per game.\n");
113 fprintf(stderr, " -move-time <n> search using limited time per move.\n");
114 fprintf(stderr, " -ponder <on/off> search during opponent time.\n");
115 fprintf(stderr, " -eval-file read eval weight from this file.\n");
116 fprintf(stderr, " -book-file load opening book from this file.\n");
117 fprintf(stderr, " -book-usage <on/off> play from the opening book.\n");
118 fprintf(stderr, " -book-randomness <n> play various but worse moves from the opening book.\n");
119 fprintf(stderr, " -auto-start <on/off> automatically restart a new game.\n");
120 fprintf(stderr, " -auto-swap <on/off> automatically Edax's color between games\n");
121 fprintf(stderr, " -auto-store <on/off> automatically save played games\n");
122 fprintf(stderr, " -game-file <file> file to store all played game/s.\n");
123 fprintf(stderr, " -search-log-file <file> file to store search detailed output/s.\n");
124 fprintf(stderr, " -ui-log-file <file> file to store input/output to the (U)ser (I)nterface.\n");
125
126 exit(EXIT_SUCCESS);
127}
128
136int options_read(const char *option, const char *value)
137{
138 int read = 0;
139
140 if (option == NULL) return read;
141 while (*option == '-') ++option;
142 if (*option == '\0' || *option == '%' || *option == '#') return read;
143
144 read = 1;
145 if (strcmp(option, "vv") == 0) options.verbosity = 2;
146 else if (strcmp(option, "q") == 0) options.verbosity = 0;
147 else if (strcmp(option, "info") == 0) options.info = true;
148 else if (strcmp(option, "debug-cassio") == 0) options.debug_cassio = true;
149 else if (strcmp(option, "follow-cassio") == 0) options.transgress_cassio = false;
150 else if (strcmp(option, "?") == 0 || strcmp(option, "help") == 0) usage();
151 else if (strcmp(option, "cpu") == 0) options.cpu_affinity = true;
152 else {
153 read = 0;
154 if (value == NULL || *value == '\0') return read;
155 read = 2;
156 if (strcmp(option, "verbose") == 0) options.verbosity = string_to_int(value, options.verbosity);
157 else if (strcmp(option, "noise") == 0) options.noise = string_to_int(value, options.noise);
158 else if (strcmp(option, "width") == 0) options.width = string_to_int(value, options.width);
159
160 else if (strcmp(option, "h") == 0 || strcmp(option, "hash-table-size") == 0) options.hash_table_size = string_to_int(value, options.hash_table_size);
161 else if (strcmp(option, "n") == 0 || strcmp(option, "n-tasks") == 0) options.n_task = string_to_int(value, options.n_task);
162 else if (strcmp(option, "l") == 0 || strcmp(option, "level") == 0) {
165 } else if (strcmp(option, "d") == 0 || strcmp(option, "depth") == 0) {
168 } else if (strcmp(option, "selectivity") == 0) {
171 } else if (strcmp(option, "t") == 0 || strcmp(option, "game-time") == 0) {
172 options.time = string_to_time(value);
174 } else if (strcmp(option, "move-time") == 0) {
175 options.time = string_to_time(value);
177 } else if (strcmp(option, "alpha") == 0) options.alpha = string_to_int(value, options.alpha);
178 else if (strcmp(option, "beta") == 0) options.beta = string_to_int(value, options.beta);
179 else if (strcmp(option, "all-best") == 0) parse_boolean(value, &options.all_best);
180
181 else if (strcmp(option, "o") == 0 || strcmp(option, "option-file") == 0) options_parse(value);
182 else if (strcmp(option, "speed") == 0) options.speed = string_to_real(value, options.speed);
183 else if (strcmp(option, "nps") == 0) options.nps = 0.001 * string_to_real(value, options.nps);
184 else if (strcmp(option, "ponder") == 0) parse_boolean(value, &options.can_ponder);
185 else if (strcmp(option, "mode") == 0) parse_int(value, &options.mode);
186
187 else if (strcmp(option, "inc-pvnode-sort-depth") == 0) options.inc_sort_depth[PV_NODE] = string_to_int(value, options.inc_sort_depth[PV_NODE]);
188 else if (strcmp(option, "inc-cutnode-sort-depth") == 0) options.inc_sort_depth[CUT_NODE] = string_to_int(value, options.inc_sort_depth[CUT_NODE]);
189 else if (strcmp(option, "inc-allnode-sort-depth") == 0) options.inc_sort_depth[ALL_NODE] = string_to_int(value, options.inc_sort_depth[ALL_NODE]);
190
191 else if (strcmp(option, "ggs-host") == 0) options.ggs_host = string_duplicate(value);
192 else if (strcmp(option, "ggs-login") == 0) options.ggs_login = string_duplicate(value);
193 else if (strcmp(option, "ggs-password") == 0) options.ggs_password = string_duplicate(value);
194 else if (strcmp(option, "ggs-port") == 0) options.ggs_port = string_duplicate(value);
195 else if (strcmp(option, "ggs-open") == 0) parse_boolean(value, &options.ggs_open);
196
197 else if (strcmp(option, "probcut-d") == 0) parse_real(value, &options.probcut_d);
198
199 else if (strcmp(option, "pv-debug") == 0) parse_boolean(value, &options.pv_debug);
200 else if (strcmp(option, "pv-check") == 0) parse_boolean(value, &options.pv_check);
201 else if (strcmp(option, "pv-guess") == 0) parse_boolean(value, &options.pv_guess);
202
203 else if (strcmp(option, "game-file") == 0) options.game_file = string_duplicate(value);
204
205 else if (strcmp(option, "eval-file") == 0) options.eval_file = string_duplicate(value);
206
207 else if (strcmp(option, "book-file") == 0) options.book_file = string_duplicate(value);
208 else if (strcmp(option, "book-usage") == 0) parse_boolean(value, &options.book_allowed);
209 else if (strcmp(option, "book-randomness") == 0) parse_int(value, &options.book_randomness);
210
211 else if (strcmp(option, "search-log-file") == 0) options.search_log_file = string_duplicate(value);
212 else if (strcmp(option, "ui-log-file") == 0) options.ui_log_file = string_duplicate(value);
213 else if (strcmp(option, "ggs-log-file") == 0) options.ggs_log_file = string_duplicate(value);
214
215 else if (strcmp(option, "name") == 0) options.name = string_duplicate(value);
216 else if (strcmp(option, "echo") == 0)parse_boolean(value, &options.echo);
217
218 else if (strcmp(option, "auto-start") == 0) parse_boolean(value, &options.auto_start);
219 else if (strcmp(option, "auto-store") == 0) parse_boolean(value, &options.auto_store);
220 else if (strcmp(option, "auto-swap") == 0) parse_boolean(value, &options.auto_swap);
221 else if (strcmp(option, "auto-quit") == 0) parse_boolean(value, &options.auto_quit);
222 else if (strcmp(option, "repeat") == 0) parse_int(value, &options.repeat);
223
224 else read = 0;
225 }
226
227 if (read) {
228 info("<set option %s %s>\n", option, value);
229 }
230
231 return read;
232}
233
234
249static const char* option_parse(const char *line, char *option, char *value, int size)
250{
251 line = parse_word(line, option, size);
252 if (strcmp(option, "set") == 0) line = parse_word(line, option, size);
253 line = parse_word(line, value, size);
254 if (strcmp(value, "=") == 0) line = parse_word(line, value, size);
255
256 options_read(option, value);
257
258 return line;
259}
260
266void options_parse(const char *file)
267{
268 char *line, *option, *value;
269 FILE *f = fopen(file, "r");
270
271 if (f != NULL) {
272
273 while ((line = string_read_line(f)) != NULL) {
274 int n = strlen(line);
275 option = (char*) malloc(n + 1);
276 value = (char*) malloc(n + 1);
277 option_parse(line, option, value, n);
278 free(line); free(option); free(value);
279 }
280
281 fclose(f);
282 }
283}
284
288void options_bound(void)
289{
290 int tmp;
291 int max_threads;
292
293 if (sizeof (void*) == 4) {
294 BOUND(options.hash_table_size, 10, 25, "hash-table-size");
295 } else {
296 BOUND(options.hash_table_size, 10, 30, "hash-table-size");
297 }
298
299 max_threads = MIN(get_cpu_number(), MAX_THREADS);
300 BOUND(options.n_task, 1, max_threads, "n-tasks");
301
302 BOUND(options.verbosity, 0, 4, "verbosity");
303 BOUND(options.noise, 0, 60, "noise");
304 BOUND(options.width, 3, 250, "width");
305 BOUND(options.level, 0, 60, "level");
306 BOUND(options.time, 1000, TIME_MAX, "time");
307
310
311 BOUND(options.speed, 1e5, 1e12, "speed");
312
313 if (options.alpha > options.beta) {
314 fprintf(stderr, "WARNING: alphabeta [%d, %d] will be inverted.\n", options.alpha, options.beta);
315 tmp = options.alpha;
317 options.beta = tmp;
318 }
319
321 if (options.game_file == NULL) options.game_file = string_duplicate("data/game.ggf");
322 if (options.eval_file == NULL) options.eval_file = string_duplicate("data/eval.dat");
323 if (options.book_file == NULL) options.book_file = string_duplicate("data/book.dat");
324}
325
330void options_dump(FILE *f)
331{
332 const char *(play_type[3]) = {"fixed depth", "fixed time per game", "fixed time per move"};
333 const char *(boolean_string[2]) = {"false", "true"};
334 const char *(mode[4]) = {"human/edax", "edax/human", "edax/edax", "human/human"};
335
336 fprintf(f, "search display options\n");
337 fprintf(f, "\tverbosity: %d\n", options.verbosity);
338 fprintf(f, "\tminimal depth (noise): %d\n", options.noise);
339 fprintf(f, "\tline width: %d\n", options.width);
340 fprintf(f, "\tuser input echo: %s\n", boolean_string[options.echo]);
341 fprintf(f, "\t<detailed info>: %s\n\n", boolean_string[options.info]);
342 fprintf(f, "Cassio options\n");
343 fprintf(f, "\tdisplay debug info in Cassio's 'fenetre de rapport': %s\n", boolean_string[options.debug_cassio]);
344 fprintf(f, "\tadapt Cassio requests to search & solve faster: %s\n\n", boolean_string[options.transgress_cassio]);
345
346 fprintf(f, "\tsearch options\n");
347 fprintf(f, "\tsize (in number of bits) of the hash table: %d\n", options.hash_table_size);
348 fprintf(f, "\tsorting depth increment: pv = %d, all = %d, cut = %d\n", options.inc_sort_depth[0], options.inc_sort_depth[1], options.inc_sort_depth[2]);
349 fprintf(f, "\ttask number for parallel search: %d\n", options.n_task);
350 fprintf(f, "\tsearch level: %d\n", options.level);
351 fprintf(f, "\tsearch alloted time:"); time_print(options.time, false, stdout); fprintf(f, "\n");
352 fprintf(f, "\tsearch with: %s\n", play_type[options.play_type]);
353 fprintf(f, "\tsearch pondering: %s\n", boolean_string[options.can_ponder]);
354 fprintf(f, "\tsearch depth: %d\n", options.depth);
355 fprintf(f, "\tsearch selectivity: %d\n", options.selectivity);
356 fprintf(f, "\tsearch speed %.0f N/s\n", options.speed);
357 fprintf(f, "\tsearch nps %.0f N/s\n", options.nps);
358 fprintf(f, "\tsearch alpha: %d\n", options.alpha);
359 fprintf(f, "\tsearch beta: %d\n", options.beta);
360 fprintf(f, "\tsearch all best moves: %s\n", boolean_string[options.all_best]);
361 fprintf(f, "\teval file: %s\n", options.eval_file);
362 fprintf(f, "\tbook file: %s\n", options.book_file);
363 fprintf(f, "\tbook allowed: %s\n", boolean_string[options.book_allowed]);
364 fprintf(f, "\tbook randomness: %d\n\n", options.book_randomness);
365
366 fprintf(f, "ggs options\n");
367 fprintf(f, "\thost: %s\n", options.ggs_host ? options.ggs_host : "?");
368 fprintf(f, "\tport: %s\n", options.ggs_port ? options.ggs_port : "?");
369 fprintf(f, "\tlogin: %s\n", options.ggs_login ? options.ggs_login : "?");
370 fprintf(f, "\tpassword: %s\n", options.ggs_password ? options.ggs_password : "?");
371 fprintf(f, "\topen: %s\n\n", boolean_string[options.ggs_open]);
372
373 fprintf(f, "PV options\n");
374 fprintf(f, "\tdebug: %s\n", boolean_string[options.pv_debug]);
375 fprintf(f, "\tcheck: %s\n", boolean_string[options.pv_check]);
376 fprintf(f, "\tguess: %s\n\n", boolean_string[options.pv_guess]);
377
378 fprintf(f, "game file: %s\n", options.game_file ? options.game_file : "?");
379
380 fprintf(f, "log files\n");
381 fprintf(f, "\tsearch: %s\n", options.search_log_file ? options.search_log_file : "?");
382 fprintf(f, "\tui: %s\n", options.ui_log_file ? options.ui_log_file : "?");
383 fprintf(f, "\tggs: %s\n", options.ggs_log_file ? options.ggs_log_file : "?");
384
385 fprintf(f, "name: %s\n", options.name ? options.name : "?");
386
387 fprintf(f, "Game play\n");
388 fprintf(f, "\tmode: %s\n", mode[options.mode]);
389 fprintf(f, "\tstart a new game after a game is over: %s\n", boolean_string[options.auto_start]);
390 fprintf(f, "\tstore each played game in the opening book: %s\n", boolean_string[options.auto_start]);
391 fprintf(f, "\tchange computer's side after each game: %s\n", boolean_string[options.auto_start]);
392 fprintf(f, "\tquit when game is over: %s\n", boolean_string[options.auto_start]);
393 fprintf(f, "\trepeat %d games (before exiting)\n\n\n", options.repeat);
394}
395
399void options_free(void)
400{
401 if (options.ggs_host) free(options.ggs_host);
404 if (options.ggs_port) free(options.ggs_port);
405
410 if (options.name) free(options.name);
413
414 options.ggs_host = NULL;
415 options.ggs_login = NULL;
416 options.ggs_password = NULL;
417 options.ggs_port = NULL;
418
419 options.game_file = NULL;
420 options.ui_log_file = NULL;
422 options.ggs_log_file = NULL;
423 options.name = NULL;
424 options.book_file = NULL;
425 options.eval_file = NULL;
426
427}
428
#define SCORE_MAX
Definition const.h:58
#define TIME_MAX
Definition const.h:61
#define MAX_THREADS
Definition const.h:15
#define EDAX_NAME
Definition const.h:89
@ CUT_NODE
Definition const.h:82
@ PV_NODE
Definition const.h:81
@ ALL_NODE
Definition const.h:83
#define SCORE_MIN
Definition const.h:55
void usage(void)
Programme usage.
Definition main.c:51
void options_usage(void)
Print options usage.
Definition options.c:90
static const char * option_parse(const char *line, char *option, char *value, int size)
parse an option from a string
Definition options.c:249
void options_free(void)
free allocated resources.
Definition options.c:399
Options options
Definition options.c:22
int options_read(const char *option, const char *value)
Read an option.
Definition options.c:136
void options_dump(FILE *f)
Print all global options.
Definition options.c:330
void options_parse(const char *file)
parse options from a file
Definition options.c:266
void options_bound(void)
Keep options between realistic values.
Definition options.c:288
@ EDAX_TIME_PER_GAME
Definition options.h:19
@ EDAX_FIXED_LEVEL
Definition options.h:18
@ EDAX_TIME_PER_MOVE
Definition options.h:20
Statistics header.
Definition options.h:24
PlayType play_type
Definition options.h:42
bool book_allowed
Definition options.h:60
bool can_ponder
Definition options.h:43
bool all_best
Definition options.h:55
bool cpu_affinity
Definition options.h:30
double speed
Definition options.h:49
int n_task
Definition options.h:29
char * ggs_host
Definition options.h:63
int level
Definition options.h:40
char * eval_file
Definition options.h:57
char * ui_log_file
Definition options.h:78
bool pv_guess
Definition options.h:73
char * ggs_password
Definition options.h:65
int depth
Definition options.h:44
bool echo
Definition options.h:35
bool pv_check
Definition options.h:72
bool auto_start
Definition options.h:83
int repeat
Definition options.h:87
int width
Definition options.h:34
int book_randomness
Definition options.h:61
int inc_sort_depth[3]
Definition options.h:27
int alpha
Definition options.h:52
char * search_log_file
Definition options.h:77
int noise
Definition options.h:33
bool pv_debug
Definition options.h:71
char * game_file
Definition options.h:75
char * ggs_login
Definition options.h:64
bool auto_quit
Definition options.h:86
bool transgress_cassio
Definition options.h:38
int mode
Definition options.h:47
double probcut_d
Definition options.h:69
bool ggs_open
Definition options.h:67
char * ggs_log_file
Definition options.h:79
long long time
Definition options.h:41
double nps
Definition options.h:50
bool auto_swap
Definition options.h:85
char * ggs_port
Definition options.h:66
bool info
Definition options.h:36
char * book_file
Definition options.h:59
int beta
Definition options.h:53
int hash_table_size
Definition options.h:25
char * name
Definition options.h:81
bool debug_cassio
Definition options.h:37
int selectivity
Definition options.h:45
bool auto_store
Definition options.h:84
int verbosity
Definition options.h:32
void time_print(long long t, bool justified, FILE *f)
Print time as "D:HH:MM:SS.CC".
Definition util.c:131
char * string_duplicate(const char *s)
Duplicate a string.
Definition util.c:299
char * parse_int(const char *string, int *result)
Parse an integer.
Definition util.c:761
char * parse_real(const char *string, double *result)
Parse a real number (as a double floating point).
Definition util.c:796
char * string_read_line(FILE *f)
Read a line.
Definition util.c:265
int string_to_int(const char *s, const int default_value)
Convert a string into an integer.
Definition util.c:457
int get_cpu_number(void)
Get the number of cpus or cores on the machine.
Definition util.c:987
double string_to_real(const char *s, const double default_value)
Convert a string into a real number.
Definition util.c:488
char * parse_word(const char *string, char *word, unsigned int n)
Parse a word.
Definition util.c:562
long long string_to_time(const char *string)
Read time as "D:HH:MM:SS.C".
Definition util.c:320
char * parse_boolean(const char *string, bool *result)
Parse a boolean.
Definition util.c:741
Miscellaneous utilities header.
#define MIN(a, b)
Definition util.h:101
#define info(...)
Display a message.
Definition util.h:382
#define BOUND(var, min, max, name)
Definition util.h:104