2011-11-12 20:44:06 +01:00
|
|
|
/*
|
|
|
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
|
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
2015-02-01 01:46:09 +01:00
|
|
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
2016-01-02 23:16:48 +01:00
|
|
|
Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
2011-11-12 20:44:06 +01:00
|
|
|
|
|
|
|
Stockfish is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Stockfish is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2013-08-20 20:02:33 +02:00
|
|
|
#ifndef SEARCH_H_INCLUDED
|
2011-11-12 20:44:06 +01:00
|
|
|
#define SEARCH_H_INCLUDED
|
|
|
|
|
2015-12-27 23:25:14 +01:00
|
|
|
#include <atomic>
|
|
|
|
#include <memory> // For std::unique_ptr
|
2012-09-16 17:16:15 +02:00
|
|
|
#include <stack>
|
2012-01-01 01:52:19 +01:00
|
|
|
#include <vector>
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2012-06-04 18:25:51 +02:00
|
|
|
#include "misc.h"
|
2012-09-16 17:16:15 +02:00
|
|
|
#include "position.h"
|
2011-11-12 20:44:06 +01:00
|
|
|
#include "types.h"
|
|
|
|
|
2012-01-01 01:52:19 +01:00
|
|
|
namespace Search {
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2015-02-01 01:46:09 +01:00
|
|
|
/// Stack struct keeps track of the information we need to remember from nodes
|
|
|
|
/// shallower and deeper in the tree during the search. Each search thread has
|
|
|
|
/// its own array of Stack objects, indexed by the current ply.
|
2012-01-01 01:52:19 +01:00
|
|
|
|
|
|
|
struct Stack {
|
2015-02-01 01:46:09 +01:00
|
|
|
Move* pv;
|
2011-11-12 20:44:06 +01:00
|
|
|
int ply;
|
|
|
|
Move currentMove;
|
|
|
|
Move excludedMove;
|
|
|
|
Move killers[2];
|
2013-05-03 19:03:42 +02:00
|
|
|
Value staticEval;
|
2015-02-01 01:46:09 +01:00
|
|
|
bool skipEarlyPruning;
|
2015-10-23 22:58:14 +02:00
|
|
|
int moveCount;
|
2011-11-12 20:44:06 +01:00
|
|
|
};
|
|
|
|
|
2015-02-01 01:46:09 +01:00
|
|
|
/// RootMove struct is used for moves at the root of the tree. For each root move
|
|
|
|
/// we store a score and a PV (really a refutation in the case of moves which
|
|
|
|
/// fail low). Score is normally set at -VALUE_INFINITE for all non-pv moves.
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2012-01-15 02:13:33 +01:00
|
|
|
struct RootMove {
|
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
explicit RootMove(Move m) : pv(1, m) {}
|
2012-01-15 02:13:33 +01:00
|
|
|
|
2015-12-27 23:25:14 +01:00
|
|
|
bool operator<(const RootMove& m) const { return m.score < score; } // Descending sort
|
2012-01-15 02:13:33 +01:00
|
|
|
bool operator==(const Move& m) const { return pv[0] == m; }
|
|
|
|
void insert_pv_in_tt(Position& pos);
|
2015-10-23 22:58:14 +02:00
|
|
|
bool extract_ponder_from_tt(Position& pos);
|
2012-01-15 02:13:33 +01:00
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
Value score = -VALUE_INFINITE;
|
|
|
|
Value previousScore = -VALUE_INFINITE;
|
2012-01-15 02:13:33 +01:00
|
|
|
std::vector<Move> pv;
|
|
|
|
};
|
|
|
|
|
2015-02-01 01:46:09 +01:00
|
|
|
typedef std::vector<RootMove> RootMoveVector;
|
2012-01-15 02:13:33 +01:00
|
|
|
|
2015-02-01 01:46:09 +01:00
|
|
|
/// LimitsType struct stores information sent by GUI about available time to
|
|
|
|
/// search the current move, maximum depth/time, if we are in analysis mode or
|
|
|
|
/// if we have to ponder while it's our opponent's turn to move.
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2012-01-01 01:52:19 +01:00
|
|
|
struct LimitsType {
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2015-02-01 01:46:09 +01:00
|
|
|
LimitsType() { // Init explicitly due to broken value-initialization of non POD in MSVC
|
2015-10-23 22:58:14 +02:00
|
|
|
nodes = time[WHITE] = time[BLACK] = inc[WHITE] = inc[BLACK] = npmsec = movestogo =
|
2015-02-01 01:46:09 +01:00
|
|
|
depth = movetime = mate = infinite = ponder = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool use_time_management() const {
|
|
|
|
return !(mate | movetime | depth | nodes | infinite);
|
2014-05-31 14:23:03 +02:00
|
|
|
}
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2014-05-31 14:23:03 +02:00
|
|
|
std::vector<Move> searchmoves;
|
2015-10-23 22:58:14 +02:00
|
|
|
int time[COLOR_NB], inc[COLOR_NB], npmsec, movestogo, depth, movetime, mate, infinite, ponder;
|
2015-02-01 01:46:09 +01:00
|
|
|
int64_t nodes;
|
2015-10-23 22:58:14 +02:00
|
|
|
TimePoint startTime;
|
2012-01-01 01:52:19 +01:00
|
|
|
};
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2015-12-27 23:25:14 +01:00
|
|
|
/// The SignalsType struct stores atomic flags updated during the search
|
2014-05-31 14:23:03 +02:00
|
|
|
/// typically in an async fashion e.g. to stop the search by the GUI.
|
2012-01-01 01:52:19 +01:00
|
|
|
|
|
|
|
struct SignalsType {
|
2015-12-27 23:25:14 +01:00
|
|
|
std::atomic_bool stop, stopOnPonderhit;
|
2011-11-12 20:44:06 +01:00
|
|
|
};
|
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
typedef std::unique_ptr<std::stack<StateInfo>> StateStackPtr;
|
2012-09-16 17:16:15 +02:00
|
|
|
|
2015-12-27 23:25:14 +01:00
|
|
|
extern SignalsType Signals;
|
2012-01-01 01:52:19 +01:00
|
|
|
extern LimitsType Limits;
|
2012-09-16 17:16:15 +02:00
|
|
|
extern StateStackPtr SetupStates;
|
2012-01-01 01:52:19 +01:00
|
|
|
|
2015-02-01 01:46:09 +01:00
|
|
|
void init();
|
2015-12-27 23:25:14 +01:00
|
|
|
void clear();
|
|
|
|
template<bool Root = true> uint64_t perft(Position& pos, Depth depth);
|
2012-01-01 01:52:19 +01:00
|
|
|
|
2012-01-15 02:13:33 +01:00
|
|
|
} // namespace Search
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2013-08-20 20:02:33 +02:00
|
|
|
#endif // #ifndef SEARCH_H_INCLUDED
|