38 unsigned long long hash_code;
45 *board = *search->
board;
63 x = hash_data->
move[0];
66 x = hash_data->
move[0];
72 spin_unlock(search->
result);
88 unsigned long long hash_code;
91 *board = *search->
board;
94 while (search_depth > 0 && x !=
NOMOVE) {
95 if (x !=
PASS) --search_depth;
101 x = hash_data->
move[0];
103 x = hash_data->
move[0];
137 return hash_data->
move[0];
156 unsigned long long hash_code;
162 int expected_depth, expected_selectivity, tmp;
163 Bound expected_bound;
165 *board = *init_board;
171 result->
move = bestmove->
x;
183 expected_depth = result->
depth = depth;
185 expected_bound = *bound;
191 fail_low = (bestmove->
score <= alpha);
198 tmp = expected_bound.
upper; expected_bound.
upper = -expected_bound.
lower; expected_bound.
lower = -tmp;
199 fail_low = !fail_low;
204 && (hash_data->
depth >= expected_depth && hash_data->
selectivity >= expected_selectivity)
206 x = hash_data->
move[0];
242 fprintf(f,
"current move: %s [%+03d, %+03d], %s => %+03d; ", (parallel ?
" // ":
" -- "), alpha, beta,
move_to_string(move->
x,
BLACK, s), move->
score);
274 assert(alpha < beta);
277 assert(depth >= 0 && depth <= search->n_empties);
281 else score =
PVS_midgame(search, alpha, beta, depth, node);
284 else if (depth == 1) score =
search_eval_1(search, alpha, beta);
285 else if (depth == 2) score =
search_eval_2(search, alpha, beta);
286 else score =
PVS_midgame(search, alpha, beta, depth, node);
338 unsigned long long hash_code;
339 assert(alpha < beta);
342 assert(depth > 0 && depth <= search->n_empties);
399 if (alpha < move->score && move->
score < beta) {
453 int low, high, left, right;
460 assert(alpha < beta);
464 assert(depth >= 0 && depth <= search->n_empties);
466 log_print(
xboard_log,
"edax (search)> search [%d, %d] %d (%d)\n", alpha, beta, depth, score);
469 if (alpha & 1) --alpha;
470 if (beta & 1) ++beta;
474 if (depth <= search->
options.multipv_depth) {
482 if (alpha < low) alpha = low;
483 if (beta > high) beta = high;
484 if (score < low) score = low;
else if (score > high) score = high;
485 if (score < alpha) score = alpha;
else if (score > beta) score = beta;
493 width = 10 - depth;
if (width < 1) width = 1;
494 if ((width & 1) && depth == search->
n_empties) ++width;
496 for (i = 0; i < 10; ++i) {
500 if (depth <= search->
options.multipv_depth || beta - alpha <= 2 * width) {
502 score =
PVS_root(search, alpha, beta, depth);
504 left = right = (i <= 0 ? 1 : i) * width;
506 low = score - left;
if (low < alpha) low = alpha;
507 high = score + right;
if (high > beta) high = beta;
508 if (low >= high)
break;
513 score =
PVS_root(search, low, high, depth);
515 if (search->
stop)
break;
517 if (score <= low && score > alpha && left > 0) {
520 }
else if (score >= high && score < beta && right > 0) {
527 if (search->
stop)
break;
531 && ((alpha < score && score < beta) || (score == alpha && score ==
options.
alpha) || (score == beta && score ==
options.
beta))
548 log_print(
search_log,
"*** UNEXPECTED ODD SCORE (score=%+d) => re-research id %d ***\n", score, search->
id);
552 if (score == old_score)
break;
577 unsigned long long hash_code;
581 *board = *search->
board;
583 *depth = *selectivity = -1;
585 for (i = 0; i < 4; ++i) {
588 x = hash_data->
move[0];
590 x = hash_data->
move[0];
593 d = hash_data->
depth + i;
596 if (d > *depth) *depth = d;
597 if (s > *selectivity) *selectivity = s;
605 return *depth > -1 && *selectivity > -1;
626 Move *bestmove, *move;
628 int score, end, start;
631 int old_depth, old_selectivity, tmp_selectivity;
633 assert(alpha < beta);
663 if (end <= 0) end = 2 - (search->
n_empties & 1);
665 start = 6 - (end & 1);
666 if (start > end - 2) start = end - 2;
667 if (start <= 0) start = 2 - (end & 1);
672 old_depth = 0; old_selectivity = search->
selectivity;
683 info(
"<hash: value = [%+02d, %+02d] ; bestmove = %s, %s ; level = %d@%d%% ; date = %d ; cost = %d>\n",
694 old_depth = hash_data->
depth;
703 score = hash_data->
lower;
719 if (start < search->n_empties) {
720 if ((start & 1) != (end & 1)) ++start;
721 if (start <= 0) start = 2 - (end & 1);
722 if (start > end) start = end;
747 bestmove = &pass; bestmove->
score = score;
879 assert(search->
height == 0);
DLL_API int last_bit(unsigned long long b)
Search the last bit set (same as log2()).
Definition bit.c:264
bool board_check_move(const Board *board, Move *move)
Check if a move is legal.
Definition board.c:452
unsigned long long board_get_hash_code(const Board *board)
Compute a hash code.
Definition board.c:1134
void board_restore(Board *board, const Move *move)
Restore a board.
Definition board.c:487
bool board_is_game_over(const Board *board)
Check if the game is over.
Definition board.c:1203
void board_update(Board *board, const Move *move)
Update a board.
Definition board.c:469
unsigned long long board_get_move(const Board *board, const int x, Move *move)
Compute a move.
Definition board.c:438
int get_stability(const unsigned long long P, const unsigned long long O)
Estimate the stability.
Definition board.c:1067
int get_mobility(const unsigned long long P, const unsigned long long O)
Count legal moves.
Definition board.c:833
DLL_API bool can_move(const unsigned long long P, const unsigned long long O)
Check if a player can move.
Definition board.c:797
#define SCORE_MAX
Definition const.h:58
#define SCORE_INF
Definition const.h:52
@ PASS
Definition const.h:37
@ NOMOVE
Definition const.h:37
@ BLACK
Definition const.h:42
@ RUNNING
Definition const.h:71
@ STOP_ON_DEMAND
Definition const.h:75
@ STOP_TIMEOUT
Definition const.h:74
@ STOP_PARALLEL_SEARCH
Definition const.h:72
@ STOP_END
Definition const.h:76
@ STOP_PONDERING
Definition const.h:73
@ PV_NODE
Definition const.h:81
#define SCORE_MIN
Definition const.h:55
int search_solve(const Search *search)
Get the final score.
Definition endgame.c:52
int search_solve_0(const Search *search)
Get the final score.
Definition endgame.c:80
bool hash_get(HashTable *hash_table, const unsigned long long hash_code, HashData *data)
Find an hash table entry according to the evaluated board hash codes.
Definition hash-lock-free.c:600
void hash_clear(HashTable *hash_table)
Clear the hashtable.
Definition hash-lock-free.c:179
unsigned int writeable_level(HashData *data)
make a level from date, cost, depth & selectivity.
Definition hash-lock-free.c:208
void hash_print(const HashData *data, FILE *f)
print HashData content.
Definition hash-lock-free.c:670
void hash_store(HashTable *hash_table, const unsigned long long hash_code, const int depth, const int selectivity, const int cost, const int alpha, const int beta, const int score, const int move)
Store an hashtable item.
Definition hash-lock-free.c:543
void hash_force(HashTable *hash_table, const unsigned long long hash_code, const int depth, const int selectivity, const int cost, const int alpha, const int beta, const int score, const int move)
Store an hashtable item.
Definition hash-lock-free.c:575
int search_eval_2(Search *search, int alpha, const int beta)
Evaluate a position at depth 2.
Definition midgame.c:147
int search_eval_0(Search *search)
evaluate a midgame position with the evaluation function.
Definition midgame.c:32
int PVS_midgame(Search *search, const int alpha, const int beta, int depth, Node *parent)
Evaluate a position with a deep Principal Variation Search algorithm.
Definition midgame.c:585
int PVS_shallow(Search *search, int alpha, int beta, int depth)
Evaluate a midgame position at shallow depth.
Definition midgame.c:381
int search_eval_1(Search *search, const int alpha, int beta)
Evaluate a position at depth 1.
Definition midgame.c:74
void line_print(const Line *line, int width, const char *separator, FILE *f)
Print a move sequence.
Definition move.c:610
bool movelist_is_empty(const MoveList *movelist)
Check if the list is empty.
Definition move.c:537
void line_push(Line *line, const int x)
Add a move to the sequence.
Definition move.c:561
const Move MOVE_PASS
Definition move.c:26
void movelist_sort(MoveList *movelist)
Sort all moves.
Definition move.c:505
Move * movelist_first(MoveList *movelist)
Return the first move of the list.
Definition move.c:422
void line_init(Line *line, const int player)
Initialize a sequence of moves.
Definition move.c:549
void movelist_sort_cost(MoveList *movelist, const HashData *hash_data)
Sort all moves except the first, based on move cost & hash_table storage.
Definition move.c:489
void movelist_evaluate(MoveList *movelist, Search *search, const HashData *hash_data, const int alpha, const int depth)
Evaluate a list of move in order to sort it.
Definition move.c:436
char * move_to_string(const int x, const int player, char *s)
Print out a move.
Definition move.c:76
Move * movelist_sort_bestmove(MoveList *movelist, const int move)
Sort a move as best.
Definition move.c:468
#define foreach_move(iter, movelist)
Definition move.h:78
Options options
Definition options.c:22
void iterative_deepening(Search *search, int alpha, int beta)
Iterative deepening.
Definition root.c:621
int PVS_root(Search *search, const int alpha, const int beta, const int depth)
Principal Variation Search algorithm at the root of the tree.
Definition root.c:330
int search_get_pv_cost(Search *search)
Compute a cost as a combination of node count, depth, etc. from hash_table.
Definition root.c:303
Log engine_log[1]
Definition cassio.c:35
static int guess_move(Search *search, Board *board)
Guess a move.
Definition root.c:123
Log search_log[1]
Definition search.c:74
static bool get_last_level(Search *search, int *depth, int *selectivity)
Retrieve the last level of the search.
Definition root.c:572
void pv_debug(Search *search, const Move *bestmove, FILE *f)
Debug PV.
Definition root.c:33
void show_current_move(FILE *f, Search *search, const Move *move, const int alpha, const int beta, const bool parallel)
Definition root.c:239
bool is_pv_ok(Search *search, int bestmove, int search_depth)
Check if PV is ok.
Definition root.c:83
static int search_route_PVS(Search *search, int alpha, int beta, const int depth, Node *node)
Reroute the PVS between midgame,endgame or terminal PVS.
Definition root.c:270
int search_bound(const Search *search, int score)
bound root scores according to stable squares
Definition root.c:253
int aspiration_search(Search *search, int alpha, int beta, const int depth, int score)
Aspiration window.
Definition root.c:451
void record_best_move(Search *search, const Board *init_board, const Move *bestmove, const int alpha, const int beta, const int depth)
Record best move.
Definition root.c:150
void * search_run(void *v)
Search the bestmove of a given board.
Definition root.c:810
void search_time_init(Search *search)
Initialize the alloted time.
Definition search.c:697
const int NO_SELECTIVITY
Definition search.c:94
bool search_continue(Search *search)
Check if it can iterate more...
Definition search.c:790
const Selectivity selectivity_table[]
Definition search.c:97
long long search_clock(Search *search)
Return the time spent by the search.
Definition search.c:1049
void search_restore_pass_midgame(Search *search)
Update the search state after a passing move.
Definition search.c:998
unsigned long long search_count_nodes(Search *search)
Return the number of nodes searched.
Definition search.c:1073
int get_pv_extension(const int depth, const int n_empties)
Compute the pv_extension.
Definition search.c:1012
void search_adjust_time(Search *search, const bool once)
Give more time.
Definition search.c:771
bool is_depth_solving(const int depth, const int n_empties)
Check if final score use pv_extension or is solved.
Definition search.c:1032
void search_restore_midgame(Search *search, const Move *move)
Restore the search state as before a move.
Definition search.c:964
void search_update_midgame(Search *search, const Move *move)
Update the search state after a move.
Definition search.c:942
int solvable_depth(const long long limit, int n_tasks)
Compute the deepest level that can be solved given a limited time...
Definition search.c:650
int search_count_tasks(const Search *search)
Count the number of tasks used in parallel search.
Definition search.c:1324
void search_setup(Search *search)
Set up various structure once the board has been set.
Definition search.c:466
void search_update_pass_midgame(Search *search)
Update the search state after a passing move.
Definition search.c:983
long long search_time(Search *search)
Return the time spent by the search.
Definition search.c:1061
#define ITERATIVE_MIN_EMPTIES
Definition settings.h:89
#define USE_PROBCUT
Definition settings.h:36
#define USE_PREVIOUS_SEARCH
Definition settings.h:62
void statistics_sum_nodes(Search *search)
Cumulate node counts from the last search.
Definition stats.c:97
Statistics statistics
Definition stats.c:21
void statistics_print(FILE *f)
Print statistics.
Definition stats.c:112
#define SEARCH_UPDATE_INTERNAL_NODES()
Definition stats.h:40
#define SEARCH_STATS(x)
Definition stats.h:27
unsigned long long player
Definition board.h:27
unsigned long long opponent
Definition board.h:27
int lower
Definition search.h:36
int upper
Definition search.h:37
unsigned char move[2]
Definition hash.h:31
signed char lower
Definition hash.h:29
unsigned char cost
Definition hash.h:27
unsigned char date
Definition hash.h:28
signed char upper
Definition hash.h:30
unsigned char selectivity
Definition hash.h:26
unsigned char depth
Definition hash.h:25
unsigned char date
Definition hash.h:55
LogFile.
Definition util.h:423
FILE * f
Definition util.h:424
int n_moves
Definition move.h:31
Move move[MAX_MOVE+2]
Definition move.h:30
unsigned int cost
Definition move.h:24
int score
Definition move.h:23
struct Move * next
Definition move.h:25
unsigned long long flipped
Definition move.h:21
int x
Definition move.h:22
int beta
Definition ybwc.h:52
bool pv_node
Definition ybwc.h:53
volatile int alpha
Definition ybwc.h:51
volatile int bestscore
Definition ybwc.h:50
volatile int bestmove
Definition ybwc.h:49
int alpha
Definition options.h:52
int noise
Definition options.h:33
int beta
Definition options.h:53
bool debug_cassio
Definition options.h:37
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
long long maxi
Definition search.h:130
volatile long long spent
Definition search.h:126
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
bool can_update
Definition search.h:128
Result * result
Definition search.h:151
int depth_pv_extension
Definition search.h:121
int height
Definition search.h:133
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
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
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
NodeType node_type[GAME_SIZE]
Definition search.h:134
Board board[1]
Definition search.h:96
int multipv_depth
Definition search.h:147
int percent
Definition search.h:28
unsigned long long n_PVS_root
Definition stats.h:76
void time_print(long long t, bool justified, FILE *f)
Print time as "D:HH:MM:SS.CC".
Definition util.c:131
unsigned long long random_get(Random *random)
Pseudo-random number generator.
Definition util.c:1043
Miscellaneous utilities header.
#define MIN(a, b)
Definition util.h:101
#define log_print(l,...)
Print into the log file.
Definition util.h:442
#define info(...)
Display a message.
Definition util.h:382
#define cassio_debug(...)
Display a message in cassio's "fenetre de rapport" .
Definition util.h:389
#define log_is_open(l)
Check if the log stream can be used.
Definition util.h:448
#define MAX(a, b)
Definition util.h:98
Log xboard_log[1]
Definition xboard.c:26
Move * node_next_move(Node *node)
Get the next move of the move list.
Definition ybwc.c:345
void node_wait_slaves(Node *node)
Wait for slaves termination.
Definition ybwc.c:212
bool node_split(Node *node, Move *move)
Node split.
Definition ybwc.c:167
void node_free(Node *node)
Free Resources allocated by a node.
Definition ybwc.c:95
Move * node_first_move(Node *node, MoveList *movelist)
Get the first move of the move list.
Definition ybwc.c:297
void node_update(Node *node, Move *move)
Update a node.
Definition ybwc.c:261
void node_init(Node *node, Search *search, const int alpha, const int beta, const int depth, const int n_moves, Node *parent)
Initialize a node.
Definition ybwc.c:59