8 #include "joseki/base.h"
10 #include "playout/moggy.h"
11 #include "playout/light.h"
12 #include "montecarlo/internal.h"
13 #include "montecarlo/montecarlo.h"
18 /* This is simple monte-carlo engine. It plays MC_GAMES random games from the
19 * current board and records win/loss ratio for each first move. The move with
20 * the biggest number of winning games gets played. */
21 /* Note that while the library is based on New Zealand rules, this engine
22 * returns moves according to Chinese rules. Thus, it does not return suicide
23 * moves. It of course respects positional superko too. */
25 /* Pass me arguments like a=b,c=d,...
26 * Supported arguments:
27 * debug[=DEBUG_LEVEL] 1 is the default; more means more debugging prints
28 * games=MC_GAMES number of random games to play
29 * gamelen=MC_GAMELEN maximal length of played random game
30 * playout={light,moggy}[:playout_params]
34 #define MC_GAMES 40000
35 #define MC_GAMELEN 400
38 /* FIXME: Cutoff rule for simulations. Currently we are so fast that this
39 * simply does not matter; even 100000 simulations are fast enough to
40 * play 5 minutes S.D. on 19x19 and anything more sounds too ridiculous
42 /* FIXME: We cannot handle seki. Any good ideas are welcome. A possibility is
43 * to consider 'pass' among the moves, but this seems tricky. */
47 board_stats_print(struct board
*board
, struct move_stat
*moves
, FILE *f
)
51 char asdf
[] = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
52 for (x
= 1; x
< board_size(board
) - 1; x
++)
53 fprintf(f
, "%c ", asdf
[x
- 1]);
55 for (x
= 1; x
< board_size(board
) - 1; x
++)
58 for (y
= board_size(board
) - 2; y
>= 1; y
--) {
59 fprintf(f
, "%2d | ", y
);
60 for (x
= 1; x
< board_size(board
) - 1; x
++)
61 if (moves
[y
* board_size(board
) + x
].games
)
62 fprintf(f
, "%0.2f ", (floating_t
) moves
[y
* board_size(board
) + x
].wins
/ moves
[y
* board_size(board
) + x
].games
);
66 for (x
= 1; x
< board_size(board
) - 1; x
++)
67 fprintf(f
, "%4d ", moves
[y
* board_size(board
) + x
].games
);
71 for (x
= 1; x
< board_size(board
) - 1; x
++)
78 montecarlo_genmove(struct engine
*e
, struct board
*b
, struct time_info
*ti
, enum stone color
, bool pass_all_alive
)
80 struct montecarlo
*mc
= e
->data
;
82 if (ti
->dim
== TD_WALLTIME
) {
83 fprintf(stderr
, "Warning: TD_WALLTIME time mode not supported, resetting to defaults.\n");
86 if (ti
->period
== TT_NULL
) {
89 ti
->len
.games
= MC_GAMES
;
91 struct time_stop stop
;
92 time_stop_conditions(ti
, b
, 20, 40, 3.0, &stop
);
94 /* resign when the hope for win vanishes */
95 coord_t top_coord
= resign
;
96 floating_t top_ratio
= mc
->resign_ratio
;
98 /* We use [0] for pass. Normally, this is an inaccessible corner
100 struct move_stat moves
[board_size2(b
)];
101 memset(moves
, 0, sizeof(moves
));
104 int i
, superko
= 0, good_games
= 0;
105 for (i
= 0; i
< stop
.desired
.playouts
; i
++) {
106 assert(!b
->superko_violation
);
112 board_play_random(&b2
, color
, &coord
, NULL
, NULL
);
113 if (!is_pass(coord
) && !group_at(&b2
, coord
)) {
114 /* Multi-stone suicide. We play chinese rules,
115 * so we can't consider this. (Note that we
116 * unfortunately still consider this in playouts.) */
118 fprintf(stderr
, "SUICIDE DETECTED at %d,%d:\n", coord_x(coord
, b
), coord_y(coord
, b
));
119 board_print(b
, stderr
);
125 fprintf(stderr
, "[%d,%d color %d] playing random game\n", coord_x(coord
, b
), coord_y(coord
, b
), color
);
127 struct playout_setup ps
= { .gamelen
= mc
->gamelen
};
128 int result
= play_random_game(&ps
, &b2
, color
, NULL
, NULL
, mc
->playout
);
130 board_done_noalloc(&b2
);
133 /* Superko. We just ignore this playout.
135 if (unlikely(superko
> 2 * stop
.desired
.playouts
)) {
136 /* Uhh. Triple ko, or something? */
138 fprintf(stderr
, "SUPERKO LOOP. I will pass. Did we hit triple ko?\n");
141 /* This playout didn't count; we should not
142 * disadvantage moves that lead to a superko.
143 * And it is supposed to be rare. */
149 fprintf(stderr
, "\tresult for other player: %d\n", result
);
151 int pos
= is_pass(coord
) ? 0 : coord
;
156 losses
+= result
> 0;
157 moves
[pos
].wins
+= 1 - (result
> 0);
159 if (unlikely(!losses
&& i
== mc
->loss_threshold
)) {
160 /* We played out many games and didn't lose once yet.
161 * This game is over. */
167 /* No moves to try??? */
169 fprintf(stderr
, "OUT OF MOVES! I will pass. But how did this happen?\n");
170 board_print(b
, stderr
);
173 top_coord
= pass
; top_ratio
= 0.5;
179 /* Simple heuristic: avoid opening too low. Do not
180 * play on second or first line as first white or
181 * first two black moves.*/
182 if (coord_x(c
, b
) < 3 || coord_x(c
, b
) > board_size(b
) - 4
183 || coord_y(c
, b
) < 3 || coord_y(c
, b
) > board_size(b
) - 4)
187 floating_t ratio
= (floating_t
) moves
[c
].wins
/ moves
[c
].games
;
188 /* Since pass is [0,0], we will pass only when we have nothing
190 if (ratio
>= top_ratio
) {
192 top_coord
= c
== 0 ? pass
: c
;
197 board_stats_print(b
, moves
, stderr
);
202 fprintf(stderr
, "*** WINNER is %d,%d with score %1.4f (%d games, %d superko)\n", coord_x(top_coord
, b
), coord_y(top_coord
, b
), top_ratio
, i
, superko
);
204 return coord_copy(top_coord
);
209 montecarlo_state_init(char *arg
, struct board
*b
)
211 struct montecarlo
*mc
= calloc2(1, sizeof(struct montecarlo
));
214 mc
->gamelen
= MC_GAMELEN
;
217 char *optspec
, *next
= arg
;
220 next
+= strcspn(next
, ",");
221 if (*next
) { *next
++ = 0; } else { *next
= 0; }
223 char *optname
= optspec
;
224 char *optval
= strchr(optspec
, '=');
225 if (optval
) *optval
++ = 0;
227 if (!strcasecmp(optname
, "debug")) {
229 mc
->debug_level
= atoi(optval
);
232 } else if (!strcasecmp(optname
, "gamelen") && optval
) {
233 mc
->gamelen
= atoi(optval
);
234 } else if (!strcasecmp(optname
, "playout") && optval
) {
235 char *playoutarg
= strchr(optval
, ':');
238 if (!strcasecmp(optval
, "moggy")) {
239 mc
->playout
= playout_moggy_init(playoutarg
, b
, joseki_load(b
->size
));
240 } else if (!strcasecmp(optval
, "light")) {
241 mc
->playout
= playout_light_init(playoutarg
, b
);
243 fprintf(stderr
, "MonteCarlo: Invalid playout policy %s\n", optval
);
246 fprintf(stderr
, "MonteCarlo: Invalid engine argument %s or missing value\n", optname
);
252 mc
->playout
= playout_light_init(NULL
, b
);
253 mc
->playout
->debug_level
= mc
->debug_level
;
255 mc
->resign_ratio
= 0.1; /* Resign when most games are lost. */
256 mc
->loss_threshold
= 5000; /* Stop reading if no loss encountered in first 5000 games. */
263 engine_montecarlo_init(char *arg
, struct board
*b
)
265 struct montecarlo
*mc
= montecarlo_state_init(arg
, b
);
266 struct engine
*e
= calloc2(1, sizeof(struct engine
));
267 e
->name
= "MonteCarlo Engine";
268 e
->comment
= "I'm playing in Monte Carlo. 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).";
269 e
->genmove
= montecarlo_genmove
;