]> git.sesse.net Git - stockfish/blob - src/ucioption.cpp
Update copyright year
[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-2009 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 <algorithm>
26 #include <cassert>
27 #include <map>
28 #include <string>
29 #include <sstream>
30 #include <vector>
31
32 #include "misc.h"
33 #include "thread.h"
34 #include "ucioption.h"
35
36
37 ////
38 //// Variables
39 ////
40
41 bool Chess960;
42
43
44 ////
45 //// Local definitions
46 ////
47
48 namespace {
49
50   ///
51   /// Types
52   ///
53
54   enum OptionType { SPIN, COMBO, CHECK, STRING, BUTTON };
55
56   typedef std::vector<std::string> ComboValues;
57
58   struct Option {
59
60     std::string name, defaultValue, currentValue;
61     OptionType type;
62     size_t idx;
63     int minValue, maxValue;
64     ComboValues comboValues;
65
66     Option();
67     Option(const char* defaultValue, OptionType = STRING);
68     Option(bool defaultValue, OptionType = CHECK);
69     Option(int defaultValue, int minValue, int maxValue);
70
71     bool operator<(const Option& o) const { return this->idx < o.idx; }
72   };
73
74   typedef std::map<std::string, Option> Options;
75
76   ///
77   /// Constants
78   ///
79
80   // load_defaults populates the options map with the hard
81   // coded names and default values.
82
83   void load_defaults(Options& o) {
84
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);
98
99        o["King Safety Curve"].comboValues.push_back("Quadratic");
100        o["King Safety Curve"].comboValues.push_back("Linear");  /*, "From File"*/
101
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["Futility Pruning (Main Search)"] = Option(true);
129     o["Futility Pruning (Quiescence Search)"] = Option(true);
130     o["LSN filtering"] = Option(true);
131     o["LSN Time Margin (sec)"] = Option(4, 1, 10);
132     o["LSN Value Margin"] = Option(200, 100, 600);
133     o["Randomness"] = Option(0, 0, 10);
134     o["Minimum Split Depth"] = Option(4, 4, 7);
135     o["Maximum Number of Threads per Split Point"] = Option(5, 4, 8);
136     o["Threads"] = Option(1, 1, 8);
137     o["Hash"] = Option(32, 4, 4096);
138     o["Clear Hash"] = Option(false, BUTTON);
139     o["Ponder"] = Option(true);
140     o["OwnBook"] = Option(true);
141     o["MultiPV"] = Option(1, 1, 500);
142     o["UCI_ShowCurrLine"] = Option(false);
143     o["UCI_Chess960"] = Option(false);
144
145     // Any option should know its name so to be easily printed
146     for (Options::iterator it = o.begin(); it != o.end(); ++it)
147         it->second.name = it->first;
148   }
149
150   ///
151   /// Variables
152   ///
153
154   Options options;
155
156   // stringify converts a value of type T to a std::string
157   template<typename T>
158   std::string stringify(const T& v) {
159
160      std::ostringstream ss;
161      ss << v;
162      return ss.str();
163   }
164
165
166   // get_option_value implements the various get_option_value_<type>
167   // functions defined later, because only the option value
168   // type changes a template seems a proper solution.
169
170   template<typename T>
171   T get_option_value(const std::string& optionName) {
172
173       T ret = T();
174       if (options.find(optionName) == options.end())
175           return ret;
176
177       std::istringstream ss(options[optionName].currentValue);
178       ss >> ret;
179       return ret;
180   }
181
182 }
183
184 ////
185 //// Functions
186 ////
187
188 /// init_uci_options() initializes the UCI options.  Currently, the only
189 /// thing this function does is to initialize the default value of the
190 /// "Threads" parameter to the number of available CPU cores.
191
192 void init_uci_options() {
193
194   load_defaults(options);
195
196   // Limit the default value of "Threads" to 7 even if we have 8 CPU cores.
197   // According to Ken Dail's tests, Glaurung plays much better with 7 than
198   // with 8 threads.  This is weird, but it is probably difficult to find out
199   // why before I have a 8-core computer to experiment with myself.
200   assert(options.find("Threads") != options.end());
201   assert(options.find("Minimum Split Depth") != options.end());
202
203   options["Threads"].defaultValue = stringify(Min(cpu_count(), 7));
204   options["Threads"].currentValue = stringify(Min(cpu_count(), 7));
205
206   // Increase the minimum split depth when the number of CPUs is big.
207   // It would probably be better to let this depend on the number of threads
208   // instead.
209   if (cpu_count() > 4)
210   {
211       options["Minimum Split Depth"].defaultValue = "6";
212       options["Minimum Split Depth"].currentValue = "6";
213   }
214 }
215
216
217 /// print_uci_options() prints all the UCI options to the standard output,
218 /// in the format defined by the UCI protocol.
219
220 void print_uci_options() {
221
222   static const char optionTypeName[][16] = {
223     "spin", "combo", "check", "string", "button"
224   };
225
226   // Build up a vector out of the options map and sort it according to idx
227   // field, that is the chronological insertion order in options map.
228   std::vector<Option> vec;
229   for (Options::const_iterator it = options.begin(); it != options.end(); ++it)
230       vec.push_back(it->second);
231
232   std::sort(vec.begin(), vec.end());
233
234   for (std::vector<Option>::const_iterator it = vec.begin(); it != vec.end(); ++it)
235   {
236       std::cout << "\noption name " << it->name
237                 << " type "         << optionTypeName[it->type];
238
239       if (it->type == BUTTON)
240           continue;
241
242       if (it->type == CHECK)
243           std::cout << " default " << (it->defaultValue == "1" ? "true" : "false");
244       else
245           std::cout << " default " << it->defaultValue;
246
247       if (it->type == SPIN)
248           std::cout << " min " << it->minValue
249                     << " max " << it->maxValue;
250
251       else if (it->type == COMBO)
252           for (ComboValues::const_iterator itc = it->comboValues.begin();
253               itc != it->comboValues.end(); ++itc)
254               std::cout << " var " << *itc;
255   }
256   std::cout << std::endl;
257 }
258
259
260 /// get_option_value_bool() returns the current value of a UCI parameter of
261 /// type "check".
262
263 bool get_option_value_bool(const std::string& optionName) {
264
265   return get_option_value<bool>(optionName);
266 }
267
268
269 /// get_option_value_int() returns the value of a UCI parameter as an integer.
270 /// Normally, this function will be used for a parameter of type "spin", but
271 /// it could also be used with a "combo" parameter, where all the available
272 /// values are integers.
273
274 int get_option_value_int(const std::string& optionName) {
275
276   return get_option_value<int>(optionName);
277 }
278
279
280 /// get_option_value_string() returns the current value of a UCI parameter as
281 /// a string. It is used with parameters of type "combo" and "string".
282
283 const std::string get_option_value_string(const std::string& optionName) {
284
285    return get_option_value<std::string>(optionName);
286 }
287
288
289 /// set_option_value() inserts a new value for a UCI parameter. Note that
290 /// the function does not check that the new value is legal for the given
291 /// parameter: This is assumed to be the responsibility of the GUI.
292
293 void set_option_value(const std::string& optionName,
294                       const std::string& newValue) {
295
296   // UCI protocol uses "true" and "false" instead of "1" and "0", so convert
297   // newValue according to standard C++ convention before to store it.
298   std::string v(newValue);
299   if (v == "true")
300       v = "1";
301   else if (v == "false")
302       v = "0";
303
304   if (options.find(optionName) != options.end())
305       options[optionName].currentValue = v;
306   else
307       std::cout << "No such option: " << optionName << std::endl;
308 }
309
310
311 /// push_button() is used to tell the engine that a UCI parameter of type
312 /// "button" has been selected:
313
314 void push_button(const std::string& buttonName) {
315
316   set_option_value(buttonName, "true");
317 }
318
319
320 /// button_was_pressed() tests whether a UCI parameter of type "button" has
321 /// been selected since the last time the function was called, in this case
322 /// it also resets the button.
323
324 bool button_was_pressed(const std::string& buttonName) {
325
326   if (!get_option_value<bool>(buttonName))
327           return false;
328
329   set_option_value(buttonName, "false");
330   return true;
331 }
332
333
334 namespace {
335
336   // Define constructors of Option class.
337
338   Option::Option() {} // To allow insertion in a std::map
339
340   Option::Option(const char* def, OptionType t)
341   : defaultValue(def), currentValue(def), type(t), idx(options.size()), minValue(0), maxValue(0) {}
342
343   Option::Option(bool def, OptionType t)
344   : defaultValue(stringify(def)), currentValue(stringify(def)), type(t), idx(options.size()), minValue(0), maxValue(0) {}
345
346   Option::Option(int def, int minv, int maxv)
347   : defaultValue(stringify(def)), currentValue(stringify(def)), type(SPIN), idx(options.size()), minValue(minv), maxValue(maxv) {}
348
349 }