]> git.sesse.net Git - stockfish/blob - src/ucioption.cpp
9e992be55c26e7fd50462fb4ac5748393d37862e
[stockfish] / src / ucioption.cpp
1 /*
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
5
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.
10
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.
15
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/>.
18 */
19
20
21 ////
22 //// Includes
23 ////
24
25 #include <cassert>
26 #include <map>
27 #include <string>
28 #include <sstream>
29 #include <vector>
30
31 #include "misc.h"
32 #include "thread.h"
33 #include "ucioption.h"
34
35
36 ////
37 //// Variables
38 ////
39
40 bool Chess960 = false;
41
42
43 ////
44 //// Local definitions
45 ////
46
47 namespace {
48
49   ///
50   /// Types
51   ///
52
53   enum OptionType { SPIN, COMBO, CHECK, STRING, BUTTON };
54
55   typedef std::vector<std::string> ComboValues;
56
57   struct Option {
58
59     std::string defaultValue, currentValue;
60     OptionType type;
61     int minValue, maxValue;
62     ComboValues comboValues;
63
64     Option();
65     Option(const std::string& defaultValue, OptionType = STRING);
66     Option(bool defaultValue, OptionType = CHECK);
67     Option(int defaultValue, int minValue, int maxValue);
68   };
69
70   typedef std::map<std::string, Option> Options;
71
72   ///
73   /// Constants
74   ///
75
76   // load_defaults populates the options map with the hard
77   // coded names and default values.
78
79   void load_defaults(Options& o) {
80
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);
94
95        o["King Safety Curve"].comboValues.push_back("Quadratic");
96        o["King Safety Curve"].comboValues.push_back("Linear");  /*, "From File"*/
97
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);
145   }
146
147   ///
148   /// Variables
149   ///
150
151   Options options;
152
153   // stringify converts a value of type T to a std::string
154   template<typename T>
155   std::string stringify(const T& v) {
156
157      std::ostringstream ss;
158      ss << v;
159      return ss.str();
160   }
161
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.
164   template<>
165   std::string stringify<bool>(const bool& v) {
166
167     return v ? "true" : "false";
168   }
169
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.
173
174   template<typename T>
175   T get_option_value(const std::string& optionName) {
176
177       T ret = T();
178       if (options.find(optionName) == options.end())
179           return ret;
180
181       std::istringstream ss(options[optionName].currentValue);
182       ss >> ret;
183       return ret;
184   }
185
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.
190
191   template<>
192   bool get_option_value<bool>(const std::string& optionName) {
193
194       if (options.find(optionName) == options.end())
195           return false;
196
197       return options[optionName].currentValue == "true";
198   }
199 }
200
201 ////
202 //// Functions
203 ////
204
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.
208
209 void init_uci_options() {
210
211   load_defaults(options);
212
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());
219
220   options["Threads"].defaultValue = stringify(Min(cpu_count(), 7));
221   options["Threads"].currentValue = stringify(Min(cpu_count(), 7));
222
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
225   // instead.
226   if (cpu_count() > 4)
227   {
228       options["Minimum Split Depth"].defaultValue = "6";
229       options["Minimum Split Depth"].currentValue = "6";
230   }
231 }
232
233
234 /// print_uci_options() prints all the UCI options to the standard output,
235 /// in the format defined by the UCI protocol.
236
237 void print_uci_options() {
238
239   static const char optionTypeName[][16] = {
240     "spin", "combo", "check", "string", "button"
241   };
242
243   for (Options::const_iterator it = options.begin(); it != options.end(); ++it)
244   {
245       const Option& o = it->second;
246       std::cout << "option name " << it->first
247                 << " type "       << optionTypeName[o.type];
248
249       if (o.type != BUTTON)
250       {
251           std::cout << " default " << o.defaultValue;
252
253           if (o.type == SPIN)
254               std::cout << " min " << o.minValue
255                         << " max " << o.maxValue;
256
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;
261       }
262       std::cout << std::endl;
263   }
264 }
265
266
267 /// get_option_value_bool() returns the current value of a UCI parameter of
268 /// type "check".
269
270 bool get_option_value_bool(const std::string& optionName) {
271
272   return get_option_value<bool>(optionName);
273 }
274
275
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.
280
281 int get_option_value_int(const std::string& optionName) {
282
283   return get_option_value<int>(optionName);
284 }
285
286
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".
289
290 const std::string get_option_value_string(const std::string& optionName) {
291
292    return get_option_value<std::string>(optionName);
293 }
294
295
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.
299
300 void set_option_value(const std::string& optionName,
301                       const std::string& newValue) {
302
303   if (options.find(optionName) != options.end())
304       options[optionName].currentValue = newValue;
305   else
306       std::cout << "No such option: " << optionName << std::endl;
307 }
308
309
310 /// push_button() is used to tell the engine that a UCI parameter of type
311 /// "button" has been selected:
312
313 void push_button(const std::string& buttonName) {
314
315   set_option_value(buttonName, "true");
316 }
317
318
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.
322
323 bool button_was_pressed(const std::string& buttonName) {
324
325   if (!get_option_value<bool>(buttonName))
326           return false;
327
328   set_option_value(buttonName, "false");
329   return true;
330 }
331
332
333 namespace {
334
335   // Define constructors of Option class.
336
337   Option::Option() {} // To allow insertion in a std::map
338
339   Option::Option(const std::string& def, OptionType t)
340   : defaultValue(def), currentValue(def), type(t), minValue(0), maxValue(0) {}
341
342   Option::Option(bool def, OptionType t)
343   : defaultValue(stringify(def)), currentValue(stringify(def)), type(t), minValue(0), maxValue(0) {}
344
345   Option::Option(int def, int minv, int maxv)
346   : defaultValue(stringify(def)), currentValue(stringify(def)), type(SPIN), minValue(minv), maxValue(maxv) {}
347
348 }