11 #include "uct/internal.h"
14 /* This implements the UCB1 policy with an extra AMAF heuristics. */
16 struct ucb1_policy_amaf
{
17 /* This is what the Modification of UCT with Patterns in Monte Carlo Go
18 * paper calls 'p'. Original UCB has this on 2, but this seems to
19 * produce way too wide searches; reduce this to get deeper and
20 * narrower readouts - try 0.2. */
22 /* First Play Urgency - if set to less than infinity (the MoGo paper
23 * above reports 1.0 as the best), new branches are explored only
24 * if none of the existing ones has higher urgency than fpu. */
26 int urg_randoma
, urg_randomm
;
34 struct tree_node
*ucb1_choose(struct uct_policy
*p
, struct tree_node
*node
, struct board
*b
, enum stone color
);
36 struct tree_node
*ucb1_descend(struct uct_policy
*p
, struct tree
*tree
, struct tree_node
*node
, int parity
, bool allow_pass
);
39 static inline float fast_sqrt(int x
)
41 static const float table
[] = {
42 0, 1, 1.41421356237309504880, 1.73205080756887729352,
43 2.00000000000000000000, 2.23606797749978969640,
44 2.44948974278317809819, 2.64575131106459059050,
45 2.82842712474619009760, 3.00000000000000000000,
46 3.16227766016837933199, 3.31662479035539984911,
47 3.46410161513775458705, 3.60555127546398929311,
48 3.74165738677394138558, 3.87298334620741688517,
49 4.00000000000000000000, 4.12310562561766054982,
50 4.24264068711928514640, 4.35889894354067355223,
51 4.47213595499957939281, 4.58257569495584000658,
52 4.69041575982342955456, 4.79583152331271954159,
53 4.89897948556635619639, 5.00000000000000000000,
54 5.09901951359278483002, 5.19615242270663188058,
55 5.29150262212918118100, 5.38516480713450403125,
56 5.47722557505166113456, 5.56776436283002192211,
57 5.65685424949238019520, 5.74456264653802865985,
58 5.83095189484530047087, 5.91607978309961604256,
59 6.00000000000000000000, 6.08276253029821968899,
60 6.16441400296897645025, 6.24499799839839820584,
61 6.32455532033675866399, 6.40312423743284868648,
62 6.48074069840786023096, 6.55743852430200065234,
63 6.63324958071079969822, 6.70820393249936908922,
64 6.78232998312526813906, 6.85565460040104412493,
65 6.92820323027550917410, 7.00000000000000000000,
66 7.07106781186547524400, 7.14142842854284999799,
67 7.21110255092797858623, 7.28010988928051827109,
68 7.34846922834953429459, 7.41619848709566294871,
69 7.48331477354788277116, 7.54983443527074969723,
70 7.61577310586390828566, 7.68114574786860817576,
71 7.74596669241483377035, 7.81024967590665439412,
72 7.87400787401181101968, 7.93725393319377177150,
74 //printf("sqrt %d\n", x);
75 if (x
< sizeof(table
) / sizeof(*table
)) {
83 ucb1rave_descend(struct uct_policy
*p
, struct tree
*tree
, struct tree_node
*node
, int parity
, bool allow_pass
)
85 struct ucb1_policy_amaf
*b
= p
->data
;
89 nconf
= sqrt(log(node
->u
.playouts
+ node
->prior
.playouts
));
92 beta
= sqrt(b
->equiv_rave
/ (3 * node
->u
.playouts
+ b
->equiv_rave
));
94 // XXX: Stack overflow danger on big boards?
95 struct tree_node
*nbest
[512] = { node
->children
}; int nbests
= 1;
96 float best_urgency
= -9999;
98 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
) {
99 /* Do not consider passing early. */
100 if (unlikely(!allow_pass
&& is_pass(ni
->coord
)))
103 /* TODO: Exploration? */
105 struct move_stats n
= ni
->u
, r
= ni
->amaf
;
106 if (p
->uct
->amaf_prior
) {
107 stats_merge(&r
, &ni
->prior
);
109 stats_merge(&n
, &ni
->prior
);
111 if (tree_parity(tree
, parity
) < 0) {
112 stats_reverse_parity(&n
);
113 stats_reverse_parity(&r
);
119 /* At the beginning, beta is at 1 and RAVE is used.
120 * At b->equiv_rate, beta is at 1/3 and gets steeper on. */
122 beta
= (float) r
.playouts
/ (r
.playouts
+ n
.playouts
+ n
.playouts
* r
.playouts
/ b
->equiv_rave
);
124 //if (node->coord == 7*11+4) // D7
125 fprintf(stderr
, "[beta %f = %d / (%d + %d + %f)]\n",
126 beta
, rgames
, rgames
, ngames
, ngames
* rgames
/ b
->equiv_rave
);
128 urgency
= beta
* r
.value
+ (1.f
- beta
) * n
.value
;
133 if (b
->explore_p
> 0)
134 urgency
+= b
->explore_p
* nconf
/ fast_sqrt(n
.playouts
);
135 } else if (r
.playouts
) {
138 /* assert(!u->even_eqex); */
143 struct board bb
; bb
.size
= 11;
144 //if (node->coord == 7*11+4) // D7
145 fprintf(stderr
, "%s<%lld>-%s<%lld> urgency %f (r %d / %d + e = %f, n %d / %d + e = %f)\n",
146 coord2sstr(ni
->parent
->coord
, &bb
), ni
->parent
->hash
,
147 coord2sstr(ni
->coord
, &bb
), ni
->hash
, urgency
,
148 rwins
, rgames
, rval
, nwins
, ngames
, nval
);
151 urgency
+= (float)(fast_random(b
->urg_randoma
) - b
->urg_randoma
/ 2) / 1000;
153 urgency
*= (float)(fast_random(b
->urg_randomm
) + 5) / b
->urg_randomm
;
155 if (urgency
- best_urgency
> __FLT_EPSILON__
) { // urgency > best_urgency
156 best_urgency
= urgency
; nbests
= 0;
158 if (urgency
- best_urgency
> -__FLT_EPSILON__
) { // urgency >= best_urgency
159 /* We want to always choose something else than a pass
160 * in case of a tie. pass causes degenerative behaviour. */
161 if (nbests
== 1 && is_pass(nbest
[0]->coord
)) {
164 nbest
[nbests
++] = ni
;
168 struct board bb
; bb
.size
= 11;
169 fprintf(stderr
, "RESULT [%s %d: ", coord2sstr(node
->coord
, &bb
), nbests
);
170 for (int zz
= 0; zz
< nbests
; zz
++)
171 fprintf(stderr
, "%s", coord2sstr(nbest
[zz
]->coord
, &bb
));
172 fprintf(stderr
, "]\n");
174 return nbest
[fast_random(nbests
)];
178 ucb1amaf_update(struct uct_policy
*p
, struct tree
*tree
, struct tree_node
*node
, enum stone node_color
, enum stone player_color
, struct playout_amafmap
*map
, int result
)
180 struct ucb1_policy_amaf
*b
= p
->data
;
181 enum stone child_color
= stone_other(node_color
);
184 struct board bb
; bb
.size
= 9+2;
185 for (struct tree_node
*ni
= node
; ni
; ni
= ni
->parent
)
186 fprintf(stderr
, "%s ", coord2sstr(ni
->coord
, &bb
));
187 fprintf(stderr
, "[color %d] update result %d (color %d)\n",
188 node_color
, result
, player_color
);
192 if (node
->parent
== NULL
)
193 assert(tree
->root_color
== stone_other(child_color
));
195 stats_add_result(&node
->u
, result
, 1);
196 if (amaf_nakade(map
->map
[node
->coord
]))
197 amaf_op(map
->map
[node
->coord
], -);
199 /* This loop ignores symmetry considerations, but they should
200 * matter only at a point when AMAF doesn't help much. */
201 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
) {
202 assert(map
->map
[ni
->coord
] != S_OFFBOARD
);
203 if (map
->map
[ni
->coord
] == S_NONE
)
205 assert(map
->game_baselen
>= 0);
206 enum stone amaf_color
= map
->map
[ni
->coord
];
207 if (amaf_nakade(map
->map
[ni
->coord
])) {
208 if (!b
->check_nakade
)
210 /* We don't care to implement both_colors
211 * properly since it sucks anyway. */
213 for (i
= map
->game_baselen
; i
< map
->gamelen
; i
++)
214 if (map
->game
[i
].coord
== ni
->coord
215 && map
->game
[i
].color
== child_color
)
217 if (i
== map
->gamelen
)
219 amaf_color
= child_color
;
223 if (amaf_color
!= child_color
) {
228 /* For child_color != player_color, we still want
229 * to record the result unmodified; in that case,
230 * we will correctly negate them at the descend phase. */
232 stats_add_result(&ni
->amaf
, nres
, 1);
235 fprintf(stderr
, "* %s<%lld> -> %s<%lld> [%d %d => %d/%d]\n", coord2sstr(node
->coord
, &bb
), node
->hash
, coord2sstr(ni
->coord
, &bb
), ni
->hash
, player_color
, child_color
, result
);
239 if (!is_pass(node
->coord
)) {
242 node
= node
->parent
; child_color
= stone_other(child_color
);
248 policy_ucb1amaf_init(struct uct
*u
, char *arg
)
250 struct uct_policy
*p
= calloc(1, sizeof(*p
));
251 struct ucb1_policy_amaf
*b
= calloc(1, sizeof(*b
));
254 p
->descend
= ucb1rave_descend
;
255 p
->choose
= ucb1_choose
;
256 p
->update
= ucb1amaf_update
;
257 p
->wants_amaf
= true;
259 // RAVE: 0.2vs0: 40% (+-7.3) 0.1vs0: 54.7% (+-3.5)
261 b
->equiv_rave
= 3000;
263 b
->check_nakade
= true;
264 b
->sylvain_rave
= true;
267 char *optspec
, *next
= arg
;
270 next
+= strcspn(next
, ":");
271 if (*next
) { *next
++ = 0; } else { *next
= 0; }
273 char *optname
= optspec
;
274 char *optval
= strchr(optspec
, '=');
275 if (optval
) *optval
++ = 0;
277 if (!strcasecmp(optname
, "explore_p")) {
278 b
->explore_p
= atof(optval
);
279 } else if (!strcasecmp(optname
, "fpu") && optval
) {
280 b
->fpu
= atof(optval
);
281 } else if (!strcasecmp(optname
, "urg_randoma") && optval
) {
282 b
->urg_randoma
= atoi(optval
);
283 } else if (!strcasecmp(optname
, "urg_randomm") && optval
) {
284 b
->urg_randomm
= atoi(optval
);
285 } else if (!strcasecmp(optname
, "equiv_rave") && optval
) {
286 b
->equiv_rave
= atof(optval
);
287 } else if (!strcasecmp(optname
, "both_colors")) {
288 b
->both_colors
= true;
289 } else if (!strcasecmp(optname
, "sylvain_rave")) {
290 b
->sylvain_rave
= !optval
|| *optval
== '1';
291 } else if (!strcasecmp(optname
, "check_nakade")) {
292 b
->check_nakade
= !optval
|| *optval
== '1';
294 fprintf(stderr
, "ucb1: Invalid policy argument %s or missing value\n", optname
);