]> git.sesse.net Git - stockfish/blob - src/ucioption.cpp
Restore development versioning and LSN filtering
[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 <string>
27 #include <sstream>
28 #include <vector>
29
30 #include "misc.h"
31 #include "thread.h"
32 #include "ucioption.h"
33
34
35 ////
36 //// Variables
37 ////
38
39 bool Chess960 = false;
40
41
42 ////
43 //// Local definitions
44 ////
45
46 namespace {
47
48   ///
49   /// Types
50   ///
51
52   enum OptionType { SPIN, COMBO, CHECK, STRING, BUTTON };
53
54   typedef std::vector<std::string> ComboValues;
55
56   struct Option {
57
58     std::string name, defaultValue, currentValue;
59     OptionType type;
60     int minValue, maxValue;
61     ComboValues comboValues;
62
63     Option(const char* name, const char* defaultValue, OptionType = STRING);
64     Option(const char* name, bool defaultValue, OptionType = CHECK);
65     Option(const char* name, int defaultValue, int minValue, int maxValue);
66   };
67
68   typedef std::vector<Option> Options;
69
70   ///
71   /// Constants
72   ///
73
74   // load_defaults populates the options vector with the hard
75   // coded names and default values.
76
77   void load_defaults(Options& o) {
78
79     o.push_back(Option("Use Search Log", false));
80     o.push_back(Option("Search Log Filename", "SearchLog.txt"));
81     o.push_back(Option("Book File", "book.bin"));
82     o.push_back(Option("Mobility (Middle Game)", 100, 0, 200));
83     o.push_back(Option("Mobility (Endgame)", 100, 0, 200));
84     o.push_back(Option("Pawn Structure (Middle Game)", 100, 0, 200));
85     o.push_back(Option("Pawn Structure (Endgame)", 100, 0, 200));
86     o.push_back(Option("Passed Pawns (Middle Game)", 100, 0, 200));
87     o.push_back(Option("Passed Pawns (Endgame)", 100, 0, 200));
88     o.push_back(Option("Aggressiveness", 100, 0, 200));
89     o.push_back(Option("Cowardice", 100, 0, 200));
90     o.push_back(Option("King Safety Curve", "Quadratic", COMBO));
91
92        o.back().comboValues.push_back("Quadratic");
93        o.back().comboValues.push_back("Linear");  /*, "From File"*/
94
95     o.push_back(Option("King Safety Coefficient", 40, 1, 100));
96     o.push_back(Option("King Safety X Intercept", 0, 0, 20));
97     o.push_back(Option("King Safety Max Slope", 30, 10, 100));
98     o.push_back(Option("King Safety Max Value", 500, 100, 1000));
99     o.push_back(Option("Queen Contact Check Bonus", 4, 0, 8));
100     o.push_back(Option("Rook Contact Check Bonus", 2, 0, 4));
101     o.push_back(Option("Queen Check Bonus", 2, 0, 4));
102     o.push_back(Option("Rook Check Bonus", 1, 0, 4));
103     o.push_back(Option("Bishop Check Bonus", 1, 0, 4));
104     o.push_back(Option("Knight Check Bonus", 1, 0, 4));
105     o.push_back(Option("Discovered Check Bonus", 3, 0, 8));
106     o.push_back(Option("Mate Threat Bonus", 3, 0, 8));
107     o.push_back(Option("Check Extension (PV nodes)", 2, 0, 2));
108     o.push_back(Option("Check Extension (non-PV nodes)", 1, 0, 2));
109     o.push_back(Option("Single Reply Extension (PV nodes)", 2, 0, 2));
110     o.push_back(Option("Single Reply Extension (non-PV nodes)", 2, 0, 2));
111     o.push_back(Option("Mate Threat Extension (PV nodes)", 0, 0, 2));
112     o.push_back(Option("Mate Threat Extension (non-PV nodes)", 0, 0, 2));
113     o.push_back(Option("Pawn Push to 7th Extension (PV nodes)", 1, 0, 2));
114     o.push_back(Option("Pawn Push to 7th Extension (non-PV nodes)", 1, 0, 2));
115     o.push_back(Option("Passed Pawn Extension (PV nodes)", 1, 0, 2));
116     o.push_back(Option("Passed Pawn Extension (non-PV nodes)", 0, 0, 2));
117     o.push_back(Option("Pawn Endgame Extension (PV nodes)", 2, 0, 2));
118     o.push_back(Option("Pawn Endgame Extension (non-PV nodes)", 2, 0, 2));
119     o.push_back(Option("Full Depth Moves (PV nodes)", 14, 1, 100));
120     o.push_back(Option("Full Depth Moves (non-PV nodes)", 3, 1, 100));
121     o.push_back(Option("Threat Depth", 5, 0, 100));
122     o.push_back(Option("Selective Plies", 7, 0, 10));
123     o.push_back(Option("Null driven IID", false));
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("LSN filtering", true));
132     o.push_back(Option("LSN Time Margin (sec)", 4, 1, 10));
133     o.push_back(Option("LSN Value Margin", 200, 100, 600));
134     o.push_back(Option("Randomness", 0, 0, 10));
135     o.push_back(Option("Minimum Split Depth", 4, 4, 7));
136     o.push_back(Option("Maximum Number of Threads per Split Point", 5, 4, 8));
137     o.push_back(Option("Threads", 1, 1, 8));
138     o.push_back(Option("Hash", 32, 4, 4096));
139     o.push_back(Option("Clear Hash", false, BUTTON));
140     o.push_back(Option("Ponder", true));
141     o.push_back(Option("OwnBook", true));
142     o.push_back(Option("MultiPV", 1, 1, 500));
143     o.push_back(Option("UCI_ShowCurrLine", false));
144     o.push_back(Option("UCI_Chess960", false));
145   }
146
147   ///
148   /// Variables
149   ///
150
151   Options options;
152
153   // Local functions
154   Options::iterator option_with_name(const std::string& optionName);
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   // We want conversion from a bool value to be "true" or "false",
166   // not "1" or "0", so add a specialization for bool type.
167   template<>
168   std::string stringify<bool>(const bool& v) {
169
170     return v ? "true" : "false";
171   }
172
173   // get_option_value implements the various get_option_value_<type>
174   // functions defined later, because only the option value
175   // type changes a template seems a proper solution.
176
177   template<typename T>
178   T get_option_value(const std::string& optionName) {
179
180       T ret = T();
181       Options::iterator it = option_with_name(optionName);
182
183       if (it != options.end())
184       {
185           std::istringstream ss(it->currentValue);
186           ss >> ret;
187       }
188       return ret;
189   }
190
191   // Unfortunatly we need a specialization to convert "false" and "true"
192   // to proper bool values. The culprit is that we use a non standard way
193   // to store a bool value in a string, in particular we use "false" and
194   // "true" instead of "0" and "1" due to how UCI protocol works.
195
196   template<>
197   bool get_option_value<bool>(const std::string& optionName) {
198
199       Options::iterator it = option_with_name(optionName);
200
201       return it != options.end() && it->currentValue == "true";
202   }
203 }
204
205 ////
206 //// Functions
207 ////
208
209 /// init_uci_options() initializes the UCI options.  Currently, the only
210 /// thing this function does is to initialize the default value of the
211 /// "Threads" parameter to the number of available CPU cores.
212
213 void init_uci_options() {
214
215   load_defaults(options);
216
217   // Limit the default value of "Threads" to 7 even if we have 8 CPU cores.
218   // According to Ken Dail's tests, Glaurung plays much better with 7 than
219   // with 8 threads.  This is weird, but it is probably difficult to find out
220   // why before I have a 8-core computer to experiment with myself.
221   Options::iterator it = option_with_name("Threads");
222
223   assert(it != options.end());
224
225   it->defaultValue = stringify(Min(cpu_count(), 7));
226   it->currentValue = stringify(Min(cpu_count(), 7));
227
228   // Increase the minimum split depth when the number of CPUs is big.
229   // It would probably be better to let this depend on the number of threads
230   // instead.
231   if(cpu_count() > 4)
232   {
233       it = option_with_name("Minimum Split Depth");
234
235       assert(it != options.end());
236
237       it->defaultValue = "6";
238       it->currentValue = "6";
239   }
240 }
241
242
243 /// print_uci_options() prints all the UCI options to the standard output,
244 /// in the format defined by the UCI protocol.
245
246 void print_uci_options() {
247
248   static const char optionTypeName[][16] = {
249     "spin", "combo", "check", "string", "button"
250   };
251
252   for (Options::iterator it = options.begin(); it != options.end(); ++it)
253   {
254       std::cout << "option name " << it->name
255                 << " type "       << optionTypeName[it->type];
256
257       if (it->type != BUTTON)
258       {
259           std::cout << " default " << it->defaultValue;
260
261           if (it->type == SPIN)
262               std::cout << " min " << it->minValue
263                         << " max " << it->maxValue;
264
265           else if (it->type == COMBO)
266               for(ComboValues::iterator itc = it->comboValues.begin();
267                   itc != it->comboValues.end(); ++itc)
268                       std::cout << " var " << *itc;
269       }
270       std::cout << std::endl;
271   }
272 }
273
274 /// get_option_value_bool() returns the current value of a UCI parameter of
275 /// type "check".
276
277 bool get_option_value_bool(const std::string& optionName) {
278
279   return get_option_value<bool>(optionName);
280 }
281
282
283 /// get_option_value_int() returns the value of a UCI parameter as an integer.
284 /// Normally, this function will be used for a parameter of type "spin", but
285 /// it could also be used with a "combo" parameter, where all the available
286 /// values are integers.
287
288 int get_option_value_int(const std::string& optionName) {
289
290   return get_option_value<int>(optionName);
291 }
292
293
294 /// get_option_value_string() returns the current value of a UCI parameter as
295 /// a string.  It is used with parameters of type "combo" and "string".
296
297 const std::string get_option_value_string(const std::string& optionName) {
298
299    return get_option_value<std::string>(optionName);
300 }
301
302
303 /// button_was_pressed() tests whether a UCI parameter of type "button" has
304 /// been selected since the last time the function was called.
305
306 bool button_was_pressed(const std::string& buttonName) {
307
308   if (get_option_value<bool>(buttonName))
309   {
310     set_option_value(buttonName, "false");
311     return true;
312   }
313
314   return false;
315 }
316
317
318 /// set_option_value() inserts a new value for a UCI parameter.  Note that
319 /// the function does not check that the new value is legal for the given
320 /// parameter:  This is assumed to be the responsibility of the GUI.
321
322 void set_option_value(const std::string& optionName,
323                       const std::string& newValue) {
324
325   Options::iterator it = option_with_name(optionName);
326
327   if (it != options.end())
328       it->currentValue = newValue;
329   else
330       std::cout << "No such option: " << optionName << std::endl;
331 }
332
333
334 /// push_button() is used to tell the engine that a UCI parameter of type
335 /// "button" has been selected:
336
337 void push_button(const std::string& buttonName) {
338
339   set_option_value(buttonName, "true");
340 }
341
342
343 namespace {
344
345     // Define constructors of Option class.
346
347     Option::Option(const char* nm, const char* def, OptionType t)
348     : name(nm), defaultValue(def), currentValue(def), type(t), minValue(0), maxValue(0) {}
349
350     Option::Option(const char* nm, bool def, OptionType t)
351     : name(nm), defaultValue(stringify(def)), currentValue(stringify(def)), type(t), minValue(0), maxValue(0) {}
352
353     Option::Option(const char* nm, int def, int minv, int maxv)
354     : name(nm), defaultValue(stringify(def)), currentValue(stringify(def)), type(SPIN), minValue(minv), maxValue(maxv) {}
355
356     // option_with_name() tries to find a UCI option with a given
357     // name.  It returns an iterator to the UCI option or to options.end(),
358     // depending on whether an option with the given name exists.
359
360     Options::iterator option_with_name(const std::string& optionName) {
361
362         for (Options::iterator it = options.begin(); it != options.end(); ++it)
363             if (it->name == optionName)
364                 return it;
365
366         return options.end();
367     }
368 }