12 #include "playout/moggy.h"
13 #include "playout/old.h"
14 #include "playout/light.h"
15 #include "uct/internal.h"
19 struct uct_policy
*policy_ucb1_init(struct uct
*u
, char *arg
);
20 struct uct_policy
*policy_ucb1tuned_init(struct uct
*u
, char *arg
);
21 struct uct_policy
*policy_ucb1amaf_init(struct uct
*u
, char *arg
);
24 #define MC_GAMES 40000
25 #define MC_GAMELEN 400
29 progress_status(struct uct
*u
, struct tree
*t
, enum stone color
, int playouts
)
35 struct tree_node
*best
= u
->policy
->choose(u
->policy
, t
->root
, t
->board
, color
);
37 fprintf(stderr
, "... No moves left\n");
40 fprintf(stderr
, "[%d] ", playouts
);
41 fprintf(stderr
, "best %f ", best
->u
.value
);
44 fprintf(stderr
, "deepest % 2d ", t
->max_depth
- t
->root
->depth
);
47 fprintf(stderr
, "| seq ");
48 for (int depth
= 0; depth
< 6; depth
++) {
49 if (best
&& best
->u
.playouts
>= 25) {
50 fprintf(stderr
, "%3s ", coord2sstr(best
->coord
, t
->board
));
51 best
= u
->policy
->choose(u
->policy
, best
, t
->board
, color
);
58 fprintf(stderr
, "| can ");
60 struct tree_node
*can
[cans
];
61 memset(can
, 0, sizeof(can
));
62 best
= t
->root
->children
;
65 while ((!can
[c
] || best
->u
.playouts
> can
[c
]->u
.playouts
) && ++c
< cans
);
66 for (int d
= 0; d
< c
; d
++) can
[d
] = can
[d
+ 1];
67 if (c
> 0) can
[c
- 1] = best
;
72 fprintf(stderr
, "%3s(%.3f) ", coord2sstr(can
[cans
]->coord
, t
->board
), can
[cans
]->u
.value
);
78 fprintf(stderr
, "\n");
83 uct_playout(struct uct
*u
, struct board
*b
, enum stone color
, struct tree
*t
)
88 struct playout_amafmap
*amaf
= NULL
;
89 if (u
->policy
->wants_amaf
) {
90 amaf
= calloc(1, sizeof(*amaf
));
91 amaf
->map
= calloc(board_size2(&b2
) + 1, sizeof(*amaf
->map
));
92 amaf
->map
++; // -1 is pass
95 /* Walk the tree until we find a leaf, then expand it and do
96 * a random playout. */
97 struct tree_node
*n
= t
->root
;
98 enum stone orig_color
= color
;
100 int pass_limit
= (board_size(&b2
) - 2) * (board_size(&b2
) - 2) / 2;
101 int passes
= is_pass(b
->last_move
.coord
);
103 fprintf(stderr
, "--- UCT walk with color %d\n", color
);
104 for (; pass
; color
= stone_other(color
)) {
105 if (tree_leaf_node(n
)) {
106 if (n
->u
.playouts
>= u
->expand_p
)
107 tree_expand_node(t
, n
, &b2
, color
, u
->radar_d
, u
->policy
, (color
== orig_color
? 1 : -1));
109 result
= play_random_game(&b2
, color
, u
->gamelen
, u
->playout_amaf
? amaf
: NULL
, u
->playout
);
110 if (orig_color
!= color
&& result
>= 0)
113 fprintf(stderr
, "[%d..%d] %s random playout result %d\n", orig_color
, color
, coord2sstr(n
->coord
, t
->board
), result
);
115 /* Reset color to the @n color. */
116 color
= stone_other(color
);
120 n
= u
->policy
->descend(u
->policy
, t
, n
, (color
== orig_color
? 1 : -1), pass_limit
);
121 assert(n
== t
->root
|| n
->parent
);
123 fprintf(stderr
, "-- UCT sent us to [%s] %f\n", coord2sstr(n
->coord
, t
->board
), n
->u
.value
);
124 if (amaf
&& n
->coord
>= -1)
125 amaf
->map
[n
->coord
] = color
;
126 struct move m
= { n
->coord
, color
};
127 int res
= board_play(&b2
, &m
);
129 if (res
< 0 || (!is_pass(m
.coord
) && !group_at(&b2
, m
.coord
)) /* suicide */
130 || b2
.superko_violation
) {
132 for (struct tree_node
*ni
= n
; ni
; ni
= ni
->parent
)
133 fprintf(stderr
, "%s ", coord2sstr(ni
->coord
, t
->board
));
134 fprintf(stderr
, "deleting invalid %s node %d,%d res %d group %d spk %d\n",
135 stone2str(color
), coord_x(n
->coord
,b
), coord_y(n
->coord
,b
),
136 res
, group_at(&b2
, m
.coord
), b2
.superko_violation
);
138 tree_delete_node(t
, n
);
143 if (is_pass(n
->coord
)) {
146 float score
= board_official_score(&b2
);
147 result
= (orig_color
== S_BLACK
) ? score
< 0 : score
> 0;
149 fprintf(stderr
, "[%d..%d] %s p-p scoring playout result %d (W %f)\n", orig_color
, color
, coord2sstr(n
->coord
, t
->board
), result
, score
);
151 board_print(&b2
, stderr
);
159 assert(n
== t
->root
|| n
->parent
);
161 u
->policy
->update(u
->policy
, t
, n
, color
, amaf
, result
);
168 board_done_noalloc(&b2
);
173 prepare_move(struct engine
*e
, struct board
*b
, enum stone color
, coord_t promote
)
175 struct uct
*u
= e
->data
;
177 if (!b
->moves
&& u
->t
) {
178 /* Stale state from last game */
184 u
->t
= tree_init(b
, color
);
185 //board_print(b, stderr);
189 /* XXX: We hope that the opponent didn't suddenly play
190 * several moves in the row. */
191 if (!is_resign(promote
) && !tree_promote_at(u
->t
, b
, promote
)) {
192 fprintf(stderr
, "CANNOT FIND NODE TO PROMOTE!\n");
195 u
->t
= tree_init(b
, color
);
200 uct_notify_play(struct engine
*e
, struct board
*b
, struct move
*m
)
202 prepare_move(e
, b
, stone_other(m
->color
), m
->coord
);
206 uct_genmove(struct engine
*e
, struct board
*b
, enum stone color
)
208 struct uct
*u
= e
->data
;
211 prepare_move(e
, b
, color
, resign
);
213 int i
, games
= u
->games
- (u
->t
->root
->u
.playouts
/ 1.5);
214 for (i
= 0; i
< games
; i
++) {
215 int result
= uct_playout(u
, b
, color
, u
->t
);
217 /* Tree descent has hit invalid move. */
221 if (i
> 0 && !(i
% 10000)) {
222 progress_status(u
, u
->t
, color
, i
);
225 if (i
> 0 && !(i
% 500)) {
226 struct tree_node
*best
= u
->policy
->choose(u
->policy
, u
->t
->root
, b
, color
);
227 if (best
&& best
->u
.playouts
>= 1000 && best
->u
.value
>= u
->loss_threshold
)
232 progress_status(u
, u
->t
, color
, i
);
234 tree_dump(u
->t
, u
->dumpthres
);
236 struct tree_node
*best
= u
->policy
->choose(u
->policy
, u
->t
->root
, b
, color
);
238 tree_done(u
->t
); u
->t
= NULL
;
239 return coord_copy(pass
);
242 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
, i
);
243 if (best
->u
.value
< u
->resign_ratio
&& !is_pass(best
->coord
)) {
244 tree_done(u
->t
); u
->t
= NULL
;
245 return coord_copy(resign
);
247 tree_promote_node(u
->t
, best
);
248 return coord_copy(best
->coord
);
252 uct_genbook(struct engine
*e
, struct board
*b
, enum stone color
)
254 struct uct
*u
= e
->data
;
255 u
->t
= tree_init(b
, color
);
259 for (i
= 0; i
< u
->games
; i
++) {
260 int result
= uct_playout(u
, b
, color
, u
->t
);
262 /* Tree descent has hit invalid move. */
266 if (i
> 0 && !(i
% 10000)) {
267 progress_status(u
, u
->t
, color
, i
);
270 progress_status(u
, u
->t
, color
, i
);
272 tree_save(u
->t
, b
, u
->games
/ 100);
280 uct_dumpbook(struct engine
*e
, struct board
*b
, enum stone color
)
282 struct uct
*u
= e
->data
;
283 u
->t
= tree_init(b
, color
);
291 uct_state_init(char *arg
)
293 struct uct
*u
= calloc(1, sizeof(struct uct
));
297 u
->gamelen
= MC_GAMELEN
;
302 char *optspec
, *next
= arg
;
305 next
+= strcspn(next
, ",");
306 if (*next
) { *next
++ = 0; } else { *next
= 0; }
308 char *optname
= optspec
;
309 char *optval
= strchr(optspec
, '=');
310 if (optval
) *optval
++ = 0;
312 if (!strcasecmp(optname
, "debug")) {
314 u
->debug_level
= atoi(optval
);
317 } else if (!strcasecmp(optname
, "games") && optval
) {
318 u
->games
= atoi(optval
);
319 } else if (!strcasecmp(optname
, "gamelen") && optval
) {
320 u
->gamelen
= atoi(optval
);
321 } else if (!strcasecmp(optname
, "expand_p") && optval
) {
322 u
->expand_p
= atoi(optval
);
323 } else if (!strcasecmp(optname
, "radar_d") && optval
) {
324 /* For 19x19, it is good idea to set this to 3. */
325 u
->radar_d
= atoi(optval
);
326 } else if (!strcasecmp(optname
, "dumpthres") && optval
) {
327 u
->dumpthres
= atoi(optval
);
328 } else if (!strcasecmp(optname
, "playout_amaf")) {
329 /* Whether to include random playout moves in
330 * AMAF as well. (Otherwise, only tree moves
331 * are included in AMAF. Of course makes sense
332 * only in connection with an AMAF policy.) */
333 u
->playout_amaf
= true;
334 } else if (!strcasecmp(optname
, "policy") && optval
) {
335 char *policyarg
= strchr(optval
, ':');
338 if (!strcasecmp(optval
, "ucb1")) {
339 u
->policy
= policy_ucb1_init(u
, policyarg
);
340 } else if (!strcasecmp(optval
, "ucb1tuned")) {
341 u
->policy
= policy_ucb1tuned_init(u
, policyarg
);
342 } else if (!strcasecmp(optval
, "ucb1amaf")) {
343 u
->policy
= policy_ucb1amaf_init(u
, policyarg
);
345 fprintf(stderr
, "UCT: Invalid tree policy %s\n", optval
);
347 } else if (!strcasecmp(optname
, "playout") && optval
) {
348 char *playoutarg
= strchr(optval
, ':');
351 if (!strcasecmp(optval
, "old")) {
352 u
->playout
= playout_old_init(playoutarg
);
353 } else if (!strcasecmp(optval
, "moggy")) {
354 u
->playout
= playout_moggy_init(playoutarg
);
355 } else if (!strcasecmp(optval
, "light")) {
356 u
->playout
= playout_light_init(playoutarg
);
358 fprintf(stderr
, "UCT: Invalid playout policy %s\n", optval
);
361 fprintf(stderr
, "uct: Invalid engine argument %s or missing value\n", optname
);
366 u
->resign_ratio
= 0.2; /* Resign when most games are lost. */
367 u
->loss_threshold
= 0.95; /* Stop reading if after at least 500 playouts this is best value. */
369 u
->policy
= policy_ucb1_init(u
, NULL
);
372 u
->playout
= playout_moggy_init(NULL
);
373 u
->playout
->debug_level
= u
->debug_level
;
380 engine_uct_init(char *arg
)
382 struct uct
*u
= uct_state_init(arg
);
383 struct engine
*e
= calloc(1, sizeof(struct engine
));
384 e
->name
= "UCT Engine";
385 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).";
386 e
->genmove
= uct_genmove
;
387 e
->notify_play
= uct_notify_play
;