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
)
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
, "best %f ", best
->value
);
43 fprintf(stderr
, "deepest %d ", t
->max_depth
);
46 fprintf(stderr
, "| seq ");
47 for (int depth
= 0; depth
< 6; depth
++) {
48 if (best
&& best
->playouts
>= 25) {
49 fprintf(stderr
, "%3s ", coord2sstr(best
->coord
, t
->board
));
50 best
= u
->policy
->choose(u
->policy
, best
, t
->board
, color
);
57 fprintf(stderr
, "| can ");
59 struct tree_node
*can
[cans
];
60 memset(can
, 0, sizeof(can
));
61 best
= t
->root
->children
;
64 while ((!can
[c
] || best
->playouts
> can
[c
]->playouts
) && ++c
< cans
);
65 for (int d
= 0; d
< c
; d
++) can
[d
] = can
[d
+ 1];
66 if (c
> 0) can
[c
- 1] = best
;
71 fprintf(stderr
, "%3s(%.3f) ", coord2sstr(can
[cans
]->coord
, t
->board
), can
[cans
]->value
);
77 fprintf(stderr
, "\n");
82 uct_playout(struct uct
*u
, struct board
*b
, enum stone color
, struct tree
*t
)
87 struct playout_amafmap
*amaf
= NULL
;
88 if (u
->policy
->wants_amaf
) {
89 amaf
= calloc(1, sizeof(*amaf
));
90 amaf
->map
= calloc(b2
.size2
+ 1, sizeof(*amaf
->map
));
91 amaf
->map
++; // -1 is pass
94 /* Walk the tree until we find a leaf, then expand it and do
95 * a random playout. */
96 struct tree_node
*n
= t
->root
;
97 enum stone orig_color
= color
;
99 int pass_limit
= (b2
.size
- 2) * (b2
.size
- 2) / 2;
100 int passes
= is_pass(b
->last_move
.coord
);
102 fprintf(stderr
, "--- UCT walk with color %d\n", color
);
103 for (; pass
; color
= stone_other(color
)) {
104 if (tree_leaf_node(n
)) {
105 if (n
->playouts
>= u
->expand_p
)
106 tree_expand_node(t
, n
, &b2
, color
, u
->radar_d
, u
->policy
);
108 result
= play_random_game(&b2
, color
, u
->gamelen
, u
->playout_amaf
? amaf
: NULL
, u
->playout
);
109 if (orig_color
!= color
&& result
>= 0)
112 fprintf(stderr
, "[%d..%d] %s random playout result %d\n", orig_color
, color
, coord2sstr(n
->coord
, t
->board
), result
);
116 n
= u
->policy
->descend(u
->policy
, t
, n
, (color
== orig_color
? 1 : -1), pass_limit
);
117 assert(n
== t
->root
|| n
->parent
);
119 fprintf(stderr
, "-- UCT sent us to [%s] %f\n", coord2sstr(n
->coord
, t
->board
), n
->value
);
120 if (amaf
&& n
->coord
>= -1)
121 amaf
->map
[n
->coord
] = color
;
122 struct move m
= { n
->coord
, color
};
123 int res
= board_play(&b2
, &m
);
124 if (res
< 0 || (!is_pass(m
.coord
) && !group_at(&b2
, m
.coord
)) /* suicide */
125 || b2
.superko_violation
) {
127 fprintf(stderr
, "deleting invalid node %d,%d\n", coord_x(n
->coord
,b
), coord_y(n
->coord
,b
));
128 tree_delete_node(t
, n
);
133 if (is_pass(n
->coord
)) {
136 float score
= board_official_score(&b2
);
137 result
= (orig_color
== S_BLACK
) ? score
< 0 : score
> 0;
139 fprintf(stderr
, "[%d..%d] %s playout result %d (W %f)\n", orig_color
, color
, coord2sstr(n
->coord
, t
->board
), result
, score
);
141 board_print(&b2
, stderr
);
149 assert(n
== t
->root
|| n
->parent
);
151 amaf
->color
= stone_other(color
);
153 u
->policy
->update(u
->policy
, n
, amaf
, result
);
160 board_done_noalloc(&b2
);
165 uct_genmove(struct engine
*e
, struct board
*b
, enum stone color
)
167 struct uct
*u
= e
->data
;
171 u
->t
= tree_init(b
, color
);
172 //board_print(b, stderr);
174 /* XXX: We hope that the opponent didn't suddenly play
175 * several moves in the row. */
176 for (struct tree_node
*ni
= u
->t
->root
->children
; ni
; ni
= ni
->sibling
)
177 if (ni
->coord
== b
->last_move
.coord
) {
178 tree_promote_node(u
->t
, ni
);
181 fprintf(stderr
, "CANNOT FIND NODE TO PROMOTE!\n");
188 for (i
= 0; i
< u
->games
; i
++) {
189 int result
= uct_playout(u
, b
, color
, u
->t
);
191 /* Tree descent has hit invalid move. */
195 if (i
> 0 && !(i
% 10000)) {
196 progress_status(u
, u
->t
, color
);
199 if (i
> 0 && !(i
% 500)) {
200 struct tree_node
*best
= u
->policy
->choose(u
->policy
, u
->t
->root
, b
, color
);
201 if (best
&& best
->playouts
>= 1000 && best
->value
>= u
->loss_threshold
)
206 progress_status(u
, u
->t
, color
);
210 struct tree_node
*best
= u
->policy
->choose(u
->policy
, u
->t
->root
, b
, color
);
212 tree_done(u
->t
); u
->t
= NULL
;
213 return coord_copy(pass
);
216 fprintf(stderr
, "*** WINNER is %d,%d with score %1.4f (%d games)\n", coord_x(best
->coord
, b
), coord_y(best
->coord
, b
), best
->value
, u
->t
->root
->playouts
);
217 if (best
->value
< u
->resign_ratio
&& !is_pass(best
->coord
)) {
218 tree_done(u
->t
); u
->t
= NULL
;
219 return coord_copy(resign
);
221 tree_promote_node(u
->t
, best
);
222 return coord_copy(best
->coord
);
227 uct_state_init(char *arg
)
229 struct uct
*u
= calloc(1, sizeof(struct uct
));
233 u
->gamelen
= MC_GAMELEN
;
237 char *optspec
, *next
= arg
;
240 next
+= strcspn(next
, ",");
241 if (*next
) { *next
++ = 0; } else { *next
= 0; }
243 char *optname
= optspec
;
244 char *optval
= strchr(optspec
, '=');
245 if (optval
) *optval
++ = 0;
247 if (!strcasecmp(optname
, "debug")) {
249 u
->debug_level
= atoi(optval
);
252 } else if (!strcasecmp(optname
, "games") && optval
) {
253 u
->games
= atoi(optval
);
254 } else if (!strcasecmp(optname
, "gamelen") && optval
) {
255 u
->gamelen
= atoi(optval
);
256 } else if (!strcasecmp(optname
, "expand_p") && optval
) {
257 u
->expand_p
= atoi(optval
);
258 } else if (!strcasecmp(optname
, "radar_d") && optval
) {
259 /* For 19x19, it is good idea to set this to 3. */
260 u
->radar_d
= atoi(optval
);
261 } else if (!strcasecmp(optname
, "playout_amaf")) {
262 /* Whether to include random playout moves in
263 * AMAF as well. (Otherwise, only tree moves
264 * are included in AMAF. Of course makes sense
265 * only in connection with an AMAF policy.) */
266 u
->playout_amaf
= true;
267 } else if (!strcasecmp(optname
, "policy") && optval
) {
268 char *policyarg
= strchr(optval
, ':');
271 if (!strcasecmp(optval
, "ucb1")) {
272 u
->policy
= policy_ucb1_init(u
, policyarg
);
273 } else if (!strcasecmp(optval
, "ucb1tuned")) {
274 u
->policy
= policy_ucb1tuned_init(u
, policyarg
);
275 } else if (!strcasecmp(optval
, "ucb1amaf")) {
276 u
->policy
= policy_ucb1amaf_init(u
, policyarg
);
278 fprintf(stderr
, "UCT: Invalid tree policy %s\n", optval
);
280 } else if (!strcasecmp(optname
, "playout") && optval
) {
281 char *playoutarg
= strchr(optval
, ':');
284 if (!strcasecmp(optval
, "old")) {
285 u
->playout
= playout_old_init(playoutarg
);
286 } else if (!strcasecmp(optval
, "moggy")) {
287 u
->playout
= playout_moggy_init(playoutarg
);
288 } else if (!strcasecmp(optval
, "light")) {
289 u
->playout
= playout_light_init(playoutarg
);
291 fprintf(stderr
, "UCT: Invalid playout policy %s\n", optval
);
294 fprintf(stderr
, "uct: Invalid engine argument %s or missing value\n", optname
);
299 u
->resign_ratio
= 0.2; /* Resign when most games are lost. */
300 u
->loss_threshold
= 0.95; /* Stop reading if after at least 500 playouts this is best value. */
302 u
->policy
= policy_ucb1_init(u
, NULL
);
305 u
->playout
= playout_light_init(NULL
);
306 u
->playout
->debug_level
= u
->debug_level
;
313 engine_uct_init(char *arg
)
315 struct uct
*u
= uct_state_init(arg
);
316 struct engine
*e
= calloc(1, sizeof(struct engine
));
317 e
->name
= "UCT Engine";
318 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).";
319 e
->genmove
= uct_genmove
;