2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
5 Stockfish is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 Stockfish is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
30 #include "hashprobe.h"
31 #include "syzygy/tbprobe.h"
37 UCI::OptionsMap Options; // Global object
38 std::unique_ptr<HashProbeThread> hash_probe_thread;
42 /// 'On change' actions, triggered by an option's value change
43 void on_clear_hash(const Option&) { Search::clear(); }
44 void on_hash_size(const Option& o) { TT.resize(size_t(o)); }
45 void on_logger(const Option& o) { start_logger(o); }
46 void on_threads(const Option& o) { Threads.set(size_t(o)); }
47 void on_tb_path(const Option& o) { Tablebases::init(o); }
48 void on_use_NNUE(const Option& ) { Eval::NNUE::init(); }
49 void on_eval_file(const Option& ) { Eval::NNUE::init(); }
50 void on_rpc_server_address(const Option& o) {
51 if (hash_probe_thread) {
52 hash_probe_thread->Shutdown();
55 hash_probe_thread.reset(new HashProbeThread(addr));
58 /// Our case insensitive less() function as required by UCI protocol
59 bool CaseInsensitiveLess::operator() (const string& s1, const string& s2) const {
61 return std::lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end(),
62 [](char c1, char c2) { return tolower(c1) < tolower(c2); });
66 /// UCI::init() initializes the UCI options to their hard-coded default values
68 void init(OptionsMap& o) {
70 constexpr int MaxHashMB = Is64Bit ? 33554432 : 2048;
72 o["Debug Log File"] << Option("", on_logger);
73 o["Threads"] << Option(1, 1, 512, on_threads);
74 o["Hash"] << Option(16, 1, MaxHashMB, on_hash_size);
75 o["Clear Hash"] << Option(on_clear_hash);
76 o["Ponder"] << Option(false);
77 o["MultiPV"] << Option(1, 1, 500);
78 o["Skill Level"] << Option(20, 0, 20);
79 o["Move Overhead"] << Option(10, 0, 5000);
80 o["Slow Mover"] << Option(100, 10, 1000);
81 o["nodestime"] << Option(0, 0, 10000);
82 o["UCI_Chess960"] << Option(false);
83 o["UCI_AnalyseMode"] << Option(false);
84 o["UCI_LimitStrength"] << Option(false);
85 o["UCI_Elo"] << Option(1350, 1350, 2850);
86 o["UCI_ShowWDL"] << Option(false);
87 o["SyzygyPath"] << Option("<empty>", on_tb_path);
88 o["SyzygyProbeDepth"] << Option(1, 1, 100);
89 o["Syzygy50MoveRule"] << Option(true);
90 o["SyzygyProbeLimit"] << Option(7, 0, 7);
91 o["Use NNUE"] << Option(true, on_use_NNUE);
92 o["EvalFile"] << Option(EvalFileDefaultName, on_eval_file);
93 o["RPCServerAddress"] << Option("<empty>", on_rpc_server_address);
97 /// operator<<() is used to print all the options default values in chronological
98 /// insertion order (the idx field) and in the format defined by the UCI protocol.
100 std::ostream& operator<<(std::ostream& os, const OptionsMap& om) {
102 for (size_t idx = 0; idx < om.size(); ++idx)
103 for (const auto& it : om)
104 if (it.second.idx == idx)
106 const Option& o = it.second;
107 os << "\noption name " << it.first << " type " << o.type;
109 if (o.type == "string" || o.type == "check" || o.type == "combo")
110 os << " default " << o.defaultValue;
112 if (o.type == "spin")
113 os << " default " << int(stof(o.defaultValue))
124 /// Option class constructors and conversion operators
126 Option::Option(const char* v, OnChange f) : type("string"), min(0), max(0), on_change(f)
127 { defaultValue = currentValue = v; }
129 Option::Option(bool v, OnChange f) : type("check"), min(0), max(0), on_change(f)
130 { defaultValue = currentValue = (v ? "true" : "false"); }
132 Option::Option(OnChange f) : type("button"), min(0), max(0), on_change(f)
135 Option::Option(double v, int minv, int maxv, OnChange f) : type("spin"), min(minv), max(maxv), on_change(f)
136 { defaultValue = currentValue = std::to_string(v); }
138 Option::Option(const char* v, const char* cur, OnChange f) : type("combo"), min(0), max(0), on_change(f)
139 { defaultValue = v; currentValue = cur; }
141 Option::operator double() const {
142 assert(type == "check" || type == "spin");
143 return (type == "spin" ? stof(currentValue) : currentValue == "true");
146 Option::operator std::string() const {
147 assert(type == "string");
151 bool Option::operator==(const char* s) const {
152 assert(type == "combo");
153 return !CaseInsensitiveLess()(currentValue, s)
154 && !CaseInsensitiveLess()(s, currentValue);
158 /// operator<<() inits options and assigns idx in the correct printing order
160 void Option::operator<<(const Option& o) {
162 static size_t insert_order = 0;
165 idx = insert_order++;
169 /// operator=() updates currentValue and triggers on_change() action. It's up to
170 /// the GUI to check for option's limits, but we could receive the new value
171 /// from the user by console window, so let's check the bounds anyway.
173 Option& Option::operator=(const string& v) {
175 assert(!type.empty());
177 if ( (type != "button" && type != "string" && v.empty())
178 || (type == "check" && v != "true" && v != "false")
179 || (type == "spin" && (stof(v) < min || stof(v) > max)))
184 OptionsMap comboMap; // To have case insensitive compare
186 std::istringstream ss(defaultValue);
188 comboMap[token] << Option();
189 if (!comboMap.count(v) || v == "var")
193 if (type != "button")
204 } // namespace Stockfish