mirror of
https://github.com/official-stockfish/Stockfish.git
synced 2026-07-23 21:27:14 +00:00
Part 2 of the Split UCI into UCIEngine and Engine refactor. This creates function callbacks for search to use when an update should occur. The benching in uci.cpp for example does this to extract the total nodes searched. No functional change
161 lines
4.6 KiB
C++
161 lines
4.6 KiB
C++
/*
|
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
Copyright (C) 2004-2024 The Stockfish developers (see AUTHORS file)
|
|
|
|
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/>.
|
|
*/
|
|
|
|
#include "engine.h"
|
|
|
|
#include <deque>
|
|
#include <memory>
|
|
#include <ostream>
|
|
#include <string_view>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
#include "evaluate.h"
|
|
#include "misc.h"
|
|
#include "nnue/network.h"
|
|
#include "nnue/nnue_common.h"
|
|
#include "perft.h"
|
|
#include "position.h"
|
|
#include "search.h"
|
|
#include "syzygy/tbprobe.h"
|
|
#include "types.h"
|
|
#include "uci.h"
|
|
#include "ucioption.h"
|
|
|
|
namespace Stockfish {
|
|
|
|
namespace NN = Eval::NNUE;
|
|
|
|
constexpr auto StartFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
|
|
|
|
Engine::Engine(std::string path) :
|
|
binaryDirectory(CommandLine::get_binary_directory(path)),
|
|
states(new std::deque<StateInfo>(1)),
|
|
networks(NN::Networks(
|
|
NN::NetworkBig({EvalFileDefaultNameBig, "None", ""}, NN::EmbeddedNNUEType::BIG),
|
|
NN::NetworkSmall({EvalFileDefaultNameSmall, "None", ""}, NN::EmbeddedNNUEType::SMALL))) {
|
|
pos.set(StartFEN, false, &states->back());
|
|
}
|
|
|
|
void Engine::go(const Search::LimitsType& limits) {
|
|
verify_networks();
|
|
|
|
if (limits.perft)
|
|
{
|
|
perft(pos.fen(), limits.perft, options["UCI_Chess960"]);
|
|
return;
|
|
}
|
|
|
|
threads.start_thinking(options, pos, states, limits);
|
|
}
|
|
void Engine::stop() { threads.stop = true; }
|
|
|
|
void Engine::search_clear() {
|
|
wait_for_search_finished();
|
|
|
|
tt.clear(options["Threads"]);
|
|
threads.clear();
|
|
|
|
// @TODO wont work with multiple instances
|
|
Tablebases::init(options["SyzygyPath"]); // Free mapped files
|
|
}
|
|
|
|
void Engine::set_on_update_no_moves(std::function<void(const Engine::InfoShort&)>&& f) {
|
|
updateContext.onUpdateNoMoves = std::move(f);
|
|
}
|
|
|
|
void Engine::set_on_update_full(std::function<void(const Engine::InfoFull&)>&& f) {
|
|
updateContext.onUpdateFull = std::move(f);
|
|
}
|
|
|
|
void Engine::set_on_iter(std::function<void(const Engine::InfoIter&)>&& f) {
|
|
updateContext.onIter = std::move(f);
|
|
}
|
|
|
|
void Engine::set_on_bestmove(std::function<void(std::string_view, std::string_view)>&& f) {
|
|
updateContext.onBestmove = std::move(f);
|
|
}
|
|
|
|
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) {
|
|
// Drop the old state and create a new one
|
|
states = StateListPtr(new std::deque<StateInfo>(1));
|
|
pos.set(fen, options["UCI_Chess960"], &states->back());
|
|
|
|
for (const auto& move : moves)
|
|
{
|
|
auto m = UCIEngine::to_move(pos, move);
|
|
|
|
if (m == Move::none())
|
|
break;
|
|
|
|
states->emplace_back();
|
|
pos.do_move(m, states->back());
|
|
}
|
|
}
|
|
|
|
// modifiers
|
|
|
|
void Engine::resize_threads() { threads.set({options, threads, tt, networks}, updateContext); }
|
|
|
|
void Engine::set_tt_size(size_t mb) {
|
|
wait_for_search_finished();
|
|
tt.resize(mb, options["Threads"]);
|
|
}
|
|
|
|
void Engine::set_ponderhit(bool b) { threads.main_manager()->ponder = b; }
|
|
|
|
// network related
|
|
|
|
void Engine::verify_networks() const {
|
|
networks.big.verify(options["EvalFile"]);
|
|
networks.small.verify(options["EvalFileSmall"]);
|
|
}
|
|
|
|
void Engine::load_networks() {
|
|
networks.big.load(binaryDirectory, options["EvalFile"]);
|
|
networks.small.load(binaryDirectory, options["EvalFileSmall"]);
|
|
}
|
|
|
|
void Engine::load_big_network(const std::string& file) { networks.big.load(binaryDirectory, file); }
|
|
|
|
void Engine::load_small_network(const std::string& file) {
|
|
networks.small.load(binaryDirectory, file);
|
|
}
|
|
|
|
void Engine::save_network(const std::pair<std::optional<std::string>, std::string> files[2]) {
|
|
networks.big.save(files[0].first);
|
|
networks.small.save(files[1].first);
|
|
}
|
|
|
|
// utility functions
|
|
|
|
OptionsMap& Engine::get_options() { return options; }
|
|
|
|
void Engine::trace_eval() const {
|
|
StateListPtr trace_states(new std::deque<StateInfo>(1));
|
|
Position p;
|
|
p.set(pos.fen(), options["UCI_Chess960"], &trace_states->back());
|
|
|
|
verify_networks();
|
|
|
|
sync_cout << "\n" << Eval::trace(p, networks) << sync_endl;
|
|
}
|
|
|
|
} |