UCT node.coord: Access using node_coord()
[pachi/t.git] / uct / policy / generic.c
blob0f1621b81e0518840898386dd48c21c50906f723
1 #include <assert.h>
2 #include <math.h>
3 #include <stdio.h>
4 #include <stdlib.h>
6 #include "board.h"
7 #include "debug.h"
8 #include "move.h"
9 #include "tactics/util.h"
10 #include "random.h"
11 #include "uct/internal.h"
12 #include "uct/tree.h"
13 #include "uct/policy/generic.h"
15 struct tree_node *
16 uctp_generic_choose(struct uct_policy *p, struct tree_node *node, struct board *b, enum stone color, coord_t exclude)
18 struct tree_node *nbest = NULL;
19 /* This function is called while the tree is updated by other threads.
20 * We rely on node->children being set only after the node has been fully expanded. */
21 for (struct tree_node *ni = node->children; ni; ni = ni->sibling)
22 // we compare playouts and choose the best-explored
23 // child; comparing values is more brittle
24 if (!nbest || ni->u.playouts > nbest->u.playouts) {
25 if (node_coord(ni) == exclude)
26 continue;
27 if (ni->hints & TREE_HINT_INVALID)
28 continue;
29 /* Play pass only if we can afford scoring */
30 if (is_pass(node_coord(ni)) && !uct_pass_is_safe(p->uct, b, color, p->uct->pass_all_alive))
31 continue;
32 nbest = ni;
34 return nbest;
37 /* Return the node with best value instead of best explored. We must use the heuristic
38 * value (using prior and possibly rave), because the raw value is meaningless for
39 * nodes evaluated rarely.
40 * This function is called while the tree is updated by other threads */
41 void
42 uctp_generic_winner(struct uct_policy *p, struct tree *tree, struct uct_descent *descent)
44 if (!p->evaluate)
45 return;
46 bool allow_pass = false; /* At worst forces some extra playouts at the end */
47 int parity = tree_node_parity(tree, descent->node);
49 uctd_try_node_children(tree, descent, allow_pass, parity, p->uct->tenuki_d, di, urgency) {
50 urgency = p->evaluate(p, tree, &di, parity);
51 } uctd_set_best_child(di, urgency);
53 uctd_get_best_child(descent);