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/>.
34 #include "ucioption.h"
41 bool Chess960 = false;
45 //// Local definitions
54 enum OptionType { SPIN, COMBO, CHECK, STRING, BUTTON };
56 typedef std::vector<std::string> ComboValues;
60 std::string name, defaultValue, currentValue;
62 int minValue, maxValue;
63 ComboValues comboValues;
67 Option(const std::string& defaultValue, OptionType = STRING);
68 Option(bool defaultValue, OptionType = CHECK);
69 Option(int defaultValue, int minValue, int maxValue);
71 bool operator<(const Option& o) { return this->idx < o.idx; }
74 typedef std::map<std::string, Option> Options;
80 // load_defaults populates the options map with the hard
81 // coded names and default values.
83 void load_defaults(Options& o) {
85 o["Use Search Log"] = Option(false);
86 o["Search Log Filename"] = Option("SearchLog.txt");
87 o["Book File"] = Option("book.bin");
88 o["Mobility (Middle Game)"] = Option(100, 0, 200);
89 o["Mobility (Endgame)"] = Option(100, 0, 200);
90 o["Pawn Structure (Middle Game)"] = Option(100, 0, 200);
91 o["Pawn Structure (Endgame)"] = Option(100, 0, 200);
92 o["Passed Pawns (Middle Game)"] = Option(100, 0, 200);
93 o["Passed Pawns (Endgame)"] = Option(100, 0, 200);
94 o["Space"] = Option(100, 0, 200);
95 o["Aggressiveness"] = Option(100, 0, 200);
96 o["Cowardice"] = Option(100, 0, 200);
97 o["King Safety Curve"] = Option("Quadratic", COMBO);
99 o["King Safety Curve"].comboValues.push_back("Quadratic");
100 o["King Safety Curve"].comboValues.push_back("Linear"); /*, "From File"*/
102 o["King Safety Coefficient"] = Option(40, 1, 100);
103 o["King Safety X Intercept"] = Option(0, 0, 20);
104 o["King Safety Max Slope"] = Option(30, 10, 100);
105 o["King Safety Max Value"] = Option(500, 100, 1000);
106 o["Queen Contact Check Bonus"] = Option(3, 0, 8);
107 o["Queen Check Bonus"] = Option(2, 0, 4);
108 o["Rook Check Bonus"] = Option(1, 0, 4);
109 o["Bishop Check Bonus"] = Option(1, 0, 4);
110 o["Knight Check Bonus"] = Option(1, 0, 4);
111 o["Discovered Check Bonus"] = Option(3, 0, 8);
112 o["Mate Threat Bonus"] = Option(3, 0, 8);
113 o["Check Extension (PV nodes)"] = Option(2, 0, 2);
114 o["Check Extension (non-PV nodes)"] = Option(1, 0, 2);
115 o["Single Reply Extension (PV nodes)"] = Option(2, 0, 2);
116 o["Single Reply Extension (non-PV nodes)"] = Option(2, 0, 2);
117 o["Mate Threat Extension (PV nodes)"] = Option(0, 0, 2);
118 o["Mate Threat Extension (non-PV nodes)"] = Option(0, 0, 2);
119 o["Pawn Push to 7th Extension (PV nodes)"] = Option(1, 0, 2);
120 o["Pawn Push to 7th Extension (non-PV nodes)"] = Option(1, 0, 2);
121 o["Passed Pawn Extension (PV nodes)"] = Option(1, 0, 2);
122 o["Passed Pawn Extension (non-PV nodes)"] = Option(0, 0, 2);
123 o["Pawn Endgame Extension (PV nodes)"] = Option(2, 0, 2);
124 o["Pawn Endgame Extension (non-PV nodes)"] = Option(2, 0, 2);
125 o["Full Depth Moves (PV nodes)"] = Option(14, 1, 100);
126 o["Full Depth Moves (non-PV nodes)"] = Option(3, 1, 100);
127 o["Threat Depth"] = Option(5, 0, 100);
128 o["Selective Plies"] = Option(7, 0, 10);
129 o["Futility Pruning (Main Search)"] = Option(true);
130 o["Futility Pruning (Quiescence Search)"] = Option(true);
131 o["Futility Margin (Quiescence Search)"] = Option(50, 0, 1000);
132 o["Futility Margin Scale Factor (Main Search)"] = Option(100, 0, 1000);
133 o["Maximum Razoring Depth"] = Option(3, 0, 4);
134 o["Razoring Margin"] = Option(300, 150, 600);
135 o["LSN filtering"] = Option(true);
136 o["LSN Time Margin (sec)"] = Option(4, 1, 10);
137 o["LSN Value Margin"] = Option(200, 100, 600);
138 o["Randomness"] = Option(0, 0, 10);
139 o["Minimum Split Depth"] = Option(4, 4, 7);
140 o["Maximum Number of Threads per Split Point"] = Option(5, 4, 8);
141 o["Threads"] = Option(1, 1, 8);
142 o["Hash"] = Option(32, 4, 4096);
143 o["Clear Hash"] = Option(false, BUTTON);
144 o["Ponder"] = Option(true);
145 o["OwnBook"] = Option(true);
146 o["MultiPV"] = Option(1, 1, 500);
147 o["UCI_ShowCurrLine"] = Option(false);
148 o["UCI_Chess960"] = Option(false);
150 // Any option should know its name so to be easily printed
151 for (Options::iterator it = o.begin(); it != o.end(); ++it)
152 it->second.name = it->first;
161 // stringify converts a value of type T to a std::string
163 std::string stringify(const T& v) {
165 std::ostringstream ss;
170 // We want conversion from a bool value to be "true" or "false",
171 // not "1" or "0", so add a specialization for bool type.
173 std::string stringify<bool>(const bool& v) {
175 return v ? "true" : "false";
178 // get_option_value implements the various get_option_value_<type>
179 // functions defined later, because only the option value
180 // type changes a template seems a proper solution.
183 T get_option_value(const std::string& optionName) {
186 if (options.find(optionName) == options.end())
189 std::istringstream ss(options[optionName].currentValue);
194 // Unfortunatly we need a specialization to convert "false" and "true"
195 // to proper bool values. The culprit is that we use a non standard way
196 // to store a bool value in a string, in particular we use "false" and
197 // "true" instead of "0" and "1" due to how UCI protocol works.
200 bool get_option_value<bool>(const std::string& optionName) {
202 if (options.find(optionName) == options.end())
205 return options[optionName].currentValue == "true";
213 /// init_uci_options() initializes the UCI options. Currently, the only
214 /// thing this function does is to initialize the default value of the
215 /// "Threads" parameter to the number of available CPU cores.
217 void init_uci_options() {
219 load_defaults(options);
221 // Limit the default value of "Threads" to 7 even if we have 8 CPU cores.
222 // According to Ken Dail's tests, Glaurung plays much better with 7 than
223 // with 8 threads. This is weird, but it is probably difficult to find out
224 // why before I have a 8-core computer to experiment with myself.
225 assert(options.find("Threads") != options.end());
226 assert(options.find("Minimum Split Depth") != options.end());
228 options["Threads"].defaultValue = stringify(Min(cpu_count(), 7));
229 options["Threads"].currentValue = stringify(Min(cpu_count(), 7));
231 // Increase the minimum split depth when the number of CPUs is big.
232 // It would probably be better to let this depend on the number of threads
236 options["Minimum Split Depth"].defaultValue = "6";
237 options["Minimum Split Depth"].currentValue = "6";
242 /// print_uci_options() prints all the UCI options to the standard output,
243 /// in the format defined by the UCI protocol.
245 void print_uci_options() {
247 static const char optionTypeName[][16] = {
248 "spin", "combo", "check", "string", "button"
251 // Build up a vector out of the options map and sort it according to idx
252 // field, that is the chronological insertion order in options map.
253 std::vector<Option> vec;
254 for (Options::const_iterator it = options.begin(); it != options.end(); ++it)
255 vec.push_back(it->second);
257 std::sort(vec.begin(), vec.end());
259 for (std::vector<Option>::const_iterator it = vec.begin(); it != vec.end(); ++it)
261 std::cout << "option name " << it->name
262 << " type " << optionTypeName[it->type];
264 if (it->type != BUTTON)
266 std::cout << " default " << it->defaultValue;
268 if (it->type == SPIN)
269 std::cout << " min " << it->minValue
270 << " max " << it->maxValue;
272 else if (it->type == COMBO)
273 for (ComboValues::const_iterator itc = it->comboValues.begin();
274 itc != it->comboValues.end(); ++itc)
275 std::cout << " var " << *itc;
277 std::cout << std::endl;
282 /// get_option_value_bool() returns the current value of a UCI parameter of
285 bool get_option_value_bool(const std::string& optionName) {
287 return get_option_value<bool>(optionName);
291 /// get_option_value_int() returns the value of a UCI parameter as an integer.
292 /// Normally, this function will be used for a parameter of type "spin", but
293 /// it could also be used with a "combo" parameter, where all the available
294 /// values are integers.
296 int get_option_value_int(const std::string& optionName) {
298 return get_option_value<int>(optionName);
302 /// get_option_value_string() returns the current value of a UCI parameter as
303 /// a string. It is used with parameters of type "combo" and "string".
305 const std::string get_option_value_string(const std::string& optionName) {
307 return get_option_value<std::string>(optionName);
311 /// set_option_value() inserts a new value for a UCI parameter. Note that
312 /// the function does not check that the new value is legal for the given
313 /// parameter: This is assumed to be the responsibility of the GUI.
315 void set_option_value(const std::string& optionName,
316 const std::string& newValue) {
318 if (options.find(optionName) != options.end())
319 options[optionName].currentValue = newValue;
321 std::cout << "No such option: " << optionName << std::endl;
325 /// push_button() is used to tell the engine that a UCI parameter of type
326 /// "button" has been selected:
328 void push_button(const std::string& buttonName) {
330 set_option_value(buttonName, "true");
334 /// button_was_pressed() tests whether a UCI parameter of type "button" has
335 /// been selected since the last time the function was called, in this case
336 /// it also resets the button.
338 bool button_was_pressed(const std::string& buttonName) {
340 if (!get_option_value<bool>(buttonName))
343 set_option_value(buttonName, "false");
350 // Define constructors of Option class.
352 Option::Option() {} // To allow insertion in a std::map
354 Option::Option(const std::string& def, OptionType t)
355 : defaultValue(def), currentValue(def), type(t), idx(options.size()), minValue(0), maxValue(0) {}
357 Option::Option(bool def, OptionType t)
358 : defaultValue(stringify(def)), currentValue(stringify(def)), type(t), idx(options.size()), minValue(0), maxValue(0) {}
360 Option::Option(int def, int minv, int maxv)
361 : defaultValue(stringify(def)), currentValue(stringify(def)), type(SPIN), idx(options.size()), minValue(minv), maxValue(maxv) {}