/*
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
- Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
+ Copyright (C) 2008-2012 Marco Costalba, Joona Kiiski, Tord Romstad
Stockfish is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
RootMove(){}
RootMove(Move m) {
- nodes = 0;
score = prevScore = -VALUE_INFINITE;
pv.push_back(m);
pv.push_back(MOVE_NONE);
void extract_pv_from_tt(Position& pos);
void insert_pv_in_tt(Position& pos);
- int64_t nodes;
Value score;
Value prevScore;
std::vector<Move> pv;
int64_t Search::perft(Position& pos, Depth depth) {
StateInfo st;
- int64_t sum = 0;
+ int64_t cnt = 0;
MoveList<MV_LEGAL> ml(pos);
// At the last ply just return the number of moves (leaf nodes)
- if (depth <= ONE_PLY)
+ if (depth == ONE_PLY)
return ml.size();
CheckInfo ci(pos);
for ( ; !ml.end(); ++ml)
{
pos.do_move(ml.move(), st, ci, pos.move_gives_check(ml.move(), ci));
- sum += perft(pos, depth - ONE_PLY);
+ cnt += perft(pos, depth - ONE_PLY);
pos.undo_move(ml.move());
}
- return sum;
+ return cnt;
}
|| count(SearchMoves.begin(), SearchMoves.end(), ml.move()))
RootMoves.push_back(RootMove(ml.move()));
- if (Options["OwnBook"].value<bool>())
+ if (Options["OwnBook"])
{
- if (Options["Book File"].value<string>() != book.name())
- book.open(Options["Book File"].value<string>());
+ if (book.name() != (string)Options["Book File"])
+ book.open(Options["Book File"]);
- Move bookMove = book.probe(pos, Options["Best Book Move"].value<bool>());
+ Move bookMove = book.probe(pos, Options["Best Book Move"]);
if ( bookMove != MOVE_NONE
&& count(RootMoves.begin(), RootMoves.end(), bookMove))
read_evaluation_uci_options(pos.side_to_move());
Threads.read_uci_options();
- TT.set_size(Options["Hash"].value<int>());
- if (Options["Clear Hash"].value<bool>())
+ TT.set_size(Options["Hash"]);
+ if (Options["Clear Hash"])
{
Options["Clear Hash"] = false;
TT.clear();
}
- UCIMultiPV = Options["MultiPV"].value<size_t>();
- SkillLevel = Options["Skill Level"].value<int>();
+ UCIMultiPV = Options["MultiPV"];
+ SkillLevel = Options["Skill Level"];
// Do we have to play with skill handicap? In this case enable MultiPV that
// we will use behind the scenes to retrieve a set of possible moves.
SkillLevelEnabled = (SkillLevel < 20);
MultiPV = (SkillLevelEnabled ? std::max(UCIMultiPV, (size_t)4) : UCIMultiPV);
- if (Options["Use Search Log"].value<bool>())
+ if (Options["Use Search Log"])
{
- Log log(Options["Search Log Filename"].value<string>());
+ Log log(Options["Search Log Filename"]);
log << "\nSearching: " << pos.to_fen()
<< "\ninfinite: " << Limits.infinite
<< " ponder: " << Limits.ponder
Threads.set_timer(0);
Threads.set_size(1);
- if (Options["Use Search Log"].value<bool>())
+ if (Options["Use Search Log"])
{
int e = elapsed_time();
- Log log(Options["Search Log Filename"].value<string>());
+ Log log(Options["Search Log Filename"]);
log << "Nodes: " << pos.nodes_searched()
<< "\nNodes/second: " << (e > 0 ? pos.nodes_searched() * 1000 / e : 0)
<< "\nBest move: " << move_to_san(pos, RootMoves[0].pv[0]);
bestValue = delta = -VALUE_INFINITE;
ss->currentMove = MOVE_NULL; // Hack to skip update gains
- // Handle the special case of a mate/stalemate position
+ // Handle the special case of a mated/stalemate position
if (RootMoves.empty())
{
- cout << "info depth 0"
+ cout << "info depth 0 score "
<< score_to_uci(pos.in_check() ? -VALUE_MATE : VALUE_DRAW) << endl;
RootMoves.push_back(MOVE_NONE);
if (SkillLevelEnabled && depth == 1 + SkillLevel)
skillBest = do_skill_level();
- if (Options["Use Search Log"].value<bool>())
+ if (Options["Use Search Log"])
pv_info_to_log(pos, depth, bestValue, elapsed_time(), &RootMoves[0].pv[0]);
// Filter out startup noise when monitoring best move stability
const bool SpNode = (NT == SplitPointPV || NT == SplitPointNonPV || NT == SplitPointRoot);
const bool RootNode = (NT == Root || NT == SplitPointRoot);
- assert(alpha >= -VALUE_INFINITE && alpha <= VALUE_INFINITE);
- assert(beta > alpha && beta <= VALUE_INFINITE);
- assert(PvNode || alpha == beta - 1);
+ assert(alpha >= -VALUE_INFINITE && alpha < beta && beta <= VALUE_INFINITE);
+ assert(PvNode == (alpha != beta - 1));
+ assert(depth > DEPTH_ZERO);
assert(pos.thread() >= 0 && pos.thread() < Threads.size());
Move movesSearched[MAX_MOVES];
- int64_t nodes;
StateInfo st;
const TTEntry *tte;
Key posKey;
|| ss->ply > PLY_MAX) && !RootNode)
return VALUE_DRAW;
- // Step 3. Mate distance pruning
+ // Step 3. Mate distance pruning. Even if we mate at the next move our score
+ // would be at best mate_in(ss->ply+1), but if alpha is already bigger because
+ // a shorter mate was found upward in the tree then there is no need to search
+ // further, we will never beat current alpha. Same logic but with reversed signs
+ // applies also in the opposite condition of being mated instead of giving mate,
+ // in this case return a fail-high score.
if (!RootNode)
{
- alpha = std::max(value_mated_in(ss->ply), alpha);
- beta = std::min(value_mate_in(ss->ply+1), beta);
+ alpha = std::max(mated_in(ss->ply), alpha);
+ beta = std::min(mate_in(ss->ply+1), beta);
if (alpha >= beta)
return alpha;
}
if ( (move = (ss-1)->currentMove) != MOVE_NULL
&& (ss-1)->eval != VALUE_NONE
&& ss->eval != VALUE_NONE
- && pos.captured_piece_type() == PIECE_TYPE_NONE
+ && pos.captured_piece_type() == NO_PIECE_TYPE
&& !is_special(move))
{
Square to = move_to(move);
if (RootNode)
{
Signals.firstRootMove = (moveCount == 1);
- nodes = pos.nodes_searched();
if (pos.thread() == 0 && elapsed_time() > 2000)
cout << "info depth " << depth / ONE_PLY
if (RootNode && !Signals.stop)
{
RootMove& rm = *find(RootMoves.begin(), RootMoves.end(), move);
- rm.nodes += pos.nodes_searched() - nodes;
// PV move or new best move ?
if (isPvMove || value > alpha)
// harmless because return value is discarded anyhow in the parent nodes.
// If we are in a singular extension search then return a fail low score.
if (!moveCount)
- return excludedMove ? oldAlpha : inCheck ? value_mated_in(ss->ply) : VALUE_DRAW;
+ return excludedMove ? oldAlpha : inCheck ? mated_in(ss->ply) : VALUE_DRAW;
// If we have pruned all the moves without searching return a fail-low score
if (bestValue == -VALUE_INFINITE)
const bool PvNode = (NT == PV);
assert(NT == PV || NT == NonPV);
- assert(alpha >= -VALUE_INFINITE && alpha <= VALUE_INFINITE);
- assert(beta >= -VALUE_INFINITE && beta <= VALUE_INFINITE);
- assert(PvNode || alpha == beta - 1);
- assert(depth <= 0);
+ assert(alpha >= -VALUE_INFINITE && alpha < beta && beta <= VALUE_INFINITE);
+ assert(PvNode == (alpha != beta - 1));
+ assert(depth <= DEPTH_ZERO);
assert(pos.thread() >= 0 && pos.thread() < Threads.size());
StateInfo st;
// All legal moves have been searched. A special case: If we're in check
// and no legal moves were found, it is checkmate.
if (inCheck && bestValue == -VALUE_INFINITE)
- return value_mated_in(ss->ply);
+ return mated_in(ss->ply); // Plies to mate from the root
// Update transposition table
move = bestValue <= oldAlpha ? MOVE_NONE : ss->bestMove;
// value_to_tt() adjusts a mate score from "plies to mate from the root" to
- // "plies to mate from the current ply". Non-mate scores are unchanged. The
- // function is called before storing a value to the transposition table.
+ // "plies to mate from the current position". Non-mate scores are unchanged.
+ // The function is called before storing a value to the transposition table.
Value value_to_tt(Value v, int ply) {
}
- // value_from_tt() is the inverse of value_to_tt(): It adjusts a mate score from
- // the transposition table to a mate score corrected for the current ply.
+ // value_from_tt() is the inverse of value_to_tt(): It adjusts a mate score
+ // from the transposition table (where refers to the plies to mate/be mated
+ // from current position) to "plies to mate/be mated from the root".
Value value_from_tt(Value v, int ply) {
std::stringstream s;
- if (abs(v) < VALUE_MATE - PLY_MAX * ONE_PLY)
- s << " score cp " << int(v) * 100 / int(PawnValueMidgame); // Scale to centipawns
+ if (abs(v) < VALUE_MATE_IN_PLY_MAX)
+ s << "cp " << v * 100 / int(PawnValueMidgame);
else
- s << " score mate " << (v > 0 ? VALUE_MATE - v + 1 : -VALUE_MATE - v) / 2;
+ s << "mate " << (v > 0 ? VALUE_MATE - v + 1 : -VALUE_MATE - v) / 2;
s << (v >= beta ? " lowerbound" : v <= alpha ? " upperbound" : "");
cout << "info depth " << d
<< " seldepth " << selDepth
- << (i == PVIdx ? score_to_uci(v, alpha, beta) : score_to_uci(v))
+ << " score " << (i == PVIdx ? score_to_uci(v, alpha, beta) : score_to_uci(v))
<< " nodes " << pos.nodes_searched()
<< " nps " << (t > 0 ? pos.nodes_searched() * 1000 / t : 0)
<< " time " << t
while (m != pv)
pos.undo_move(*--m);
- Log l(Options["Search Log Filename"].value<string>());
+ Log l(Options["Search Log Filename"]);
l << s.str() << endl;
}