time_in_byoyomi(): Rewrite, smoother to read and more flexible now
[pachi.git] / timeinfo.c
blob2a9eec78cb8ba8f2c17d4d2925243ef5a4b8c0eb
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.timer_start = 0;
41 break;
43 return true;
46 /* Update time settings according to gtp time_settings or kgs-time_settings command. */
47 void
48 time_settings(struct time_info *ti, int main_time, int byoyomi_time, int byoyomi_stones, int byoyomi_periods)
50 if (byoyomi_time > 0 && byoyomi_stones == 0) {
51 ti->period = TT_NULL; // no time limit, rely on engine default
52 } else {
53 ti->period = TT_TOTAL;
54 ti->dim = TD_WALLTIME;
55 ti->len.t.main_time = (double) main_time;
56 ti->len.t.byoyomi_time = (double) byoyomi_time;
57 if (byoyomi_stones > 0)
58 ti->len.t.byoyomi_time /= byoyomi_stones;
59 ti->len.t.byoyomi_periods = byoyomi_periods;
60 ti->len.t.timer_start = 0;
64 /* Update time information according to gtp time_left command.
65 * kgs doesn't give time_left for the first move, so make sure
66 * that just time_settings + time_stop_conditions still work. */
67 void
68 time_left(struct time_info *ti, int time_left, int stones_left)
70 assert(ti->period != TT_NULL);
71 ti->dim = TD_WALLTIME;
73 if (ti->len.t.byoyomi_periods > 0 && stones_left > 0) {
74 ti->len.t.byoyomi_periods = stones_left; // field misused by kgs
75 stones_left = 1;
77 if (stones_left == 0) {
78 /* Main time */
79 ti->period = TT_TOTAL;
80 ti->len.t.main_time = time_left;
81 /* byoyomi_time kept fully charged. */
82 } else {
83 /* Byoyomi */
84 ti->period = TT_MOVE;
85 ti->len.t.main_time = 0;
86 ti->len.t.byoyomi_time = ((double)time_left)/stones_left;
87 /* XXX: We need to keep stones_left info in time_info
88 * to be able to deduce this in stop_conditions() instead. */
89 ti->len.t.max_time = time_left;
90 ti->len.t.recommended_time = ti->len.t.byoyomi_time;
94 /* Returns true if we are in byoyomi (or should play as if in byo yomi
95 * because remaining time per move in main time is less than byoyomi time
96 * per move). */
97 bool
98 time_in_byoyomi(struct time_info *ti) {
99 assert(ti->dim == TD_WALLTIME && ti->period == TT_MOVE);
100 if (!ti->len.t.byoyomi_time)
101 return false; // there is no byoyomi!
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 + 0.001)
105 return true; // our basic time left is less than byoyomi period
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 /* Minimum net lag (seconds) to be reserved in the time for move. */
147 double net_lag = MAX_NET_LAG;
149 /* Special-case limit by number of simulations. */
150 if (ti->dim == TD_GAMES) {
151 if (ti->period == TT_TOTAL) {
152 ti->period = TT_MOVE;
153 ti->len.games /= board_estimated_moves_left(b);
156 stop->desired.playouts = ti->len.games;
157 /* We force worst == desired, so note that we will NOT loop
158 * until best == winner. */
159 stop->worst.playouts = ti->len.games;
160 return;
163 assert(ti->dim == TD_WALLTIME);
166 /*** Transform @ti to TT_MOVE and set up recommended/max time and
167 * net lag information. */
169 /* Make sure timer_start is set up, adjust net_lag. */
170 double now = time_now();
171 if (!ti->len.t.timer_start) {
172 ti->len.t.timer_start = now; // we're playing the first game move
173 } else {
174 net_lag += now - ti->len.t.timer_start;
175 // TODO: keep statistics to get good estimate of lag not just current move
178 /* Set up initial recommendations. */
179 if (ti->len.t.main_time) {
180 ti->len.t.max_time = ti->len.t.recommended_time = ti->len.t.main_time;
181 } // otherwise we have already set up max_, recommended_ in time_left().
183 if (ti->period == TT_TOTAL) {
184 int moves_left = board_estimated_moves_left(b);
185 if (ti->len.t.byoyomi_time > 0) {
186 /* For non-canadian byoyomi with N>1 periods, we use N-1 periods as main time,
187 * keeping the last one as insurance against unexpected net lag. */
188 if (ti->len.t.byoyomi_periods > 2) {
189 ti->len.t.max_time += (ti->len.t.byoyomi_periods - 2) * ti->len.t.byoyomi_time;
190 // Will add 1 more byoyomi_time just below
192 ti->len.t.max_time += ti->len.t.byoyomi_time;
193 ti->len.t.recommended_time = ti->len.t.max_time;
195 /* Maximize the number of moves played uniformly in main time, while
196 * not playing faster in main time than in byoyomi. At this point,
197 * the main time remaining is ti->len.t.max_time and already includes
198 * the first (canadian) or N-1 byoyomi periods.
199 * main_speed = max_time / main_moves >= byoyomi_time
200 * => main_moves <= max_time / byoyomi_time */
201 double actual_byoyomi = ti->len.t.byoyomi_time - net_lag;
202 if (actual_byoyomi > 0) {
203 int main_moves = (int)(ti->len.t.max_time / actual_byoyomi);
204 if (moves_left > main_moves)
205 moves_left = main_moves; // will do the rest in byoyomi
206 if (moves_left <= 0) // possible if too much lag
207 moves_left = 1;
210 ti->period = TT_MOVE;
211 ti->len.t.recommended_time /= moves_left;
213 // To simplify the engine code, do not leave negative times:
214 if (ti->len.t.recommended_time < 0)
215 ti->len.t.recommended_time = 0;
216 if (ti->len.t.max_time < 0)
217 ti->len.t.max_time = 0;
218 assert(ti->len.t.recommended_time <= ti->len.t.max_time + 0.001);
220 /* Use a larger safety margin if we risk losing on time on this move: */
221 double safe_margin = RESERVED_BYOYOMI_PERCENT * ti->len.t.byoyomi_time/100;
222 if (safe_margin > MAX_NET_LAG && ti->len.t.recommended_time >= ti->len.t.max_time - net_lag) {
223 net_lag = safe_margin;
226 if (DEBUGL(1))
227 fprintf(stderr, "recommended_time %0.2f, max_time %0.2f, byoyomi %0.2f, lag %0.2f max %0.2f\n",
228 ti->len.t.recommended_time, ti->len.t.max_time, ti->len.t.byoyomi_time, net_lag,
229 net_lag);
232 /*** Setup desired/worst time limits based on recommended/max time. */
234 assert(ti->period == TT_MOVE);
236 double desired_time = ti->len.t.recommended_time;
237 double worst_time;
238 if (time_in_byoyomi(ti)) {
239 // make recommended == average(desired, worst)
240 worst_time = desired_time * MAX_BYOYOMI_TIME_EXTENSION;
241 desired_time *= (2 - MAX_BYOYOMI_TIME_EXTENSION);
243 } else {
244 int bsize = (board_size(b)-2)*(board_size(b)-2);
245 fuseki_end = fuseki_end * bsize / 100; // move nb at fuseki end
246 yose_start = yose_start * bsize / 100; // move nb at yose start
247 assert(fuseki_end < yose_start);
249 /* Before yose, spend some extra. */
250 if (b->moves < yose_start) {
251 int moves_to_yose = (yose_start - b->moves) / 2;
252 // ^- /2 because we only consider the moves we have to play ourselves
253 int left_at_yose_start = board_estimated_moves_left(b) - moves_to_yose;
254 if (left_at_yose_start < MIN_MOVES_LEFT)
255 left_at_yose_start = MIN_MOVES_LEFT;
256 double longest_time = ti->len.t.max_time / left_at_yose_start;
257 if (longest_time < desired_time) {
258 // Should rarely happen, but keep desired_time anyway
259 } else if (b->moves < fuseki_end) {
260 assert(fuseki_end > 0);
261 desired_time += ((longest_time - desired_time) * b->moves) / fuseki_end;
262 } else { assert(b->moves < yose_start);
263 desired_time = longest_time;
266 worst_time = desired_time * MAX_MAIN_TIME_EXTENSION;
268 if (worst_time > ti->len.t.max_time)
269 worst_time = ti->len.t.max_time;
270 if (desired_time > worst_time)
271 desired_time = worst_time;
273 stop->desired.time = ti->len.t.timer_start + desired_time - net_lag;
274 stop->worst.time = ti->len.t.timer_start + worst_time - net_lag;
275 // Both stop points may be in the past if too much lag.
277 if (DEBUGL(2))
278 fprintf(stderr, "desired time %.02f, worst %.02f\n", desired_time, worst_time);