search.h
Go to the documentation of this file.
1 
11 #ifndef EDAX_SEARCH_H
12 #define EDAX_SEARCH_H
13 
14 #include "board.h"
15 #include "const.h"
16 #include "empty.h"
17 #include "eval.h"
18 #include "hash.h"
19 #include "move.h"
20 #include "util.h"
21 
22 #include <stdio.h>
23 
25 typedef struct Selectivity {
26  double t;
27  int level;
28  int percent;
29 } Selectivity;
30 
31 struct Task;
32 struct TaskQueue;
33 
35 typedef struct Bound {
36  int lower;
37  int upper;
38 } Bound;
39 
41 typedef struct Result {
42  int depth;
44  int move;
45  int score;
47  Line pv[1];
48  long long time;
49  unsigned long long n_nodes;
50  bool book_move;
51  int n_moves;
53  SpinLock spin;
54 } Result;
55 
62 typedef struct Hint {
63  int depth;
65  int move;
66  int score;
67  int upper;
68  int lower;
69  Line pv[1];
70  long long time;
71  unsigned long long n_nodes;
72  bool book_move;
73 } Hint;
74 
81 typedef struct HintList {
83  int n_hints;
84 } HintList;
85 
86 
88 extern struct Level {
89  int depth;
91 } LEVEL[61][61];
92 
93 
95 typedef struct Search {
96  Board board[1];
99  int n_empties;
100  int player;
101  int id;
106  Eval eval[1];
109  struct TaskStack *tasks;
110  struct Task *task;
111  SpinLock spin;
112  struct Search *parent;
114  struct Search *master;
115  volatile int n_child;
117  int depth;
120  unsigned int parity;
122  volatile Stop stop;
124  struct {
125  long long extra;
126  volatile long long spent;
127  bool extended;
128  bool can_update;
129  long long mini;
130  long long maxi;
131  } time;
133  int height;
137  struct {
138  int depth;
139  int selectivity;
140  long long time;
142  int verbosity;
143  bool keep_date;
144  const char *header;
145  const char *separator;
146  bool guess_pv;
148  int hash_size;
149  } options;
153  void (*observer)(Result*);
155  volatile unsigned long long n_nodes;
156  volatile unsigned long long child_nodes;
158 } Search;
159 
160 struct Node;
161 
162 extern const int QUADRANT_ID[];
163 extern const Selectivity selectivity_table[];
164 extern const int NO_SELECTIVITY;
165 extern const int NWS_STABILITY_THRESHOLD[];
166 extern const int PVS_STABILITY_THRESHOLD[];
167 extern const int SQUARE_TYPE[];
168 
169 /* function definition */
170 void search_global_init(void);
171 void search_init(Search*);
172 void search_free(Search*);
173 void search_cleanup(Search*);
174 void search_setup(Search*);
175 void search_clone(Search*, Search*);
176 void search_set_board(Search*, const Board*, const int);
177 void search_set_level(Search*, const int, const int);
178 void search_set_ponder_level(Search*, const int, const int);
180 
181 void search_set_game_time(Search*, const long long);
182 void search_set_move_time(Search*, const long long);
183 void search_time_init(Search*);
184 void search_time_reset(Search*, const Board*);
185 void search_adjust_time(Search*, const bool);
186 bool search_continue(Search *);
188 
189 void search_set_task_number(Search*, const int);
190 
191 void search_swap_parity(Search*, const int);
192 void search_get_movelist(const Search*, MoveList*);
193 void search_update_endgame(Search*, const Move*);
194 void search_restore_endgame(Search*, const Move*);
196 void search_update_midgame(Search*, const Move*);
197 void search_restore_midgame(Search*, const Move*);
200 long long search_clock(Search*);
201 long long search_time(Search*);
202 unsigned long long search_count_nodes(Search*);
203 void search_print_pv(Search*, const int, const char*, FILE*);
204 void search_print(Search*, const int, const int, const char, FILE*);
205 int get_pv_extension(const int, const int);
206 
207 void result_print(Result*, FILE*);
208 
209 bool search_SC_PVS(Search*, volatile int*, volatile int*, int*);
210 bool search_SC_NWS(Search*, const int, int*);
211 bool search_TC_PVS(HashData*, const int, const int, volatile int*, volatile int*, int*);
212 bool search_TC_NWS(HashData*, const int, const int, const int, int*);
213 bool search_ETC_PVS(Search*, MoveList*, unsigned long long, const int, const int, volatile int*, volatile int*, int*);
214 bool search_ETC_NWS(Search*, MoveList*, unsigned long long, const int, const int, const int, int*);
215 
216 NodeType next_node_type(const NodeType parent, const bool first_move);
217 
218 int search_solve(const Search*);
219 int search_solve_0(const Search*);
220 extern int board_score_1(const Board*, const int, const int);
221 int NWS_endgame(Search*, const int);
222 
223 int search_eval_0(Search*);
224 int search_eval_1(Search*, const int, int);
225 int search_eval_2(Search*, int, const int);
226 int NWS_midgame(Search*, const int, int, struct Node*);
227 int PVS_midgame(Search*, const int, const int, int, struct Node*);
228 int NWS_shallow(Search*, const int, int, HashTable*);
229 int PVS_shallow(Search*, int, int, int);
230 
231 bool is_pv_ok(Search*, int, int);
232 void record_best_move(Search*, const Board*, const Move*, const int, const int, const int);
233 int PVS_root(Search*, const int, const int, const int);
234 int aspiration_search(Search*, int, int, const int, int);
235 void iterative_deepening(Search*, int, int);
236 void* search_run(void*);
237 int search_guess(Search*, const Board*);
238 void search_stop_all(Search*, const Stop);
239 void search_set_state(Search*, const Stop);
240 
241 void search_observer(Result*);
242 void search_set_observer(Search*, void (*Observer)(Result*));
243 
244 void search_share(const Search*, Search*);
245 int search_count_tasks(const Search *);
246 
247 bool is_depth_solving(const int, const int);
248 int solvable_depth(const long long, int);
249 void pv_debug(Search*, const Move*, FILE*);
251 void show_current_move(FILE *f, Search*, const Move*, const int, const int, const bool);
252 int search_bound(const Search*, int);
253 
254 #endif
255 
void record_best_move(Search *, const Board *, const Move *, const int, const int, const int)
Record best move.
Definition: root.c:150
bool search_SC_NWS(Search *, const int, int *)
Stability Cutoff (TC).
Definition: search.c:1170
void search_cleanup(Search *)
Clean-up some search data.
Definition: search.c:578
bool search_TC_PVS(HashData *, const int, const int, volatile int *, volatile int *, int *)
Transposition Cutoff (TC).
Definition: search.c:1196
int get_pv_extension(const int, const int)
Compute the pv_extension.
Definition: search.c:1012
unsigned long long n_nodes
Definition: search.h:49
int depth
Definition: search.h:63
struct Search Search
int n_hints
Definition: search.h:83
Result * result
Definition: search.h:151
#define MAX_THREADS
Definition: const.h:15
MoveList movelist[1]
Definition: search.h:132
bool search_TC_NWS(HashData *, const int, const int, const int, int *)
Transposition Cutoff (TC).
Definition: search.c:1230
Definition: hash.h:47
Definition: util.h:87
volatile unsigned long long child_nodes
Definition: search.h:156
SpinLock spin
Definition: search.h:111
int probcut_level
Definition: search.h:119
long long maxi
Definition: search.h:130
int height
Definition: search.h:133
struct Search * parent
Definition: search.h:112
int search_eval_2(Search *, int, const int)
Evaluate a position at depth 2.
Definition: midgame.c:147
int verbosity
Definition: search.h:142
void search_stop_all(Search *, const Stop)
Stop the search.
Definition: search.c:1335
struct Hint Hint
const int NO_SELECTIVITY
Definition: search.c:94
void show_current_move(FILE *f, Search *, const Move *, const int, const int, const bool)
Definition: root.c:239
long long time
Definition: search.h:70
void search_restore_midgame(Search *, const Move *)
Restore the search state as before a move.
Definition: search.c:964
int move
Definition: search.h:44
struct Level LEVEL[61][61]
#define BOARD_SIZE
Definition: const.h:21
long long extra
Definition: search.h:125
Definition: search.h:88
void search_init(Search *)
Init the main search.
Definition: search.c:351
const Selectivity selectivity_table[]
Definition: search.c:97
long long time
Definition: search.h:140
Definition: move.h:20
HashTable shallow_table[1]
Definition: search.h:105
void search_print_pv(Search *, const int, const char *, FILE *)
int selectivity
Definition: search.h:43
void search_adjust_time(Search *, const bool)
Give more time.
Definition: search.c:771
Definition: ybwc.h:48
int PVS_root(Search *, const int, const int, const int)
Principal Variation Search algorithm at the root of the tree.
Definition: root.c:330
int NWS_endgame(Search *, const int)
Evaluate an endgame position with a Null Window Search algorithm.
Definition: endgame.c:449
Board board[1]
Definition: search.h:96
const int QUADRANT_ID[]
Definition: search.c:81
void search_set_state(Search *, const Stop)
Set the search running/waiting state.
Definition: search.c:1353
bool search_SC_PVS(Search *, volatile int *, volatile int *, int *)
Stability Cutoff (SC).
Definition: search.c:1146
void search_update_pass_midgame(Search *)
Update the search state after a passing move.
Definition: search.c:983
long long mini
Definition: search.h:129
void search_print(Search *, const int, const int, const char, FILE *)
struct Selectivity Selectivity
int search_get_pv_cost(Search *)
Compute a cost as a combination of node count, depth, etc. from hash_table.
Definition: root.c:303
void search_pass_endgame(Search *)
Update the search state after a passing move.
Definition: search.c:928
struct Task * task
Definition: search.h:110
bool search_continue(Search *)
Check if it can iterate more...
Definition: search.c:790
int NWS_midgame(Search *, const int, int, struct Node *)
Evaluate a midgame position with a Null Window Search algorithm.
Definition: midgame.c:473
int percent
Definition: search.h:28
HashTable hash_table[1]
Definition: search.h:103
long long time
Definition: search.h:48
Definition: board.h:26
Random random[1]
Definition: search.h:107
bool guess_pv
Definition: search.h:146
int n_moves
Definition: search.h:51
int lower
Definition: search.h:36
struct Search * search
Definition: ybwc.h:59
unsigned int parity
Definition: search.h:120
void search_update_endgame(Search *, const Move *)
Update the search state after a move.
Definition: search.c:900
struct Search::@25 options
void search_global_init(void)
Definition: search.c:151
Stop
Definition: const.h:70
int search_count_tasks(const Search *)
Count the number of tasks used in parallel search.
Definition: search.c:1324
int search_bound(const Search *, int)
bound root scores according to stable squares
Definition: root.c:253
int score
Definition: search.h:45
int PVS_shallow(Search *, int, int, int)
Evaluate a midgame position at shallow depth.
Definition: midgame.c:381
evaluation function
Definition: eval.h:18
bool is_pv_ok(Search *, int, int)
Check if PV is ok.
Definition: root.c:83
Definition: search.h:81
int player
Definition: search.h:100
long long search_clock(Search *)
Return the time spent by the search.
Definition: search.c:1049
struct TaskStack * tasks
Definition: search.h:109
int n_moves_left
Definition: search.h:52
int depth
Definition: search.h:117
void search_free(Search *)
Free the search allocated ressource.
Definition: search.c:441
int board_score_1(const Board *, const int, const int)
Get the final score.
Definition: endgame.c:96
int multipv_depth
Definition: search.h:147
int id
Definition: search.h:101
void search_get_movelist(const Search *, MoveList *)
Get a list of legal moves.
Definition: search.c:875
Line pv[1]
Definition: search.h:47
const int PVS_STABILITY_THRESHOLD[]
Definition: search.c:120
bool allow_node_splitting
Definition: search.h:123
int upper
Definition: search.h:37
Definition: search.h:95
bool is_depth_solving(const int, const int)
Check if final score use pv_extension or is solved.
Definition: search.c:1032
NodeType
Definition: const.h:80
void search_set_level(Search *, const int, const int)
Set the search level.
Definition: search.c:609
SquareList empties[BOARD_SIZE+2]
Definition: search.h:97
Definition: search.h:62
Miscellaneous utilities header.
void search_time_init(Search *)
Initialize the alloted time.
Definition: search.c:697
int search_guess(Search *, const Board *)
Guess the bestmove of a given board.
Definition: search.c:1369
void search_set_board(Search *, const Board *, const int)
Set the board to analyze.
Definition: search.c:593
volatile int n_child
Definition: search.h:115
Definition: search.h:41
int search_solve(const Search *)
Get the final score.
Definition: endgame.c:52
void result_print(Result *, FILE *)
Print the current search result.
Definition: search.c:1106
bool keep_date
Definition: search.h:143
int PVS_midgame(Search *, const int, const int, int, struct Node *)
Evaluate a position with a deep Principal Variation Search algorithm.
Definition: midgame.c:585
int search_eval_0(Search *)
evaluate a midgame position with the evaluation function.
Definition: midgame.c:32
struct Search * child[MAX_THREADS]
Definition: search.h:113
Definition: move.h:35
double t
Definition: search.h:26
Eval eval[1]
Definition: search.h:106
int upper
Definition: search.h:67
struct Bound Bound
struct Search * master
Definition: search.h:114
int depth
Definition: search.h:42
unsigned long long n_nodes
Definition: search.h:71
int move
Definition: search.h:65
bool book_move
Definition: search.h:72
void search_time_reset(Search *, const Board *)
Reset the alloted time.
Definition: search.c:732
Definition: hash.h:24
SquareList * x_to_empties[BOARD_SIZE+2]
Definition: search.h:98
void search_restore_endgame(Search *, const Move *)
Restore the search state as before a move.
Definition: search.c:915
int NWS_shallow(Search *, const int, int, HashTable *)
Evaluate a midgame position with a Null Window Search algorithm.
Definition: midgame.c:300
int depth
Definition: search.h:89
int n_empties
Definition: search.h:99
void search_setup(Search *)
Set up various structure once the board has been set.
Definition: search.c:466
int selectivity
Definition: search.h:64
const char * separator
Definition: search.h:145
NodeType next_node_type(const NodeType parent, const bool first_move)
void iterative_deepening(Search *, int, int)
Iterative deepening.
Definition: root.c:621
long long search_time(Search *)
Return the time spent by the search.
Definition: search.c:1061
const int SQUARE_TYPE[]
Definition: search.c:132
void search_clone(Search *, Search *)
Clone a search for parallel search.
Definition: search.c:540
void * search_run(void *)
Search the bestmove of a given board.
Definition: root.c:810
struct Result Result
bool extended
Definition: search.h:127
SpinLock spin
Definition: search.h:53
void search_observer(Result *)
default observer.
Definition: search.c:1083
int score
Definition: search.h:66
void pv_debug(Search *, const Move *, FILE *)
Debug PV.
Definition: root.c:33
#define GAME_SIZE
Definition: const.h:25
int search_solve_0(const Search *)
Get the final score.
Definition: endgame.c:80
bool search_ETC_NWS(Search *, MoveList *, unsigned long long, const int, const int, const int, int *)
Enhanced Transposition Cutoff (ETC).
Definition: search.c:1264
void search_resize_hashtable(Search *)
Definition: search.c:333
int hash_size
Definition: search.h:148
int search_eval_1(Search *, const int, int)
Evaluate a position at depth 1.
Definition: midgame.c:74
const char * header
Definition: search.h:144
volatile Stop stop
Definition: search.h:122
int selectivity
Definition: search.h:118
Line pv[1]
Definition: search.h:69
Definition: ybwc.h:93
NodeType node_type[GAME_SIZE]
Definition: search.h:134
void search_set_ponder_level(Search *, const int, const int)
Set the search level while pondering.
Definition: search.c:629
void(* observer)(Result *)
Definition: search.h:153
struct Search::@24 time
void search_set_move_time(Search *, const long long)
set time to search.
Definition: search.c:686
int lower
Definition: search.h:68
int solvable_depth(const long long, int)
Compute the deepest level that can be solved given a limited time...
Definition: search.c:650
bool search_ETC_PVS(Search *, MoveList *, unsigned long long, const int, const int, volatile int *, volatile int *, int *)
bool time_per_move
Definition: search.h:141
Definition: search.h:25
const int NWS_STABILITY_THRESHOLD[]
Definition: search.c:108
Definition: move.h:29
void search_share(const Search *, Search *)
Share search information.
Definition: search.c:1312
#define MAX_MOVE
Definition: const.h:18
int aspiration_search(Search *, int, int, const int, int)
Aspiration window.
Definition: root.c:451
void search_swap_parity(Search *, const int)
Change parity.
Definition: search.c:860
Definition: ybwc.h:29
void search_check_timeout(Search *search)
Check if it can iterate more...
Definition: search.c:800
volatile unsigned long long n_nodes
Definition: search.h:155
struct HintList HintList
bool book_move
Definition: search.h:50
Hint hint[MAX_MOVE+2]
Definition: search.h:82
int depth_pv_extension
Definition: search.h:121
unsigned long long search_count_nodes(Search *)
Return the number of nodes searched.
Definition: search.c:1073
void search_update_midgame(Search *, const Move *)
Update the search state after a move.
Definition: search.c:942
int selectivity
Definition: search.h:90
void search_restore_pass_midgame(Search *)
Update the search state after a passing move.
Definition: search.c:998
void search_set_task_number(Search *, const int)
Change the number of task.
Definition: search.c:847
void search_set_game_time(Search *, const long long)
set time to search.
Definition: search.c:671
Bound bound[BOARD_SIZE+2]
Definition: search.h:46
int level
Definition: search.h:27
Definition: search.h:35
bool can_update
Definition: search.h:128
Bound stability_bound
Definition: search.h:135
Definition: empty.h:15
void search_set_observer(Search *, void(*Observer)(Result *))
set observer.
Definition: search.c:1095
HashTable pv_table[1]
Definition: search.h:104
volatile long long spent
Definition: search.h:126
struct Node * parent
Definition: ybwc.h:61