zzgo -t: TD_GAMES TT_TOTAL is supported
[pachi/t.git] / timeinfo.c
blob4081a596b2324125c0dc19eb7a600026ec6bc854
1 #include <assert.h>
2 #include <ctype.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <math.h>
6 #include <time.h>
8 #define DEBUG
10 #include "debug.h"
11 #include "timeinfo.h"
13 #define MAX_NET_LAG 2.0 /* Max net lag in seconds. TODO: estimate dynamically. */
14 #define RESERVED_BYOYOMI_PERCENT 15 /* Reserve 15% of byoyomi time as safety margin if risk of losing on time */
16 /* For safety, use at most 3 times the desired time on a single move
17 * in main time, and 1.1 times in byoyomi. */
18 #define MAX_MAIN_TIME_EXTENSION 3.0
19 #define MAX_BYOYOMI_TIME_EXTENSION 1.1
21 bool
22 time_parse(struct time_info *ti, char *s)
24 switch (s[0]) {
25 case '_': ti->period = TT_TOTAL; s++; break;
26 default: ti->period = TT_MOVE; break;
28 switch (s[0]) {
29 case '=':
30 ti->dim = TD_GAMES;
31 ti->len.games = atoi(++s);
32 break;
33 default:
34 if (!isdigit(s[0]))
35 return false;
36 ti->dim = TD_WALLTIME;
37 ti->len.t.main_time = atof(s);
38 ti->len.t.byoyomi_time = 0.0;
39 ti->len.t.byoyomi_periods = 0;
40 ti->len.t.byoyomi_stones = 0;
41 ti->len.t.timer_start = 0;
42 break;
44 return true;
47 /* Update time settings according to gtp time_settings or kgs-time_settings command. */
48 void
49 time_settings(struct time_info *ti, int main_time, int byoyomi_time, int byoyomi_stones, int byoyomi_periods)
51 if (byoyomi_time > 0 && byoyomi_stones == 0) {
52 ti->period = TT_NULL; // no time limit, rely on engine default
53 } else {
54 ti->period = TT_TOTAL;
55 ti->dim = TD_WALLTIME;
56 ti->len.t.main_time = (double) main_time;
57 ti->len.t.byoyomi_time = (double) byoyomi_time;
58 ti->len.t.byoyomi_periods = byoyomi_periods > 1 ? byoyomi_periods : 1;
59 ti->len.t.byoyomi_stones = byoyomi_stones > 1 ? byoyomi_stones : 1;
60 ti->len.t.canadian = byoyomi_stones > 0;
61 ti->len.t.timer_start = 0;
65 /* Update time information according to gtp time_left command.
66 * kgs doesn't give time_left for the first move, so make sure
67 * that just time_settings + time_stop_conditions still work. */
68 void
69 time_left(struct time_info *ti, int time_left, int stones_left)
71 assert(ti->period != TT_NULL);
72 ti->dim = TD_WALLTIME;
74 if (stones_left == 0) {
75 /* Main time */
76 ti->period = TT_TOTAL;
77 ti->len.t.main_time = time_left;
78 /* byoyomi_time kept fully charged. */
79 } else {
80 /* Byoyomi */
81 ti->period = TT_MOVE;
82 ti->len.t.main_time = 0;
83 ti->len.t.byoyomi_time = time_left;
84 if (ti->len.t.canadian) {
85 ti->len.t.byoyomi_stones = stones_left;
86 } else {
87 // field misused by kgs
88 ti->len.t.byoyomi_periods = stones_left;
93 /* Returns true if we are in byoyomi (or should play as if in byo yomi
94 * because remaining time per move in main time is less than byoyomi time
95 * per move). */
96 bool
97 time_in_byoyomi(struct time_info *ti) {
98 assert(ti->dim == TD_WALLTIME);
99 if (!ti->len.t.byoyomi_time)
100 return false; // there is no byoyomi!
101 assert(ti->len.t.byoyomi_stones > 0);
102 if (!ti->len.t.main_time)
103 return true; // we _are_ in byoyomi
104 if (ti->len.t.main_time <= ti->len.t.byoyomi_time / ti->len.t.byoyomi_stones + 0.001)
105 return true; // our basic time left is less than byoyomi time per move
106 return false;
109 /* Start our timer. kgs does this (correctly) on "play" not "genmove"
110 * unless we are making the first move of the game. */
111 void
112 time_start_timer(struct time_info *ti)
114 if (ti->period != TT_NULL && ti->dim == TD_WALLTIME)
115 ti->len.t.timer_start = time_now();
118 /* Returns the current time. */
119 double
120 time_now(void)
122 struct timespec now;
123 clock_gettime(CLOCK_REALTIME, &now);
124 return now.tv_sec + now.tv_nsec/1000000000.0;
127 /* Sleep for a given interval (in seconds). Return immediately if interval < 0. */
128 void
129 time_sleep(double interval)
131 struct timespec ts;
132 double sec;
133 ts.tv_nsec = (int)(modf(interval, &sec)*1000000000.0);
134 ts.tv_sec = (int)sec;
135 nanosleep(&ts, NULL); /* ignore error if interval was < 0 */
139 /* Pre-process time_info for search control and sets the desired stopping conditions. */
140 void
141 time_stop_conditions(struct time_info *ti, struct board *b, int fuseki_end, int yose_start, struct time_stop *stop)
143 /* We must have _some_ limits by now, be it random default values! */
144 assert(ti->period != TT_NULL);
146 /* Special-case limit by number of simulations. */
147 if (ti->dim == TD_GAMES) {
148 if (ti->period == TT_TOTAL) {
149 ti->period = TT_MOVE;
150 ti->len.games /= board_estimated_moves_left(b);
153 stop->desired.playouts = ti->len.games;
154 /* We force worst == desired, so note that we will NOT loop
155 * until best == winner. */
156 stop->worst.playouts = ti->len.games;
157 return;
160 assert(ti->dim == TD_WALLTIME);
163 /*** Transform @ti to TT_MOVE and set up recommended/max time and
164 * net lag information. */
167 /* Minimum net lag (seconds) to be reserved in the time for move. */
168 double net_lag = MAX_NET_LAG;
169 /* Make sure timer_start is set up, adjust net_lag. */
170 if (!ti->len.t.timer_start) {
171 ti->len.t.timer_start = time_now(); // we're playing the first game move
172 } else {
173 net_lag += time_now() - ti->len.t.timer_start;
174 // TODO: keep statistics to get good estimate of lag not just current move
177 /* Absolute maximum time possible to spend on the move. */
178 double max_time;
179 /* Ideal/reasonable time to spend on the move. */
180 double recommended_time;
181 /* Set up initial recommendations. */
182 if (!ti->len.t.main_time) {
183 max_time = ti->len.t.byoyomi_time;
184 assert(ti->len.t.byoyomi_stones > 0);
185 recommended_time = ti->len.t.byoyomi_time / ti->len.t.byoyomi_stones;
186 } else {
187 max_time = recommended_time = ti->len.t.main_time;
190 if (ti->period == TT_TOTAL) {
191 int moves_left = board_estimated_moves_left(b);
192 if (ti->len.t.byoyomi_time > 0) {
193 assert(ti->len.t.byoyomi_stones > 0);
194 /* Time for one move in byoyomi. */
195 double move_time = ti->len.t.byoyomi_time / ti->len.t.byoyomi_stones;
197 /* For Japanese byoyomi with N>1 periods, we use N-1 periods as main time,
198 * keeping the last one as insurance against unexpected net lag. */
199 if (ti->len.t.byoyomi_periods > 2) {
200 max_time += (ti->len.t.byoyomi_periods - 2) * move_time;
201 // Will add 1 more byoyomi_time just below
203 max_time += move_time;
204 recommended_time = max_time;
206 /* Maximize the number of moves played uniformly in main time, while
207 * not playing faster in main time than in byoyomi. At this point,
208 * the main time remaining is ti->len.t.max_time and already includes
209 * the first (canadian) or N-1 byoyomi periods.
210 * main_speed = max_time / main_moves >= move_time
211 * => main_moves <= max_time / move_time */
212 double actual_byoyomi = move_time - net_lag;
213 if (actual_byoyomi > 0) {
214 int main_moves = (int)(max_time / actual_byoyomi);
215 if (moves_left > main_moves)
216 moves_left = main_moves; // will do the rest in byoyomi
217 if (moves_left <= 0) // possible if too much lag
218 moves_left = 1;
221 ti->period = TT_MOVE;
222 recommended_time /= moves_left;
224 // To simplify the engine code, do not leave negative times:
225 if (recommended_time < 0)
226 recommended_time = 0;
227 if (max_time < 0)
228 max_time = 0;
229 assert(recommended_time <= max_time + 0.001);
231 /* Use a larger safety margin if we risk losing on time on this move: */
232 double safe_margin = RESERVED_BYOYOMI_PERCENT * ti->len.t.byoyomi_time/100;
233 if (safe_margin > 0) {
234 assert(ti->len.t.byoyomi_stones > 0);
235 safe_margin /= ti->len.t.byoyomi_stones;
237 if (safe_margin > MAX_NET_LAG && recommended_time >= max_time - net_lag) {
238 net_lag = safe_margin;
241 if (DEBUGL(1))
242 fprintf(stderr, "recommended_time %0.2f, max_time %0.2f, byoyomi %0.2f/%d, lag %0.2f\n",
243 recommended_time, max_time,
244 ti->len.t.byoyomi_time, ti->len.t.byoyomi_stones,
245 net_lag);
248 /*** Setup desired/worst time limits based on recommended/max time. */
250 assert(ti->period == TT_MOVE);
252 double desired_time = recommended_time;
253 double worst_time;
254 if (time_in_byoyomi(ti)) {
255 // make recommended == average(desired, worst)
256 worst_time = desired_time * MAX_BYOYOMI_TIME_EXTENSION;
257 desired_time *= (2 - MAX_BYOYOMI_TIME_EXTENSION);
259 } else {
260 int bsize = (board_size(b)-2)*(board_size(b)-2);
261 fuseki_end = fuseki_end * bsize / 100; // move nb at fuseki end
262 yose_start = yose_start * bsize / 100; // move nb at yose start
263 assert(fuseki_end < yose_start);
265 /* Before yose, spend some extra. */
266 if (b->moves < yose_start) {
267 int moves_to_yose = (yose_start - b->moves) / 2;
268 // ^- /2 because we only consider the moves we have to play ourselves
269 int left_at_yose_start = board_estimated_moves_left(b) - moves_to_yose;
270 if (left_at_yose_start < MIN_MOVES_LEFT)
271 left_at_yose_start = MIN_MOVES_LEFT;
272 double longest_time = max_time / left_at_yose_start;
273 if (longest_time < desired_time) {
274 // Should rarely happen, but keep desired_time anyway
275 } else if (b->moves < fuseki_end) {
276 assert(fuseki_end > 0);
277 desired_time += ((longest_time - desired_time) * b->moves) / fuseki_end;
278 } else { assert(b->moves < yose_start);
279 desired_time = longest_time;
282 worst_time = desired_time * MAX_MAIN_TIME_EXTENSION;
284 if (worst_time > max_time)
285 worst_time = max_time;
286 if (desired_time > worst_time)
287 desired_time = worst_time;
289 stop->desired.time = ti->len.t.timer_start + desired_time - net_lag;
290 stop->worst.time = ti->len.t.timer_start + worst_time - net_lag;
291 // Both stop points may be in the past if too much lag.
293 if (DEBUGL(2))
294 fprintf(stderr, "desired time %.02f, worst %.02f\n", desired_time, worst_time);