Merge branch 'master' into libmap
[pachi.git] / playout.c
blobec10001c7355e1acc5e52a09239acc5cc0b6ca4b
1 #define DEBUG
2 #include <assert.h>
3 #include <math.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
8 #include "board.h"
9 #include "debug.h"
10 #include "engine.h"
11 #include "libmap.h"
12 #include "move.h"
13 #include "ownermap.h"
14 #include "playout.h"
16 /* Whether to set global debug level to the same as the playout
17 * has, in case it is different. This can make sure e.g. tactical
18 * reading produces proper level of debug prints during simulations.
19 * But it is safe to enable this only in single-threaded instances! */
20 //#define DEBUGL_BY_PLAYOUT
22 #define PLDEBUGL(n) DEBUGL_(policy->debug_level, n)
25 coord_t
26 play_random_move(struct playout_setup *setup,
27 struct board *b, enum stone color,
28 struct playout_policy *policy)
30 coord_t coord = pass;
32 if (setup->prepolicy_hook) {
33 coord = setup->prepolicy_hook(policy, setup, b, color);
34 // fprintf(stderr, "prehook: %s\n", coord2sstr(coord, b));
37 if (is_pass(coord)) {
38 coord = policy->choose(policy, setup, b, color);
39 // fprintf(stderr, "policy: %s\n", coord2sstr(coord, b));
42 if (is_pass(coord) && setup->postpolicy_hook) {
43 coord = setup->postpolicy_hook(policy, setup, b, color);
44 // fprintf(stderr, "posthook: %s\n", coord2sstr(coord, b));
47 if (is_pass(coord)) {
48 play_random:
49 /* Defer to uniformly random move choice. */
50 /* This must never happen if the policy is tracking
51 * internal board state, obviously. */
52 assert(!policy->setboard);
53 board_play_random(b, color, &coord, (ppr_permit) policy->permit, policy);
55 } else {
56 struct move m;
57 m.coord = coord; m.color = color;
58 if (board_play(b, &m) < 0) {
59 if (PLDEBUGL(4)) {
60 fprintf(stderr, "Pre-picked move %d,%d is ILLEGAL:\n",
61 coord_x(coord, b), coord_y(coord, b));
62 board_print(b, stderr);
64 goto play_random;
68 return coord;
71 int
72 play_random_game(struct playout_setup *setup,
73 struct board *b, enum stone starting_color,
74 struct playout_amafmap *amafmap,
75 struct board_ownermap *ownermap,
76 struct playout_policy *policy)
78 assert(setup && policy);
80 int gamelen = setup->gamelen - b->moves;
81 if (gamelen < 10)
82 gamelen = 10;
84 if (policy->setboard)
85 policy->setboard(policy, b);
86 #ifdef DEBUGL_BY_PLAYOUT
87 int debug_level_orig = debug_level;
88 debug_level = policy->debug_level;
89 #endif
91 enum stone color = starting_color;
93 int passes = is_pass(b->last_move.coord) && b->moves > 0;
95 while (gamelen-- && passes < 2) {
96 coord_t coord = play_random_move(setup, b, color, policy);
98 #if 0
99 /* For UCT, superko test here is downright harmful since
100 * in superko-likely situation we throw away literally
101 * 95% of our playouts; UCT will deal with this fine by
102 * itself. */
103 if (unlikely(b->superko_violation)) {
104 /* We ignore superko violations that are suicides. These
105 * are common only at the end of the game and are
106 * rather harmless. (They will not go through as a root
107 * move anyway.) */
108 if (group_at(b, coord)) {
109 if (DEBUGL(3)) {
110 fprintf(stderr, "Superko fun at %d,%d in\n", coord_x(coord, b), coord_y(coord, b));
111 if (DEBUGL(4))
112 board_print(b, stderr);
114 return 0;
115 } else {
116 if (DEBUGL(6)) {
117 fprintf(stderr, "Ignoring superko at %d,%d in\n", coord_x(coord, b), coord_y(coord, b));
118 board_print(b, stderr);
120 b->superko_violation = false;
123 #endif
125 if (PLDEBUGL(7)) {
126 fprintf(stderr, "%s %s\n", stone2str(color), coord2sstr(coord, b));
127 if (PLDEBUGL(8))
128 board_print(b, stderr);
131 if (unlikely(is_pass(coord))) {
132 passes++;
133 } else {
134 passes = 0;
136 if (amafmap) {
137 assert(amafmap->gamelen < MAX_GAMELEN);
138 amafmap->is_ko_capture[amafmap->gamelen] = board_playing_ko_threat(b);
139 amafmap->game[amafmap->gamelen++] = coord;
142 if (setup->mercymin && abs(b->captures[S_BLACK] - b->captures[S_WHITE]) > setup->mercymin)
143 break;
145 color = stone_other(color);
148 floating_t score = board_fast_score(b);
149 int result = (starting_color == S_WHITE ? score * 2 : - (score * 2));
151 if (DEBUGL(6)) {
152 fprintf(stderr, "Random playout result: %d (W %f)\n", result, score);
153 if (DEBUGL(7))
154 board_print(b, stderr);
157 if (ownermap)
158 board_ownermap_fill(ownermap, b);
159 if (b->libmap)
160 libmap_queue_process(b->libmap, b, score > 0 ? S_WHITE : S_BLACK);
162 if (b->ps)
163 free(b->ps);
165 #ifdef DEBUGL_BY_PLAYOUT
166 debug_level = debug_level_orig;
167 #endif
169 return result;