playoutp_assess(): Iterate over prior map within policy itself
[pachi.git] / uct / prior.c
blobde470a093a710e75040860925f8d612ec8ac6d50
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 "tactics.h"
12 #include "uct/internal.h"
13 #include "uct/prior.h"
14 #include "uct/tree.h"
16 /* Applying heuristic values to the tree nodes, skewing the reading in
17 * most interesting directions. */
20 void
21 uct_prior_even(struct uct *u, struct tree_node *node, struct prior_map *map)
23 /* Q_{even} */
24 /* This may be dubious for normal UCB1 but is essential for
25 * reading stability of RAVE, it appears. */
26 foreach_point_and_pass(map->b) {
27 if (!map->consider[c])
28 continue;
29 add_prior_value(map, c, u->even_eqex / 2, u->even_eqex);
30 } foreach_point_end;
33 void
34 uct_prior_eye(struct uct *u, struct tree_node *node, struct prior_map *map)
36 /* Discourage playing into our own eyes. However, we cannot
37 * completely prohibit it:
38 * #######
39 * ...XX.#
40 * XOOOXX#
41 * X.OOOO#
42 * .XXXX.# */
43 foreach_point(map->b) {
44 if (!map->consider[c])
45 continue;
46 if (!board_is_one_point_eye(map->b, &c, map->to_play))
47 continue;
48 add_prior_value(map, c, -u->eqex, u->eqex);
49 } foreach_point_end;
52 void
53 uct_prior_b19(struct uct *u, struct tree_node *node, struct prior_map *map)
55 /* Q_{b19} */
56 /* Specific hints for 19x19 board - priors for certain edge distances. */
57 foreach_point(map->b) {
58 if (!map->consider[c])
59 continue;
60 int d = coord_edge_distance(c, map->b);
61 if (d != 1 && d != 3)
62 continue;
63 /* The bonus applies only with no stones in immediate
64 * vincinity. */
65 if (board_stone_radar(map->b, c, 2))
66 continue;
67 /* First line: -eqex */
68 /* Third line: +eqex */
69 int v = d == 1 ? -1 : 1;
70 add_prior_value(map, c, v * u->b19_eqex, u->b19_eqex);
71 } foreach_point_end;
74 void
75 uct_prior_grandparent(struct uct *u, struct tree_node *node, struct prior_map *map)
77 /* Q_{grandparent} */
78 foreach_point_and_pass(map->b) {
79 if (!map->consider[c])
80 continue;
81 if (!node->parent || !node->parent->parent)
82 continue;
83 struct tree_node *gpp = node->parent->parent;
84 for (struct tree_node *ni = gpp->children; ni; ni = ni->sibling) {
85 /* Be careful not to emphasize too random results. */
86 if (ni->coord == node->coord && ni->u.playouts > u->gp_eqex) {
87 /* We purposefuly ignore the parity. */
88 map->prior[c].playouts += u->gp_eqex;
89 map->prior[c].wins += u->gp_eqex * ni->u.wins / ni->u.playouts;
92 } foreach_point_end;
95 void
96 uct_prior_playout(struct uct *u, struct tree_node *node, struct prior_map *map)
98 /* Q_{playout-policy} */
99 if (u->playout->assess)
100 u->playout->assess(u->playout, map, u->policy_eqex);
103 void
104 uct_prior(struct uct *u, struct tree_node *node, struct prior_map *map)
106 if (u->even_eqex)
107 uct_prior_even(u, node, map);
108 uct_prior_eye(u, node, map);
109 if (u->b19_eqex)
110 uct_prior_b19(u, node, map);
111 if (u->gp_eqex)
112 uct_prior_grandparent(u, node, map);
113 if (u->policy_eqex)
114 uct_prior_playout(u, node, map);