14 #include "playout/moggy.h"
15 #include "playout/light.h"
17 #include "uct/internal.h"
21 struct uct_policy
*policy_ucb1_init(struct uct
*u
, char *arg
);
22 struct uct_policy
*policy_ucb1tuned_init(struct uct
*u
, char *arg
);
23 struct uct_policy
*policy_ucb1amaf_init(struct uct
*u
, char *arg
);
26 #define MC_GAMES 80000
27 #define MC_GAMELEN 400
31 progress_status(struct uct
*u
, struct tree
*t
, enum stone color
, int playouts
)
37 struct tree_node
*best
= u
->policy
->choose(u
->policy
, t
->root
, t
->board
, color
);
39 fprintf(stderr
, "... No moves left\n");
42 fprintf(stderr
, "[%d] ", playouts
);
43 fprintf(stderr
, "best %f ", best
->u
.value
);
46 fprintf(stderr
, "deepest % 2d ", t
->max_depth
- t
->root
->depth
);
49 fprintf(stderr
, "| seq ");
50 for (int depth
= 0; depth
< 6; depth
++) {
51 if (best
&& best
->u
.playouts
>= 25) {
52 fprintf(stderr
, "%3s ", coord2sstr(best
->coord
, t
->board
));
53 best
= u
->policy
->choose(u
->policy
, best
, t
->board
, color
);
60 fprintf(stderr
, "| can ");
62 struct tree_node
*can
[cans
];
63 memset(can
, 0, sizeof(can
));
64 best
= t
->root
->children
;
67 while ((!can
[c
] || best
->u
.playouts
> can
[c
]->u
.playouts
) && ++c
< cans
);
68 for (int d
= 0; d
< c
; d
++) can
[d
] = can
[d
+ 1];
69 if (c
> 0) can
[c
- 1] = best
;
74 fprintf(stderr
, "%3s(%.3f) ", coord2sstr(can
[cans
]->coord
, t
->board
), can
[cans
]->u
.value
);
80 fprintf(stderr
, "\n");
85 uct_leaf_node(struct uct
*u
, struct board
*b
, enum stone player_color
,
86 struct playout_amafmap
*amaf
,
87 struct tree
*t
, struct tree_node
*n
, enum stone node_color
,
90 enum stone next_color
= stone_other(node_color
);
91 if (n
->u
.playouts
>= u
->expand_p
) {
92 // fprintf(stderr, "expanding %s (%p ^-%p)\n", coord2sstr(n->coord, b), n, n->parent);
93 tree_expand_node(t
, n
, b
, next_color
, u
->radar_d
, u
->policy
,
94 (next_color
== player_color
? 1 : -1));
97 fprintf(stderr
, "%s*-- UCT playout #%d start [%s] %f\n",
98 spaces
, n
->u
.playouts
, coord2sstr(n
->coord
, t
->board
), n
->u
.value
);
100 int result
= play_random_game(b
, next_color
, u
->gamelen
, u
->playout_amaf
? amaf
: NULL
, u
->playout
);
101 if (player_color
!= next_color
&& result
>= 0)
104 fprintf(stderr
, "%s -- [%d..%d] %s random playout result %d\n",
105 spaces
, player_color
, next_color
, coord2sstr(n
->coord
, t
->board
), result
);
111 uct_playout(struct uct
*u
, struct board
*b
, enum stone player_color
, struct tree
*t
)
116 struct playout_amafmap
*amaf
= NULL
;
117 if (u
->policy
->wants_amaf
) {
118 amaf
= calloc(1, sizeof(*amaf
));
119 amaf
->map
= calloc(board_size2(&b2
) + 1, sizeof(*amaf
->map
));
120 amaf
->map
++; // -1 is pass
123 /* Walk the tree until we find a leaf, then expand it and do
124 * a random playout. */
125 struct tree_node
*n
= t
->root
;
126 enum stone node_color
= stone_other(player_color
);
128 int pass_limit
= (board_size(&b2
) - 2) * (board_size(&b2
) - 2) / 2;
129 int passes
= is_pass(b
->last_move
.coord
);
132 static char spaces
[] = "\0 ";
135 fprintf(stderr
, "--- UCT walk with color %d\n", player_color
);
137 while (!tree_leaf_node(n
) && passes
< 2) {
138 spaces
[depth
++] = ' '; spaces
[depth
] = 0;
140 /* Parity is chosen already according to the child color, since
141 * it is applied to children. */
142 node_color
= stone_other(node_color
);
143 n
= u
->policy
->descend(u
->policy
, t
, n
, (node_color
== player_color
? 1 : -1), pass_limit
);
145 assert(n
== t
->root
|| n
->parent
);
147 fprintf(stderr
, "%s+-- UCT sent us to [%s:%d] %f\n",
148 spaces
, coord2sstr(n
->coord
, t
->board
), n
->coord
, n
->u
.value
);
150 if (amaf
&& n
->coord
>= -1 && !is_pass(n
->coord
)) {
151 if (amaf
->map
[n
->coord
] == S_NONE
) {
152 amaf
->map
[n
->coord
] = node_color
;
154 amaf_op(amaf
->map
[n
->coord
], +);
158 struct move m
= { n
->coord
, node_color
};
159 int res
= board_play(&b2
, &m
);
161 if (res
< 0 || (!is_pass(m
.coord
) && !group_at(&b2
, m
.coord
)) /* suicide */
162 || b2
.superko_violation
) {
164 for (struct tree_node
*ni
= n
; ni
; ni
= ni
->parent
)
165 fprintf(stderr
, "%s ", coord2sstr(ni
->coord
, t
->board
));
166 fprintf(stderr
, "deleting invalid %s node %d,%d res %d group %d spk %d\n",
167 stone2str(node_color
), coord_x(n
->coord
,b
), coord_y(n
->coord
,b
),
168 res
, group_at(&b2
, m
.coord
), b2
.superko_violation
);
170 tree_delete_node(t
, n
);
175 if (is_pass(n
->coord
))
181 if (tree_leaf_node(n
)) {
182 result
= uct_leaf_node(u
, &b2
, player_color
, amaf
, t
, n
, node_color
, spaces
);
184 } else { assert(passes
>= 2);
186 float score
= board_official_score(&b2
);
187 result
= (player_color
== S_BLACK
) ? score
< 0 : score
> 0;
190 fprintf(stderr
, "[%d..%d] %s p-p scoring playout result %d (W %f)\n",
191 player_color
, node_color
, coord2sstr(n
->coord
, t
->board
), result
, score
);
193 board_print(&b2
, stderr
);
196 assert(n
== t
->root
|| n
->parent
);
198 u
->policy
->update(u
->policy
, t
, n
, node_color
, player_color
, amaf
, result
);
205 board_done_noalloc(&b2
);
210 prepare_move(struct engine
*e
, struct board
*b
, enum stone color
, coord_t promote
)
212 struct uct
*u
= e
->data
;
214 if (!b
->moves
&& u
->t
) {
215 /* Stale state from last game */
221 u
->t
= tree_init(b
, color
);
223 fast_srandom(u
->force_seed
);
225 fprintf(stderr
, "Fresh board with random seed %lu\n", fast_getseed());
226 //board_print(b, stderr);
227 tree_load(u
->t
, b
, color
);
230 /* XXX: We hope that the opponent didn't suddenly play
231 * several moves in the row. */
232 if (!is_resign(promote
) && !tree_promote_at(u
->t
, b
, promote
)) {
234 fprintf(stderr
, "<cannot find node to promote>\n");
237 u
->t
= tree_init(b
, color
);
241 /* Set in main thread in case the playouts should stop. */
242 static volatile sig_atomic_t halt
= 0;
245 uct_playouts(struct uct
*u
, struct board
*b
, enum stone color
, struct tree
*t
)
247 int i
, games
= u
->games
;
248 if (t
->root
->children
)
249 games
-= t
->root
->u
.playouts
/ 1.5;
250 /* else this is highly read-out but dead-end branch of opening book;
251 * we need to start from scratch; XXX: Maybe actually base the readout
252 * count based on number of playouts of best node? */
253 for (i
= 0; i
< games
; i
++) {
254 int result
= uct_playout(u
, b
, color
, t
);
256 /* Tree descent has hit invalid move. */
260 if (i
> 0 && !(i
% 10000)) {
261 progress_status(u
, t
, color
, i
);
264 if (i
> 0 && !(i
% 500)) {
265 struct tree_node
*best
= u
->policy
->choose(u
->policy
, t
->root
, b
, color
);
266 if (best
&& best
->u
.playouts
>= 5000 && best
->u
.value
>= u
->loss_threshold
)
272 fprintf(stderr
, "<halting early, %d games skipped>\n", games
- i
);
277 progress_status(u
, t
, color
, i
);
279 tree_dump(t
, u
->dumpthres
);
283 static pthread_mutex_t finish_mutex
= PTHREAD_MUTEX_INITIALIZER
;
284 static pthread_cond_t finish_cond
= PTHREAD_COND_INITIALIZER
;
285 static volatile int finish_thread
;
286 static pthread_mutex_t finish_serializer
= PTHREAD_MUTEX_INITIALIZER
;
299 spawn_helper(void *ctx_
)
301 struct spawn_ctx
*ctx
= ctx_
;
303 fast_srandom(ctx
->seed
);
305 ctx
->games
= uct_playouts(ctx
->u
, ctx
->b
, ctx
->color
, ctx
->t
);
307 pthread_mutex_lock(&finish_serializer
);
308 pthread_mutex_lock(&finish_mutex
);
309 finish_thread
= ctx
->tid
;
310 pthread_cond_signal(&finish_cond
);
311 pthread_mutex_unlock(&finish_mutex
);
316 uct_notify_play(struct engine
*e
, struct board
*b
, struct move
*m
)
318 prepare_move(e
, b
, stone_other(m
->color
), m
->coord
);
322 uct_genmove(struct engine
*e
, struct board
*b
, enum stone color
)
324 struct uct
*u
= e
->data
;
327 prepare_move(e
, b
, color
, resign
);
329 int played_games
= 0;
331 played_games
= uct_playouts(u
, b
, color
, u
->t
);
333 pthread_t threads
[u
->threads
];
336 pthread_mutex_lock(&finish_mutex
);
337 /* Spawn threads... */
338 for (int ti
= 0; ti
< u
->threads
; ti
++) {
339 struct spawn_ctx
*ctx
= malloc(sizeof(*ctx
));
340 ctx
->u
= u
; ctx
->b
= b
; ctx
->color
= color
;
341 ctx
->t
= tree_copy(u
->t
); ctx
->tid
= ti
;
342 ctx
->seed
= fast_random(65536) + ti
;
343 pthread_create(&threads
[ti
], NULL
, spawn_helper
, ctx
);
345 fprintf(stderr
, "Spawned thread %d\n", ti
);
347 /* ...and collect them back: */
348 while (joined
< u
->threads
) {
349 /* Wait for some thread to finish... */
350 pthread_cond_wait(&finish_cond
, &finish_mutex
);
351 /* ...and gather its remnants. */
352 struct spawn_ctx
*ctx
;
353 pthread_join(threads
[finish_thread
], (void **) &ctx
);
354 played_games
+= ctx
->games
;
356 tree_merge(u
->t
, ctx
->t
);
360 fprintf(stderr
, "Joined thread %d\n", finish_thread
);
361 /* Do not get stalled by slow threads. */
362 if (joined
>= u
->threads
/ 2)
364 pthread_mutex_unlock(&finish_serializer
);
366 pthread_mutex_unlock(&finish_mutex
);
370 tree_dump(u
->t
, u
->dumpthres
);
372 struct tree_node
*best
= u
->policy
->choose(u
->policy
, u
->t
->root
, b
, color
);
374 tree_done(u
->t
); u
->t
= NULL
;
375 return coord_copy(pass
);
378 progress_status(u
, u
->t
, color
, played_games
);
380 fprintf(stderr
, "*** WINNER is %s (%d,%d) with score %1.4f (%d/%d:%d games)\n", coord2sstr(best
->coord
, b
), coord_x(best
->coord
, b
), coord_y(best
->coord
, b
), best
->u
.value
, best
->u
.playouts
, u
->t
->root
->u
.playouts
, played_games
);
381 if (best
->u
.value
< u
->resign_ratio
&& !is_pass(best
->coord
)) {
382 tree_done(u
->t
); u
->t
= NULL
;
383 return coord_copy(resign
);
385 tree_promote_node(u
->t
, best
);
386 return coord_copy(best
->coord
);
390 uct_genbook(struct engine
*e
, struct board
*b
, enum stone color
)
392 struct uct
*u
= e
->data
;
393 u
->t
= tree_init(b
, color
);
394 tree_load(u
->t
, b
, color
);
397 for (i
= 0; i
< u
->games
; i
++) {
398 int result
= uct_playout(u
, b
, color
, u
->t
);
400 /* Tree descent has hit invalid move. */
404 if (i
> 0 && !(i
% 10000)) {
405 progress_status(u
, u
->t
, color
, i
);
408 progress_status(u
, u
->t
, color
, i
);
410 tree_save(u
->t
, b
, u
->games
/ 100);
418 uct_dumpbook(struct engine
*e
, struct board
*b
, enum stone color
)
420 struct uct
*u
= e
->data
;
421 u
->t
= tree_init(b
, color
);
422 tree_load(u
->t
, b
, color
);
429 uct_state_init(char *arg
)
431 struct uct
*u
= calloc(1, sizeof(struct uct
));
435 u
->gamelen
= MC_GAMELEN
;
438 u
->playout_amaf
= false;
441 char *optspec
, *next
= arg
;
444 next
+= strcspn(next
, ",");
445 if (*next
) { *next
++ = 0; } else { *next
= 0; }
447 char *optname
= optspec
;
448 char *optval
= strchr(optspec
, '=');
449 if (optval
) *optval
++ = 0;
451 if (!strcasecmp(optname
, "debug")) {
453 u
->debug_level
= atoi(optval
);
456 } else if (!strcasecmp(optname
, "games") && optval
) {
457 u
->games
= atoi(optval
);
458 } else if (!strcasecmp(optname
, "gamelen") && optval
) {
459 u
->gamelen
= atoi(optval
);
460 } else if (!strcasecmp(optname
, "expand_p") && optval
) {
461 u
->expand_p
= atoi(optval
);
462 } else if (!strcasecmp(optname
, "radar_d") && optval
) {
463 /* For 19x19, it is good idea to set this to 3. */
464 u
->radar_d
= atoi(optval
);
465 } else if (!strcasecmp(optname
, "dumpthres") && optval
) {
466 u
->dumpthres
= atoi(optval
);
467 } else if (!strcasecmp(optname
, "playout_amaf")) {
468 /* Whether to include random playout moves in
469 * AMAF as well. (Otherwise, only tree moves
470 * are included in AMAF. Of course makes sense
471 * only in connection with an AMAF policy.) */
472 /* with-without: 55.5% (+-4.1) */
473 if (optval
&& *optval
== '0')
474 u
->playout_amaf
= false;
476 u
->playout_amaf
= true;
477 } else if (!strcasecmp(optname
, "policy") && optval
) {
478 char *policyarg
= strchr(optval
, ':');
481 if (!strcasecmp(optval
, "ucb1")) {
482 u
->policy
= policy_ucb1_init(u
, policyarg
);
483 } else if (!strcasecmp(optval
, "ucb1tuned")) {
484 u
->policy
= policy_ucb1tuned_init(u
, policyarg
);
485 } else if (!strcasecmp(optval
, "ucb1amaf")) {
486 u
->policy
= policy_ucb1amaf_init(u
, policyarg
);
488 fprintf(stderr
, "UCT: Invalid tree policy %s\n", optval
);
490 } else if (!strcasecmp(optname
, "playout") && optval
) {
491 char *playoutarg
= strchr(optval
, ':');
494 if (!strcasecmp(optval
, "moggy")) {
495 u
->playout
= playout_moggy_init(playoutarg
);
496 } else if (!strcasecmp(optval
, "light")) {
497 u
->playout
= playout_light_init(playoutarg
);
499 fprintf(stderr
, "UCT: Invalid playout policy %s\n", optval
);
501 } else if (!strcasecmp(optname
, "threads") && optval
) {
502 u
->threads
= atoi(optval
);
503 } else if (!strcasecmp(optname
, "force_seed") && optval
) {
504 u
->force_seed
= atoi(optval
);
506 fprintf(stderr
, "uct: Invalid engine argument %s or missing value\n", optname
);
511 u
->resign_ratio
= 0.2; /* Resign when most games are lost. */
512 u
->loss_threshold
= 0.85; /* Stop reading if after at least 5000 playouts this is best value. */
514 u
->policy
= policy_ucb1amaf_init(u
, NULL
);
517 u
->playout
= playout_moggy_init(NULL
);
518 u
->playout
->debug_level
= u
->debug_level
;
525 engine_uct_init(char *arg
)
527 struct uct
*u
= uct_state_init(arg
);
528 struct engine
*e
= calloc(1, sizeof(struct engine
));
529 e
->name
= "UCT Engine";
530 e
->comment
= "I'm playing UCT. When we both pass, I will consider all the stones on the board alive. If you are reading this, write 'yes'. Please bear with me at the game end, I need to fill the whole board; if you help me, we will both be happier. Filling the board will not lose points (NZ rules).";
531 e
->genmove
= uct_genmove
;
532 e
->notify_play
= uct_notify_play
;