Check all memory allocations to avoid core dump when running out of memory.
[pachi.git] / uct / policy / generic.h
blob73b6163bdfea64d10eb701df6983f3d266949d3b
1 #ifndef ZZGO_UCT_POLICY_GENERIC_H
2 #define ZZGO_UCT_POLICY_GENERIC_H
4 /* Some default policy routines and templates. */
6 #include "stone.h"
7 #include "uct/internal.h"
9 struct board;
10 struct tree_node;
12 struct tree_node *uctp_generic_choose(struct uct_policy *p, struct tree_node *node, struct board *b, enum stone color, coord_t exclude);
13 void uctp_generic_winner(struct uct_policy *p, struct tree *tree, struct uct_descent *descent);
16 /* Some generic stitching for tree descent. */
18 #define uctd_try_node_children(tree, descent, allow_pass, parity, tenuki_d, di, urgency) \
19 /* Information abound best children. */ \
20 /* XXX: Stack overflow danger on big boards? */ \
21 struct uct_descent dbest[512] = { { .node = descent->node->children, .lnode = NULL } }; int dbests = 1; \
22 float best_urgency = -9999; \
23 /* Descent children iterator. */ \
24 struct uct_descent dci = { .node = descent->node->children, .lnode = descent->lnode ? descent->lnode->children : NULL }; \
26 for (; dci.node; dci.node = dci.node->sibling) { \
27 float urgency; \
28 /* Do not consider passing early. */ \
29 if (unlikely((!allow_pass && is_pass(dci.node->coord)) || (dci.node->hints & TREE_HINT_INVALID))) \
30 continue; \
31 /* Position dci.lnode to point at or right after the local
32 * node corresponding to dci.node. */ \
33 while (dci.lnode && dci.lnode->coord < dci.node->coord) \
34 dci.lnode = dci.lnode->sibling; \
35 /* Set up descent-further iterator. This is the public-accessible
36 * one, and usually is similar to dci. However, in case of local
37 * trees, we may keep next-candidate pointer in dci while storing
38 * actual-specimen in di. */ \
39 struct uct_descent di = dci; \
40 if (dci.lnode) { \
41 /* Set lnode to local tree node corresponding
42 * to node (dci.lnode, pass-lnode or NULL). */ \
43 di.lnode = tree_lnode_for_node(tree, dci.node, dci.lnode, tenuki_d); \
46 /* ...your urgency computation code goes here... */
48 #define uctd_set_best_child(di, urgency) \
49 if (urgency - best_urgency > __FLT_EPSILON__) { /* urgency > best_urgency */ \
50 best_urgency = urgency; dbests = 0; \
51 } \
52 if (urgency - best_urgency > -__FLT_EPSILON__) { /* urgency >= best_urgency */ \
53 /* We want to always choose something else than a pass \
54 * in case of a tie. pass causes degenerative behaviour. */ \
55 if (dbests == 1 && is_pass(dbest[0].node->coord)) { \
56 dbests--; \
57 } \
58 struct uct_descent db = di; \
59 /* Make sure lnode information is meaningful. */ \
60 if (db.lnode && is_pass(db.lnode->coord)) \
61 db.lnode = NULL; \
62 dbest[dbests++] = db; \
63 } \
66 #define uctd_get_best_child(descent) *(descent) = dbest[fast_random(dbests)];
69 #endif