Skip to content

Commit

Permalink
Use less time on recaptures, credit to peregrine in Discord.
Browse files Browse the repository at this point in the history
Bench 1836777
  • Loading branch information
xoto10 committed Apr 27, 2024
1 parent 49ef4c9 commit bdf7ba1
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 17 deletions.
11 changes: 9 additions & 2 deletions src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,12 @@ void Engine::set_on_bestmove(std::function<void(std::string_view, std::string_vi

void Engine::wait_for_search_finished() { threads.main_thread()->wait_for_search_finished(); }

void Engine::set_position(const std::string& fen, const std::vector<std::string>& moves) {
Square Engine::set_position(const std::string& fen, const std::vector<std::string>& moves) {
// Drop the old state and create a new one
states = StateListPtr(new std::deque<StateInfo>(1));
pos.set(fen, options["UCI_Chess960"], &states->back());

Square capSq = SQ_NONE;
for (const auto& move : moves)
{
auto m = UCIEngine::to_move(pos, move);
Expand All @@ -111,7 +112,13 @@ void Engine::set_position(const std::string& fen, const std::vector<std::string>

states->emplace_back();
pos.do_move(m, states->back());

capSq = SQ_NONE;
DirtyPiece& dp = states->back().dirtyPiece;
if (dp.dirty_num > 1 && dp.to[1] == SQ_NONE)
capSq = m.to_sq();
}
return capSq;
}

// modifiers
Expand Down Expand Up @@ -172,4 +179,4 @@ std::string Engine::visualize() const {
return ss.str();
}

}
}
5 changes: 3 additions & 2 deletions src/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ class Engine {
// blocking call to wait for search to finish
void wait_for_search_finished();
// set a new position, moves are in UCI format
void set_position(const std::string& fen, const std::vector<std::string>& moves);
// If last of moves is a capture, the capture square is returned
Square set_position(const std::string& fen, const std::vector<std::string>& moves);

// modifiers

Expand Down Expand Up @@ -104,4 +105,4 @@ class Engine {
} // namespace Stockfish


#endif // #ifndef ENGINE_H_INCLUDED
#endif // #ifndef ENGINE_H_INCLUDED
4 changes: 3 additions & 1 deletion src/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,9 +446,11 @@ void Search::Worker::iterative_deepening() {
double reduction = (1.48 + mainThread->previousTimeReduction) / (2.17 * timeReduction);
double bestMoveInstability = 1 + 1.88 * totBestMoveChanges / threads.size();
int el = std::clamp((bestValue + 750) / 150, 0, 9);
double recapture =
limits.capSq != SQ_NONE && limits.capSq == rootMoves[0].pv[0].to_sq() ? 0.898 : 0.945;

double totalTime = mainThread->tm.optimum() * fallingEval * reduction
* bestMoveInstability * EvalLevel[el];
* bestMoveInstability * EvalLevel[el] * recapture;

// Cap used time in case of a single legal move for a better viewer experience
if (rootMoves.size() == 1)
Expand Down
1 change: 1 addition & 0 deletions src/search.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ struct LimitsType {
int movestogo, depth, mate, perft, infinite;
uint64_t nodes;
bool ponderMode;
Square capSq;
};


Expand Down
20 changes: 11 additions & 9 deletions src/uci.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ UCIEngine::UCIEngine(int argc, char** argv) :
void UCIEngine::loop() {

std::string token, cmd;
Square capSq = SQ_NONE;

for (int i = 1; i < cli.argc; ++i)
cmd += std::string(cli.argv[i]) + " ";
Expand Down Expand Up @@ -129,9 +130,9 @@ void UCIEngine::loop() {
else if (token == "setoption")
setoption(is);
else if (token == "go")
go(is);
go(is, capSq);
else if (token == "position")
position(is);
capSq = position(is);
else if (token == "ucinewgame")
engine.search_clear();
else if (token == "isready")
Expand Down Expand Up @@ -177,11 +178,12 @@ void UCIEngine::loop() {
} while (token != "quit" && cli.argc == 1); // The command-line arguments are one-shot
}

Search::LimitsType UCIEngine::parse_limits(std::istream& is) {
Search::LimitsType UCIEngine::parse_limits(std::istream& is, Square capSq) {
Search::LimitsType limits;
std::string token;

limits.startTime = now(); // The search starts as early as possible
limits.capSq = capSq;

while (is >> token)
if (token == "searchmoves") // Needs to be the last command on the line
Expand Down Expand Up @@ -216,9 +218,9 @@ Search::LimitsType UCIEngine::parse_limits(std::istream& is) {
return limits;
}

void UCIEngine::go(std::istringstream& is) {
void UCIEngine::go(std::istringstream& is, Square capSq) {

Search::LimitsType limits = parse_limits(is);
Search::LimitsType limits = parse_limits(is, capSq);

if (limits.perft)
perft(limits);
Expand Down Expand Up @@ -255,7 +257,7 @@ void UCIEngine::bench(std::istream& args) {
<< std::endl;
if (token == "go")
{
Search::LimitsType limits = parse_limits(is);
Search::LimitsType limits = parse_limits(is, SQ_NONE);

if (limits.perft)
nodes = perft(limits);
Expand Down Expand Up @@ -306,7 +308,7 @@ std::uint64_t UCIEngine::perft(const Search::LimitsType& limits) {
return nodes;
}

void UCIEngine::position(std::istringstream& is) {
Square UCIEngine::position(std::istringstream& is) {
std::string token, fen;

is >> token;
Expand All @@ -320,7 +322,7 @@ void UCIEngine::position(std::istringstream& is) {
while (is >> token && token != "moves")
fen += token + " ";
else
return;
return SQ_NONE;

std::vector<std::string> moves;

Expand All @@ -329,7 +331,7 @@ void UCIEngine::position(std::istringstream& is) {
moves.push_back(token);
}

engine.set_position(fen, moves);
return engine.set_position(fen, moves);
}

namespace {
Expand Down
6 changes: 3 additions & 3 deletions src/uci.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,18 @@ class UCIEngine {
static std::string to_lower(std::string str);
static Move to_move(const Position& pos, std::string str);

static Search::LimitsType parse_limits(std::istream& is);
static Search::LimitsType parse_limits(std::istream& is, Square capSq);

auto& engine_options() { return engine.get_options(); }

private:
Engine engine;
CommandLine cli;

void go(std::istringstream& is);
void go(std::istringstream& is, Square capSq);
void bench(std::istream& args);
void position(std::istringstream& is);
void setoption(std::istringstream& is);
Square position(std::istringstream& is);
std::uint64_t perft(const Search::LimitsType&);

static void on_update_no_moves(const Engine::InfoShort& info);
Expand Down

0 comments on commit bdf7ba1

Please sign in to comment.