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 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/>.
33 #include "ucioption.h"
40 bool Chess960 = false;
44 //// Local definitions
53 enum OptionType { SPIN, COMBO, CHECK, STRING, BUTTON };
55 typedef std::vector<std::string> ComboValues;
59 std::string defaultValue, currentValue;
61 int minValue, maxValue;
62 ComboValues comboValues;
65 Option(const std::string& defaultValue, OptionType = STRING);
66 Option(bool defaultValue, OptionType = CHECK);
67 Option(int defaultValue, int minValue, int maxValue);
70 typedef std::map<std::string, Option> Options;
76 // load_defaults populates the options map with the hard
77 // coded names and default values.
79 void load_defaults(Options& o) {
81 o["Use Search Log"] = Option(false);
82 o["Search Log Filename"] = Option("SearchLog.txt");
83 o["Book File"] = Option("book.bin");
84 o["Mobility (Middle Game)"] = Option(100, 0, 200);
85 o["Mobility (Endgame)"] = Option(100, 0, 200);
86 o["Pawn Structure (Middle Game)"] = Option(100, 0, 200);
87 o["Pawn Structure (Endgame)"] = Option(100, 0, 200);
88 o["Passed Pawns (Middle Game)"] = Option(100, 0, 200);
89 o["Passed Pawns (Endgame)"] = Option(100, 0, 200);
90 o["Space"] = Option(100, 0, 200);
91 o["Aggressiveness"] = Option(100, 0, 200);
92 o["Cowardice"] = Option(100, 0, 200);
93 o["King Safety Curve"] = Option("Quadratic", COMBO);
95 o["King Safety Curve"].comboValues.push_back("Quadratic");
96 o["King Safety Curve"].comboValues.push_back("Linear"); /*, "From File"*/
98 o["King Safety Coefficient"] = Option(40, 1, 100);
99 o["King Safety X Intercept"] = Option(0, 0, 20);
100 o["King Safety Max Slope"] = Option(30, 10, 100);
101 o["King Safety Max Value"] = Option(500, 100, 1000);
102 o["Queen Contact Check Bonus"] = Option(3, 0, 8);
103 o["Queen Check Bonus"] = Option(2, 0, 4);
104 o["Rook Check Bonus"] = Option(1, 0, 4);
105 o["Bishop Check Bonus"] = Option(1, 0, 4);
106 o["Knight Check Bonus"] = Option(1, 0, 4);
107 o["Discovered Check Bonus"] = Option(3, 0, 8);
108 o["Mate Threat Bonus"] = Option(3, 0, 8);
109 o["Check Extension (PV nodes)"] = Option(2, 0, 2);
110 o["Check Extension (non-PV nodes)"] = Option(1, 0, 2);
111 o["Single Reply Extension (PV nodes)"] = Option(2, 0, 2);
112 o["Single Reply Extension (non-PV nodes)"] = Option(2, 0, 2);
113 o["Mate Threat Extension (PV nodes)"] = Option(0, 0, 2);
114 o["Mate Threat Extension (non-PV nodes)"] = Option(0, 0, 2);
115 o["Pawn Push to 7th Extension (PV nodes)"] = Option(1, 0, 2);
116 o["Pawn Push to 7th Extension (non-PV nodes)"] = Option(1, 0, 2);
117 o["Passed Pawn Extension (PV nodes)"] = Option(1, 0, 2);
118 o["Passed Pawn Extension (non-PV nodes)"] = Option(0, 0, 2);
119 o["Pawn Endgame Extension (PV nodes)"] = Option(2, 0, 2);
120 o["Pawn Endgame Extension (non-PV nodes)"] = Option(2, 0, 2);
121 o["Full Depth Moves (PV nodes)"] = Option(14, 1, 100);
122 o["Full Depth Moves (non-PV nodes)"] = Option(3, 1, 100);
123 o["Threat Depth"] = Option(5, 0, 100);
124 o["Selective Plies"] = Option(7, 0, 10);
125 o["Futility Pruning (Main Search)"] = Option(true);
126 o["Futility Pruning (Quiescence Search)"] = Option(true);
127 o["Futility Margin (Quiescence Search)"] = Option(50, 0, 1000);
128 o["Futility Margin Scale Factor (Main Search)"] = Option(100, 0, 1000);
129 o["Maximum Razoring Depth"] = Option(3, 0, 4);
130 o["Razoring Margin"] = Option(300, 150, 600);
131 o["LSN filtering"] = Option(true);
132 o["LSN Time Margin (sec)"] = Option(4, 1, 10);
133 o["LSN Value Margin"] = Option(200, 100, 600);
134 o["Randomness"] = Option(0, 0, 10);
135 o["Minimum Split Depth"] = Option(4, 4, 7);
136 o["Maximum Number of Threads per Split Point"] = Option(5, 4, 8);
137 o["Threads"] = Option(1, 1, 8);
138 o["Hash"] = Option(32, 4, 4096);
139 o["Clear Hash"] = Option(false, BUTTON);
140 o["Ponder"] = Option(true);
141 o["OwnBook"] = Option(true);
142 o["MultiPV"] = Option(1, 1, 500);
143 o["UCI_ShowCurrLine"] = Option(false);
144 o["UCI_Chess960"] = Option(false);
153 // stringify converts a value of type T to a std::string
155 std::string stringify(const T& v) {
157 std::ostringstream ss;
162 // We want conversion from a bool value to be "true" or "false",
163 // not "1" or "0", so add a specialization for bool type.
165 std::string stringify<bool>(const bool& v) {
167 return v ? "true" : "false";
170 // get_option_value implements the various get_option_value_<type>
171 // functions defined later, because only the option value
172 // type changes a template seems a proper solution.
175 T get_option_value(const std::string& optionName) {
178 if (options.find(optionName) == options.end())
181 std::istringstream ss(options[optionName].currentValue);
186 // Unfortunatly we need a specialization to convert "false" and "true"
187 // to proper bool values. The culprit is that we use a non standard way
188 // to store a bool value in a string, in particular we use "false" and
189 // "true" instead of "0" and "1" due to how UCI protocol works.
192 bool get_option_value<bool>(const std::string& optionName) {
194 if (options.find(optionName) == options.end())
197 return options[optionName].currentValue == "true";
205 /// init_uci_options() initializes the UCI options. Currently, the only
206 /// thing this function does is to initialize the default value of the
207 /// "Threads" parameter to the number of available CPU cores.
209 void init_uci_options() {
211 load_defaults(options);
213 // Limit the default value of "Threads" to 7 even if we have 8 CPU cores.
214 // According to Ken Dail's tests, Glaurung plays much better with 7 than
215 // with 8 threads. This is weird, but it is probably difficult to find out
216 // why before I have a 8-core computer to experiment with myself.
217 assert(options.find("Threads") != options.end());
218 assert(options.find("Minimum Split Depth") != options.end());
220 options["Threads"].defaultValue = stringify(Min(cpu_count(), 7));
221 options["Threads"].currentValue = stringify(Min(cpu_count(), 7));
223 // Increase the minimum split depth when the number of CPUs is big.
224 // It would probably be better to let this depend on the number of threads
228 options["Minimum Split Depth"].defaultValue = "6";
229 options["Minimum Split Depth"].currentValue = "6";
234 /// print_uci_options() prints all the UCI options to the standard output,
235 /// in the format defined by the UCI protocol.
237 void print_uci_options() {
239 static const char optionTypeName[][16] = {
240 "spin", "combo", "check", "string", "button"
243 for (Options::const_iterator it = options.begin(); it != options.end(); ++it)
245 const Option& o = it->second;
246 std::cout << "option name " << it->first
247 << " type " << optionTypeName[o.type];
249 if (o.type != BUTTON)
251 std::cout << " default " << o.defaultValue;
254 std::cout << " min " << o.minValue
255 << " max " << o.maxValue;
257 else if (o.type == COMBO)
258 for (ComboValues::const_iterator itc = o.comboValues.begin();
259 itc != o.comboValues.end(); ++itc)
260 std::cout << " var " << *itc;
262 std::cout << std::endl;
267 /// get_option_value_bool() returns the current value of a UCI parameter of
270 bool get_option_value_bool(const std::string& optionName) {
272 return get_option_value<bool>(optionName);
276 /// get_option_value_int() returns the value of a UCI parameter as an integer.
277 /// Normally, this function will be used for a parameter of type "spin", but
278 /// it could also be used with a "combo" parameter, where all the available
279 /// values are integers.
281 int get_option_value_int(const std::string& optionName) {
283 return get_option_value<int>(optionName);
287 /// get_option_value_string() returns the current value of a UCI parameter as
288 /// a string. It is used with parameters of type "combo" and "string".
290 const std::string get_option_value_string(const std::string& optionName) {
292 return get_option_value<std::string>(optionName);
296 /// set_option_value() inserts a new value for a UCI parameter. Note that
297 /// the function does not check that the new value is legal for the given
298 /// parameter: This is assumed to be the responsibility of the GUI.
300 void set_option_value(const std::string& optionName,
301 const std::string& newValue) {
303 if (options.find(optionName) != options.end())
304 options[optionName].currentValue = newValue;
306 std::cout << "No such option: " << optionName << std::endl;
310 /// push_button() is used to tell the engine that a UCI parameter of type
311 /// "button" has been selected:
313 void push_button(const std::string& buttonName) {
315 set_option_value(buttonName, "true");
319 /// button_was_pressed() tests whether a UCI parameter of type "button" has
320 /// been selected since the last time the function was called, in this case
321 /// it also resets the button.
323 bool button_was_pressed(const std::string& buttonName) {
325 if (!get_option_value<bool>(buttonName))
328 set_option_value(buttonName, "false");
335 // Define constructors of Option class.
337 Option::Option() {} // To allow insertion in a std::map
339 Option::Option(const std::string& def, OptionType t)
340 : defaultValue(def), currentValue(def), type(t), minValue(0), maxValue(0) {}
342 Option::Option(bool def, OptionType t)
343 : defaultValue(stringify(def)), currentValue(stringify(def)), type(t), minValue(0), maxValue(0) {}
345 Option::Option(int def, int minv, int maxv)
346 : defaultValue(stringify(def)), currentValue(stringify(def)), type(SPIN), minValue(minv), maxValue(maxv) {}