#include "lock.h"
#include "san.h"
#include "search.h"
+#include "timeman.h"
#include "thread.h"
#include "tt.h"
#include "ucioption.h"
Depth PassedPawnExtension[2], PawnEndgameExtension[2], MateThreatExtension[2];
// Minimum depth for use of singular extension
- const Depth SingularExtensionDepth[2] = { 8 * OnePly /* non-PV */, 6 * OnePly /* PV */};
+ const Depth SingularExtensionDepth[2] = { 7 * OnePly /* non-PV */, 6 * OnePly /* PV */};
// If the TT move is at least SingularExtensionMargin better then the
// remaining ones we will extend it.
int MultiPV;
// Time managment variables
- int SearchStartTime, MaxNodes, MaxDepth, MaxSearchTime;
- int AbsoluteMaxSearchTime, ExtraSearchTime, ExactMaxTime;
+ int SearchStartTime, MaxNodes, MaxDepth, ExactMaxTime;
bool UseTimeManagement, InfiniteSearch, PonderSearch, StopOnPonderhit;
bool FirstRootMove, AbortSearch, Quit, AspirationFailLow;
+ TimeManager TimeMgr;
// Log file
bool UseLogFile;
// Initialize global search variables
StopOnPonderhit = AbortSearch = Quit = AspirationFailLow = false;
- MaxSearchTime = AbsoluteMaxSearchTime = ExtraSearchTime = 0;
NodesSincePoll = 0;
TM.resetNodeCounters();
SearchStartTime = get_system_time();
int myTime = time[pos.side_to_move()];
int myIncrement = increment[pos.side_to_move()];
if (UseTimeManagement)
- {
- if (!movesToGo) // Sudden death time control
- {
- if (myIncrement)
- {
- MaxSearchTime = myTime / 30 + myIncrement;
- AbsoluteMaxSearchTime = Max(myTime / 4, myIncrement - 100);
- }
- else // Blitz game without increment
- {
- MaxSearchTime = myTime / 30;
- AbsoluteMaxSearchTime = myTime / 8;
- }
- }
- else // (x moves) / (y minutes)
- {
- if (movesToGo == 1)
- {
- MaxSearchTime = myTime / 2;
- AbsoluteMaxSearchTime = (myTime > 3000)? (myTime - 500) : ((myTime * 3) / 4);
- }
- else
- {
- MaxSearchTime = myTime / Min(movesToGo, 20);
- AbsoluteMaxSearchTime = Min((4 * myTime) / movesToGo, myTime / 3);
- }
- }
-
- if (get_option_value_bool("Ponder"))
- {
- MaxSearchTime += MaxSearchTime / 4;
- MaxSearchTime = Min(MaxSearchTime, AbsoluteMaxSearchTime);
- }
- }
+ TimeMgr.init(myTime, myIncrement, movesToGo, pos.startpos_ply_counter());
// Set best NodesBetweenPolls interval to avoid lagging under
// heavy time pressure.
if ( Iteration >= 8
&& EasyMove == pv[0]
&& ( ( rml.get_move_cumulative_nodes(0) > (nodes * 85) / 100
- && current_search_time() > MaxSearchTime / 16)
+ && current_search_time() > TimeMgr.available_time() / 16)
||( rml.get_move_cumulative_nodes(0) > (nodes * 98) / 100
- && current_search_time() > MaxSearchTime / 32)))
+ && current_search_time() > TimeMgr.available_time() / 32)))
stopSearch = true;
// Add some extra time if the best move has changed during the last two iterations
if (Iteration > 5 && Iteration <= 50)
- ExtraSearchTime = BestMoveChangesByIteration[Iteration] * (MaxSearchTime / 2)
- + BestMoveChangesByIteration[Iteration-1] * (MaxSearchTime / 3);
+ TimeMgr.pv_unstability(BestMoveChangesByIteration[Iteration],
+ BestMoveChangesByIteration[Iteration-1]);
// Stop search if most of MaxSearchTime is consumed at the end of the
// iteration. We probably don't have enough time to search the first
// move at the next iteration anyway.
- if (current_search_time() > ((MaxSearchTime + ExtraSearchTime) * 80) / 128)
+ if (current_search_time() > (TimeMgr.available_time() * 80) / 128)
stopSearch = true;
if (stopSearch)
Move movesSearched[256];
EvalInfo ei;
StateInfo st;
- const TTEntry* tte;
+ const TTEntry *tte, *ttx;
Key posKey;
Move ttMove, move, excludedMove, threatMove;
Depth ext, newDepth;
// Refresh tte entry to avoid aging
TT.store(posKey, tte->value(), tte->type(), tte->depth(), ttMove, tte->static_value(), tte->king_danger());
- ss->currentMove = ttMove; // Can be MOVE_NONE
+ ss->bestMove = ttMove; // Can be MOVE_NONE
return value_from_tt(tte->value(), ply);
}
- // Step 5. Evaluate the position statically
- // At PV nodes we do this only to update gain statistics
+ // Step 5. Evaluate the position statically and
+ // update gain statistics of parent move.
isCheck = pos.is_check();
- if (!isCheck)
+ if (isCheck)
+ ss->eval = VALUE_NONE;
+ else if (tte)
{
- if (tte)
- {
- assert(tte->static_value() != VALUE_NONE);
- ss->eval = tte->static_value();
- ei.kingDanger[pos.side_to_move()] = tte->king_danger();
- }
- else
- {
- ss->eval = evaluate(pos, ei);
- TT.store(posKey, VALUE_NONE, VALUE_TYPE_NONE, DEPTH_NONE, MOVE_NONE, ss->eval, ei.kingDanger[pos.side_to_move()]);
- }
+ assert(tte->static_value() != VALUE_NONE);
- refinedValue = refine_eval(tte, ss->eval, ply); // Enhance accuracy with TT value if possible
- update_gains(pos, (ss-1)->currentMove, (ss-1)->eval, ss->eval);
+ ss->eval = tte->static_value();
+ ei.kingDanger[pos.side_to_move()] = tte->king_danger();
+ refinedValue = refine_eval(tte, ss->eval, ply);
}
else
- ss->eval = VALUE_NONE;
+ {
+ refinedValue = ss->eval = evaluate(pos, ei);
+ TT.store(posKey, VALUE_NONE, VALUE_TYPE_NONE, DEPTH_NONE, MOVE_NONE, ss->eval, ei.kingDanger[pos.side_to_move()]);
+ }
+
+ // Save gain for the parent non-capture move
+ update_gains(pos, (ss-1)->currentMove, (ss-1)->eval, ss->eval);
// Step 6. Razoring (is omitted in PV nodes)
if ( !PvNode
if ( !PvNode
&& !ss->skipNullMove
&& depth < RazorDepth
- && refinedValue >= beta + futility_margin(depth, 0)
&& !isCheck
+ && refinedValue >= beta + futility_margin(depth, 0)
&& !value_is_mate(beta)
&& pos.non_pawn_material(pos.side_to_move()))
return refinedValue - futility_margin(depth, 0);
if ( !PvNode
&& !ss->skipNullMove
&& depth > OnePly
- && refinedValue >= beta - (depth >= 4 * OnePly ? NullMoveMargin : 0)
&& !isCheck
+ && refinedValue >= beta - (depth >= 4 * OnePly ? NullMoveMargin : 0)
&& !value_is_mate(beta)
&& pos.non_pawn_material(pos.side_to_move()))
{
if (nullValue == value_mated_in(ply + 2))
mateThreat = true;
- threatMove = (ss+1)->currentMove;
+ threatMove = (ss+1)->bestMove;
if ( depth < ThreatDepth
&& (ss-1)->reduction
&& connected_moves(pos, (ss-1)->currentMove, threatMove))
// Initialize a MovePicker object for the current position
MovePicker mp = MovePicker(pos, ttMove, depth, H, ss, (PvNode ? -VALUE_INFINITE : beta));
CheckInfo ci(pos);
+ ss->bestMove = MOVE_NONE;
singleEvasion = isCheck && mp.number_of_evasions() == 1;
singularExtensionNode = depth >= SingularExtensionDepth[PvNode]
- && tte && tte->move()
+ && tte
+ && tte->move()
&& !excludedMove // Do not allow recursive singular extension search
&& is_lower_bound(tte->type())
&& tte->depth() >= depth - 3 * OnePly;
&& move == tte->move()
&& ext < OnePly)
{
+ // Avoid to do an expensive singular extension search on nodes where
+ // such search have already been done in the past, so assume the last
+ // singular extension search result is still valid.
+ if ( !PvNode
+ && depth < SingularExtensionDepth[PvNode] + 5 * OnePly
+ && (ttx = TT.retrieve(pos.get_exclusion_key())) != NULL)
+ {
+ if (is_upper_bound(ttx->type()))
+ ext = OnePly;
+
+ singularExtensionNode = false;
+ }
+
Value ttValue = value_from_tt(tte->value(), ply);
- if (abs(ttValue) < VALUE_KNOWN_WIN)
+ if (singularExtensionNode && abs(ttValue) < VALUE_KNOWN_WIN)
{
Value b = ttValue - SingularExtensionMargin;
ss->excludedMove = move;
Value v = search<NonPV>(pos, ss, b - 1, b, depth / 2, ply);
ss->skipNullMove = false;
ss->excludedMove = MOVE_NONE;
+ ss->bestMove = MOVE_NONE;
if (v < b)
ext = OnePly;
}
bestValue = value;
if (value > alpha)
{
- if (PvNode && value < beta) // This guarantees that always: alpha < beta
+ if (PvNode && value < beta) // We want always alpha < beta
alpha = value;
if (value == value_mate_in(ply + 1))
// no legal moves, it must be mate or stalemate.
// If one move was excluded return fail low score.
if (!moveCount)
- return excludedMove ? oldAlpha : (isCheck ? value_mated_in(ply) : VALUE_DRAW);
+ return excludedMove ? oldAlpha : isCheck ? value_mated_in(ply) : VALUE_DRAW;
// Step 20. Update tables
// If the search is not aborted, update the transposition table,
if (AbortSearch || TM.thread_should_stop(threadID))
return bestValue;
- ValueType f = (bestValue <= oldAlpha ? VALUE_TYPE_UPPER : bestValue >= beta ? VALUE_TYPE_LOWER : VALUE_TYPE_EXACT);
+ ValueType vt = (bestValue <= oldAlpha ? VALUE_TYPE_UPPER : bestValue >= beta ? VALUE_TYPE_LOWER : VALUE_TYPE_EXACT);
move = (bestValue <= oldAlpha ? MOVE_NONE : ss->bestMove);
- TT.store(posKey, value_to_tt(bestValue, ply), f, depth, move, ss->eval, ei.kingDanger[pos.side_to_move()]);
+ TT.store(posKey, value_to_tt(bestValue, ply), vt, depth, move, ss->eval, ei.kingDanger[pos.side_to_move()]);
// Update killers and history only for non capture moves that fails high
if (bestValue >= beta)
if (!PvNode && tte && ok_to_use_TT(tte, depth, beta, ply))
{
- ss->currentMove = ttMove; // Can be MOVE_NONE
+ ss->bestMove = ttMove; // Can be MOVE_NONE
return value_from_tt(tte->value(), ply);
}
if (tte)
{
assert(tte->static_value() != VALUE_NONE);
+
ei.kingDanger[pos.side_to_move()] = tte->king_danger();
bestValue = tte->static_value();
}
// Update transposition table
Depth d = (depth == Depth(0) ? Depth(0) : Depth(-1));
- ValueType f = (bestValue <= oldAlpha ? VALUE_TYPE_UPPER : bestValue >= beta ? VALUE_TYPE_LOWER : VALUE_TYPE_EXACT);
- TT.store(pos.get_key(), value_to_tt(bestValue, ply), f, d, ss->bestMove, ss->eval, ei.kingDanger[pos.side_to_move()]);
+ ValueType vt = (bestValue <= oldAlpha ? VALUE_TYPE_UPPER : bestValue >= beta ? VALUE_TYPE_LOWER : VALUE_TYPE_EXACT);
+ TT.store(pos.get_key(), value_to_tt(bestValue, ply), vt, d, ss->bestMove, ss->eval, ei.kingDanger[pos.side_to_move()]);
// Update killers only for checking moves that fails high
if ( bestValue >= beta
Value refine_eval(const TTEntry* tte, Value defaultEval, int ply) {
- if (!tte)
- return defaultEval;
+ assert(tte);
Value v = value_from_tt(tte->value(), ply);
&& before != VALUE_NONE
&& after != VALUE_NONE
&& pos.captured_piece() == NO_PIECE_TYPE
- && !move_is_castle(m)
- && !move_is_promotion(m))
+ && !move_is_special(m))
H.set_gain(pos.piece_on(move_to(m)), move_to(m), -(before + after));
}
bool stillAtFirstMove = FirstRootMove
&& !AspirationFailLow
- && t > MaxSearchTime + ExtraSearchTime;
+ && t > TimeMgr.available_time();
- bool noMoreTime = t > AbsoluteMaxSearchTime
+ bool noMoreTime = t > TimeMgr.maximum_time()
|| stillAtFirstMove;
if ( (Iteration >= 3 && UseTimeManagement && noMoreTime)
bool stillAtFirstMove = FirstRootMove
&& !AspirationFailLow
- && t > MaxSearchTime + ExtraSearchTime;
+ && t > TimeMgr.available_time();
- bool noMoreTime = t > AbsoluteMaxSearchTime
+ bool noMoreTime = t > TimeMgr.maximum_time()
|| stillAtFirstMove;
if (Iteration >= 3 && UseTimeManagement && (noMoreTime || StopOnPonderhit))
#endif
// Initialize global locks
- lock_init(&MPLock, NULL);
- lock_init(&WaitLock, NULL);
+ lock_init(&MPLock);
+ lock_init(&WaitLock);
#if !defined(_MSC_VER)
pthread_cond_init(&WaitCond, NULL);
// Initialize splitPoints[] locks
for (i = 0; i < MAX_THREADS; i++)
for (int j = 0; j < MAX_ACTIVE_SPLIT_POINTS; j++)
- lock_init(&(threads[i].splitPoints[j].lock), NULL);
+ lock_init(&(threads[i].splitPoints[j].lock));
// Will be set just before program exits to properly end the threads
AllThreadsShouldExit = false;