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