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