time_info.len.t.main_time: Introduce, make recommended,max_time second-class citizens
[pachi/json.git] / timeinfo.c
blobef95d61aeeae5bc86ede16ecc04a94743672b94f
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 return ti->period == TT_MOVE && ti->dim == TD_WALLTIME && ti->len.t.byoyomi_time > 0
100 && ti->len.t.recommended_time <= ti->len.t.byoyomi_time + 0.001;
103 /* Start our timer. kgs does this (correctly) on "play" not "genmove"
104 * unless we are making the first move of the game. */
105 void
106 time_start_timer(struct time_info *ti)
108 if (ti->period != TT_NULL && ti->dim == TD_WALLTIME)
109 ti->len.t.timer_start = time_now();
112 /* Returns the current time. */
113 double
114 time_now(void)
116 struct timespec now;
117 clock_gettime(CLOCK_REALTIME, &now);
118 return now.tv_sec + now.tv_nsec/1000000000.0;
121 /* Sleep for a given interval (in seconds). Return immediately if interval < 0. */
122 void
123 time_sleep(double interval)
125 struct timespec ts;
126 double sec;
127 ts.tv_nsec = (int)(modf(interval, &sec)*1000000000.0);
128 ts.tv_sec = (int)sec;
129 nanosleep(&ts, NULL); /* ignore error if interval was < 0 */
133 /* Pre-process time_info for search control and sets the desired stopping conditions. */
134 void
135 time_stop_conditions(struct time_info *ti, struct board *b, int fuseki_end, int yose_start, struct time_stop *stop)
137 /* We must have _some_ limits by now, be it random default values! */
138 assert(ti->period != TT_NULL);
140 /* Minimum net lag (seconds) to be reserved in the time for move. */
141 double net_lag = MAX_NET_LAG;
143 /* Special-case limit by number of simulations. */
144 if (ti->dim == TD_GAMES) {
145 if (ti->period == TT_TOTAL) {
146 ti->period = TT_MOVE;
147 ti->len.games /= board_estimated_moves_left(b);
150 stop->desired.playouts = ti->len.games;
151 /* We force worst == desired, so note that we will NOT loop
152 * until best == winner. */
153 stop->worst.playouts = ti->len.games;
154 return;
157 assert(ti->dim == TD_WALLTIME);
160 /*** Transform @ti to TT_MOVE and set up recommended/max time and
161 * net lag information. */
163 /* Make sure timer_start is set up, adjust net_lag. */
164 double now = time_now();
165 if (!ti->len.t.timer_start) {
166 ti->len.t.timer_start = now; // we're playing the first game move
167 } else {
168 net_lag += now - ti->len.t.timer_start;
169 // TODO: keep statistics to get good estimate of lag not just current move
172 /* Set up initial recommendations. */
173 if (ti->len.t.main_time) {
174 ti->len.t.max_time = ti->len.t.recommended_time = ti->len.t.main_time;
175 } // otherwise we have already set up max_, recommended_ in time_left().
177 if (ti->period == TT_TOTAL) {
178 int moves_left = board_estimated_moves_left(b);
179 if (ti->len.t.byoyomi_time > 0) {
180 /* For non-canadian byoyomi with N>1 periods, we use N-1 periods as main time,
181 * keeping the last one as insurance against unexpected net lag. */
182 if (ti->len.t.byoyomi_periods > 2) {
183 ti->len.t.max_time += (ti->len.t.byoyomi_periods - 2) * ti->len.t.byoyomi_time;
184 // Will add 1 more byoyomi_time just below
186 ti->len.t.max_time += ti->len.t.byoyomi_time;
187 ti->len.t.recommended_time = ti->len.t.max_time;
189 /* Maximize the number of moves played uniformly in main time, while
190 * not playing faster in main time than in byoyomi. At this point,
191 * the main time remaining is ti->len.t.max_time and already includes
192 * the first (canadian) or N-1 byoyomi periods.
193 * main_speed = max_time / main_moves >= byoyomi_time
194 * => main_moves <= max_time / byoyomi_time */
195 double actual_byoyomi = ti->len.t.byoyomi_time - net_lag;
196 if (actual_byoyomi > 0) {
197 int main_moves = (int)(ti->len.t.max_time / actual_byoyomi);
198 if (moves_left > main_moves)
199 moves_left = main_moves; // will do the rest in byoyomi
200 if (moves_left <= 0) // possible if too much lag
201 moves_left = 1;
204 ti->period = TT_MOVE;
205 ti->len.t.recommended_time /= moves_left;
207 // To simplify the engine code, do not leave negative times:
208 if (ti->len.t.recommended_time < 0)
209 ti->len.t.recommended_time = 0;
210 if (ti->len.t.max_time < 0)
211 ti->len.t.max_time = 0;
212 assert(ti->len.t.recommended_time <= ti->len.t.max_time + 0.001);
214 /* Use a larger safety margin if we risk losing on time on this move: */
215 double safe_margin = RESERVED_BYOYOMI_PERCENT * ti->len.t.byoyomi_time/100;
216 if (safe_margin > MAX_NET_LAG && ti->len.t.recommended_time >= ti->len.t.max_time - net_lag) {
217 net_lag = safe_margin;
220 if (DEBUGL(1))
221 fprintf(stderr, "recommended_time %0.2f, max_time %0.2f, byoyomi %0.2f, lag %0.2f max %0.2f\n",
222 ti->len.t.recommended_time, ti->len.t.max_time, ti->len.t.byoyomi_time, net_lag,
223 net_lag);
226 /*** Setup desired/worst time limits based on recommended/max time. */
228 assert(ti->period == TT_MOVE);
230 double desired_time = ti->len.t.recommended_time;
231 double worst_time;
232 if (time_in_byoyomi(ti)) {
233 // make recommended == average(desired, worst)
234 worst_time = desired_time * MAX_BYOYOMI_TIME_EXTENSION;
235 desired_time *= (2 - MAX_BYOYOMI_TIME_EXTENSION);
237 } else {
238 int bsize = (board_size(b)-2)*(board_size(b)-2);
239 fuseki_end = fuseki_end * bsize / 100; // move nb at fuseki end
240 yose_start = yose_start * bsize / 100; // move nb at yose start
241 assert(fuseki_end < yose_start);
243 /* Before yose, spend some extra. */
244 if (b->moves < yose_start) {
245 int moves_to_yose = (yose_start - b->moves) / 2;
246 // ^- /2 because we only consider the moves we have to play ourselves
247 int left_at_yose_start = board_estimated_moves_left(b) - moves_to_yose;
248 if (left_at_yose_start < MIN_MOVES_LEFT)
249 left_at_yose_start = MIN_MOVES_LEFT;
250 double longest_time = ti->len.t.max_time / left_at_yose_start;
251 if (longest_time < desired_time) {
252 // Should rarely happen, but keep desired_time anyway
253 } else if (b->moves < fuseki_end) {
254 assert(fuseki_end > 0);
255 desired_time += ((longest_time - desired_time) * b->moves) / fuseki_end;
256 } else { assert(b->moves < yose_start);
257 desired_time = longest_time;
260 worst_time = desired_time * MAX_MAIN_TIME_EXTENSION;
262 if (worst_time > ti->len.t.max_time)
263 worst_time = ti->len.t.max_time;
264 if (desired_time > worst_time)
265 desired_time = worst_time;
267 stop->desired.time = ti->len.t.timer_start + desired_time - net_lag;
268 stop->worst.time = ti->len.t.timer_start + worst_time - net_lag;
269 // Both stop points may be in the past if too much lag.
271 if (DEBUGL(2))
272 fprintf(stderr, "desired time %.02f, worst %.02f\n", desired_time, worst_time);