]> git.sesse.net Git - stockfish/blob - src/ucioption.cpp
Merge remote-tracking branch 'upstream/master' into HEAD
[stockfish] / src / ucioption.cpp
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
4   Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
5   Copyright (C) 2015-2020 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
6
7   Stockfish is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   Stockfish is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <algorithm>
22 #include <cassert>
23 #include <ostream>
24 #include <sstream>
25
26 #include "misc.h"
27 #include "search.h"
28 #include "thread.h"
29 #include "tt.h"
30 #include "uci.h"
31 #include "hashprobe.h"
32 #include "syzygy/tbprobe.h"
33
34 using std::string;
35
36 UCI::OptionsMap Options; // Global object
37 std::unique_ptr<HashProbeThread> hash_probe_thread;
38
39 namespace UCI {
40
41 /// 'On change' actions, triggered by an option's value change
42 void on_clear_hash(const Option&) { Search::clear(); }
43 void on_hash_size(const Option& o) { TT.resize(size_t(o)); }
44 void on_logger(const Option& o) { start_logger(o); }
45 void on_threads(const Option& o) { Threads.set(size_t(o)); }
46 void on_tb_path(const Option& o) { Tablebases::init(o); }
47 void on_rpc_server_address(const Option& o) {
48         if (hash_probe_thread) {
49                 hash_probe_thread->Shutdown();
50         }
51         std::string addr = o;
52         hash_probe_thread.reset(new HashProbeThread(addr));
53 }
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 /// UCI::init() initializes the UCI options to their hard-coded default values
64
65 void init(OptionsMap& o) {
66
67   constexpr int MaxHashMB = Is64Bit ? 33554432 : 2048;
68
69   o["Debug Log File"]        << Option("", on_logger);
70   o["Contempt"]              << Option(24, -100, 100);
71   o["Analysis Contempt"]     << Option("Both var Off var White var Black var Both", "Both");
72   o["Threads"]               << Option(1, 1, 512, on_threads);
73   o["Hash"]                  << Option(16, 1, MaxHashMB, on_hash_size);
74   o["Clear Hash"]            << Option(on_clear_hash);
75   o["Ponder"]                << Option(false);
76   o["MultiPV"]               << Option(1, 1, 500);
77   o["Skill Level"]           << Option(20, 0, 20);
78   o["Move Overhead"]         << Option(10, 0, 5000);
79   o["Slow Mover"]            << Option(100, 10, 1000);
80   o["nodestime"]             << Option(0, 0, 10000);
81   o["UCI_Chess960"]          << Option(false);
82   o["UCI_AnalyseMode"]       << Option(false);
83   o["UCI_LimitStrength"]     << Option(false);
84   o["UCI_Elo"]               << Option(1350, 1350, 2850);
85   o["UCI_ShowWDL"]           << Option(false);
86   o["SyzygyPath"]            << Option("<empty>", on_tb_path);
87   o["SyzygyProbeDepth"]      << Option(1, 1, 100);
88   o["Syzygy50MoveRule"]      << Option(true);
89   o["SyzygyProbeLimit"]      << Option(7, 0, 7);
90   o["RPCServerAddress"]      << Option("<empty>", on_rpc_server_address);
91 }
92
93
94 /// operator<<() is used to print all the options default values in chronological
95 /// insertion order (the idx field) and in the format defined by the UCI protocol.
96
97 std::ostream& operator<<(std::ostream& os, const OptionsMap& om) {
98
99   for (size_t idx = 0; idx < om.size(); ++idx)
100       for (const auto& it : om)
101           if (it.second.idx == idx)
102           {
103               const Option& o = it.second;
104               os << "\noption name " << it.first << " type " << o.type;
105
106               if (o.type == "string" || o.type == "check" || o.type == "combo")
107                   os << " default " << o.defaultValue;
108
109               if (o.type == "spin")
110                   os << " default " << int(stof(o.defaultValue))
111                      << " min "     << o.min
112                      << " max "     << o.max;
113
114               break;
115           }
116
117   return os;
118 }
119
120
121 /// Option class constructors and conversion operators
122
123 Option::Option(const char* v, OnChange f) : type("string"), min(0), max(0), on_change(f)
124 { defaultValue = currentValue = v; }
125
126 Option::Option(bool v, OnChange f) : type("check"), min(0), max(0), on_change(f)
127 { defaultValue = currentValue = (v ? "true" : "false"); }
128
129 Option::Option(OnChange f) : type("button"), min(0), max(0), on_change(f)
130 {}
131
132 Option::Option(double v, int minv, int maxv, OnChange f) : type("spin"), min(minv), max(maxv), on_change(f)
133 { defaultValue = currentValue = std::to_string(v); }
134
135 Option::Option(const char* v, const char* cur, OnChange f) : type("combo"), min(0), max(0), on_change(f)
136 { defaultValue = v; currentValue = cur; }
137
138 Option::operator double() const {
139   assert(type == "check" || type == "spin");
140   return (type == "spin" ? stof(currentValue) : currentValue == "true");
141 }
142
143 Option::operator std::string() const {
144   assert(type == "string");
145   return currentValue;
146 }
147
148 bool Option::operator==(const char* s) const {
149   assert(type == "combo");
150   return   !CaseInsensitiveLess()(currentValue, s)
151         && !CaseInsensitiveLess()(s, currentValue);
152 }
153
154
155 /// operator<<() inits options and assigns idx in the correct printing order
156
157 void Option::operator<<(const Option& o) {
158
159   static size_t insert_order = 0;
160
161   *this = o;
162   idx = insert_order++;
163 }
164
165
166 /// operator=() updates currentValue and triggers on_change() action. It's up to
167 /// the GUI to check for option's limits, but we could receive the new value
168 /// from the user by console window, so let's check the bounds anyway.
169
170 Option& Option::operator=(const string& v) {
171
172   assert(!type.empty());
173
174   if (   (type != "button" && v.empty())
175       || (type == "check" && v != "true" && v != "false")
176       || (type == "spin" && (stof(v) < min || stof(v) > max)))
177       return *this;
178
179   if (type == "combo")
180   {
181       OptionsMap comboMap; // To have case insensitive compare
182       string token;
183       std::istringstream ss(defaultValue);
184       while (ss >> token)
185           comboMap[token] << Option();
186       if (!comboMap.count(v) || v == "var")
187           return *this;
188   }
189
190   if (type != "button")
191       currentValue = v;
192
193   if (on_change)
194       on_change(*this);
195
196   return *this;
197 }
198
199 } // namespace UCI