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-2009 Marco Costalba
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["King Safety Curve"] = Option("Quadratic", COMBO);
93 o["King Safety Curve"].comboValues.push_back("Quadratic");
94 o["King Safety Curve"].comboValues.push_back("Linear"); /*, "From File"*/
96 o["King Safety Coefficient"] = Option(40, 1, 100);
97 o["King Safety X Intercept"] = Option(0, 0, 20);
98 o["King Safety Max Slope"] = Option(30, 10, 100);
99 o["King Safety Max Value"] = Option(500, 100, 1000);
100 o["Queen Contact Check Bonus"] = Option(3, 0, 8);
101 o["Queen Check Bonus"] = Option(2, 0, 4);
102 o["Rook Check Bonus"] = Option(1, 0, 4);
103 o["Bishop Check Bonus"] = Option(1, 0, 4);
104 o["Knight Check Bonus"] = Option(1, 0, 4);
105 o["Discovered Check Bonus"] = Option(3, 0, 8);
106 o["Mate Threat Bonus"] = Option(3, 0, 8);
107 o["Check Extension (PV nodes)"] = Option(2, 0, 2);
108 o["Check Extension (non-PV nodes)"] = Option(1, 0, 2);
109 o["Single Reply Extension (PV nodes)"] = Option(2, 0, 2);
110 o["Single Reply Extension (non-PV nodes)"] = Option(2, 0, 2);
111 o["Mate Threat Extension (PV nodes)"] = Option(0, 0, 2);
112 o["Mate Threat Extension (non-PV nodes)"] = Option(0, 0, 2);
113 o["Pawn Push to 7th Extension (PV nodes)"] = Option(1, 0, 2);
114 o["Pawn Push to 7th Extension (non-PV nodes)"] = Option(1, 0, 2);
115 o["Passed Pawn Extension (PV nodes)"] = Option(1, 0, 2);
116 o["Passed Pawn Extension (non-PV nodes)"] = Option(0, 0, 2);
117 o["Pawn Endgame Extension (PV nodes)"] = Option(2, 0, 2);
118 o["Pawn Endgame Extension (non-PV nodes)"] = Option(2, 0, 2);
119 o["Full Depth Moves (PV nodes)"] = Option(14, 1, 100);
120 o["Full Depth Moves (non-PV nodes)"] = Option(3, 1, 100);
121 o["Threat Depth"] = Option(5, 0, 100);
122 o["Randomness"] = Option(0, 0, 10);
123 o["Minimum Split Depth"] = Option(4, 4, 7);
124 o["Maximum Number of Threads per Split Point"] = Option(5, 4, 8);
125 o["Threads"] = Option(1, 1, 8);
126 o["Hash"] = Option(32, 4, 4096);
127 o["Clear Hash"] = Option(false, BUTTON);
128 o["Ponder"] = Option(true);
129 o["OwnBook"] = Option(true);
130 o["MultiPV"] = Option(1, 1, 500);
131 o["UCI_ShowCurrLine"] = Option(false);
132 o["UCI_Chess960"] = Option(false);
133 o["UCI_AnalyseMode"] = Option(false);
134 o["UCI_LimitStrength"] = Option(false);
135 o["UCI_Elo"] = Option(2900, 2100, 2900);
137 // Any option should know its name so to be easily printed
138 for (Options::iterator it = o.begin(); it != o.end(); ++it)
139 it->second.name = it->first;
148 // stringify converts a value of type T to a std::string
150 string stringify(const T& v) {
152 std::ostringstream ss;
158 // get_option_value implements the various get_option_value_<type>
159 // functions defined later, because only the option value
160 // type changes a template seems a proper solution.
163 T get_option_value(const string& optionName) {
166 if (options.find(optionName) == options.end())
169 std::istringstream ss(options[optionName].currentValue);
180 /// init_uci_options() initializes the UCI options. Currently, the only
181 /// thing this function does is to initialize the default value of the
182 /// "Threads" parameter to the number of available CPU cores.
184 void init_uci_options() {
186 load_defaults(options);
188 // Limit the default value of "Threads" to 7 even if we have 8 CPU cores.
189 // According to Ken Dail's tests, Glaurung plays much better with 7 than
190 // with 8 threads. This is weird, but it is probably difficult to find out
191 // why before I have a 8-core computer to experiment with myself.
192 assert(options.find("Threads") != options.end());
193 assert(options.find("Minimum Split Depth") != options.end());
195 options["Threads"].defaultValue = stringify(Min(cpu_count(), 7));
196 options["Threads"].currentValue = stringify(Min(cpu_count(), 7));
200 /// print_uci_options() prints all the UCI options to the standard output,
201 /// in the format defined by the UCI protocol.
203 void print_uci_options() {
205 static const char optionTypeName[][16] = {
206 "spin", "combo", "check", "string", "button"
209 // Build up a vector out of the options map and sort it according to idx
210 // field, that is the chronological insertion order in options map.
211 std::vector<Option> vec;
212 for (Options::const_iterator it = options.begin(); it != options.end(); ++it)
213 vec.push_back(it->second);
215 std::sort(vec.begin(), vec.end());
217 for (std::vector<Option>::const_iterator it = vec.begin(); it != vec.end(); ++it)
219 std::cout << "\noption name " << it->name
220 << " type " << optionTypeName[it->type];
222 if (it->type == BUTTON)
225 if (it->type == CHECK)
226 std::cout << " default " << (it->defaultValue == "1" ? "true" : "false");
228 std::cout << " default " << it->defaultValue;
230 if (it->type == SPIN)
231 std::cout << " min " << it->minValue << " max " << it->maxValue;
232 else if (it->type == COMBO)
233 for (ComboValues::const_iterator itc = it->comboValues.begin();
234 itc != it->comboValues.end(); ++itc)
235 std::cout << " var " << *itc;
237 std::cout << std::endl;
241 /// get_option_value_bool() returns the current value of a UCI parameter of
244 bool get_option_value_bool(const string& optionName) {
246 return get_option_value<bool>(optionName);
250 /// get_option_value_int() returns the value of a UCI parameter as an integer.
251 /// Normally, this function will be used for a parameter of type "spin", but
252 /// it could also be used with a "combo" parameter, where all the available
253 /// values are integers.
255 int get_option_value_int(const string& optionName) {
257 return get_option_value<int>(optionName);
261 /// get_option_value_string() returns the current value of a UCI parameter as
262 /// a string. It is used with parameters of type "combo" and "string".
264 string get_option_value_string(const string& optionName) {
266 return get_option_value<string>(optionName);
270 /// set_option_value() inserts a new value for a UCI parameter. Note that
271 /// the function does not check that the new value is legal for the given
272 /// parameter: This is assumed to be the responsibility of the GUI.
274 void set_option_value(const string& name, const string& value) {
276 // UCI protocol uses "true" and "false" instead of "1" and "0", so convert
277 // value according to standard C++ convention before to store it.
281 else if (v == "false")
284 if (options.find(name) != options.end())
285 options[name].currentValue = v;
287 std::cout << "No such option: " << name << std::endl;
291 /// push_button() is used to tell the engine that a UCI parameter of type
292 /// "button" has been selected:
294 void push_button(const string& buttonName) {
296 set_option_value(buttonName, "true");
300 /// button_was_pressed() tests whether a UCI parameter of type "button" has
301 /// been selected since the last time the function was called, in this case
302 /// it also resets the button.
304 bool button_was_pressed(const string& buttonName) {
306 if (!get_option_value<bool>(buttonName))
309 set_option_value(buttonName, "false");
316 // Define constructors of Option class.
318 Option::Option() {} // To allow insertion in a std::map
320 Option::Option(const char* def, OptionType t)
321 : defaultValue(def), currentValue(def), type(t), idx(options.size()), minValue(0), maxValue(0) {}
323 Option::Option(bool def, OptionType t)
324 : defaultValue(stringify(def)), currentValue(stringify(def)), type(t), idx(options.size()), minValue(0), maxValue(0) {}
326 Option::Option(int def, int minv, int maxv)
327 : defaultValue(stringify(def)), currentValue(stringify(def)), type(SPIN), idx(options.size()), minValue(minv), maxValue(maxv) {}