]> git.sesse.net Git - stockfish/blob - src/ucioption.cpp
Remove UCI_AnalyseMode Option
[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
39 using std::string;
40
41 namespace Stockfish {
42
43 UCI::OptionsMap Options;  // Global object
44
45 namespace UCI {
46
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(); }
54
55 // Our case insensitive less() function as required by UCI protocol
56 bool CaseInsensitiveLess::operator()(const string& s1, const string& s2) const {
57
58     return std::lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end(),
59                                         [](char c1, char c2) { return tolower(c1) < tolower(c2); });
60 }
61
62
63 // Initializes the UCI options to their hard-coded default values
64 void init(OptionsMap& o) {
65
66     constexpr int MaxHashMB = Is64Bit ? 33554432 : 2048;
67
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["nodestime"] << Option(0, 0, 10000);
77     o["UCI_Chess960"] << Option(false);
78     o["UCI_LimitStrength"] << Option(false);
79     o["UCI_Elo"] << Option(1320, 1320, 3190);
80     o["UCI_ShowWDL"] << Option(false);
81     o["SyzygyPath"] << Option("<empty>", on_tb_path);
82     o["SyzygyProbeDepth"] << Option(1, 1, 100);
83     o["Syzygy50MoveRule"] << Option(true);
84     o["SyzygyProbeLimit"] << Option(7, 0, 7);
85     o["EvalFile"] << Option(EvalFileDefaultName, on_eval_file);
86 }
87
88
89 // Used to print all the options default values in chronological
90 // insertion order (the idx field) and in the format defined by the UCI protocol.
91 std::ostream& operator<<(std::ostream& os, const OptionsMap& om) {
92
93     for (size_t idx = 0; idx < om.size(); ++idx)
94         for (const auto& it : om)
95             if (it.second.idx == idx)
96             {
97                 const Option& o = it.second;
98                 os << "\noption name " << it.first << " type " << o.type;
99
100                 if (o.type == "string" || o.type == "check" || o.type == "combo")
101                     os << " default " << o.defaultValue;
102
103                 if (o.type == "spin")
104                     os << " default " << int(stof(o.defaultValue)) << " min " << o.min << " max "
105                        << o.max;
106
107                 break;
108             }
109
110     return os;
111 }
112
113
114 // Option class constructors and conversion operators
115
116 Option::Option(const char* v, OnChange f) :
117     type("string"),
118     min(0),
119     max(0),
120     on_change(f) {
121     defaultValue = currentValue = v;
122 }
123
124 Option::Option(bool v, OnChange f) :
125     type("check"),
126     min(0),
127     max(0),
128     on_change(f) {
129     defaultValue = currentValue = (v ? "true" : "false");
130 }
131
132 Option::Option(OnChange f) :
133     type("button"),
134     min(0),
135     max(0),
136     on_change(f) {}
137
138 Option::Option(double v, int minv, int maxv, OnChange f) :
139     type("spin"),
140     min(minv),
141     max(maxv),
142     on_change(f) {
143     defaultValue = currentValue = std::to_string(v);
144 }
145
146 Option::Option(const char* v, const char* cur, OnChange f) :
147     type("combo"),
148     min(0),
149     max(0),
150     on_change(f) {
151     defaultValue = v;
152     currentValue = cur;
153 }
154
155 Option::operator int() const {
156     assert(type == "check" || type == "spin");
157     return (type == "spin" ? std::stoi(currentValue) : currentValue == "true");
158 }
159
160 Option::operator std::string() const {
161     assert(type == "string");
162     return currentValue;
163 }
164
165 bool Option::operator==(const char* s) const {
166     assert(type == "combo");
167     return !CaseInsensitiveLess()(currentValue, s) && !CaseInsensitiveLess()(s, currentValue);
168 }
169
170
171 // Inits options and assigns idx in the correct printing order
172
173 void Option::operator<<(const Option& o) {
174
175     static size_t insert_order = 0;
176
177     *this = o;
178     idx   = insert_order++;
179 }
180
181
182 // Updates currentValue and triggers on_change() action. It's up to
183 // the GUI to check for option's limits, but we could receive the new value
184 // from the user by console window, so let's check the bounds anyway.
185 Option& Option::operator=(const string& v) {
186
187     assert(!type.empty());
188
189     if ((type != "button" && type != "string" && v.empty())
190         || (type == "check" && v != "true" && v != "false")
191         || (type == "spin" && (stof(v) < min || stof(v) > max)))
192         return *this;
193
194     if (type == "combo")
195     {
196         OptionsMap         comboMap;  // To have case insensitive compare
197         string             token;
198         std::istringstream ss(defaultValue);
199         while (ss >> token)
200             comboMap[token] << Option();
201         if (!comboMap.count(v) || v == "var")
202             return *this;
203     }
204
205     if (type != "button")
206         currentValue = v;
207
208     if (on_change)
209         on_change(*this);
210
211     return *this;
212 }
213
214 }  // namespace UCI
215
216 }  // namespace Stockfish