Remove deprecated code
[matilda.git] / src / engine.c
blobe99937fc385c3b37cdfe28ded0fb5f8424f0edad
1 /*
2 Functions that control the flow of information of Matilda as a complete Go
3 playing program. Allows executing strategies with some abstraction, performing
4 maintenance if needed.
5 */
7 #include "config.h"
9 #include <stdio.h>
10 #include <string.h>
11 #include <sys/types.h> /* opendir */
12 #include <dirent.h> /* opendir */
14 #include "alloc.h"
15 #include "board.h"
16 #include "cfg_board.h"
17 #include "flog.h"
18 #include "game_record.h"
19 #include "mcts.h"
20 #include "opening_book.h"
21 #include "stringm.h"
22 #include "transpositions.h"
23 #include "types.h"
24 #include "version.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.
36 void version_string(
37 char * dst
38 ) {
39 #ifdef COMMITN
40 if (strlen(COMMITN)) {
41 snprintf(dst, MAX_PAGE_SIZ, "%s (%s)", MATILDA_VERSION, COMMITN);
42 } else {
43 snprintf(dst, MAX_PAGE_SIZ, "%s", MATILDA_VERSION);
45 #else
46 snprintf(dst, MAX_PAGE_SIZ, "%s", MATILDA_VERSION);
47 #endif
51 Obtains the current data folder path. It may be absolute or relative and ends
52 with a path separator.
53 RETURNS folder path
55 const char * data_folder() {
56 return _data_folder;
60 Test if folder exists and is accessible.
61 RETURNS true if exists
63 bool folder_exists(
64 const char * filename
65 ) {
66 DIR * dir = opendir(filename);
68 if (dir != NULL) {
69 closedir(dir);
70 return false;
73 return true;
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
81 bool set_data_folder(
82 const char * s
83 ) {
84 u32 l = strlen(s);
86 if (l < 2 || l >= MAX_PATH_SIZ - 2) {
87 return false;
90 if (folder_exists(s)) {
91 return false;
94 if (s[l - 1] == '/') {
95 snprintf(_data_folder, MAX_PATH_SIZ, "%s", s);
96 } else {
97 snprintf(_data_folder, MAX_PATH_SIZ, "%s/", s);
100 return true;
104 Set whether to attempt to use, or not, opening books prior to MCTS.
106 void set_use_of_opening_book(
107 bool use_ob
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(
118 const board * b,
119 bool is_black,
120 out_board * out_b,
121 u64 stop_time,
122 u64 early_stop_time
124 if (use_opening_book) {
125 board tmp;
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);
131 return true;
135 bool ret = mcts_start_timed(out_b, b, is_black, stop_time, early_stop_time);
136 tt_requires_maintenance = true;
137 return ret;
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(
145 const board * b,
146 bool is_black,
147 out_board * out_b,
148 u32 simulations
150 if (use_opening_book) {
151 board tmp;
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);
157 return true;
161 bool ret = mcts_start_sims(out_b, b, is_black, simulations);
162 tt_requires_maintenance = true;
163 return ret;
167 Evaluate the position for a short amount of time, ignoring the quality matrix
168 produced.
170 void evaluate_in_background(
171 const board * b,
172 bool is_black
174 mcts_resume(b, is_black);
175 tt_requires_maintenance = true;
178 static void freed_mem_message(
179 u32 states
181 if (states == 0) {
182 return;
185 char * s = alloc();
186 char * s2 = alloc();
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);
192 release(s2);
193 release(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
209 is_black.
211 void opt_turn_maintenance(
212 const board * b,
213 bool is_black
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
224 doesn't.
226 void assert_data_folder_exists() {
227 DIR * dir = opendir(data_folder());
229 if (dir == NULL) {
230 char * s = alloc();
231 snprintf(s, MAX_PAGE_SIZ, "data folder %s does not exist or is unavailable\n", data_folder());
232 flog_crit("data", s);
233 release(s);
234 } else {
235 closedir(dir);