11 #include "montecarlo/hint.h"
12 #include "uct/internal.h"
16 struct uct_policy
*policy_ucb1_init(struct uct
*u
, char *arg
);
19 #define MC_GAMES 40000
20 #define MC_GAMELEN 400
24 domainhint_policy(void *playout_policy
, struct board
*b
, enum stone my_color
)
26 struct uct
*u
= playout_policy
;
27 return domain_hint(&u
->mc
, b
, my_color
);
31 uct_playout(struct uct
*u
, struct board
*b
, enum stone color
, struct tree
*t
)
36 /* Walk the tree until we find a leaf, then expand it and do
37 * a random playout. */
38 struct tree_node
*n
= t
->root
;
39 enum stone orig_color
= color
;
41 int pass_limit
= (b2
.size
- 2) * (b2
.size
- 2) / 2;
42 int passes
= is_pass(b
->last_move
.coord
);
44 fprintf(stderr
, "--- UCT walk\n");
45 for (; pass
; color
= stone_other(color
)) {
46 if (tree_leaf_node(n
)) {
47 if (n
->playouts
>= u
->expand_p
)
48 tree_expand_node(t
, n
, &b2
);
50 result
= play_random_game(&b2
, stone_other(color
), u
->gamelen
, domainhint_policy
, u
);
51 if (orig_color
== color
&& result
>= 0)
54 fprintf(stderr
, "[%d..%d] %s playout result %d\n", orig_color
, color
, coord2sstr(n
->coord
, t
->board
), result
);
58 n
= u
->policy
->descend(u
->policy
, t
, n
, (color
== orig_color
? 1 : -1), pass_limit
);
60 fprintf(stderr
, "-- UCT sent us to [%s] %f\n", coord2sstr(n
->coord
, t
->board
), n
->value
);
61 struct move m
= { n
->coord
, color
};
62 int res
= board_play(&b2
, &m
);
63 if (res
< 0 || (!is_pass(m
.coord
) && !group_at(&b2
, m
.coord
)) /* suicide */
64 || b2
.superko_violation
) {
66 fprintf(stderr
, "deleting invalid node %d,%d\n", coord_x(n
->coord
,b
), coord_y(n
->coord
,b
));
68 board_done_noalloc(&b2
);
72 if (is_pass(n
->coord
)) {
75 float score
= board_official_score(&b2
);
76 result
= (orig_color
== S_BLACK
) ? score
< 0 : score
> 0;
78 fprintf(stderr
, "[%d..%d] %s playout result %d (W %f)\n", orig_color
, color
, coord2sstr(n
->coord
, t
->board
), result
, score
);
80 board_print(&b2
, stderr
);
89 tree_uct_update(n
, result
);
90 board_done_noalloc(&b2
);
95 uct_genmove(struct engine
*e
, struct board
*b
, enum stone color
)
97 struct uct
*u
= e
->data
;
102 //board_print(b, stderr);
104 /* XXX: We hope that the opponent didn't suddenly play
105 * several moves in the row. */
106 for (struct tree_node
*ni
= u
->t
->root
->children
; ni
; ni
= ni
->sibling
)
107 if (ni
->coord
== b
->last_move
.coord
) {
108 tree_promote_node(u
->t
, ni
);
111 fprintf(stderr
, "CANNOT FIND NODE TO PROMOTE!\n");
118 for (i
= 0; i
< u
->games
; i
++) {
119 int result
= uct_playout(u
, b
, color
, u
->t
);
121 /* Tree descent has hit invalid move. */
125 if (i
> 0 && !(i
% 1000)) {
126 struct tree_node
*best
= u
->policy
->choose(u
->policy
, u
->t
->root
, b
, color
);
127 if (best
&& best
->playouts
>= 500 && best
->value
>= u
->loss_threshold
)
135 struct tree_node
*best
= u
->policy
->choose(u
->policy
, u
->t
->root
, b
, color
);
137 tree_done(u
->t
); u
->t
= NULL
;
138 return coord_copy(pass
);
141 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
, i
);
142 if (best
->value
< u
->resign_ratio
&& !is_pass(best
->coord
)) {
143 tree_done(u
->t
); u
->t
= NULL
;
144 return coord_copy(resign
);
146 tree_promote_node(u
->t
, best
);
147 return coord_copy(best
->coord
);
152 uct_state_init(char *arg
)
154 struct uct
*u
= calloc(1, sizeof(struct uct
));
158 u
->gamelen
= MC_GAMELEN
;
160 u
->mc
.capture_rate
= 100;
161 u
->mc
.atari_rate
= 100;
163 // Looking at the actual playouts, this just encourages MC to make
165 u
->mc
.local_rate
= 0;
168 char *optspec
, *next
= arg
;
171 next
+= strcspn(next
, ",");
172 if (*next
) { *next
++ = 0; } else { *next
= 0; }
174 char *optname
= optspec
;
175 char *optval
= strchr(optspec
, '=');
176 if (optval
) *optval
++ = 0;
178 if (!strcasecmp(optname
, "debug")) {
180 u
->debug_level
= atoi(optval
);
183 } else if (!strcasecmp(optname
, "games") && optval
) {
184 u
->games
= atoi(optval
);
185 } else if (!strcasecmp(optname
, "gamelen") && optval
) {
186 u
->gamelen
= atoi(optval
);
187 } else if (!strcasecmp(optname
, "expand_p") && optval
) {
188 u
->expand_p
= atoi(optval
);
189 } else if (!strcasecmp(optname
, "policy") && optval
) {
190 char *policyarg
= strchr(optval
, '+');
193 if (!strcasecmp(optval
, "ucb1")) {
194 u
->policy
= policy_ucb1_init(u
, policyarg
);
196 } else if (!strcasecmp(optname
, "pure")) {
197 u
->mc
.capture_rate
= u
->mc
.local_rate
= u
->mc
.cut_rate
= 0;
198 } else if (!strcasecmp(optname
, "capturerate") && optval
) {
199 u
->mc
.capture_rate
= atoi(optval
);
200 } else if (!strcasecmp(optname
, "atarirate") && optval
) {
201 u
->mc
.atari_rate
= atoi(optval
);
202 } else if (!strcasecmp(optname
, "localrate") && optval
) {
203 u
->mc
.local_rate
= atoi(optval
);
204 } else if (!strcasecmp(optname
, "cutrate") && optval
) {
205 u
->mc
.cut_rate
= atoi(optval
);
207 fprintf(stderr
, "uct: Invalid engine argument %s or missing value\n", optname
);
212 u
->resign_ratio
= 0.2; /* Resign when most games are lost. */
213 u
->loss_threshold
= 0.95; /* Stop reading if after at least 500 playouts this is best value. */
214 u
->mc
.debug_level
= u
->debug_level
;
215 u
->policy
= policy_ucb1_init(u
, NULL
);
222 engine_uct_init(char *arg
)
224 struct uct
*u
= uct_state_init(arg
);
225 struct engine
*e
= calloc(1, sizeof(struct engine
));
226 e
->name
= "UCT Engine";
227 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).";
228 e
->genmove
= uct_genmove
;