// evaluation of the position is more than NullMoveMargin below beta.
const Value NullMoveMargin = Value(0x300);
- //Null move search refutes move when Nullvalue >= Beta - Delta. Index is depth
- //in full plies. Last index is 9+.
- const Value NullMoveDeltaMidgame[] =
- { Value(-8), Value( 6), Value(-15), Value( 9), Value(21),
- Value(34), Value(54), Value( 59), Value(61), Value(61) };
-
- const Value NullMoveDeltaEndgame[] =
- { Value( 6), Value( 0), Value(-13), Value(-9), Value(-35),
- Value(12), Value(24), Value( 9), Value( 5), Value( 5) };
-
// Pruning criterions. See the code and comments in ok_to_prune() to
// understand their precise meaning.
const bool PruneEscapeMoves = false;
// Iteration counters
int Iteration;
- bool LastIterations;
BetaCounterType BetaCounter;
// Scores and number of times the best move changed for each iteration:
int SearchStartTime;
int MaxNodes, MaxDepth;
int MaxSearchTime, AbsoluteMaxSearchTime, ExtraSearchTime;
- Move BestRootMove, PonderMove, EasyMove;
+ Move EasyMove;
int RootMoveNumber;
bool InfiniteSearch;
bool PonderSearch;
Depth depth, int ply, int threadID);
void sp_search(SplitPoint *sp, int threadID);
void sp_search_pv(SplitPoint *sp, int threadID);
- void init_node(const Position &pos, SearchStack ss[], int ply, int threadID);
+ void init_node(SearchStack ss[], int ply, int threadID);
void update_pv(SearchStack ss[], int ply);
void sp_update_pv(SearchStack *pss, SearchStack ss[], int ply);
bool connected_moves(const Position &pos, Move m1, Move m2);
pv[ply] = pv[ply + 1] = MOVE_NONE;
currentMove = threatMove = MOVE_NONE;
reduction = Depth(0);
- currentMoveCaptureValue = Value(0);
}
void SearchStack::initKillers() {
// Initialize global search variables
Idle = false;
SearchStartTime = get_system_time();
- BestRootMove = MOVE_NONE;
- PonderMove = MOVE_NONE;
EasyMove = MOVE_NONE;
for (int i = 0; i < THREAD_MAX; i++)
{
ValueByIteration[0] = Value(0);
ValueByIteration[1] = rml.get_move_score(0);
Iteration = 1;
- LastIterations = false;
EasyMove = rml.scan_for_easy_move();
ExtraSearchTime = BestMoveChangesByIteration[Iteration] * (MaxSearchTime / 2)
+ BestMoveChangesByIteration[Iteration-1] * (MaxSearchTime / 3);
- // Try to guess if the current iteration is the last one or the last two
- LastIterations = (current_search_time() > ((MaxSearchTime + ExtraSearchTime)*58) / 128);
-
// 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.
<< " hashfull " << TT.full() << std::endl;
// Print the best move and the ponder move to the standard output
+ if (ss[0].pv[0] == MOVE_NONE)
+ {
+ ss[0].pv[0] = rml.get_move(0);
+ ss[0].pv[1] = MOVE_NONE;
+ }
std::cout << "bestmove " << ss[0].pv[0];
if (ss[0].pv[1] != MOVE_NONE)
std::cout << " ponder " << ss[0].pv[1];
// Initialize, and make an early exit in case of an aborted search,
// an instant draw, maximum ply reached, etc.
- init_node(pos, ss, ply, threadID);
+ init_node(ss, ply, threadID);
// After init_node() that calls poll()
if (AbortSearch || thread_should_stop(threadID))
movesSearched[moveCount++] = ss[ply].currentMove = move;
- if (moveIsCapture)
- ss[ply].currentMoveCaptureValue =
- move_is_ep(move)? PawnValueMidgame : pos.midgame_value_of_piece_on(move_to(move));
- else
- ss[ply].currentMoveCaptureValue = Value(0);
-
// Decide the new search depth
bool dangerous;
Depth ext = extension(pos, move, true, moveIsCapture, moveIsCheck, singleReply, mateThreat, &dangerous);
// Initialize, and make an early exit in case of an aborted search,
// an instant draw, maximum ply reached, etc.
- init_node(pos, ss, ply, threadID);
+ init_node(ss, ply, threadID);
// After init_node() that calls poll()
if (AbortSearch || thread_should_stop(threadID))
&& ok_to_do_nullmove(pos)
&& approximateEval >= beta - NullMoveMargin)
{
- //Calculate correct delta. Idea and tuning from Joona Kiiski.
- ScaleFactor factor[2] = { SCALE_FACTOR_NORMAL, SCALE_FACTOR_NORMAL };
- Phase phase = pos.game_phase();
- int i = Min(depth / OnePly, 9);
- Value delta = scale_by_game_phase(NullMoveDeltaMidgame[i], NullMoveDeltaEndgame[i], phase, factor);
-
ss[ply].currentMove = MOVE_NULL;
StateInfo st;
pos.do_null_move(st);
- int R = (depth >= 4 * OnePly ? 4 : 3); // Null move dynamic reduction
+ int R = (depth >= 5 * OnePly ? 4 : 3); // Null move dynamic reduction
- Value nullValue = -search(pos, ss, -(beta-delta-1), depth-R*OnePly, ply+1, false, threadID);
+ Value nullValue = -search(pos, ss, -(beta-1), depth-R*OnePly, ply+1, false, threadID);
pos.undo_null_move();
{
/* Do not return unproven mates */
}
- else if (nullValue >= beta - delta)
+ else if (nullValue >= beta)
{
if (depth < 6 * OnePly)
return beta;
{
Value v = qsearch(pos, ss, beta-1, beta, Depth(0), ply, threadID);
if ( (v < beta - RazorMargin - RazorMargin / 4)
- || (depth < 3*OnePly && v < beta - RazorMargin)
- || (depth < 2*OnePly && v < beta - RazorMargin / 2))
+ || (depth <= 2*OnePly && v < beta - RazorMargin)
+ || (depth <= OnePly && v < beta - RazorMargin / 2))
return v;
}
}
TT.store(pos, value_to_tt(bestValue, ply), depth, m, VALUE_TYPE_LOWER);
}
+
+ assert(bestValue > -VALUE_INFINITE && bestValue < VALUE_INFINITE);
+
return bestValue;
}
// Initialize, and make an early exit in case of an aborted search,
// an instant draw, maximum ply reached, etc.
- init_node(pos, ss, ply, threadID);
+ init_node(ss, ply, threadID);
// After init_node() that calls poll()
if (AbortSearch || thread_should_stop(threadID))
if (pos.is_draw())
return VALUE_DRAW;
- // Transposition table lookup
- const TTEntry* tte = TT.retrieve(pos);
- if (tte && ok_to_use_TT(tte, depth, beta, ply))
- return value_from_tt(tte->value(), ply);
+ // Transposition table lookup, only when not in PV
+ TTEntry* tte = NULL;
+ bool pvNode = (beta - alpha != 1);
+ if (!pvNode)
+ {
+ tte = TT.retrieve(pos);
+ if (tte && ok_to_use_TT(tte, depth, beta, ply))
+ {
+ assert(tte->type() != VALUE_TYPE_EVAL);
+
+ return value_from_tt(tte->value(), ply);
+ }
+ }
// Evaluate the position statically
EvalInfo ei;
+ Value staticValue;
bool isCheck = pos.is_check();
- Value staticValue = (isCheck ? -VALUE_INFINITE : evaluate(pos, ei, threadID));
+ ei.futilityMargin = Value(0); // Manually initialize futilityMargin
+
+ if (isCheck)
+ staticValue = -VALUE_INFINITE;
+
+ else if (tte && tte->type() == VALUE_TYPE_EVAL)
+ {
+ // Use the cached evaluation score if possible
+ assert(tte->value() == evaluate(pos, ei, threadID));
+ assert(ei.futilityMargin == Value(0));
+
+ staticValue = tte->value();
+ }
+ else
+ staticValue = evaluate(pos, ei, threadID);
if (ply == PLY_MAX - 1)
return evaluate(pos, ei, threadID);
Value bestValue = staticValue;
if (bestValue >= beta)
+ {
+ // Store the score to avoid a future costly evaluation() call
+ if (!isCheck && !tte && ei.futilityMargin == 0)
+ TT.store(pos, value_to_tt(bestValue, ply), Depth(-127*OnePly), MOVE_NONE, VALUE_TYPE_EVAL);
+
return bestValue;
+ }
if (bestValue > alpha)
alpha = bestValue;
// Initialize a MovePicker object for the current position, and prepare
// to search the moves. Because the depth is <= 0 here, only captures,
// queen promotions and checks (only if depth == 0) will be generated.
- bool pvNode = (beta - alpha != 1);
- MovePicker mp = MovePicker(pos, pvNode, MOVE_NONE, EmptySearchStack, depth, isCheck ? NULL : &ei);
+ MovePicker mp = MovePicker(pos, pvNode, MOVE_NONE, EmptySearchStack, depth);
Move move;
int moveCount = 0;
Bitboard dcCandidates = mp.discovered_check_candidates();
assert(bestValue > -VALUE_INFINITE && bestValue < VALUE_INFINITE);
// Update transposition table
- TT.store(pos, value_to_tt(bestValue, ply), depth, MOVE_NONE, VALUE_TYPE_EXACT);
+ if (!pvNode)
+ {
+ Depth d = (depth == Depth(0) ? Depth(0) : Depth(-1));
+ if (bestValue < beta)
+ TT.store(pos, value_to_tt(bestValue, ply), d, MOVE_NONE, VALUE_TYPE_UPPER);
+ else
+ TT.store(pos, value_to_tt(bestValue, ply), d, MOVE_NONE, VALUE_TYPE_LOWER);
+ }
// Update killers only for good check moves
Move m = ss[ply].currentMove;
assert(move_is_ok(move));
- if (moveIsCapture)
- ss[sp->ply].currentMoveCaptureValue =
- move_is_ep(move)? PawnValueMidgame : pos.midgame_value_of_piece_on(move_to(move));
- else
- ss[sp->ply].currentMoveCaptureValue = Value(0);
-
lock_grab(&(sp->lock));
int moveCount = ++sp->moves;
lock_release(&(sp->lock));
// NodesBetweenPolls nodes, init_node() also calls poll(), which polls
// for user input and checks whether it is time to stop the search.
- void init_node(const Position &pos, SearchStack ss[], int ply, int threadID) {
+ void init_node(SearchStack ss[], int ply, int threadID) {
assert(ply >= 0 && ply < PLY_MAX);
assert(threadID >= 0 && threadID < ActiveThreads);
|| ( !FailHigh && !fail_high_ply_1() && !Problem
&& t > 6*(MaxSearchTime + ExtraSearchTime));
- if ( (Iteration >= 2 && (!InfiniteSearch && overTime))
+ if ( (Iteration >= 3 && (!InfiniteSearch && overTime))
|| (ExactMaxTime && t >= ExactMaxTime)
|| (Iteration >= 3 && MaxNodes && nodes_searched() >= MaxNodes))
AbortSearch = true;
void ponderhit() {
int t = current_search_time();
PonderSearch = false;
- if(Iteration >= 2 &&
+ if(Iteration >= 3 &&
(!InfiniteSearch && (StopOnPonderhit ||
t > AbsoluteMaxSearchTime ||
(RootMoveNumber == 1 &&