uctp_generic_choose: fix determination of the second best move
[pachi.git] / uct / policy / generic.c
blob2e5df6f7dbe4df66f0498a4a27145565a72550fc
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 = node->children;
19 if (!nbest) return NULL;
20 struct tree_node *nbest2 = nbest->sibling;
22 /* This function is called while the tree is updated by other threads.
23 * We rely on node->children being set only after the node has been fully expanded. */
24 for (struct tree_node *ni = nbest2; ni; ni = ni->sibling) {
25 // we compare playouts and choose the best-explored
26 // child; comparing values is more brittle
27 if (node_coord(ni) == exclude || ni->hints & TREE_HINT_INVALID)
28 continue;
29 if (ni->u.playouts > nbest->u.playouts) {
30 nbest2 = nbest;
31 nbest = ni;
32 } else if (ni->u.playouts > nbest2->u.playouts) {
33 nbest2 = ni;
36 /* Play pass only if we can afford scoring. Call expensive uct_pass_is_safe() only if
37 * pass is indeed the best move. */
38 if (is_pass(node_coord(nbest)) && !uct_pass_is_safe(p->uct, b, color, p->uct->pass_all_alive))
39 return nbest2;
40 return nbest;
43 /* Return the node with best value instead of best explored. We must use the heuristic
44 * value (using prior and possibly rave), because the raw value is meaningless for
45 * nodes evaluated rarely.
46 * This function is called while the tree is updated by other threads */
47 void
48 uctp_generic_winner(struct uct_policy *p, struct tree *tree, struct uct_descent *descent)
50 if (!p->evaluate)
51 return;
52 bool allow_pass = false; /* At worst forces some extra playouts at the end */
53 int parity = tree_node_parity(tree, descent->node);
55 uctd_try_node_children(tree, descent, allow_pass, parity, p->uct->tenuki_d, di, urgency) {
56 urgency = p->evaluate(p, tree, &di, parity);
57 } uctd_set_best_child(di, urgency);
59 uctd_get_best_child(descent);