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-2010 Marco Costalba, Joona Kiiski, Tord Romstad
6 Stockfish is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Stockfish is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
34 #include "ucioption.h"
39 //// Local definitions
48 enum OptionType { SPIN, COMBO, CHECK, STRING, BUTTON };
50 typedef std::vector<string> ComboValues;
54 string name, defaultValue, currentValue;
57 int minValue, maxValue;
58 ComboValues comboValues;
61 Option(const char* defaultValue, OptionType = STRING);
62 Option(bool defaultValue, OptionType = CHECK);
63 Option(int defaultValue, int minValue, int maxValue);
65 bool operator<(const Option& o) const { return this->idx < o.idx; }
68 typedef std::map<string, Option> Options;
74 // load_defaults populates the options map with the hard
75 // coded names and default values.
77 void load_defaults(Options& o) {
79 o["Use Search Log"] = Option(false);
80 o["Search Log Filename"] = Option("SearchLog.txt");
81 o["Book File"] = Option("book.bin");
82 o["Mobility (Middle Game)"] = Option(100, 0, 200);
83 o["Mobility (Endgame)"] = Option(100, 0, 200);
84 o["Pawn Structure (Middle Game)"] = Option(100, 0, 200);
85 o["Pawn Structure (Endgame)"] = Option(100, 0, 200);
86 o["Passed Pawns (Middle Game)"] = Option(100, 0, 200);
87 o["Passed Pawns (Endgame)"] = Option(100, 0, 200);
88 o["Space"] = Option(100, 0, 200);
89 o["Aggressiveness"] = Option(100, 0, 200);
90 o["Cowardice"] = Option(100, 0, 200);
91 o["Check Extension (PV nodes)"] = Option(2, 0, 2);
92 o["Check Extension (non-PV nodes)"] = Option(1, 0, 2);
93 o["Single Evasion Extension (PV nodes)"] = Option(2, 0, 2);
94 o["Single Evasion Extension (non-PV nodes)"] = Option(2, 0, 2);
95 o["Mate Threat Extension (PV nodes)"] = Option(0, 0, 2);
96 o["Mate Threat Extension (non-PV nodes)"] = Option(0, 0, 2);
97 o["Pawn Push to 7th Extension (PV nodes)"] = Option(1, 0, 2);
98 o["Pawn Push to 7th Extension (non-PV nodes)"] = Option(1, 0, 2);
99 o["Passed Pawn Extension (PV nodes)"] = Option(1, 0, 2);
100 o["Passed Pawn Extension (non-PV nodes)"] = Option(0, 0, 2);
101 o["Pawn Endgame Extension (PV nodes)"] = Option(2, 0, 2);
102 o["Pawn Endgame Extension (non-PV nodes)"] = Option(2, 0, 2);
103 o["Randomness"] = Option(0, 0, 10);
104 o["Minimum Split Depth"] = Option(4, 4, 7);
105 o["Maximum Number of Threads per Split Point"] = Option(5, 4, 8);
106 o["Threads"] = Option(1, 1, MAX_THREADS);
107 o["Hash"] = Option(32, 4, 8192);
108 o["Clear Hash"] = Option(false, BUTTON);
109 o["New Game"] = Option(false, BUTTON);
110 o["Ponder"] = Option(true);
111 o["OwnBook"] = Option(true);
112 o["MultiPV"] = Option(1, 1, 500);
113 o["UCI_Chess960"] = Option(false);
114 o["UCI_AnalyseMode"] = Option(false);
116 // Temporary hack for 1.7.1 to be removed in next release
117 o["Zugzwang detection"] = Option(false);
119 // Any option should know its name so to be easily printed
120 for (Options::iterator it = o.begin(); it != o.end(); ++it)
121 it->second.name = it->first;
130 // stringify converts a value of type T to a std::string
132 string stringify(const T& v) {
134 std::ostringstream ss;
140 // get_option_value implements the various get_option_value_<type>
141 // functions defined later, because only the option value
142 // type changes a template seems a proper solution.
145 T get_option_value(const string& optionName) {
148 if (options.find(optionName) == options.end())
151 std::istringstream ss(options[optionName].currentValue);
156 // Specialization for std::string where instruction 'ss >> ret;'
157 // would erroneusly tokenize a string with spaces.
160 string get_option_value<string>(const string& optionName) {
162 if (options.find(optionName) == options.end())
165 return options[optionName].currentValue;
174 /// init_uci_options() initializes the UCI options. Currently, the only
175 /// thing this function does is to initialize the default value of the
176 /// "Threads" parameter to the number of available CPU cores.
178 void init_uci_options() {
180 load_defaults(options);
182 // Set optimal value for parameter "Minimum Split Depth"
183 // according to number of available cores.
184 assert(options.find("Threads") != options.end());
185 assert(options.find("Minimum Split Depth") != options.end());
187 Option& thr = options["Threads"];
188 Option& msd = options["Minimum Split Depth"];
190 thr.defaultValue = thr.currentValue = stringify(cpu_count());
192 if (cpu_count() >= 8)
193 msd.defaultValue = msd.currentValue = stringify(7);
197 /// print_uci_options() prints all the UCI options to the standard output,
198 /// in the format defined by the UCI protocol.
200 void print_uci_options() {
202 static const char optionTypeName[][16] = {
203 "spin", "combo", "check", "string", "button"
206 // Build up a vector out of the options map and sort it according to idx
207 // field, that is the chronological insertion order in options map.
208 std::vector<Option> vec;
209 for (Options::const_iterator it = options.begin(); it != options.end(); ++it)
210 vec.push_back(it->second);
212 std::sort(vec.begin(), vec.end());
214 for (std::vector<Option>::const_iterator it = vec.begin(); it != vec.end(); ++it)
216 std::cout << "\noption name " << it->name
217 << " type " << optionTypeName[it->type];
219 if (it->type == BUTTON)
222 if (it->type == CHECK)
223 std::cout << " default " << (it->defaultValue == "1" ? "true" : "false");
225 std::cout << " default " << it->defaultValue;
227 if (it->type == SPIN)
228 std::cout << " min " << it->minValue << " max " << it->maxValue;
229 else if (it->type == COMBO)
230 for (ComboValues::const_iterator itc = it->comboValues.begin();
231 itc != it->comboValues.end(); ++itc)
232 std::cout << " var " << *itc;
234 std::cout << std::endl;
238 /// get_option_value_bool() returns the current value of a UCI parameter of
241 bool get_option_value_bool(const string& optionName) {
243 return get_option_value<bool>(optionName);
247 /// get_option_value_int() returns the value of a UCI parameter as an integer.
248 /// Normally, this function will be used for a parameter of type "spin", but
249 /// it could also be used with a "combo" parameter, where all the available
250 /// values are integers.
252 int get_option_value_int(const string& optionName) {
254 return get_option_value<int>(optionName);
258 /// get_option_value_string() returns the current value of a UCI parameter as
259 /// a string. It is used with parameters of type "combo" and "string".
261 string get_option_value_string(const string& optionName) {
263 return get_option_value<string>(optionName);
267 /// set_option_value() inserts a new value for a UCI parameter. Note that
268 /// the function does not check that the new value is legal for the given
269 /// parameter: This is assumed to be the responsibility of the GUI.
271 void set_option_value(const string& name, const string& value) {
273 // UCI protocol uses "true" and "false" instead of "1" and "0", so convert
274 // value according to standard C++ convention before to store it.
278 else if (v == "false")
281 if (options.find(name) == options.end())
283 std::cout << "No such option: " << name << std::endl;
287 // Normally it's up to the GUI to check for option's limits,
288 // but we could receive the new value directly from the user
289 // by teminal window. So let's check the bounds anyway.
290 Option& opt = options[name];
292 if (opt.type == CHECK && v != "0" && v != "1")
295 else if (opt.type == SPIN)
297 int val = atoi(v.c_str());
298 if (val < opt.minValue || val > opt.maxValue)
302 opt.currentValue = v;
306 /// push_button() is used to tell the engine that a UCI parameter of type
307 /// "button" has been selected:
309 void push_button(const string& buttonName) {
311 set_option_value(buttonName, "true");
315 /// button_was_pressed() tests whether a UCI parameter of type "button" has
316 /// been selected since the last time the function was called, in this case
317 /// it also resets the button.
319 bool button_was_pressed(const string& buttonName) {
321 if (!get_option_value<bool>(buttonName))
324 set_option_value(buttonName, "false");
331 // Define constructors of Option class.
333 Option::Option() {} // To allow insertion in a std::map
335 Option::Option(const char* def, OptionType t)
336 : defaultValue(def), currentValue(def), type(t), idx(options.size()), minValue(0), maxValue(0) {}
338 Option::Option(bool def, OptionType t)
339 : defaultValue(stringify(def)), currentValue(stringify(def)), type(t), idx(options.size()), minValue(0), maxValue(0) {}
341 Option::Option(int def, int minv, int maxv)
342 : defaultValue(stringify(def)), currentValue(stringify(def)), type(SPIN), idx(options.size()), minValue(minv), maxValue(maxv) {}