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