time_info.{recommended,max}_time: Convert to local variables of time_stop_conditions()
[pachi/t.git] / timeinfo.c
blob92060a1a3fc438a0ec1761835320dcacd211a023
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 /* Minimum net lag (seconds) to be reserved in the time for move. */
147 double net_lag = MAX_NET_LAG;
148 /* Absolute maximum time possible to spend on the move. */
149 double max_time;
150 /* Ideal/reasonable time to spend on the move. */
151 double recommended_time;
153 /* Special-case limit by number of simulations. */
154 if (ti->dim == TD_GAMES) {
155 if (ti->period == TT_TOTAL) {
156 ti->period = TT_MOVE;
157 ti->len.games /= board_estimated_moves_left(b);
160 stop->desired.playouts = ti->len.games;
161 /* We force worst == desired, so note that we will NOT loop
162 * until best == winner. */
163 stop->worst.playouts = ti->len.games;
164 return;
167 assert(ti->dim == TD_WALLTIME);
170 /*** Transform @ti to TT_MOVE and set up recommended/max time and
171 * net lag information. */
173 /* Make sure timer_start is set up, adjust net_lag. */
174 double now = time_now();
175 if (!ti->len.t.timer_start) {
176 ti->len.t.timer_start = now; // we're playing the first game move
177 } else {
178 net_lag += now - ti->len.t.timer_start;
179 // TODO: keep statistics to get good estimate of lag not just current move
182 /* Set up initial recommendations. */
183 if (ti->len.t.main_time) {
184 max_time = recommended_time = ti->len.t.main_time;
185 } else {
186 max_time = ti->len.t.byoyomi_time;
187 assert(ti->len.t.byoyomi_stones > 0);
188 recommended_time = ti->len.t.byoyomi_time / ti->len.t.byoyomi_stones;
191 if (ti->period == TT_TOTAL) {
192 int moves_left = board_estimated_moves_left(b);
193 if (ti->len.t.byoyomi_time > 0) {
194 assert(ti->len.t.byoyomi_stones > 0);
195 /* Time for one move in byoyomi. */
196 double move_time = ti->len.t.byoyomi_time / ti->len.t.byoyomi_stones;
198 /* For Japanese byoyomi with N>1 periods, we use N-1 periods as main time,
199 * keeping the last one as insurance against unexpected net lag. */
200 if (ti->len.t.byoyomi_periods > 2) {
201 max_time += (ti->len.t.byoyomi_periods - 2) * move_time;
202 // Will add 1 more byoyomi_time just below
204 max_time += move_time;
205 recommended_time = max_time;
207 /* Maximize the number of moves played uniformly in main time, while
208 * not playing faster in main time than in byoyomi. At this point,
209 * the main time remaining is ti->len.t.max_time and already includes
210 * the first (canadian) or N-1 byoyomi periods.
211 * main_speed = max_time / main_moves >= move_time
212 * => main_moves <= max_time / move_time */
213 double actual_byoyomi = move_time - net_lag;
214 if (actual_byoyomi > 0) {
215 int main_moves = (int)(max_time / actual_byoyomi);
216 if (moves_left > main_moves)
217 moves_left = main_moves; // will do the rest in byoyomi
218 if (moves_left <= 0) // possible if too much lag
219 moves_left = 1;
222 ti->period = TT_MOVE;
223 recommended_time /= moves_left;
225 // To simplify the engine code, do not leave negative times:
226 if (recommended_time < 0)
227 recommended_time = 0;
228 if (max_time < 0)
229 max_time = 0;
230 assert(recommended_time <= max_time + 0.001);
232 /* Use a larger safety margin if we risk losing on time on this move: */
233 double safe_margin = RESERVED_BYOYOMI_PERCENT * ti->len.t.byoyomi_time/100;
234 if (safe_margin > 0) {
235 assert(ti->len.t.byoyomi_stones > 0);
236 safe_margin /= ti->len.t.byoyomi_stones;
238 if (safe_margin > MAX_NET_LAG && recommended_time >= max_time - net_lag) {
239 net_lag = safe_margin;
242 if (DEBUGL(1))
243 fprintf(stderr, "recommended_time %0.2f, max_time %0.2f, byoyomi %0.2f/%d, lag %0.2f\n",
244 recommended_time, max_time,
245 ti->len.t.byoyomi_time, ti->len.t.byoyomi_stones,
246 net_lag);
249 /*** Setup desired/worst time limits based on recommended/max time. */
251 assert(ti->period == TT_MOVE);
253 double desired_time = recommended_time;
254 double worst_time;
255 if (time_in_byoyomi(ti)) {
256 // make recommended == average(desired, worst)
257 worst_time = desired_time * MAX_BYOYOMI_TIME_EXTENSION;
258 desired_time *= (2 - MAX_BYOYOMI_TIME_EXTENSION);
260 } else {
261 int bsize = (board_size(b)-2)*(board_size(b)-2);
262 fuseki_end = fuseki_end * bsize / 100; // move nb at fuseki end
263 yose_start = yose_start * bsize / 100; // move nb at yose start
264 assert(fuseki_end < yose_start);
266 /* Before yose, spend some extra. */
267 if (b->moves < yose_start) {
268 int moves_to_yose = (yose_start - b->moves) / 2;
269 // ^- /2 because we only consider the moves we have to play ourselves
270 int left_at_yose_start = board_estimated_moves_left(b) - moves_to_yose;
271 if (left_at_yose_start < MIN_MOVES_LEFT)
272 left_at_yose_start = MIN_MOVES_LEFT;
273 double longest_time = max_time / left_at_yose_start;
274 if (longest_time < desired_time) {
275 // Should rarely happen, but keep desired_time anyway
276 } else if (b->moves < fuseki_end) {
277 assert(fuseki_end > 0);
278 desired_time += ((longest_time - desired_time) * b->moves) / fuseki_end;
279 } else { assert(b->moves < yose_start);
280 desired_time = longest_time;
283 worst_time = desired_time * MAX_MAIN_TIME_EXTENSION;
285 if (worst_time > max_time)
286 worst_time = max_time;
287 if (desired_time > worst_time)
288 desired_time = worst_time;
290 stop->desired.time = ti->len.t.timer_start + desired_time - net_lag;
291 stop->worst.time = ti->len.t.timer_start + worst_time - net_lag;
292 // Both stop points may be in the past if too much lag.
294 if (DEBUGL(2))
295 fprintf(stderr, "desired time %.02f, worst %.02f\n", desired_time, worst_time);