play_random_game(): Return score difference instead of just boolean
[pachi.git] / uct / policy / ucb1.c
blobb892ce62f7850c911292f1a8eebc2225b373e24c
1 #include <assert.h>
2 #include <math.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
7 #include "board.h"
8 #include "debug.h"
9 #include "move.h"
10 #include "random.h"
11 #include "uct/internal.h"
12 #include "uct/tree.h"
14 /* This implements the basic UCB1 policy. */
16 struct ucb1_policy {
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. */
21 float explore_p;
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. */
25 float fpu;
26 int urg_randoma, urg_randomm;
30 struct tree_node *
31 ucb1_choose(struct uct_policy *p, struct tree_node *node, struct board *b, enum stone color)
33 struct tree_node *nbest = NULL;
34 for (struct tree_node *ni = node->children; ni; ni = ni->sibling)
35 // we compare playouts and choose the best-explored
36 // child; comparing values is more brittle
37 if (!nbest || ni->u.playouts > nbest->u.playouts) {
38 /* Play pass only if we can afford scoring */
39 if (is_pass(ni->coord)) {
40 float score = board_official_score(b);
41 if (color == S_BLACK)
42 score = -score;
43 //fprintf(stderr, "%d score %f\n", color, score);
44 if (score <= 0)
45 continue;
47 nbest = ni;
49 return nbest;
53 struct tree_node *
54 ucb1_descend(struct uct_policy *p, struct tree *tree, struct tree_node *node, int parity, bool allow_pass)
56 /* We want to count in the prior stats here after all. Otherwise,
57 * nodes with positive prior will get explored _LESS_ since the
58 * urgency will be always higher; even with normal FPU because
59 * of the explore coefficient. */
61 struct ucb1_policy *b = p->data;
62 float xpl = log(node->u.playouts + node->prior.playouts) * b->explore_p;
64 // XXX: Stack overflow danger on big boards?
65 struct tree_node *nbest[512] = { node->children }; int nbests = 1;
66 float best_urgency = -9999;
68 for (struct tree_node *ni = node->children; ni; ni = ni->sibling) {
69 /* Do not consider passing early. */
70 if (likely(!allow_pass) && unlikely(is_pass(ni->coord)))
71 continue;
72 int uct_playouts = ni->u.playouts + ni->prior.playouts;
74 float urgency = b->fpu;
75 if (uct_playouts) {
76 /* prior-normal ratio. */
77 float alpha = ni->u.playouts / uct_playouts;
78 urgency = alpha * tree_node_get_value(tree, ni, u, parity)
79 + (1 - alpha) * tree_node_get_value(tree, ni, prior, parity);
80 urgency += sqrt(xpl / uct_playouts);
83 #if 0
85 struct board b2; b2.size = 9+2;
86 fprintf(stderr, "[%s -> %s] UCB1 urgency %f (%f + %f : %f)\n", coord2sstr(node->coord, &b2), coord2sstr(ni->coord, &b2), urgency, ni->u.value, sqrt(xpl / ni->u.playouts), b->fpu);
88 #endif
89 if (b->urg_randoma)
90 urgency += (float)(fast_random(b->urg_randoma) - b->urg_randoma / 2) / 1000;
91 if (b->urg_randomm)
92 urgency *= (float)(fast_random(b->urg_randomm) + 5) / b->urg_randomm;
93 if (urgency - best_urgency > __FLT_EPSILON__) { // urgency > best_urgency
94 best_urgency = urgency; nbests = 0;
96 if (urgency - best_urgency > -__FLT_EPSILON__) { // urgency >= best_urgency
97 /* We want to always choose something else than a pass
98 * in case of a tie. pass causes degenerative behaviour. */
99 if (nbests == 1 && is_pass(nbest[0]->coord)) {
100 nbests--;
102 nbest[nbests++] = ni;
105 return nbest[fast_random(nbests)];
108 void
109 ucb1_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)
111 /* It is enough to iterate by a single chain; we will
112 * update all the preceding positions properly since
113 * they had to all occur in all branches, only in
114 * different order. */
115 for (; node; node = node->parent) {
116 stats_add_result(&node->u, result > 0, 1);
121 struct uct_policy *
122 policy_ucb1_init(struct uct *u, char *arg)
124 struct uct_policy *p = calloc(1, sizeof(*p));
125 struct ucb1_policy *b = calloc(1, sizeof(*b));
126 p->uct = u;
127 p->data = b;
128 p->descend = ucb1_descend;
129 p->choose = ucb1_choose;
130 p->update = ucb1_update;
132 b->explore_p = 0.2;
133 b->fpu = 1.1; //INFINITY;
135 if (arg) {
136 char *optspec, *next = arg;
137 while (*next) {
138 optspec = next;
139 next += strcspn(next, ":");
140 if (*next) { *next++ = 0; } else { *next = 0; }
142 char *optname = optspec;
143 char *optval = strchr(optspec, '=');
144 if (optval) *optval++ = 0;
146 if (!strcasecmp(optname, "explore_p") && optval) {
147 b->explore_p = atof(optval);
148 } else if (!strcasecmp(optname, "fpu") && optval) {
149 b->fpu = atof(optval);
150 } else if (!strcasecmp(optname, "urg_randoma") && optval) {
151 b->urg_randoma = atoi(optval);
152 } else if (!strcasecmp(optname, "urg_randomm") && optval) {
153 b->urg_randomm = atoi(optval);
154 } else {
155 fprintf(stderr, "ucb1: Invalid policy argument %s or missing value\n", optname);
156 exit(1);
161 return p;