uct_search(): Move state to struct uct_search_state, setup in uct_search_start()
[pachi/derm.git] / uct / internal.h
blob701f94867b5b963d1ada9aaee2796efcbec087fb
1 #ifndef ZZGO_UCT_INTERNAL_H
2 #define ZZGO_UCT_INTERNAL_H
4 #include <signal.h> // sig_atomic_t
6 #include "debug.h"
7 #include "move.h"
8 #include "ownermap.h"
9 #include "playout.h"
10 #include "stats.h"
11 #include "distributed/distributed.h"
13 struct tree;
14 struct tree_node;
15 struct uct_policy;
16 struct uct_prior;
17 struct uct_dynkomi;
19 /* Internal UCT structures */
21 /* Distributed stats for each child of the root node. */
22 struct node_stats {
23 struct move_stats2 last_sent_own;
24 struct move_stats2 added_from_others;
25 struct tree_node *node;
28 /* Internal engine state. */
29 struct uct {
30 int debug_level;
31 int games, gamelen;
32 float resign_ratio;
33 float loss_threshold;
34 double best2_ratio, bestr_ratio;
35 bool pass_all_alive;
36 bool territory_scoring;
37 int expand_p;
38 bool playout_amaf, playout_amaf_nakade;
39 bool amaf_prior;
40 int playout_amaf_cutoff;
41 int dumpthres;
42 int force_seed;
43 bool no_book;
44 bool fast_alloc;
45 unsigned long max_tree_size;
46 int mercymin;
48 int threads;
49 enum uct_thread_model {
50 TM_ROOT, /* Root parallelization. */
51 TM_TREE, /* Tree parallelization w/o virtual loss. */
52 TM_TREEVL, /* Tree parallelization with virtual loss. */
53 } thread_model;
54 bool parallel_tree;
55 bool virtual_loss;
56 bool pondering_opt; /* User wants pondering */
57 bool pondering; /* Actually pondering now */
58 bool slave; /* Act as slave in distributed engine. */
59 enum stone my_color;
61 int fuseki_end;
62 int yose_start;
64 int dynkomi_mask;
65 int dynkomi_interval;
66 struct uct_dynkomi *dynkomi;
68 float val_scale;
69 int val_points;
70 bool val_extra;
72 int random_policy_chance;
73 int local_tree;
74 int tenuki_d;
75 float local_tree_aging;
76 bool local_tree_allseq;
77 /* Playout-localtree integration. */
78 bool local_tree_playout; // can be true only if ELO playout
79 bool local_tree_pseqroot;
81 char *banner;
83 struct uct_policy *policy;
84 struct uct_policy *random_policy;
85 struct playout_policy *playout;
86 struct uct_prior *prior;
88 /* Used within frame of single genmove. */
89 struct board_ownermap ownermap;
90 /* Used for coordination among slaves of the distributed engine. */
91 struct node_stats *stats;
92 int played_own;
93 int played_all; /* games played by all slaves */
95 /* Game state - maintained by setup_state(), reset_state(). */
96 struct tree *t;
99 #define UDEBUGL(n) DEBUGL_(u->debug_level, n)
101 extern volatile sig_atomic_t uct_halt;
102 extern __thread int thread_id;
103 extern bool thread_manager_running;
105 bool uct_pass_is_safe(struct uct *u, struct board *b, enum stone color, bool pass_all_alive);
107 void uct_prepare_move(struct uct *u, struct board *b, enum stone color);
108 void uct_search_setup(struct uct *u, struct board *b, enum stone color);
109 int uct_search(struct uct *u, struct board *b, struct time_info *ti, enum stone color, struct tree *t, bool *keep_looking);
110 struct tree_node *uct_search_best(struct uct *u, struct board *b, enum stone color, bool pass_all_alive, int played_games, int base_playouts, coord_t *best_coord);
113 /* This is the state used for descending the tree; we use this wrapper
114 * structure in order to be able to easily descend in multiple trees
115 * in parallel (e.g. main tree and local tree) or compute cummulative
116 * "path value" throughout the tree descent. */
117 struct uct_descent {
118 /* Active tree nodes: */
119 struct tree_node *node; /* Main tree. */
120 struct tree_node *lnode; /* Local tree. */
121 /* Value of main tree node (with all value factors, but unbiased
122 * - without exploration factor), from black's perspective. */
123 struct move_stats value;
127 typedef struct tree_node *(*uctp_choose)(struct uct_policy *p, struct tree_node *node, struct board *b, enum stone color, coord_t exclude);
128 typedef float (*uctp_evaluate)(struct uct_policy *p, struct tree *tree, struct uct_descent *descent, int parity);
129 typedef void (*uctp_descend)(struct uct_policy *p, struct tree *tree, struct uct_descent *descent, int parity, bool allow_pass);
130 typedef void (*uctp_winner)(struct uct_policy *p, struct tree *tree, struct uct_descent *descent);
131 typedef void (*uctp_prior)(struct uct_policy *p, struct tree *tree, struct tree_node *node, struct board *b, enum stone color, int parity);
132 typedef void (*uctp_update)(struct uct_policy *p, struct tree *tree, struct tree_node *node, enum stone node_color, enum stone player_color, struct playout_amafmap *amaf, float result);
134 struct uct_policy {
135 struct uct *uct;
136 uctp_choose choose;
137 uctp_winner winner;
138 uctp_evaluate evaluate;
139 uctp_descend descend;
140 uctp_update update;
141 uctp_prior prior;
142 bool wants_amaf;
143 void *data;
146 #endif