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