2 Functions that control the flow of information of Matilda as a complete Go
3 playing program. Allows executing strategies with some abstraction, performing
11 #include <sys/types.h> /* opendir */
12 #include <dirent.h> /* opendir */
16 #include "cfg_board.h"
18 #include "game_record.h"
20 #include "opening_book.h"
22 #include "transpositions.h"
26 static bool use_opening_book
= true;
28 bool tt_requires_maintenance
= false; /* set after MCTS start/resume call */
31 static char _data_folder
[MAX_PATH_SIZ
] = DEFAULT_DATA_PATH
;
34 Produce a short version string. Does not include program name.
40 if (strlen(COMMITN
)) {
41 snprintf(dst
, MAX_PAGE_SIZ
, "%s (%s)", MATILDA_VERSION
, COMMITN
);
43 snprintf(dst
, MAX_PAGE_SIZ
, "%s", MATILDA_VERSION
);
46 snprintf(dst
, MAX_PAGE_SIZ
, "%s", MATILDA_VERSION
);
51 Obtains the current data folder path. It may be absolute or relative and ends
52 with a path separator.
55 const char * data_folder() {
60 Test if folder exists and is accessible.
61 RETURNS true if exists
66 DIR * dir
= opendir(filename
);
77 Sets the new data folder path. If the path is too long, short, or otherwise
78 invalid, nothing is changed and false is returned.
79 RETURNS true on success
86 if (l
< 2 || l
>= MAX_PATH_SIZ
- 2) {
90 if (folder_exists(s
)) {
94 if (s
[l
- 1] == '/') {
95 snprintf(_data_folder
, MAX_PATH_SIZ
, "%s", s
);
97 snprintf(_data_folder
, MAX_PATH_SIZ
, "%s/", s
);
104 Set whether to attempt to use, or not, opening books prior to MCTS.
106 void set_use_of_opening_book(
109 use_opening_book
= use_ob
;
113 Evaluates the position given the time available to think, by using a number of
114 strategies in succession.
115 RETURNS true if a play or pass is suggested instead of resigning
117 bool evaluate_position_timed(
124 if (use_opening_book
) {
126 memcpy(&tmp
, b
, sizeof(board
));
127 d8 reduction
= reduce_auto(&tmp
, true);
129 if (opening_book(out_b
, &tmp
)) {
130 out_board_revert_reduce(out_b
, reduction
);
135 bool ret
= mcts_start_timed(out_b
, b
, is_black
, stop_time
, early_stop_time
);
136 tt_requires_maintenance
= true;
141 Evaluates the position with the number of simulations available.
142 RETURNS true if a play or pass is suggested instead of resigning
144 bool evaluate_position_sims(
150 if (use_opening_book
) {
152 memcpy(&tmp
, b
, sizeof(board
));
153 d8 reduction
= reduce_auto(&tmp
, is_black
);
155 if (opening_book(out_b
, &tmp
)) {
156 out_board_revert_reduce(out_b
, reduction
);
161 bool ret
= mcts_start_sims(out_b
, b
, is_black
, simulations
);
162 tt_requires_maintenance
= true;
167 Evaluate the position for a short amount of time, ignoring the quality matrix
170 void evaluate_in_background(
174 mcts_resume(b
, is_black
);
175 tt_requires_maintenance
= true;
178 static void freed_mem_message(
188 format_mem_size(s2
, states
* sizeof(tt_stats
));
189 snprintf(s
, MAX_PAGE_SIZ
, "freed %u states (%s)", states
, s2
);
190 flog_info("engn", s
);
197 Inform that we are currently between matches and proceed with the maintenance
198 that is suitable at the moment.
200 void new_match_maintenance() {
201 u32 freed
= tt_clean_all();
202 tt_requires_maintenance
= false;
203 freed_mem_message(freed
);
207 Perform between-turn maintenance. If there is any information from MCTS-UCT that
208 can be freed, it will be done to the states not reachable from state b played by
211 void opt_turn_maintenance(
215 if (tt_requires_maintenance
) {
216 u32 freed
= tt_clean_unreachable(b
, is_black
);
217 tt_requires_maintenance
= false;
218 freed_mem_message(freed
);
223 Asserts the ./data folder exists, closing the program and warning the user if it
226 void assert_data_folder_exists() {
227 DIR * dir
= opendir(data_folder());
231 snprintf(s
, MAX_PAGE_SIZ
, "data folder %s does not exist or is unavailable\n", data_folder());
232 flog_crit("data", s
);