Support the kgs-game_over command.
[pachi.git] / uct / dynkomi.c
blobbd0595ade0c252aa550895abfe8bdba6631b6e2c
1 #define DEBUG
2 #include <assert.h>
3 #include <math.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
8 #include "board.h"
9 #include "debug.h"
10 #include "tactics.h"
11 #include "uct/dynkomi.h"
12 #include "uct/internal.h"
13 #include "uct/tree.h"
16 static void
17 uct_dynkomi_generic_done(struct uct_dynkomi *d)
19 if (d->data) free(d->data);
20 free(d);
24 /* NONE dynkomi strategy - never fiddle with komi values. */
26 struct uct_dynkomi *
27 uct_dynkomi_init_none(struct uct *u, char *arg, struct board *b)
29 struct uct_dynkomi *d = calloc(1, sizeof(*d));
30 d->uct = u;
31 d->permove = NULL;
32 d->persim = NULL;
33 d->done = uct_dynkomi_generic_done;
34 d->data = NULL;
36 if (arg) {
37 fprintf(stderr, "uct: Dynkomi method none accepts no arguments\n");
38 exit(1);
41 return d;
45 /* LINEAR dynkomi strategy - Linearly Decreasing Handicap Compensation. */
46 /* At move 0, we impose extra komi of handicap_count*handicap_value, then
47 * we linearly decrease this extra komi throughout the game down to 0
48 * at @moves moves. */
50 struct dynkomi_linear {
51 int handicap_value;
52 int moves;
53 bool rootbased;
56 float
57 uct_dynkomi_linear_permove(struct uct_dynkomi *d, struct board *b, struct tree *tree)
59 struct dynkomi_linear *l = d->data;
60 if (b->moves >= l->moves)
61 return 0;
63 float base_komi = board_effective_handicap(b, l->handicap_value);
64 float extra_komi = base_komi * (l->moves - b->moves) / l->moves;
65 return extra_komi;
68 float
69 uct_dynkomi_linear_persim(struct uct_dynkomi *d, struct board *b, struct tree *tree, struct tree_node *node)
71 struct dynkomi_linear *l = d->data;
72 if (l->rootbased)
73 return tree->extra_komi;
74 /* We don't reuse computed value from tree->extra_komi,
75 * since we want to use value correct for this node depth.
76 * This also means the values will stay correct after
77 * node promotion. */
78 return uct_dynkomi_linear_permove(d, b, tree);
81 struct uct_dynkomi *
82 uct_dynkomi_init_linear(struct uct *u, char *arg, struct board *b)
84 struct uct_dynkomi *d = calloc(1, sizeof(*d));
85 d->uct = u;
86 d->permove = uct_dynkomi_linear_permove;
87 d->persim = uct_dynkomi_linear_persim;
88 d->done = uct_dynkomi_generic_done;
90 struct dynkomi_linear *l = calloc(1, sizeof(*l));
91 d->data = l;
93 if (board_size(b) - 2 >= 19)
94 l->moves = 200;
95 l->handicap_value = 7;
97 if (arg) {
98 char *optspec, *next = arg;
99 while (*next) {
100 optspec = next;
101 next += strcspn(next, ":");
102 if (*next) { *next++ = 0; } else { *next = 0; }
104 char *optname = optspec;
105 char *optval = strchr(optspec, '=');
106 if (optval) *optval++ = 0;
108 if (!strcasecmp(optname, "moves") && optval) {
109 /* Dynamic komi in handicap game; linearly
110 * decreases to basic settings until move
111 * #optval. */
112 l->moves = atoi(optval);
113 } else if (!strcasecmp(optname, "handicap_value") && optval) {
114 /* Point value of single handicap stone,
115 * for dynkomi computation. */
116 l->handicap_value = atoi(optval);
117 } else if (!strcasecmp(optname, "rootbased")) {
118 /* If set, the extra komi applied will be
119 * the same for all simulations within a move,
120 * instead of being same for all simulations
121 * within the tree node. */
122 l->rootbased = !optval || atoi(optval);
123 } else {
124 fprintf(stderr, "uct: Invalid dynkomi argument %s or missing value\n", optname);
125 exit(1);
130 return d;
134 /* ADAPTIVE dynkomi strategy - Adaptive Situational Compensation */
135 /* We adapt the komi based on current situation:
136 * (i) score-based: We maintain the average score outcome of our
137 * games and adjust the komi by a fractional step towards the expected
138 * score;
139 * (ii) value-based: While winrate is above given threshold, adjust
140 * the komi by a fixed step in the appropriate direction.
141 * These adjustments can be
142 * (a) Move-stepped, new extra komi value is always set only at the
143 * beginning of the tree search for next move;
144 * (b) Continuous, new extra komi value is periodically re-determined
145 * and adjusted throughout a single tree search. [TODO] */
147 struct dynkomi_adaptive {
148 /* Do not take measured average score into regard for
149 * first @lead_moves - the variance is just too much.
150 * (Instead, we consider the handicap-based komi provided
151 * by linear dynkomi.) */
152 int lead_moves;
153 /* Maximum komi to pretend the opponent to give. */
154 float max_losing_komi;
156 /* Value-based adaptation. */
157 bool value_based;
158 float zone_red, zone_green;
159 int score_step;
160 float komi_latch; // runtime, not configuration
162 float (*adapter)(struct dynkomi_adaptive *a, struct board *b);
163 float adapt_base; // [0,1)
164 /* Sigmoid adaptation rate parameter; see below for details. */
165 float adapt_phase; // [0,1]
166 float adapt_rate; // [1,infty)
167 bool adapt_aport; // alternative game portion determination
168 /* Linear adaptation rate parameter. */
169 int adapt_moves;
170 float adapt_dir; // [-1,1]
172 #define TRUSTWORTHY_KOMI_PLAYOUTS 200
174 float
175 adapter_sigmoid(struct dynkomi_adaptive *a, struct board *b)
177 /* Figure out how much to adjust the komi based on the game
178 * stage. The adaptation rate is 0 at the beginning,
179 * at game stage a->adapt_phase crosses though 0.5 and
180 * approaches 1 at the game end; the slope is controlled
181 * by a->adapt_rate. */
182 float game_portion;
183 if (!a->adapt_aport) {
184 int total_moves = b->moves + 2 * board_estimated_moves_left(b);
185 game_portion = (float) b->moves / total_moves;
186 } else {
187 int brsize = board_size(b) - 2;
188 game_portion = 1.0 - (float) b->flen / (brsize * brsize);
190 float l = game_portion - a->adapt_phase;
191 return 1.0 / (1.0 + exp(-a->adapt_rate * l));
194 float
195 adapter_linear(struct dynkomi_adaptive *a, struct board *b)
197 /* Figure out how much to adjust the komi based on the game
198 * stage. We just linearly increase/decrease the adaptation
199 * rate for first N moves. */
200 if (b->moves > a->adapt_moves)
201 return 0;
202 if (a->adapt_dir < 0)
203 return 1 - (- a->adapt_dir) * b->moves / a->adapt_moves;
204 else
205 return a->adapt_dir * b->moves / a->adapt_moves;
208 float
209 uct_dynkomi_adaptive_permove(struct uct_dynkomi *d, struct board *b, struct tree *tree)
211 struct dynkomi_adaptive *a = d->data;
212 if (DEBUGL(3))
213 fprintf(stderr, "m %d/%d ekomi %f permove %f/%d\n",
214 b->moves, a->lead_moves, tree->extra_komi,
215 tree->score.value, tree->score.playouts);
216 if (b->moves <= a->lead_moves)
217 return board_effective_handicap(b, 7 /* XXX */);
219 /* Get lower bound on komi value so that we don't underperform
220 * too much. XXX: We rely on the fact that we don't use dynkomi
221 * as white for now. */
222 float min_komi = - a->max_losing_komi;
224 /* Perhaps we are adaptive value-based, not score-based? */
225 if (a->value_based) {
226 /* In that case, we have three zones:
227 * red zone | yellow zone | green zone
228 * ~45% ~60%
229 * red zone: reduce komi
230 * yellow zone: do not touch komi
231 * green zone: enlage komi.
233 * Also, at some point komi will be tuned in such way
234 * that it will be in green zone but increasing it will
235 * be unfeasible. Thus, we have a _latch_ - we will
236 * remember the last komi that has put us into the
237 * red zone, and not use it or go over it. We use the
238 * latch only when giving extra komi, we always want
239 * to try to reduce extra komi we take.
241 * TODO: Make the latch expire after a while. */
242 if (tree->root->u.playouts < TRUSTWORTHY_KOMI_PLAYOUTS)
243 return tree->extra_komi;
244 float value = tree->root->u.value;
245 float extra_komi = tree->extra_komi;
246 if (value < a->zone_red) {
247 /* Red zone. Take extra komi. */
248 if (extra_komi > 0) a->komi_latch = extra_komi;
249 extra_komi -= a->score_step; // XXX: we depend on being black
250 return extra_komi > min_komi ? extra_komi : min_komi;
251 } else if (value < a->zone_green) {
252 /* Yellow zone, do nothing. */
253 return extra_komi;
254 } else {
255 /* Green zone. Give extra komi. */
256 extra_komi += a->score_step; // XXX: we depend on being black
257 return extra_komi < a->komi_latch ? extra_komi : a->komi_latch - 1;
261 if (tree->score.playouts < TRUSTWORTHY_KOMI_PLAYOUTS)
262 return tree->extra_komi;
264 struct move_stats score = tree->score;
265 /* Almost-reset tree->score to gather fresh stats. */
266 tree->score.playouts = 1;
268 /* Look at average score and push extra_komi in that direction. */
269 float p = a->adapter(a, b);
270 p = a->adapt_base + p * (1 - a->adapt_base);
271 if (p > 0.9) p = 0.9; // don't get too eager!
272 float extra_komi = tree->extra_komi + p * score.value;
273 if (DEBUGL(3))
274 fprintf(stderr, "mC %f + %f * %f = %f\n",
275 tree->extra_komi, p, score.value, extra_komi);
276 return extra_komi > min_komi ? extra_komi : min_komi;
279 float
280 uct_dynkomi_adaptive_persim(struct uct_dynkomi *d, struct board *b, struct tree *tree, struct tree_node *node)
282 return tree->extra_komi;
285 struct uct_dynkomi *
286 uct_dynkomi_init_adaptive(struct uct *u, char *arg, struct board *b)
288 struct uct_dynkomi *d = calloc(1, sizeof(*d));
289 d->uct = u;
290 d->permove = uct_dynkomi_adaptive_permove;
291 d->persim = uct_dynkomi_adaptive_persim;
292 d->done = uct_dynkomi_generic_done;
294 struct dynkomi_adaptive *a = calloc(1, sizeof(*a));
295 d->data = a;
297 if (board_size(b) - 2 >= 19)
298 a->lead_moves = 20;
299 else
300 a->lead_moves = 4; // XXX
301 a->max_losing_komi = 10;
302 a->adapter = adapter_sigmoid;
303 a->adapt_rate = 20;
304 a->adapt_phase = 0.5;
305 a->adapt_moves = 200;
306 a->adapt_dir = -0.5;
308 a->zone_red = 0.45;
309 a->zone_green = 0.6;
310 a->score_step = 2;
311 a->komi_latch = 1000;
313 if (arg) {
314 char *optspec, *next = arg;
315 while (*next) {
316 optspec = next;
317 next += strcspn(next, ":");
318 if (*next) { *next++ = 0; } else { *next = 0; }
320 char *optname = optspec;
321 char *optval = strchr(optspec, '=');
322 if (optval) *optval++ = 0;
324 if (!strcasecmp(optname, "lead_moves") && optval) {
325 /* Do not adjust komi adaptively for first
326 * N moves. */
327 a->lead_moves = atoi(optval);
328 } else if (!strcasecmp(optname, "max_losing_komi") && optval) {
329 a->max_losing_komi = atof(optval);
331 } else if (!strcasecmp(optname, "value_based")) {
332 a->value_based = !optval || atoi(optval);
333 } else if (!strcasecmp(optname, "zone_red") && optval) {
334 a->zone_red = atof(optval);
335 } else if (!strcasecmp(optname, "zone_green") && optval) {
336 a->zone_green = atof(optval);
337 } else if (!strcasecmp(optname, "score_step") && optval) {
338 a->score_step = atoi(optval);
340 } else if (!strcasecmp(optname, "adapter") && optval) {
341 /* Adaptatation method. */
342 if (!strcasecmp(optval, "sigmoid")) {
343 a->adapter = adapter_sigmoid;
344 } else if (!strcasecmp(optval, "linear")) {
345 a->adapter = adapter_linear;
346 } else {
347 fprintf(stderr, "UCT: Invalid adapter %s\n", optval);
348 exit(1);
350 } else if (!strcasecmp(optname, "adapt_base") && optval) {
351 /* Adaptation base rate; see above. */
352 a->adapt_base = atof(optval);
353 } else if (!strcasecmp(optname, "adapt_rate") && optval) {
354 /* Adaptation slope; see above. */
355 a->adapt_rate = atof(optval);
356 } else if (!strcasecmp(optname, "adapt_phase") && optval) {
357 /* Adaptation phase shift; see above. */
358 a->adapt_phase = atof(optval);
359 } else if (!strcasecmp(optname, "adapt_moves") && optval) {
360 /* Adaptation move amount; see above. */
361 a->adapt_moves = atoi(optval);
362 } else if (!strcasecmp(optname, "adapt_aport")) {
363 a->adapt_aport = !optval || atoi(optval);
364 } else if (!strcasecmp(optname, "adapt_dir") && optval) {
365 /* Adaptation direction vector; see above. */
366 a->adapt_dir = atof(optval);
367 } else {
368 fprintf(stderr, "uct: Invalid dynkomi argument %s or missing value\n", optname);
369 exit(1);
374 return d;