2 Glaurung, a UCI chess playing engine.
3 Copyright (C) 2004-2008 Tord Romstad
5 Glaurung is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 Glaurung is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
31 #include "ucioption.h"
38 bool Chess960 = false;
42 //// Local definitions
51 enum OptionType { SPIN, COMBO, CHECK, STRING, BUTTON, OPTION_TYPE_NONE };
53 typedef std::vector<std::string> ComboValues;
57 std::string name, defaultValue, currentValue;
59 int minValue, maxValue;
60 ComboValues comboValues;
62 // Helper to convert a bool or an int to a std::string
63 template<typename T> std::string stringify(const T& v);
65 Option(const char* name, const char* defaultValue, OptionType = STRING);
66 Option(const char* name, bool defaultValue);
67 Option(const char* name, int defaultValue, int minValue, int maxValue);
70 typedef std::vector<Option> Options;
76 // load-defaults populates the options map with the hard
77 // coded options names and their default values.
78 void load_defaults(Options& o) {
80 o.push_back(Option("Use Search Log", false));
81 o.push_back(Option("Search Log Filename", "SearchLog.txt"));
82 o.push_back(Option("Book File", "book.bin"));
83 o.push_back(Option("Mobility (Middle Game)", 100, 0, 200));
84 o.push_back(Option("Mobility (Endgame)", 100, 0, 200));
85 o.push_back(Option("Pawn Structure (Middle Game)", 100, 0, 200));
86 o.push_back(Option("Pawn Structure (Endgame)", 100, 0, 200));
87 o.push_back(Option("Passed Pawns (Middle Game)", 100, 0, 200));
88 o.push_back(Option("Passed Pawns (Endgame)", 100, 0, 200));
89 o.push_back(Option("Aggressiveness", 100, 0, 200));
90 o.push_back(Option("Cowardice", 100, 0, 200));
91 o.push_back(Option("King Safety Curve", "Quadratic", COMBO));
93 o.back().comboValues.push_back("Quadratic");
94 o.back().comboValues.push_back("Linear"); /*, "From File"*/
96 o.push_back(Option("King Safety Coefficient", 40, 1, 100));
97 o.push_back(Option("King Safety X Intercept", 0, 0, 20));
98 o.push_back(Option("King Safety Max Slope", 30, 10, 100));
99 o.push_back(Option("King Safety Max Value", 500, 100, 1000));
100 o.push_back(Option("Queen Contact Check Bonus", 4, 0, 8));
101 o.push_back(Option("Rook Contact Check Bonus", 2, 0, 4));
102 o.push_back(Option("Queen Check Bonus", 2, 0, 4));
103 o.push_back(Option("Rook Check Bonus", 1, 0, 4));
104 o.push_back(Option("Bishop Check Bonus", 1, 0, 4));
105 o.push_back(Option("Knight Check Bonus", 1, 0, 4));
106 o.push_back(Option("Discovered Check Bonus", 3, 0, 8));
107 o.push_back(Option("Mate Threat Bonus", 3, 0, 8));
108 o.push_back(Option("Check Extension (PV nodes)", 2, 0, 2));
109 o.push_back(Option("Check Extension (non-PV nodes)", 1, 0, 2));
110 o.push_back(Option("Single Reply Extension (PV nodes)", 2, 0, 2));
111 o.push_back(Option("Single Reply Extension (non-PV nodes)", 2, 0, 2));
112 o.push_back(Option("Mate Threat Extension (PV nodes)", 2, 0, 2));
113 o.push_back(Option("Mate Threat Extension (non-PV nodes)", 0, 0, 2));
114 o.push_back(Option("Pawn Push to 7th Extension (PV nodes)", 1, 0, 2));
115 o.push_back(Option("Pawn Push to 7th Extension (non-PV nodes)", 1, 0, 2));
116 o.push_back(Option("Passed Pawn Extension (PV nodes)", 1, 0, 2));
117 o.push_back(Option("Passed Pawn Extension (non-PV nodes)", 0, 0, 2));
118 o.push_back(Option("Pawn Endgame Extension (PV nodes)", 2, 0, 2));
119 o.push_back(Option("Pawn Endgame Extension (non-PV nodes)", 2, 0, 2));
120 o.push_back(Option("Full Depth Moves (PV nodes)", 14, 1, 100));
121 o.push_back(Option("Full Depth Moves (non-PV nodes)", 3, 1, 100));
122 o.push_back(Option("Threat Depth", 5, 0, 100));
123 o.push_back(Option("Selective Plies", 7, 0, 10));
124 o.push_back(Option("Futility Pruning (Main Search)", true));
125 o.push_back(Option("Futility Pruning (Quiescence Search)", true));
126 o.push_back(Option("Futility Margin 0", 50, 0, 1000));
127 o.push_back(Option("Futility Margin 1", 100, 0, 1000));
128 o.push_back(Option("Futility Margin 2", 300, 0, 1000));
129 o.push_back(Option("Maximum Razoring Depth", 3, 0, 4));
130 o.push_back(Option("Razoring Margin", 300, 150, 600));
131 o.push_back(Option("Randomness", 0, 0, 10));
132 o.push_back(Option("Minimum Split Depth", 4, 4, 7));
133 o.push_back(Option("Maximum Number of Threads per Split Point", 5, 4, 8));
134 o.push_back(Option("Threads", 1, 1, 8));
135 o.push_back(Option("Hash", 32, 4, 4096));
136 o.push_back(Option("Clear Hash", false));
137 o.push_back(Option("Ponder", true));
138 o.push_back(Option("OwnBook", true));
139 o.push_back(Option("MultiPV", 1, 1, 500));
140 o.push_back(Option("UCI_ShowCurrLine", false));
141 o.push_back(Option("UCI_Chess960", false));
152 template<typename T> T get_option_value(const std::string& optionName);
153 Options::iterator option_from_name(const std::string& optionName);
161 /// init_uci_options() initializes the UCI options. Currently, the only
162 /// thing this function does is to initialize the default value of the
163 /// "Threads" parameter to the number of available CPU cores.
165 void init_uci_options() {
167 load_defaults(options);
169 // Limit the default value of "Threads" to 7 even if we have 8 CPU cores.
170 // According to Ken Dail's tests, Glaurung plays much better with 7 than
171 // with 8 threads. This is weird, but it is probably difficult to find out
172 // why before I have a 8-core computer to experiment with myself.
173 Options::iterator it = option_from_name("Threads");
175 assert(it != options.end());
177 it->defaultValue = Min(cpu_count(), 7);
178 it->currentValue = Min(cpu_count(), 7);
180 // Increase the minimum split depth when the number of CPUs is big.
181 // It would probably be better to let this depend on the number of threads
185 it = option_from_name("Minimum Split Depth");
187 assert(it != options.end());
189 it->defaultValue = 6;
190 it->currentValue = 6;
195 /// print_uci_options() prints all the UCI options to the standard output,
196 /// in the format defined by the UCI protocol.
198 void print_uci_options() {
200 static const char optionTypeName[][16] = {
201 "spin", "combo", "check", "string", "button"
204 for (Options::iterator it = options.begin(); it != options.end(); ++it)
206 std::cout << "option name " << it->name
207 << " type " << optionTypeName[it->type];
209 if (it->type != BUTTON)
211 std::cout << " default " << it->defaultValue;
213 if (it->type == SPIN)
214 std::cout << " min " << it->minValue
215 << " max " << it->maxValue;
217 else if (it->type == COMBO)
218 for(ComboValues::iterator itc = it->comboValues.begin();
219 itc != it->comboValues.end(); ++itc)
220 std::cout << " var " << *itc;
222 std::cout << std::endl;
226 /// get_option_value_bool() returns the current value of a UCI parameter of
229 bool get_option_value_bool(const std::string& optionName) {
231 return get_option_value<bool>(optionName);
235 /// get_option_value_int() returns the value of a UCI parameter as an integer.
236 /// Normally, this function will be used for a parameter of type "spin", but
237 /// it could also be used with a "combo" parameter, where all the available
238 /// values are integers.
240 int get_option_value_int(const std::string& optionName) {
242 return get_option_value<int>(optionName);
246 /// get_option_value_string() returns the current value of a UCI parameter as
247 /// a string. It is used with parameters of type "combo" and "string".
249 const std::string get_option_value_string(const std::string& optionName) {
251 return get_option_value<std::string>(optionName);
255 /// button_was_pressed() tests whether a UCI parameter of type "button" has
256 /// been selected since the last time the function was called.
258 bool button_was_pressed(const std::string& buttonName) {
260 if (get_option_value<bool>(buttonName))
262 set_option_value(buttonName, "false");
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 std::string& optionName,
275 const std::string& newValue) {
277 Options::iterator it = option_from_name(optionName);
279 if (it != options.end())
280 it->currentValue = newValue;
282 std::cout << "No such option: " << optionName << std::endl;
286 /// push_button() is used to tell the engine that a UCI parameter of type
287 /// "button" has been selected:
289 void push_button(const std::string& buttonName) {
291 set_option_value(buttonName, "true");
297 // methods and c'tors of Option class.
300 std::string Option::stringify(const T& v)
302 std::ostringstream ss;
308 Option::Option(const char* nm, const char* def, OptionType t)
309 : name(nm), defaultValue(def), currentValue(def), type(t), minValue(0), maxValue(0) {}
311 Option::Option(const char* nm, bool def)
312 : name(nm), defaultValue(stringify(def)), currentValue(stringify(def)), type(CHECK), minValue(0), maxValue(0) {}
314 Option::Option(const char* nm, int def, int minv, int maxv)
315 : name(nm), defaultValue(stringify(def)), currentValue(stringify(def)), type(SPIN), minValue(minv), maxValue(maxv) {}
318 // get_option_value is the implementation of the various
319 // get_option_value_<type>, because only the option value
320 // type changes a template is the proper solution.
323 T get_option_value(const std::string& optionName) {
327 Options::iterator it = option_from_name(optionName);
329 if (it != options.end())
331 std::istringstream ss(it->currentValue);
338 // option_from_name returns an iterator to the option
339 // with name optionName.
341 Options::iterator option_from_name(const std::string& optionName) {
343 for (Options::iterator it = options.begin(); it != options.end(); ++it)
344 if (it->name == optionName)
347 return options.end();