]> git.sesse.net Git - stockfish/blob - src/ucioption.cpp
2c84084813f1d8ae369054c5b9c9fa535b75d532
[stockfish] / src / ucioption.cpp
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2023 The Stockfish developers (see AUTHORS file)
4
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.
9
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.
14
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/>.
17 */
18
19 #include <algorithm>
20 #include <cassert>
21 #include <cctype>
22 #include <cstddef>
23 #include <iosfwd>
24 #include <istream>
25 #include <map>
26 #include <ostream>
27 #include <sstream>
28 #include <string>
29
30 #include "evaluate.h"
31 #include "misc.h"
32 #include "search.h"
33 #include "syzygy/tbprobe.h"
34 #include "thread.h"
35 #include "tt.h"
36 #include "types.h"
37 #include "uci.h"
38 #include "hashprobe.h"
39
40 using std::string;
41
42 namespace Stockfish {
43
44 UCI::OptionsMap Options;  // Global object
45 std::unique_ptr<HashProbeThread> hash_probe_thread;
46
47 namespace UCI {
48
49 // 'On change' actions, triggered by an option's value change
50 static void on_clear_hash(const Option&) { Search::clear(); }
51 static void on_hash_size(const Option& o) { TT.resize(size_t(o)); }
52 static void on_logger(const Option& o) { start_logger(o); }
53 static void on_threads(const Option& o) { Threads.set(size_t(o)); }
54 static void on_tb_path(const Option& o) { Tablebases::init(o); }
55 static void on_use_NNUE(const Option& ) { Eval::NNUE::init(); }
56 static void on_eval_file(const Option& ) { Eval::NNUE::init(); }
57 static void on_rpc_server_address(const Option& o) {
58         if (hash_probe_thread) {
59                 hash_probe_thread->Shutdown();
60         }
61         std::string addr = o;
62         hash_probe_thread.reset(new HashProbeThread(addr));
63 }
64
65 // Our case insensitive less() function as required by UCI protocol
66 bool CaseInsensitiveLess::operator()(const string& s1, const string& s2) const {
67
68     return std::lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end(),
69                                         [](char c1, char c2) { return tolower(c1) < tolower(c2); });
70 }
71
72
73 // Initializes the UCI options to their hard-coded default values
74 void init(OptionsMap& o) {
75
76     constexpr int MaxHashMB = Is64Bit ? 33554432 : 2048;
77
78     o["Debug Log File"] << Option("", on_logger);
79     o["Threads"] << Option(1, 1, 1024, on_threads);
80     o["Hash"] << Option(16, 1, MaxHashMB, on_hash_size);
81     o["Clear Hash"] << Option(on_clear_hash);
82     o["Ponder"] << Option(false);
83     o["MultiPV"] << Option(1, 1, 500);
84     o["Skill Level"] << Option(20, 0, 20);
85     o["Move Overhead"] << Option(10, 0, 5000);
86     o["nodestime"] << Option(0, 0, 10000);
87     o["UCI_Chess960"] << Option(false);
88     o["UCI_LimitStrength"] << Option(false);
89     o["UCI_Elo"] << Option(1320, 1320, 3190);
90     o["UCI_ShowWDL"] << Option(false);
91     o["SyzygyPath"] << Option("<empty>", on_tb_path);
92     o["SyzygyProbeDepth"] << Option(1, 1, 100);
93     o["Syzygy50MoveRule"] << Option(true);
94     o["SyzygyProbeLimit"] << Option(7, 0, 7);
95     o["EvalFile"] << Option(EvalFileDefaultName, on_eval_file);
96     o["RPCServerAddress"] << Option("<empty>", on_rpc_server_address);
97 }
98
99
100 // Used to print all the options default values in chronological
101 // insertion order (the idx field) and in the format defined by the UCI protocol.
102 std::ostream& operator<<(std::ostream& os, const OptionsMap& om) {
103
104     for (size_t idx = 0; idx < om.size(); ++idx)
105         for (const auto& it : om)
106             if (it.second.idx == idx)
107             {
108                 const Option& o = it.second;
109                 os << "\noption name " << it.first << " type " << o.type;
110
111                 if (o.type == "string" || o.type == "check" || o.type == "combo")
112                     os << " default " << o.defaultValue;
113
114                 if (o.type == "spin")
115                     os << " default " << int(stof(o.defaultValue)) << " min " << o.min << " max "
116                        << o.max;
117
118                 break;
119             }
120
121     return os;
122 }
123
124
125 // Option class constructors and conversion operators
126
127 Option::Option(const char* v, OnChange f) :
128     type("string"),
129     min(0),
130     max(0),
131     on_change(f) {
132     defaultValue = currentValue = v;
133 }
134
135 Option::Option(bool v, OnChange f) :
136     type("check"),
137     min(0),
138     max(0),
139     on_change(f) {
140     defaultValue = currentValue = (v ? "true" : "false");
141 }
142
143 Option::Option(OnChange f) :
144     type("button"),
145     min(0),
146     max(0),
147     on_change(f) {}
148
149 Option::Option(double v, int minv, int maxv, OnChange f) :
150     type("spin"),
151     min(minv),
152     max(maxv),
153     on_change(f) {
154     defaultValue = currentValue = std::to_string(v);
155 }
156
157 Option::Option(const char* v, const char* cur, OnChange f) :
158     type("combo"),
159     min(0),
160     max(0),
161     on_change(f) {
162     defaultValue = v;
163     currentValue = cur;
164 }
165
166 Option::operator int() const {
167     assert(type == "check" || type == "spin");
168     return (type == "spin" ? std::stoi(currentValue) : currentValue == "true");
169 }
170
171 Option::operator std::string() const {
172     assert(type == "string");
173     return currentValue;
174 }
175
176 bool Option::operator==(const char* s) const {
177     assert(type == "combo");
178     return !CaseInsensitiveLess()(currentValue, s) && !CaseInsensitiveLess()(s, currentValue);
179 }
180
181
182 // Inits options and assigns idx in the correct printing order
183
184 void Option::operator<<(const Option& o) {
185
186     static size_t insert_order = 0;
187
188     *this = o;
189     idx   = insert_order++;
190 }
191
192
193 // Updates currentValue and triggers on_change() action. It's up to
194 // the GUI to check for option's limits, but we could receive the new value
195 // from the user by console window, so let's check the bounds anyway.
196 Option& Option::operator=(const string& v) {
197
198     assert(!type.empty());
199
200     if ((type != "button" && type != "string" && v.empty())
201         || (type == "check" && v != "true" && v != "false")
202         || (type == "spin" && (stof(v) < min || stof(v) > max)))
203         return *this;
204
205     if (type == "combo")
206     {
207         OptionsMap         comboMap;  // To have case insensitive compare
208         string             token;
209         std::istringstream ss(defaultValue);
210         while (ss >> token)
211             comboMap[token] << Option();
212         if (!comboMap.count(v) || v == "var")
213             return *this;
214     }
215
216     if (type != "button")
217         currentValue = v;
218
219     if (on_change)
220         on_change(*this);
221
222     return *this;
223 }
224
225 }  // namespace UCI
226
227 }  // namespace Stockfish