11 #include "uct/dynkomi.h"
12 #include "uct/internal.h"
17 generic_done(struct uct_dynkomi
*d
)
19 if (d
->data
) free(d
->data
);
24 /* NONE dynkomi strategy - never fiddle with komi values. */
27 uct_dynkomi_init_none(struct uct
*u
, char *arg
, struct board
*b
)
29 struct uct_dynkomi
*d
= calloc2(1, sizeof(*d
));
33 d
->done
= generic_done
;
37 fprintf(stderr
, "uct: Dynkomi method none accepts no arguments\n");
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
50 struct dynkomi_linear
{
57 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
)
63 float base_komi
= board_effective_handicap(b
, l
->handicap_value
);
64 float extra_komi
= base_komi
* (l
->moves
- b
->moves
) / l
->moves
;
69 linear_persim(struct uct_dynkomi
*d
, struct board
*b
, struct tree
*tree
, struct tree_node
*node
)
71 struct dynkomi_linear
*l
= d
->data
;
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
78 return linear_permove(d
, b
, tree
);
82 uct_dynkomi_init_linear(struct uct
*u
, char *arg
, struct board
*b
)
84 struct uct_dynkomi
*d
= calloc2(1, sizeof(*d
));
86 d
->permove
= linear_permove
;
87 d
->persim
= linear_persim
;
88 d
->done
= generic_done
;
90 struct dynkomi_linear
*l
= calloc2(1, sizeof(*l
));
93 if (board_size(b
) - 2 >= 19)
95 l
->handicap_value
= 7;
98 char *optspec
, *next
= arg
;
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
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
);
124 fprintf(stderr
, "uct: Invalid dynkomi argument %s or missing value\n", optname
);
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
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. */
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.) */
153 /* Maximum komi to pretend the opponent to give. */
154 float max_losing_komi
;
155 float (*indicator
)(struct uct_dynkomi
*d
, struct board
*b
, struct tree
*tree
, enum stone color
);
157 /* Value-based adaptation. */
158 float zone_red
, zone_green
;
160 float score_step_byavg
; // use portion of average score as increment
161 bool use_komi_ratchet
;
162 int komi_ratchet_maxage
;
163 // runtime, not configuration:
164 int komi_ratchet_age
;
167 /* Score-based adaptation. */
168 float (*adapter
)(struct uct_dynkomi
*d
, struct board
*b
);
169 float adapt_base
; // [0,1)
170 /* Sigmoid adaptation rate parameter; see below for details. */
171 float adapt_phase
; // [0,1]
172 float adapt_rate
; // [1,infty)
173 bool adapt_aport
; // alternative game portion determination
174 /* Linear adaptation rate parameter. */
176 float adapt_dir
; // [-1,1]
178 #define TRUSTWORTHY_KOMI_PLAYOUTS 200
181 adapter_sigmoid(struct uct_dynkomi
*d
, struct board
*b
)
183 struct dynkomi_adaptive
*a
= d
->data
;
184 /* Figure out how much to adjust the komi based on the game
185 * stage. The adaptation rate is 0 at the beginning,
186 * at game stage a->adapt_phase crosses though 0.5 and
187 * approaches 1 at the game end; the slope is controlled
188 * by a->adapt_rate. */
190 if (!a
->adapt_aport
) {
191 int total_moves
= b
->moves
+ 2 * board_estimated_moves_left(b
);
192 game_portion
= (float) b
->moves
/ total_moves
;
194 int brsize
= board_size(b
) - 2;
195 game_portion
= 1.0 - (float) b
->flen
/ (brsize
* brsize
);
197 float l
= game_portion
- a
->adapt_phase
;
198 return 1.0 / (1.0 + exp(-a
->adapt_rate
* l
));
202 adapter_linear(struct uct_dynkomi
*d
, struct board
*b
)
204 struct dynkomi_adaptive
*a
= d
->data
;
205 /* Figure out how much to adjust the komi based on the game
206 * stage. We just linearly increase/decrease the adaptation
207 * rate for first N moves. */
208 if (b
->moves
> a
->adapt_moves
)
210 if (a
->adapt_dir
< 0)
211 return 1 - (- a
->adapt_dir
) * b
->moves
/ a
->adapt_moves
;
213 return a
->adapt_dir
* b
->moves
/ a
->adapt_moves
;
217 komi_by_score(struct uct_dynkomi
*d
, struct board
*b
, struct tree
*tree
, enum stone color
)
219 struct dynkomi_adaptive
*a
= d
->data
;
220 if (d
->score
.playouts
< TRUSTWORTHY_KOMI_PLAYOUTS
)
221 return tree
->extra_komi
;
223 struct move_stats score
= d
->score
;
224 /* Almost-reset tree->score to gather fresh stats. */
225 d
->score
.playouts
= 1;
227 /* Look at average score and push extra_komi in that direction. */
228 float p
= a
->adapter(d
, b
);
229 p
= a
->adapt_base
+ p
* (1 - a
->adapt_base
);
230 if (p
> 0.9) p
= 0.9; // don't get too eager!
231 float extra_komi
= tree
->extra_komi
+ p
* score
.value
;
233 fprintf(stderr
, "mC += %f * %f\n", p
, score
.value
);
238 komi_by_value(struct uct_dynkomi
*d
, struct board
*b
, struct tree
*tree
, enum stone color
)
240 struct dynkomi_adaptive
*a
= d
->data
;
241 if (d
->value
.playouts
< TRUSTWORTHY_KOMI_PLAYOUTS
)
242 return tree
->extra_komi
;
244 struct move_stats value
= d
->value
;
245 /* Almost-reset tree->value to gather fresh stats. */
246 d
->value
.playouts
= 1;
247 /* Correct color POV. */
248 if (color
== S_WHITE
)
249 value
.value
= 1 - value
.value
;
251 /* We have three "value zones":
252 * red zone | yellow zone | green zone
254 * red zone: reduce komi
255 * yellow zone: do not touch komi
256 * green zone: enlage komi.
258 * Also, at some point komi will be tuned in such way
259 * that it will be in green zone but increasing it will
260 * be unfeasible. Thus, we have a _ratchet_ - we will
261 * remember the last komi that has put us into the
262 * red zone, and not use it or go over it. We use the
263 * ratchet only when giving extra komi, we always want
264 * to try to reduce extra komi we take.
266 * TODO: Make the ratchet expire after a while. */
267 /* We use komi_by_color() first to normalize komi
268 * additions/subtractions, then apply it again on
269 * return value to restore original komi parity. */
270 float extra_komi
= komi_by_color(tree
->extra_komi
, color
);
271 int score_step_red
= -a
->score_step
;
272 int score_step_green
= a
->score_step
;
274 if (a
->score_step_byavg
!= 0) {
275 struct move_stats score
= d
->score
;
276 /* Almost-reset tree->score to gather fresh stats. */
277 d
->score
.playouts
= 1;
278 /* Correct color POV. */
279 if (color
== S_WHITE
)
280 score
.value
= - score
.value
;
282 score_step_green
= round(score
.value
* a
->score_step_byavg
);
284 score_step_red
= round(-score
.value
* a
->score_step_byavg
);
287 if (value
.value
< a
->zone_red
) {
288 /* Red zone. Take extra komi. */
290 fprintf(stderr
, "[red] %f, step %d | komi ratchet %f age %d/%d -> %f\n",
291 value
.value
, score_step_red
, a
->komi_ratchet
, a
->komi_ratchet_age
, a
->komi_ratchet_maxage
, extra_komi
);
292 if (extra_komi
> 0) {
293 assert(extra_komi
< a
->komi_ratchet
);
294 a
->komi_ratchet
= extra_komi
;
295 a
->komi_ratchet_age
= 0;
297 extra_komi
+= score_step_red
;
298 return komi_by_color(extra_komi
, color
);
300 } else if (value
.value
< a
->zone_green
) {
301 /* Yellow zone, do nothing. */
302 return komi_by_color(extra_komi
, color
);
305 /* Green zone. Give extra komi. */
306 extra_komi
+= score_step_green
;
308 fprintf(stderr
, "[green] %f, step %d | komi ratchet %f age %d/%d\n",
309 value
.value
, score_step_green
, a
->komi_ratchet
, a
->komi_ratchet_age
, a
->komi_ratchet_maxage
);
310 if (a
->komi_ratchet_maxage
> 0 && a
->komi_ratchet_age
> a
->komi_ratchet_maxage
) {
311 a
->komi_ratchet
= 1000;
312 a
->komi_ratchet_age
= 0;
314 if (a
->use_komi_ratchet
&& extra_komi
>= a
->komi_ratchet
) {
315 extra_komi
= a
->komi_ratchet
- 1;
316 a
->komi_ratchet_age
++;
318 return komi_by_color(extra_komi
, color
);
323 bounded_komi(enum stone color
, float komi
, float max_losing_komi
)
325 /* Get lower bound on komi we take so that we don't underperform
327 float min_komi
= komi_by_color(- max_losing_komi
, color
);
329 if (komi_by_color(komi
- min_komi
, color
) > 0)
336 adaptive_permove(struct uct_dynkomi
*d
, struct board
*b
, struct tree
*tree
)
338 struct dynkomi_adaptive
*a
= d
->data
;
339 enum stone color
= stone_other(tree
->root_color
);
341 fprintf(stderr
, "m %d/%d ekomi %f permove %f/%d\n",
342 b
->moves
, a
->lead_moves
, tree
->extra_komi
,
343 d
->score
.value
, d
->score
.playouts
);
345 if (b
->moves
<= a
->lead_moves
)
346 return bounded_komi(color
, board_effective_handicap(b
, 7 /* XXX */), a
->max_losing_komi
);
348 float komi
= a
->indicator(d
, b
, tree
, color
);
350 fprintf(stderr
, "dynkomi: %f -> %f\n", tree
->extra_komi
, komi
);
351 return bounded_komi(color
, komi
, a
->max_losing_komi
);
355 adaptive_persim(struct uct_dynkomi
*d
, struct board
*b
, struct tree
*tree
, struct tree_node
*node
)
357 return tree
->extra_komi
;
361 uct_dynkomi_init_adaptive(struct uct
*u
, char *arg
, struct board
*b
)
363 struct uct_dynkomi
*d
= calloc2(1, sizeof(*d
));
365 d
->permove
= adaptive_permove
;
366 d
->persim
= adaptive_persim
;
367 d
->done
= generic_done
;
369 struct dynkomi_adaptive
*a
= calloc2(1, sizeof(*a
));
372 if (board_size(b
) - 2 >= 19)
375 a
->lead_moves
= 4; // XXX
376 a
->max_losing_komi
= 0;
377 a
->indicator
= komi_by_score
;
379 a
->adapter
= adapter_sigmoid
;
381 a
->adapt_phase
= 0.65;
382 a
->adapt_moves
= 200;
386 a
->zone_green
= 0.55;
388 a
->use_komi_ratchet
= true;
389 a
->komi_ratchet_maxage
= 0;
390 a
->komi_ratchet
= 1000;
393 char *optspec
, *next
= arg
;
396 next
+= strcspn(next
, ":");
397 if (*next
) { *next
++ = 0; } else { *next
= 0; }
399 char *optname
= optspec
;
400 char *optval
= strchr(optspec
, '=');
401 if (optval
) *optval
++ = 0;
403 if (!strcasecmp(optname
, "lead_moves") && optval
) {
404 /* Do not adjust komi adaptively for first
406 a
->lead_moves
= atoi(optval
);
407 } else if (!strcasecmp(optname
, "max_losing_komi") && optval
) {
408 a
->max_losing_komi
= atof(optval
);
409 } else if (!strcasecmp(optname
, "indicator")) {
410 /* Adaptatation indicator - how to decide
411 * the adaptation rate and direction. */
412 if (!strcasecmp(optval
, "value")) {
413 /* Winrate w/ komi so far. */
414 a
->indicator
= komi_by_value
;
415 } else if (!strcasecmp(optval
, "score")) {
416 /* Expected score w/ current komi. */
417 a
->indicator
= komi_by_score
;
419 fprintf(stderr
, "UCT: Invalid indicator %s\n", optval
);
423 /* value indicator settings */
424 } else if (!strcasecmp(optname
, "zone_red") && optval
) {
425 a
->zone_red
= atof(optval
);
426 } else if (!strcasecmp(optname
, "zone_green") && optval
) {
427 a
->zone_green
= atof(optval
);
428 } else if (!strcasecmp(optname
, "score_step") && optval
) {
429 a
->score_step
= atoi(optval
);
430 } else if (!strcasecmp(optname
, "score_step_byavg") && optval
) {
431 a
->score_step_byavg
= atof(optval
);
432 } else if (!strcasecmp(optname
, "use_komi_ratchet")) {
433 a
->use_komi_ratchet
= !optval
|| atoi(optval
);
434 } else if (!strcasecmp(optname
, "komi_ratchet_age") && optval
) {
435 a
->komi_ratchet_maxage
= atoi(optval
);
437 /* score indicator settings */
438 } else if (!strcasecmp(optname
, "adapter") && optval
) {
439 /* Adaptatation method. */
440 if (!strcasecmp(optval
, "sigmoid")) {
441 a
->adapter
= adapter_sigmoid
;
442 } else if (!strcasecmp(optval
, "linear")) {
443 a
->adapter
= adapter_linear
;
445 fprintf(stderr
, "UCT: Invalid adapter %s\n", optval
);
448 } else if (!strcasecmp(optname
, "adapt_base") && optval
) {
449 /* Adaptation base rate; see above. */
450 a
->adapt_base
= atof(optval
);
451 } else if (!strcasecmp(optname
, "adapt_rate") && optval
) {
452 /* Adaptation slope; see above. */
453 a
->adapt_rate
= atof(optval
);
454 } else if (!strcasecmp(optname
, "adapt_phase") && optval
) {
455 /* Adaptation phase shift; see above. */
456 a
->adapt_phase
= atof(optval
);
457 } else if (!strcasecmp(optname
, "adapt_moves") && optval
) {
458 /* Adaptation move amount; see above. */
459 a
->adapt_moves
= atoi(optval
);
460 } else if (!strcasecmp(optname
, "adapt_aport")) {
461 a
->adapt_aport
= !optval
|| atoi(optval
);
462 } else if (!strcasecmp(optname
, "adapt_dir") && optval
) {
463 /* Adaptation direction vector; see above. */
464 a
->adapt_dir
= atof(optval
);
467 fprintf(stderr
, "uct: Invalid dynkomi argument %s or missing value\n", optname
);