2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2023 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/>.
33 #include "syzygy/tbprobe.h"
43 UCI::OptionsMap Options; // Global object
47 // 'On change' actions, triggered by an option's value change
48 static void on_clear_hash(const Option&) { Search::clear(); }
49 static void on_hash_size(const Option& o) { TT.resize(size_t(o)); }
50 static void on_logger(const Option& o) { start_logger(o); }
51 static void on_threads(const Option& o) { Threads.set(size_t(o)); }
52 static void on_tb_path(const Option& o) { Tablebases::init(o); }
53 static void on_eval_file(const Option&) { Eval::NNUE::init(); }
55 // Our case insensitive less() function as required by UCI protocol
56 bool CaseInsensitiveLess::operator()(const string& s1, const string& s2) const {
58 return std::lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end(),
59 [](char c1, char c2) { return tolower(c1) < tolower(c2); });
63 // Initializes the UCI options to their hard-coded default values
64 void init(OptionsMap& o) {
66 constexpr int MaxHashMB = Is64Bit ? 33554432 : 2048;
68 o["Debug Log File"] << Option("", on_logger);
69 o["Threads"] << Option(1, 1, 1024, on_threads);
70 o["Hash"] << Option(16, 1, MaxHashMB, on_hash_size);
71 o["Clear Hash"] << Option(on_clear_hash);
72 o["Ponder"] << Option(false);
73 o["MultiPV"] << Option(1, 1, 500);
74 o["Skill Level"] << Option(20, 0, 20);
75 o["Move Overhead"] << Option(10, 0, 5000);
76 o["Slow Mover"] << Option(100, 10, 1000);
77 o["nodestime"] << Option(0, 0, 10000);
78 o["UCI_Chess960"] << Option(false);
79 o["UCI_AnalyseMode"] << Option(false);
80 o["UCI_LimitStrength"] << Option(false);
81 o["UCI_Elo"] << Option(1320, 1320, 3190);
82 o["UCI_ShowWDL"] << Option(false);
83 o["SyzygyPath"] << Option("<empty>", on_tb_path);
84 o["SyzygyProbeDepth"] << Option(1, 1, 100);
85 o["Syzygy50MoveRule"] << Option(true);
86 o["SyzygyProbeLimit"] << Option(7, 0, 7);
87 o["EvalFile"] << Option(EvalFileDefaultName, on_eval_file);
91 // Used to print all the options default values in chronological
92 // insertion order (the idx field) and in the format defined by the UCI protocol.
93 std::ostream& operator<<(std::ostream& os, const OptionsMap& om) {
95 for (size_t idx = 0; idx < om.size(); ++idx)
96 for (const auto& it : om)
97 if (it.second.idx == idx)
99 const Option& o = it.second;
100 os << "\noption name " << it.first << " type " << o.type;
102 if (o.type == "string" || o.type == "check" || o.type == "combo")
103 os << " default " << o.defaultValue;
105 if (o.type == "spin")
106 os << " default " << int(stof(o.defaultValue)) << " min " << o.min << " max "
116 // Option class constructors and conversion operators
118 Option::Option(const char* v, OnChange f) :
123 defaultValue = currentValue = v;
126 Option::Option(bool v, OnChange f) :
131 defaultValue = currentValue = (v ? "true" : "false");
134 Option::Option(OnChange f) :
140 Option::Option(double v, int minv, int maxv, OnChange f) :
145 defaultValue = currentValue = std::to_string(v);
148 Option::Option(const char* v, const char* cur, OnChange f) :
157 Option::operator int() const {
158 assert(type == "check" || type == "spin");
159 return (type == "spin" ? std::stoi(currentValue) : currentValue == "true");
162 Option::operator std::string() const {
163 assert(type == "string");
167 bool Option::operator==(const char* s) const {
168 assert(type == "combo");
169 return !CaseInsensitiveLess()(currentValue, s) && !CaseInsensitiveLess()(s, currentValue);
173 // Inits options and assigns idx in the correct printing order
175 void Option::operator<<(const Option& o) {
177 static size_t insert_order = 0;
180 idx = insert_order++;
184 // Updates currentValue and triggers on_change() action. It's up to
185 // the GUI to check for option's limits, but we could receive the new value
186 // from the user by console window, so let's check the bounds anyway.
187 Option& Option::operator=(const string& v) {
189 assert(!type.empty());
191 if ((type != "button" && type != "string" && v.empty())
192 || (type == "check" && v != "true" && v != "false")
193 || (type == "spin" && (stof(v) < min || stof(v) > max)))
198 OptionsMap comboMap; // To have case insensitive compare
200 std::istringstream ss(defaultValue);
202 comboMap[token] << Option();
203 if (!comboMap.count(v) || v == "var")
207 if (type != "button")
218 } // namespace Stockfish