Skip to content

Commit

Permalink
Implemented PVS at root nodes, using previous' iteration move as firs…
Browse files Browse the repository at this point in the history
…t move
  • Loading branch information
crybot committed Jun 28, 2017
1 parent afdacb9 commit 3e55e41
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 23 deletions.
Binary file modified NapoleonPP
Binary file not shown.
82 changes: 63 additions & 19 deletions search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
namespace Napoleon
{
TranspositionTable Search::Table;
bool Search::pondering = false;
std::atomic<bool> Search::PonderHit(false);
std::atomic<bool> Search::StopSignal(true);
std::atomic<bool> Search::quit(false);
int Search::GameTime[2];
Expand All @@ -34,6 +36,12 @@ namespace Napoleon
int Search::cores;
const int Search::default_cores = 1;

int Search::predictTime(Color color)
{
int gameTime = GameTime[color];
return gameTime / 30 - (gameTime / (60 * 1000));
}

// direct interface to the client.
// it sends the move to the uci gui
Move Search::StartThinking(SearchType type, Board& board, bool verbose, bool san)
Expand All @@ -44,10 +52,12 @@ namespace Napoleon

sendOutput = verbose;
StopSignal = false;
pondering = false;
searchInfo.SetDepthLimit(depth_limit);

if (type == SearchType::Infinite)
if (type == SearchType::Infinite || type == SearchType::Ponder)
{
if (type == SearchType::Ponder) pondering = true;
searchInfo.NewSearch(); // default time = Time::Infinite
}
else
Expand All @@ -71,10 +81,23 @@ namespace Napoleon

if (sendOutput)
{
Move ponder = getPonderMove(board, move);
if (san)
Uci::SendCommand<Command::Move>(move.ToSan(board));
{
if (ponder.IsNull())
Uci::SendCommand<Command::Move>(move.ToSan(board));
else
Uci::SendCommand<Command::Move>(move.ToSan(board), ponder.ToSan(board));
}
else
Uci::SendCommand<Command::Move>(move.ToAlgebraic());
{
if (ponder.IsNull())
Uci::SendCommand<Command::Move>(move.ToAlgebraic());
else
Uci::SendCommand<Command::Move>(move.ToAlgebraic(), ponder.ToAlgebraic());

}

}

searchInfo.StopSearch();
Expand Down Expand Up @@ -166,10 +189,16 @@ namespace Napoleon
score = searchRoot(searchInfo.MaxDepth(), -Constants::Infinity, Constants::Infinity, move, board);
searchInfo.IncrementDepth();

while ((searchInfo.MaxDepth() < 100 && !searchInfo.TimeOver()))
while ((searchInfo.MaxDepth() < 100 && !searchInfo.TimeOver()) || pondering)
{
if (StopSignal)
break;
if(PonderHit)
{
searchInfo.SetGameTime(predictTime(board.SideToMove()));
PonderHit = false;
pondering = false;
}

searchInfo.MaxPly = 0;
searchInfo.ResetNodes();
Expand All @@ -181,13 +210,10 @@ namespace Napoleon
temp = searchRoot(searchInfo.MaxDepth(), score - AspirationValue, score + AspirationValue, move, board);

if (temp <= score - AspirationValue)
{
temp = searchRoot(searchInfo.MaxDepth(), -Constants::Infinity, score + AspirationValue, move, board);
}
else if (temp >= score + AspirationValue)
{

if (temp >= score + AspirationValue)
temp = searchRoot(searchInfo.MaxDepth(), score - AspirationValue, Constants::Infinity, move, board);
}

score = temp;

Expand Down Expand Up @@ -218,6 +244,7 @@ namespace Napoleon
}

moves.Sort<false>();
moves.hashMove = moveToMake;

int i = 0;
for (auto move = moves.First(); !move.IsNull(); move = moves.Next(), i++)
Expand All @@ -229,7 +256,12 @@ namespace Napoleon
if (i == 0) // leftmost node
score = -Search::search<NodeType::PV>(depth - 1, -beta, -alpha, 1, board, false); // pv node
else
score = -Search::search<NodeType::NONPV>(depth - 1, -beta, -alpha, 1, board, true); // cut node
{
score = -search<NodeType::NONPV>(depth - 1, -alpha - 1, -alpha, 1, board, true);

if (score > alpha)
score = -search<NodeType::PV>(depth - 1, -beta, -alpha, 1, board, false);
}
board.UndoMove(move);

if (score > alpha)
Expand Down Expand Up @@ -395,15 +427,16 @@ namespace Napoleon

MoveSelector moves(board, searchInfo);

if (moves.count == 1) // forced move extension
{
assert(false);
extension = true;
++depth;
}

MoveGenerator::GetPseudoLegalMoves<false>(moves.moves, moves.count, attackers, board); // get captures and non-captures

/*
if (moves.count == 1) // forced move extension
{
extension = true;
++depth;
}
*/

moves.Sort<false>(ply);
moves.hashMove = best;

Expand Down Expand Up @@ -519,13 +552,13 @@ namespace Napoleon
{
R = 1;

if (eval + board.MaterialBalance(Utils::Piece::GetOpposite(board.SideToMove())) + newDepth * 250 <= alpha)
if (moveNumber > 9)
{
R = 2;
}
}

newDepth = depth - R;
newDepth = std::max(1, depth - R);

score = -search<NodeType::NONPV>(newDepth - 1, -alpha - 1, -alpha, ply + 1, board, !cut_node);

Expand Down Expand Up @@ -703,4 +736,15 @@ namespace Napoleon

return info.str();
}

Move Search::getPonderMove(Board& board, const Move toMake)
{
Move move = Constants::NullMove;
board.MakeMove(toMake);
move = Table.GetPv(board.zobrist);
board.UndoMove(toMake);

return move;
}

}
6 changes: 5 additions & 1 deletion search.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Napoleon
{
enum class SearchType
{
Infinite, TimePerGame, TimePerMove
Infinite, TimePerGame, TimePerMove, Ponder
};

enum class NodeType
Expand All @@ -28,6 +28,8 @@ namespace Napoleon
namespace Search
{
extern const int AspirationValue;
extern bool pondering;
extern std::atomic<bool> PonderHit;
extern std::atomic<bool> StopSignal;
extern int MoveTime;
extern int GameTime[2]; // by color
Expand All @@ -46,9 +48,11 @@ namespace Napoleon
void KillThreads();
void signalThreads(int, int, int, const Board&, bool);
void parallelSearch();
int predictTime(Color);

std::string GetInfo(Board&, Move, int, int, int);
std::string GetPv(Board&, Move, int);
Move getPonderMove(Board&, const Move);

Move StartThinking(SearchType, Board&, bool=true, bool=false);
void StopThinking();
Expand Down
6 changes: 6 additions & 0 deletions searchinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,11 @@ namespace Napoleon
nodes = 0;
}

void SearchInfo::SetGameTime(int time)
{
allocatedTime = time;
timer.Restart();
}


}
1 change: 1 addition & 0 deletions searchinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace Napoleon
void SetKillers(Move, int);
void SetHistory(Move, Color, int);
void SetDepthLimit(int);
void SetGameTime(int);

Move FirstKiller(int);
Move SecondKiller(int);
Expand Down
1 change: 1 addition & 0 deletions transpositiontable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ namespace Napoleon
std::memset(table, 0, entries*sizeof(HashEntry));
}

//TODO return BEST pv move (exact score)
Move TranspositionTable::GetPv(ZobristKey key)
{
auto hash = at(key);
Expand Down
10 changes: 10 additions & 0 deletions uci.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ namespace Napoleon
}
else if (cmd == "position")
{
//Search::StopThinking();
Move move;
string token;
stream >> token;
Expand Down Expand Up @@ -137,6 +138,11 @@ namespace Napoleon
if (Search::StopSignal)
go(stream);
}
else if (cmd == "ponderhit")
{
assert(false);
Search::PonderHit = true;
}
}
}

Expand Down Expand Up @@ -173,6 +179,10 @@ namespace Napoleon
{
type = SearchType::Infinite;
}
else if (token == "ponder")
{
type = SearchType::Ponder;
}

}

Expand Down
8 changes: 5 additions & 3 deletions uci.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,25 @@ namespace Napoleon
void Start();

template<Command>
void SendCommand(std::string);
void SendCommand(std::string, std::string="");
void go(std::istringstream&);

extern Board board;
extern std::thread search;
}

template<Command cmdType>
void Uci::SendCommand(std::string command)
void Uci::SendCommand(std::string command, std::string ponder)
{
switch(cmdType)
{
case Command::Generic:
std::cout << command << std::endl;
break;
case Command::Move:
std::cout << "bestmove " << command << std::endl;
std::cout << "bestmove " << command;
if(!ponder.empty()) std::cout << " ponder " << ponder;
std::cout << std::endl;
break;
case Command::Info:
std::cout << "info " << command << std::endl;
Expand Down

0 comments on commit 3e55e41

Please sign in to comment.