// root move, we store a score, a node count, and a PV (really a refutation
// in the case of moves which fail low).
- class RootMove {
+ struct RootMove {
- public:
RootMove();
+ bool operator<(const RootMove&); // used to sort
+
Move move;
Value score;
int64_t nodes, cumulativeNodes;
void set_move_nodes(int moveNum, int64_t nodes);
void set_move_pv(int moveNum, const Move pv[]);
Move get_move_pv(int moveNum, int i) const;
- int64_t get_move_cumulative_nodes(int moveNum);
+ int64_t get_move_cumulative_nodes(int moveNum) const;
int move_count() const;
Move scan_for_easy_move() const;
void sort();
void sort_multipv(int n);
private:
- static bool compare_root_moves(const RootMove &rm1, const RootMove &rm2);
static const int MaxRootMoves = 500;
RootMove moves[MaxRootMoves];
int count;
bool singleReply, bool mateThreat);
bool ok_to_do_nullmove(const Position &pos);
bool ok_to_prune(const Position &pos, Move m, Move threat, Depth d);
+ bool ok_to_use_TT(const TTEntry* tte, Depth depth, Value beta, int ply);
bool fail_high_ply_1();
int current_search_time();
Value search_pv(Position &pos, SearchStack ss[], Value alpha, Value beta,
Depth depth, int ply, int threadID) {
+
assert(alpha >= -VALUE_INFINITE && alpha <= VALUE_INFINITE);
assert(beta > alpha && beta <= VALUE_INFINITE);
assert(ply >= 0 && ply < PLY_MAX);
// an instant draw, maximum ply reached, etc.
Value oldAlpha = alpha;
- if(AbortSearch || thread_should_stop(threadID))
- return Value(0);
+ if (AbortSearch || thread_should_stop(threadID))
+ return Value(0);
- if(depth < OnePly)
- return qsearch(pos, ss, alpha, beta, Depth(0), ply, threadID);
+ if (depth < OnePly)
+ return qsearch(pos, ss, alpha, beta, Depth(0), ply, threadID);
init_node(pos, ss, ply, threadID);
- if(pos.is_draw())
- return VALUE_DRAW;
+ if (pos.is_draw())
+ return VALUE_DRAW;
- if(ply >= PLY_MAX - 1)
- return evaluate(pos, ei, threadID);
+ if (ply >= PLY_MAX - 1)
+ return evaluate(pos, ei, threadID);
// Mate distance pruning
alpha = Max(value_mated_in(ply), alpha);
beta = Min(value_mate_in(ply+1), beta);
- if(alpha >= beta)
- return alpha;
+ if (alpha >= beta)
+ return alpha;
// Transposition table lookup. At PV nodes, we don't use the TT for
// pruning, but only for move ordering.
- Value ttValue;
- Depth ttDepth;
- Move ttMove = MOVE_NONE;
- ValueType ttValueType;
+ const TTEntry* tte = TT.retrieve(pos);
- TT.retrieve(pos, &ttValue, &ttDepth, &ttMove, &ttValueType);
+ Move ttMove = (tte ? tte->move() : MOVE_NONE);
- // Internal iterative deepening.
- if(UseIIDAtPVNodes && ttMove == MOVE_NONE && depth >= 5*OnePly) {
- search_pv(pos, ss, alpha, beta, depth-2*OnePly, ply, threadID);
- ttMove = ss[ply].pv[ply];
+ // Go with internal iterative deepening if we don't have a TT move.
+ if (UseIIDAtPVNodes && ttMove == MOVE_NONE && depth >= 5*OnePly)
+ {
+ search_pv(pos, ss, alpha, beta, depth-2*OnePly, ply, threadID);
+ ttMove = ss[ply].pv[ply];
}
// Initialize a MovePicker object for the current position, and prepare
// to search all moves:
MovePicker mp = MovePicker(pos, true, ttMove, ss[ply].mateKiller,
ss[ply].killer1, ss[ply].killer2, depth);
+
Move move, movesSearched[256];
int moveCount = 0;
Value value, bestValue = -VALUE_INFINITE;
Bitboard dcCandidates = mp.discovered_check_candidates();
- bool mateThreat =
- MateThreatExtension[1] > Depth(0)
- && pos.has_mate_threat(opposite_color(pos.side_to_move()));
+ bool mateThreat = MateThreatExtension[1] > Depth(0)
+ && pos.has_mate_threat(opposite_color(pos.side_to_move()));
// Loop through all legal moves until no moves remain or a beta cutoff
// occurs.
- while(alpha < beta && !thread_should_stop(threadID)
- && (move = mp.get_next_move()) != MOVE_NONE) {
- UndoInfo u;
- Depth ext, newDepth;
+ while ( alpha < beta
+ && (move = mp.get_next_move()) != MOVE_NONE
+ && !thread_should_stop(threadID))
+ {
+ assert(move_is_ok(move));
+
bool singleReply = (pos.is_check() && mp.number_of_moves() == 1);
bool moveIsCheck = pos.move_is_check(move, dcCandidates);
bool moveIsCapture = pos.move_is_capture(move);
bool moveIsPassedPawnPush = pos.move_is_passed_pawn_push(move);
- assert(move_is_ok(move));
movesSearched[moveCount++] = ss[ply].currentMove = move;
- ss[ply].currentMoveCaptureValue = move_is_ep(move)?
+ ss[ply].currentMoveCaptureValue = move_is_ep(move) ?
PawnValueMidgame : pos.midgame_value_of_piece_on(move_to(move));
// Decide the new search depth.
- ext = extension(pos, move, true, moveIsCheck, singleReply, mateThreat);
- newDepth = depth - OnePly + ext;
+ Depth ext = extension(pos, move, true, moveIsCheck, singleReply, mateThreat);
+ Depth newDepth = depth - OnePly + ext;
// Make and search the move.
+ UndoInfo u;
pos.do_move(move, u, dcCandidates);
- if(moveCount == 1)
- value = -search_pv(pos, ss, -beta, -alpha, newDepth, ply+1, threadID);
- else {
- if(depth >= 2*OnePly && ext == Depth(0) && moveCount >= LMRPVMoves
- && !moveIsCapture && !move_promotion(move)
- && !moveIsPassedPawnPush && !move_is_castle(move)
- && move != ss[ply].killer1 && move != ss[ply].killer2) {
- ss[ply].reduction = OnePly;
- value = -search(pos, ss, -alpha, newDepth-OnePly, ply+1, true,
- threadID);
+ if (moveCount == 1) // The first move in list is the PV
+ value = -search_pv(pos, ss, -beta, -alpha, newDepth, ply+1, threadID);
+ else
+ {
+ // Try to reduce non-pv search depth by one ply if move seems not problematic,
+ // if the move fails high will be re-searched at full depth.
+ if ( depth >= 2*OnePly
+ && ext == Depth(0)
+ && moveCount >= LMRPVMoves
+ && !moveIsCapture
+ && !move_promotion(move)
+ && !moveIsPassedPawnPush
+ && !move_is_castle(move)
+ && move != ss[ply].killer1
+ && move != ss[ply].killer2)
+ {
+ ss[ply].reduction = OnePly;
+ value = -search(pos, ss, -alpha, newDepth-OnePly, ply+1, true, threadID);
}
- else value = alpha + 1;
- if(value > alpha) {
- ss[ply].reduction = Depth(0);
- value = -search(pos, ss, -alpha, newDepth, ply+1, true, threadID);
- if(value > alpha && value < beta) {
- if(ply == 1 && RootMoveNumber == 1)
- // When the search fails high at ply 1 while searching the first
- // move at the root, set the flag failHighPly1. This is used for
- // time managment: We don't want to stop the search early in
- // such cases, because resolving the fail high at ply 1 could
- // result in a big drop in score at the root.
- Threads[threadID].failHighPly1 = true;
- value = -search_pv(pos, ss, -beta, -alpha, newDepth, ply+1,
- threadID);
- Threads[threadID].failHighPly1 = false;
+ else
+ value = alpha + 1; // Just to trigger next condition
+
+ if (value > alpha) // Go with full depth non-pv search
+ {
+ ss[ply].reduction = Depth(0);
+ value = -search(pos, ss, -alpha, newDepth, ply+1, true, threadID);
+ if (value > alpha && value < beta)
+ {
+ // When the search fails high at ply 1 while searching the first
+ // move at the root, set the flag failHighPly1. This is used for
+ // time managment: We don't want to stop the search early in
+ // such cases, because resolving the fail high at ply 1 could
+ // result in a big drop in score at the root.
+ if (ply == 1 && RootMoveNumber == 1)
+ Threads[threadID].failHighPly1 = true;
+
+ // A fail high occurred. Re-search at full window (pv search)
+ value = -search_pv(pos, ss, -beta, -alpha, newDepth, ply+1, threadID);
+ Threads[threadID].failHighPly1 = false;
}
}
}
assert(value > -VALUE_INFINITE && value < VALUE_INFINITE);
// New best move?
- if(value > bestValue) {
- bestValue = value;
- if(value > alpha) {
- alpha = value;
- update_pv(ss, ply);
- if(value == value_mate_in(ply + 1))
- ss[ply].mateKiller = move;
- }
- // If we are at ply 1, and we are searching the first root move at
- // ply 0, set the 'Problem' variable if the score has dropped a lot
- // (from the computer's point of view) since the previous iteration:
- if(Iteration >= 2 &&
- -value <= ValueByIteration[Iteration-1] - ProblemMargin)
- Problem = true;
+ if (value > bestValue)
+ {
+ bestValue = value;
+ if (value > alpha)
+ {
+ alpha = value;
+ update_pv(ss, ply);
+ if (value == value_mate_in(ply + 1))
+ ss[ply].mateKiller = move;
+ }
+ // If we are at ply 1, and we are searching the first root move at
+ // ply 0, set the 'Problem' variable if the score has dropped a lot
+ // (from the computer's point of view) since the previous iteration:
+ if (Iteration >= 2 && -value <= ValueByIteration[Iteration-1] - ProblemMargin)
+ Problem = true;
}
// Split?
- if(ActiveThreads > 1 && bestValue < beta && depth >= MinimumSplitDepth
- && Iteration <= 99 && idle_thread_exists(threadID)
- && !AbortSearch && !thread_should_stop(threadID)
- && split(pos, ss, ply, &alpha, &beta, &bestValue, depth,
- &moveCount, &mp, dcCandidates, threadID, true))
- break;
+ if ( ActiveThreads > 1
+ && bestValue < beta && depth >= MinimumSplitDepth
+ && Iteration <= 99 && idle_thread_exists(threadID)
+ && !AbortSearch && !thread_should_stop(threadID)
+ && split(pos, ss, ply, &alpha, &beta, &bestValue, depth,
+ &moveCount, &mp, dcCandidates, threadID, true))
+ break;
}
// All legal moves have been searched. A special case: If there were
// no legal moves, it must be mate or stalemate:
- if(moveCount == 0) {
- if(pos.is_check())
- return value_mated_in(ply);
- else
- return VALUE_DRAW;
- }
+ if (moveCount == 0)
+ return (pos.is_check() ? value_mated_in(ply) : VALUE_DRAW);
// If the search is not aborted, update the transposition table,
// history counters, and killer moves. This code is somewhat messy,
// and definitely needs to be cleaned up. FIXME
- if(!AbortSearch && !thread_should_stop(threadID)) {
- if(bestValue <= oldAlpha)
- TT.store(pos, value_to_tt(bestValue, ply), depth, MOVE_NONE,
- VALUE_TYPE_UPPER);
- else if(bestValue >= beta) {
+ if (AbortSearch || thread_should_stop(threadID))
+ return bestValue;
+
+ if (bestValue <= oldAlpha)
+ TT.store(pos, value_to_tt(bestValue, ply), depth, MOVE_NONE, VALUE_TYPE_UPPER);
+
+ else if (bestValue >= beta)
+ {
Move m = ss[ply].pv[ply];
- if(pos.square_is_empty(move_to(m)) && !move_promotion(m) &&
- !move_is_ep(m)) {
- for(int i = 0; i < moveCount - 1; i++)
- if(pos.square_is_empty(move_to(movesSearched[i]))
- && !move_promotion(movesSearched[i])
- && !move_is_ep(movesSearched[i]))
- H.failure(pos.piece_on(move_from(movesSearched[i])),
- movesSearched[i]);
+ if (pos.square_is_empty(move_to(m)) && !move_promotion(m) && !move_is_ep(m))
+ {
+ for(int i = 0; i < moveCount - 1; i++)
+ if( pos.square_is_empty(move_to(movesSearched[i]))
+ && !move_promotion(movesSearched[i])
+ && !move_is_ep(movesSearched[i]))
+ H.failure(pos.piece_on(move_from(movesSearched[i])), movesSearched[i]);
- H.success(pos.piece_on(move_from(m)), m, depth);
+ H.success(pos.piece_on(move_from(m)), m, depth);
- if(m != ss[ply].killer1) {
+ if (m != ss[ply].killer1)
+ {
ss[ply].killer2 = ss[ply].killer1;
ss[ply].killer1 = m;
}
}
TT.store(pos, value_to_tt(bestValue, ply), depth, m, VALUE_TYPE_LOWER);
- }
- else
- TT.store(pos, value_to_tt(bestValue, ply), depth, ss[ply].pv[ply],
- VALUE_TYPE_EXACT);
}
+ else
+ TT.store(pos, value_to_tt(bestValue, ply), depth, ss[ply].pv[ply], VALUE_TYPE_EXACT);
return bestValue;
}
return beta-1;
// Transposition table lookup
- bool ttFound;
- Value ttValue;
- Depth ttDepth;
- Move ttMove = MOVE_NONE;
- ValueType ttValueType;
-
- ttFound = TT.retrieve(pos, &ttValue, &ttDepth, &ttMove, &ttValueType);
- if(ttFound) {
- ttValue = value_from_tt(ttValue, ply);
- if(ttDepth >= depth
- || ttValue >= Max(value_mate_in(100), beta)
- || ttValue < Min(value_mated_in(100), beta)) {
- if((is_lower_bound(ttValueType) && ttValue >= beta) ||
- (is_upper_bound(ttValueType) && ttValue < beta)) {
- ss[ply].currentMove = ttMove;
- return ttValue;
- }
- }
+ const TTEntry* tte = TT.retrieve(pos);
+
+ Move ttMove = (tte ? tte->move() : MOVE_NONE);
+
+ if (tte && ok_to_use_TT(tte, depth, beta, ply))
+ {
+ ss[ply].currentMove = ttMove; // can be MOVE_NONE ?
+ return value_from_tt(tte->value(), ply);
}
Value approximateEval = quick_evaluate(pos);
lock_release(&(sp->lock));
}
+ // ok_to_use_TT() returns true if a transposition table score
+ // can be used at a given point in search.
+
+ bool ok_to_use_TT(const TTEntry* tte, Depth depth, Value beta, int ply) {
+
+ Value v = value_from_tt(tte->value(), ply);
+
+ return ( tte->depth() >= depth
+ || v >= Max(value_mate_in(100), beta)
+ || v < Min(value_mated_in(100), beta))
+
+ && ( (is_lower_bound(tte->type()) && v >= beta)
+ || (is_upper_bound(tte->type()) && v < beta));
+ }
/// The RootMove class
nodes = cumulativeNodes = 0ULL;
}
+ // RootMove::operator<() is the comparison function used when
+ // sorting the moves. A move m1 is considered to be better
+ // than a move m2 if it has a higher score, or if the moves
+ // have equal score but m1 has the higher node count.
+
+ bool RootMove::operator<(const RootMove& m) {
+
+ if (score != m.score)
+ return (score < m.score);
+
+ return nodes <= m.nodes;
+ }
/// The RootMoveList class
// Simple accessor methods for the RootMoveList class
- Move RootMoveList::get_move(int moveNum) const {
+ inline Move RootMoveList::get_move(int moveNum) const {
return moves[moveNum].move;
}
- Value RootMoveList::get_move_score(int moveNum) const {
+ inline Value RootMoveList::get_move_score(int moveNum) const {
return moves[moveNum].score;
}
- void RootMoveList::set_move_score(int moveNum, Value score) {
+ inline void RootMoveList::set_move_score(int moveNum, Value score) {
moves[moveNum].score = score;
}
- void RootMoveList::set_move_nodes(int moveNum, int64_t nodes) {
+ inline void RootMoveList::set_move_nodes(int moveNum, int64_t nodes) {
moves[moveNum].nodes = nodes;
moves[moveNum].cumulativeNodes += nodes;
}
moves[moveNum].pv[j] = MOVE_NONE;
}
- Move RootMoveList::get_move_pv(int moveNum, int i) const {
+ inline Move RootMoveList::get_move_pv(int moveNum, int i) const {
return moves[moveNum].pv[i];
}
- int64_t RootMoveList::get_move_cumulative_nodes(int moveNum) {
+ inline int64_t RootMoveList::get_move_cumulative_nodes(int moveNum) const {
return moves[moveNum].cumulativeNodes;
}
- int RootMoveList::move_count() const {
+ inline int RootMoveList::move_count() const {
return count;
}
return MOVE_NONE;
}
-
- // RootMoveList::compare_root_moves() is the comparison function used by
- // RootMoveList::sort when sorting the moves. A move m1 is considered to
- // be better than a move m2 if it has a higher score, or if the moves have
- // equal score but m1 has the higher node count.
-
- bool RootMoveList::compare_root_moves(const RootMove &rm1,
- const RootMove &rm2) {
- if (rm1.score != rm2.score)
- return (rm1.score < rm2.score);
-
- return rm1.nodes <= rm2.nodes;
- }
-
-
// RootMoveList::sort() sorts the root move list at the beginning of a new
// iteration.
- void RootMoveList::sort() {
- for(int i = 1; i < count; i++) {
- RootMove rm = moves[i];
- int j;
- for(j = i; j > 0 && compare_root_moves(moves[j-1], rm); j--)
- moves[j] = moves[j-1];
- moves[j] = rm;
- }
+ inline void RootMoveList::sort() {
+
+ sort_multipv(count - 1); // all items
}
// RootMoveList::sort_multipv() sorts the first few moves in the root move
- // list by their scores and depths. It is used to order the different PVs
+ // list by their scores and depths. It is used to order the different PVs
// correctly in MultiPV mode.
void RootMoveList::sort_multipv(int n) {
- for(int i = 1; i <= n; i++) {
+
+ for (int i = 1; i <= n; i++)
+ {
RootMove rm = moves[i];
int j;
- for(j = i; j > 0 && moves[j-1].score < rm.score; j--)
- moves[j] = moves[j-1];
+ for (j = i; j > 0 && moves[j-1] < rm; j--)
+ moves[j] = moves[j-1];
moves[j] = rm;
}
}