My Project
Classes | Typedefs | Functions | Variables
search.h File Reference
#include "board.h"
#include "const.h"
#include "empty.h"
#include "eval.h"
#include "hash.h"
#include "move.h"
#include "util.h"
#include <stdio.h>

Go to the source code of this file.

Classes

struct  Selectivity
 
struct  Bound
 
struct  Result
 
struct  Hint
 
struct  HintList
 
struct  Level
 
struct  Search
 

Typedefs

typedef struct Selectivity Selectivity
 
typedef struct Bound Bound
 
typedef struct Result Result
 
typedef struct Hint Hint
 
typedef struct HintList HintList
 
typedef struct Search Search
 

Functions

void search_global_init (void)
 
void search_init (Search *)
 Init the main search. More...
 
void search_free (Search *)
 Free the search allocated ressource. More...
 
void search_cleanup (Search *)
 Clean-up some search data. More...
 
void search_setup (Search *)
 Set up various structure once the board has been set. More...
 
void search_clone (Search *, Search *)
 Clone a search for parallel search. More...
 
void search_set_board (Search *, const Board *, const int)
 Set the board to analyze. More...
 
void search_set_level (Search *, const int, const int)
 Set the search level. More...
 
void search_set_ponder_level (Search *, const int, const int)
 Set the search level while pondering. More...
 
void search_resize_hashtable (Search *)
 
void search_set_game_time (Search *, const long long)
 set time to search. More...
 
void search_set_move_time (Search *, const long long)
 set time to search. More...
 
void search_time_init (Search *)
 Initialize the alloted time. More...
 
void search_time_reset (Search *, const Board *)
 Reset the alloted time. More...
 
void search_adjust_time (Search *, const bool)
 Give more time. More...
 
bool search_continue (Search *)
 Check if it can iterate more... More...
 
void search_check_timeout (Search *search)
 Check if it can iterate more... More...
 
void search_set_task_number (Search *, const int)
 Change the number of task. More...
 
void search_swap_parity (Search *, const int)
 Change parity. More...
 
void search_get_movelist (const Search *, MoveList *)
 Get a list of legal moves. More...
 
void search_update_endgame (Search *, const Move *)
 Update the search state after a move. More...
 
void search_restore_endgame (Search *, const Move *)
 Restore the search state as before a move. More...
 
void search_pass_endgame (Search *)
 Update the search state after a passing move. More...
 
void search_update_midgame (Search *, const Move *)
 Update the search state after a move. More...
 
void search_restore_midgame (Search *, const Move *)
 Restore the search state as before a move. More...
 
void search_update_pass_midgame (Search *)
 Update the search state after a passing move. More...
 
void search_restore_pass_midgame (Search *)
 Update the search state after a passing move. More...
 
long long search_clock (Search *)
 Return the time spent by the search. More...
 
long long search_time (Search *)
 Return the time spent by the search. More...
 
unsigned long long search_count_nodes (Search *)
 Return the number of nodes searched. More...
 
void search_print_pv (Search *, const int, const char *, FILE *)
 
void search_print (Search *, const int, const int, const char, FILE *)
 
int get_pv_extension (const int, const int)
 Compute the pv_extension. More...
 
void result_print (Result *, FILE *)
 Print the current search result. More...
 
bool search_SC_PVS (Search *, volatile int *, volatile int *, int *)
 Stability Cutoff (SC). More...
 
bool search_SC_NWS (Search *, const int, int *)
 Stability Cutoff (TC). More...
 
bool search_TC_PVS (HashData *, const int, const int, volatile int *, volatile int *, int *)
 Transposition Cutoff (TC). More...
 
bool search_TC_NWS (HashData *, const int, const int, const int, int *)
 Transposition Cutoff (TC). More...
 
bool search_ETC_PVS (Search *, MoveList *, unsigned long long, const int, const int, volatile int *, volatile int *, int *)
 
bool search_ETC_NWS (Search *, MoveList *, unsigned long long, const int, const int, const int, int *)
 Enhanced Transposition Cutoff (ETC). More...
 
NodeType next_node_type (const NodeType parent, const bool first_move)
 
int search_solve (const Search *)
 Get the final score. More...
 
int search_solve_0 (const Search *)
 Get the final score. More...
 
int board_score_1 (const Board *, const int, const int)
 Get the final score. More...
 
int NWS_endgame (Search *, const int)
 Evaluate an endgame position with a Null Window Search algorithm. More...
 
int search_eval_0 (Search *)
 evaluate a midgame position with the evaluation function. More...
 
int search_eval_1 (Search *, const int, int)
 Evaluate a position at depth 1. More...
 
int search_eval_2 (Search *, int, const int)
 Evaluate a position at depth 2. More...
 
int NWS_midgame (Search *, const int, int, struct Node *)
 Evaluate a midgame position with a Null Window Search algorithm. More...
 
int PVS_midgame (Search *, const int, const int, int, struct Node *)
 Evaluate a position with a deep Principal Variation Search algorithm. More...
 
int NWS_shallow (Search *, const int, int, HashTable *)
 Evaluate a midgame position with a Null Window Search algorithm. More...
 
int PVS_shallow (Search *, int, int, int)
 Evaluate a midgame position at shallow depth. More...
 
bool is_pv_ok (Search *, int, int)
 Check if PV is ok. More...
 
void record_best_move (Search *, const Board *, const Move *, const int, const int, const int)
 Record best move. More...
 
int PVS_root (Search *, const int, const int, const int)
 Principal Variation Search algorithm at the root of the tree. More...
 
int aspiration_search (Search *, int, int, const int, int)
 Aspiration window. More...
 
void iterative_deepening (Search *, int, int)
 Iterative deepening. More...
 
void * search_run (void *)
 Search the bestmove of a given board. More...
 
int search_guess (Search *, const Board *)
 Guess the bestmove of a given board. More...
 
void search_stop_all (Search *, const Stop)
 Stop the search. More...
 
void search_set_state (Search *, const Stop)
 Set the search running/waiting state. More...
 
void search_observer (Result *)
 default observer. More...
 
void search_set_observer (Search *, void(*Observer)(Result *))
 set observer. More...
 
void search_share (const Search *, Search *)
 Share search information. More...
 
int search_count_tasks (const Search *)
 Count the number of tasks used in parallel search. More...
 
bool is_depth_solving (const int, const int)
 Check if final score use pv_extension or is solved. More...
 
int solvable_depth (const long long, int)
 Compute the deepest level that can be solved given a limited time... More...
 
void pv_debug (Search *, const Move *, FILE *)
 Debug PV. More...
 
int search_get_pv_cost (Search *)
 Compute a cost as a combination of node count, depth, etc. from hash_table. More...
 
void show_current_move (FILE *f, Search *, const Move *, const int, const int, const bool)
 
int search_bound (const Search *, int)
 bound root scores according to stable squares More...
 

Variables

struct Level LEVEL [61][61]
 
const int QUADRANT_ID []
 
const Selectivity selectivity_table []
 
const int NO_SELECTIVITY
 
const int NWS_STABILITY_THRESHOLD []
 
const int PVS_STABILITY_THRESHOLD []
 
const int SQUARE_TYPE []
 

Detailed Description

Search's header file.

Date
1998 - 2017
Author
Richard Delorme
Version
4.4

Typedef Documentation

◆ Bound

typedef struct Bound Bound

◆ Hint

typedef struct Hint Hint

Hint (for libEdax)

Author
lavox
Date
2018/1/17

◆ HintList

typedef struct HintList HintList

Hint list (for libEdax)

Author
lavox
Date
2018/1/17

◆ Result

typedef struct Result Result

◆ Search

typedef struct Search Search

search stare

◆ Selectivity

typedef struct Selectivity Selectivity

Selectivity probcut

Function Documentation

◆ aspiration_search()

int aspiration_search ( Search search,
int  alpha,
int  beta,
const int  depth,
int  score 
)

Aspiration window.

Parameters
searchSearch.
alphaAlpha bound.
betaBeta bound.
depthDepth.
scorePrevious score.
Returns
A score.

◆ board_score_1()

int board_score_1 ( const Board board,
const int  beta,
const int  x 
)
inline

Get the final score.

Get the final score, when 1 empty squares remain. The following code has been adapted from Zebra by Gunnar Anderson.

Parameters
boardBoard to evaluate.
betaBeta bound.
xLast empty square to play.
Returns
The final opponent score, as a disc difference.

◆ get_pv_extension()

int get_pv_extension ( const int  depth,
const int  n_empties 
)

Compute the pv_extension.

Parameters
depthDepth.
n_emptiesGame stage (number of empty squares).

◆ is_depth_solving()

bool is_depth_solving ( const int  depth,
const int  n_empties 
)

Check if final score use pv_extension or is solved.

Parameters
depthsearch depth.
n_emptiesposition depth.
Returns
true if the score should be a final score.

◆ is_pv_ok()

bool is_pv_ok ( Search search,
int  bestmove,
int  search_depth 
)

Check if PV is ok.

Parameters
searchSearch.
bestmoveBest move.
search_depthDepth.
Returns
true if PV is ok.

◆ iterative_deepening()

void iterative_deepening ( Search search,
int  alpha,
int  beta 
)

Iterative deepening.

Search the current position at increasing depths, and once full depth (equaling the number of empties) has been reached at decreasing selectivity ; until the position is eventually solved.

Parameters
searchSearch.
alphaAlpha bound.
betaBeta bound.

◆ next_node_type()

NodeType next_node_type ( const NodeType  parent,
const bool  first_move 
)

◆ NWS_endgame()

int NWS_endgame ( Search search,
const int  alpha 
)

Evaluate an endgame position with a Null Window Search algorithm.

This function is used when there are still many empty squares on the board. Move ordering, hash table cutoff, enhanced transposition cutoff, etc. are used in order to diminish the size of the tree to analyse, but at the expense of a slower speed.

Parameters
searchSearch.
alphaAlpha bound.
Returns
The final score, as a disc difference.

◆ NWS_midgame()

int NWS_midgame ( Search search,
const int  alpha,
int  depth,
Node parent 
)

Evaluate a midgame position with a Null Window Search algorithm.

This function is used when there are still many empty squares on the board. Move ordering, hash table cutoff, enhanced transposition cutoff, etc. are used in order to diminish the size of the tree to analyse, but at the expense of a slower speed.

Parameters
searchSearch.
alphaAlpha bound.
depthDepth.
parentParent node.
Returns
A score, as a disc difference.

◆ NWS_shallow()

int NWS_shallow ( Search search,
const int  alpha,
int  depth,
HashTable hash_table 
)

Evaluate a midgame position with a Null Window Search algorithm.

This function is used when there are still many empty squares on the board. Move ordering, hash table cutoff, enhanced transposition cutoff, etc. are used in order to diminish the size of the tree to analyse, but at the expense of a slower speed.

Parameters
searchsearch.
alphalower bound.
depthSearch remaining depth.
hash_tableHash Table to use.
Returns
An evaluated score, as a disc difference.

◆ pv_debug()

void pv_debug ( Search search,
const Move bestmove,
FILE *  f 
)

Debug PV.

Parameters
searchSearch.
bestmoveBest move.
fOutput stream.

◆ PVS_midgame()

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.

This function is used when there are still many empty squares on the board. Move ordering, hash table cutoff, enhanced transposition cutoff, etc. are used in order to diminish the size of the tree to analyse, but at the expense of a slower speed.

Parameters
searchSearch.
alphaLower bound.
betaUpper bound.
depthDepth.
parentParent node.
Returns
A score, as a disc difference.

◆ PVS_root()

int PVS_root ( Search search,
const int  alpha,
const int  beta,
const int  depth 
)

Principal Variation Search algorithm at the root of the tree.

this function solves the position provided within the limits set by the alpha and beta bounds. The movelist parameter is updated so that the bestmove is the first of the list when the search ended.

Parameters
searchSearch.
alphaAlpha bound.
betaBeta bound.
depthRemaining depth.
Returns
A score, as a disc difference.

◆ PVS_shallow()

int PVS_shallow ( Search search,
int  alpha,
int  beta,
int  depth 
)

Evaluate a midgame position at shallow depth.

This function is used when there are still many empty squares on the board. Move ordering, hash table cutoff, enhanced transposition cutoff, etc. are used in order to diminish the size of the tree to analyse, but at the expense of a slower speed.

Parameters
searchSearch.
alphaAlpha bound.
betaBeta bound.
depthSearch depth.
Returns
An evaluated score, as a disc difference.

◆ record_best_move()

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.

Parameters
searchSearch.
init_boardInitial board.
bestmoveBest move.
alphaAlpha Bound.
betaBeta Bound.
depthDepth.

◆ result_print()

void result_print ( Result result,
FILE *  f 
)

Print the current search result.

Parameters
resultLast search result.
foutput stream.

◆ search_adjust_time()

void search_adjust_time ( Search search,
const bool  once 
)

Give more time.

Parameters
searchSearch.
onceA flag to prevent further time extension.

◆ search_bound()

int search_bound ( const Search search,
int  score 
)

bound root scores according to stable squares

Parameters
searchPosition to search.
scorescore to bound.
Returns
score;

◆ search_check_timeout()

void search_check_timeout ( Search search)

Check if it can iterate more...

Parameters
searchSearch.

◆ search_cleanup()

void search_cleanup ( Search search)

Clean-up some search data.

Parameters
searchsearch.

◆ search_clock()

long long search_clock ( Search search)

Return the time spent by the search.

Parameters
searchSearch.
Returns
time (in milliseconds).

◆ search_clone()

void search_clone ( Search search,
Search master 
)

Clone a search for parallel search.

Parameters
searchsearch.
mastersearch to be cloned.

◆ search_continue()

bool search_continue ( Search search)

Check if it can iterate more...

Parameters
searchSearch.

◆ search_count_nodes()

unsigned long long search_count_nodes ( Search search)

Return the number of nodes searched.

Parameters
searchSearch.
Returns
node count.

◆ search_count_tasks()

int search_count_tasks ( const Search search)

Count the number of tasks used in parallel search.

Parameters
searchSearch.
Returns
number of tasks.

◆ search_ETC_NWS()

bool search_ETC_NWS ( Search search,
MoveList movelist,
unsigned long long  hash_code,
const int  depth,
const int  selectivity,
const int  alpha,
int *  score 
)

Enhanced Transposition Cutoff (ETC).

Before looping over each moves to search at next ply, ETC looks if a cutoff is available from the hashtable. This version also looks for a cutoff based on stability.

Parameters
searchCurrent position.
movelistList of moves for the current position.
hash_codeHashing code.
depthSearch depth.
selectivitySearch selectivity.
alphaAlpha bound.
scoreScore to return in case a cutoff is found.
Returns
'true' if a cutoff is found, 'false' otherwise.

◆ search_ETC_PVS()

bool search_ETC_PVS ( Search ,
MoveList ,
unsigned long long  ,
const int  ,
const int  ,
volatile int *  ,
volatile int *  ,
int *   
)

◆ search_eval_0()

int search_eval_0 ( Search search)

evaluate a midgame position with the evaluation function.

Parameters
searchPosition to evaluate.

◆ search_eval_1()

int search_eval_1 ( Search search,
const int  alpha,
int  beta 
)

Evaluate a position at depth 1.

As an optimization, the last move is used to only updates the evaluation features.

Parameters
searchPosition to evaluate.
alphaAlpha bound.
betaBeta bound.

◆ search_eval_2()

int search_eval_2 ( Search search,
int  alpha,
const int  beta 
)

Evaluate a position at depth 2.

Simple alpha-beta with no move sorting.

Parameters
searchPosition to evaluate.
alphaLower bound
betaUpper bound

◆ search_free()

void search_free ( Search search)

Free the search allocated ressource.

Free a previously initialized search structure.

Parameters
searchsearch.

◆ search_get_movelist()

void search_get_movelist ( const Search search,
MoveList movelist 
)

Get a list of legal moves.

Compute the complete list of legal moves and store it into a simple linked list, to fasten ulterior move sorting. Note: at this point the list is sorted from A1 to H8 (i.e. unsorted).

Parameters
searchSearch.
movelistList of moves.

◆ search_get_pv_cost()

int search_get_pv_cost ( Search search)

Compute a cost as a combination of node count, depth, etc. from hash_table.

The board is supposed to be updated by a move after the root position.

Parameters
searchSearch.
Returns
A search cost.

◆ search_global_init()

void search_global_init ( void  )

Global initialisations.

Todo:
Add more global initialization from here?

◆ search_guess()

int search_guess ( Search search,
const Board board 
)

Guess the bestmove of a given board.

Look for a move from the hash tables.

Parameters
searchSearch.
boardBoard.
Returns
the best move.

◆ search_init()

void search_init ( Search search)

Init the main search.

Initialize a new search structure.

Parameters
searchsearch.

◆ search_observer()

void search_observer ( Result result)

default observer.

Parameters
resultsearch results to print.

◆ search_pass_endgame()

void search_pass_endgame ( Search search)

Update the search state after a passing move.

Parameters
searchsearch.

◆ search_print()

void search_print ( Search ,
const int  ,
const int  ,
const char  ,
FILE *   
)

◆ search_print_pv()

void search_print_pv ( Search ,
const int  ,
const char *  ,
FILE *   
)

◆ search_resize_hashtable()

void search_resize_hashtable ( Search search)

◆ search_restore_endgame()

void search_restore_endgame ( Search search,
const Move move 
)

Restore the search state as before a move.

Parameters
searchsearch.
moveplayed move.

◆ search_restore_midgame()

void search_restore_midgame ( Search search,
const Move move 
)

Restore the search state as before a move.

Parameters
searchsearch.
moveplayed move.

◆ search_restore_pass_midgame()

void search_restore_pass_midgame ( Search search)

Update the search state after a passing move.

Parameters
searchsearch.

◆ search_run()

void * search_run ( void *  v)

Search the bestmove of a given board.

this is a function runable within its own thread. The board is supposed to have been set (by search_set_board()), and all search options (level, time, etc.) too. this function proceeds to some internal initialisations and then call the iterative deepening function, from where the search is actually done. After the search ends, some finalizations are done before the function returns.

Parameters
vSearch cast as void.
Returns
The search result.

◆ search_SC_NWS()

bool search_SC_NWS ( Search search,
const int  alpha,
int *  score 
)

Stability Cutoff (TC).

Parameters
searchCurrent position.
alphaAlpha bound.
scoreScore to return in case of a cutoff is found.
Returns
'true' if a cutoff is found, false otherwise.

◆ search_SC_PVS()

bool search_SC_PVS ( Search search,
volatile int *  alpha,
volatile int *  beta,
int *  score 
)

Stability Cutoff (SC).

Parameters
searchCurrent position.
alphaAlpha bound.
betaBeta bound, to adjust if necessary.
scoreScore to return in case of a cutoff is found.
Returns
'true' if a cutoff is found, false otherwise.

◆ search_set_board()

void search_set_board ( Search search,
const Board board,
const int  player 
)

Set the board to analyze.

Parameters
searchsearch.
boardboard.
playerplayer's turn.

◆ search_set_game_time()

void search_set_game_time ( Search search,
const long long  t 
)

set time to search.

This function should be called before each new search, with the accurate remaining time to do the whole search for the game.

Parameters
searchSearch.
ttime in ms.

◆ search_set_level()

void search_set_level ( Search search,
const int  level,
const int  n_empties 
)

Set the search level.

Compute the couple (depth, selectivity) as a function of (level, n_empties)

Parameters
searchsearch.
levelsearch level.
n_emptiesSearch stage.

◆ search_set_move_time()

void search_set_move_time ( Search search,
const long long  t 
)

set time to search.

This function should be called before each new search, with the accurate remaining time to do the whole search for this move.

Parameters
searchSearch.
ttime in ms.

◆ search_set_observer()

void search_set_observer ( Search search,
void(*)(Result *)  observer 
)

set observer.

Parameters
searchSearched position.
observercall back function to print the current search.

◆ search_set_ponder_level()

void search_set_ponder_level ( Search search,
const int  level,
const int  n_empties 
)

Set the search level while pondering.

Compute the couple (depth, selectivity) as a function of (level, n_empties)

Parameters
searchSearch.
levelSearch level.
n_emptiesSearch stage.

◆ search_set_state()

void search_set_state ( Search search,
const Stop  stop 
)

Set the search running/waiting state.

Parameters
searchSearch.
stopState.

◆ search_set_task_number()

void search_set_task_number ( Search search,
const int  n 
)

Change the number of task.

Parameters
searchSearch.
nNew task number.

◆ search_setup()

void search_setup ( Search search)

Set up various structure once the board has been set.

Initialize the list of empty squares, the parity and the evaluation function.

Parameters
searchsearch.

◆ search_share()

void search_share ( const Search src,
Search dest 
)

Share search information.

Parameters
srcsource search.
destdestination search.

◆ search_solve()

int search_solve ( const Search search)

Get the final score.

Get the final score, when no move can be made.

Parameters
searchSearch.
Returns
The final score, as a disc difference.

◆ search_solve_0()

int search_solve_0 ( const Search search)

Get the final score.

Get the final score, when the board is full.

Parameters
searchSearch.
Returns
The final score, as a disc difference.

◆ search_stop_all()

void search_stop_all ( Search search,
const Stop  stop 
)

Stop the search.

Parameters
searchSearch.
stopSource of stopping.

◆ search_swap_parity()

void search_swap_parity ( Search search,
const int  x 
)

Change parity.

Parameters
searchSearch.
xPlayed square.

◆ search_TC_NWS()

bool search_TC_NWS ( HashData data,
const int  depth,
const int  selectivity,
const int  alpha,
int *  score 
)

Transposition Cutoff (TC).

Parameters
dataTransposition data.
depthSearch depth.
selectivitySearch selecticity level.
alphaAlpha bound.
scoreScore to return in case of a cutoff is found.
Returns
'true' if a cutoff is found, false otherwise.

◆ search_TC_PVS()

bool search_TC_PVS ( HashData data,
const int  depth,
const int  selectivity,
volatile int *  alpha,
volatile int *  beta,
int *  score 
)

Transposition Cutoff (TC).

Parameters
dataTransposition data.
depthSearch depth.
selectivitySearch selecticity level.
alphaAlpha bound, to adjust of necessary.
betaBeta bound, to adjust of necessary.
scoreScore to return in case of a cutoff is found.
Returns
'true' if a cutoff is found, false otherwise.

◆ search_time()

long long search_time ( Search search)

Return the time spent by the search.

Parameters
searchSearch.
Returns
time (in milliseconds).

◆ search_time_init()

void search_time_init ( Search search)

Initialize the alloted time.

Parameters
searchSearch.

◆ search_time_reset()

void search_time_reset ( Search search,
const Board initial_board 
)

Reset the alloted time.

Adjust the time when continuing the search from pondering.

Parameters
searchSearch.
initial_boardInitial board.

◆ search_update_endgame()

void search_update_endgame ( Search search,
const Move move 
)

Update the search state after a move.

Parameters
searchsearch.
moveplayed move.

◆ search_update_midgame()

void search_update_midgame ( Search search,
const Move move 
)

Update the search state after a move.

Parameters
searchsearch.
moveplayed move.

◆ search_update_pass_midgame()

void search_update_pass_midgame ( Search search)

Update the search state after a passing move.

Parameters
searchsearch.

◆ show_current_move()

void show_current_move ( FILE *  f,
Search search,
const Move move,
const int  alpha,
const int  beta,
const bool  parallel 
)

◆ solvable_depth()

int solvable_depth ( const long long  limit,
int  n_tasks 
)

Compute the deepest level that can be solved given a limited time...

This is a very approximate computation... SMP_W & SMP_C depends on the depth and the position. The branching factor depends also of the position.

Parameters
limitTime limit in ms.
n_tasksNumber of parallel tasks.
Returns
Reachable depth.

Variable Documentation

◆ LEVEL

struct Level LEVEL[61][61]

◆ NO_SELECTIVITY

const int NO_SELECTIVITY
extern

level with no selectivity

◆ NWS_STABILITY_THRESHOLD

const int NWS_STABILITY_THRESHOLD[]
extern

threshold values to try stability cutoff during NWS search

◆ PVS_STABILITY_THRESHOLD

const int PVS_STABILITY_THRESHOLD[]
extern

threshold values to try stability cutoff during PVS search

◆ QUADRANT_ID

const int QUADRANT_ID[]
extern

a quadrant id for each square

◆ selectivity_table

const Selectivity selectivity_table[]
extern

predefined selectivity

◆ SQUARE_TYPE

const int SQUARE_TYPE[]
extern

square type