]> git.sesse.net Git - stockfish/blob - src/ucioption.cpp
37c0d40f9954f700107261f4c796d6c1aee8ea54
[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, OPTION_TYPE_NONE };
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     // Helper to convert a bool or an int to a std::string
63     template<typename T> std::string stringify(const T& v);
64
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);
68   };
69
70   typedef std::vector<Option> Options;
71
72   ///
73   /// Constants
74   ///
75
76   // load-defaults populates the options map with the hard
77   // coded options names and their default values.
78   void load_defaults(Options& o) {
79
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));
92
93        o.back().comboValues.push_back("Quadratic");
94        o.back().comboValues.push_back("Linear");  /*, "From File"*/
95
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));
142   }
143
144
145   ///
146   /// Variables
147   ///
148
149   Options options;
150
151   // Local functions
152   template<typename T> T get_option_value(const std::string& optionName);
153   Options::iterator option_from_name(const std::string& optionName);
154
155 }
156
157 ////
158 //// Functions
159 ////
160
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.
164
165 void init_uci_options() {
166
167   load_defaults(options);
168
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");
174
175   assert(it != options.end());
176
177   it->defaultValue = Min(cpu_count(), 7);
178   it->currentValue = Min(cpu_count(), 7);
179
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
182   // instead.
183   if(cpu_count() > 4)
184   {
185       it = option_from_name("Minimum Split Depth");
186
187       assert(it != options.end());
188
189       it->defaultValue = 6;
190       it->currentValue = 6;
191   }
192 }
193
194
195 /// print_uci_options() prints all the UCI options to the standard output,
196 /// in the format defined by the UCI protocol.
197
198 void print_uci_options() {
199
200   static const char optionTypeName[][16] = {
201     "spin", "combo", "check", "string", "button"
202   };
203
204   for (Options::iterator it = options.begin(); it != options.end(); ++it)
205   {
206       std::cout << "option name " << it->name
207                 << " type "       << optionTypeName[it->type];
208
209       if (it->type != BUTTON)
210       {
211           std::cout << " default " << it->defaultValue;
212
213           if (it->type == SPIN)
214               std::cout << " min " << it->minValue
215                         << " max " << it->maxValue;
216
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;
221       }
222       std::cout << std::endl;
223   }
224 }
225
226 /// get_option_value_bool() returns the current value of a UCI parameter of
227 /// type "check".
228
229 bool get_option_value_bool(const std::string& optionName) {
230
231   return get_option_value<bool>(optionName);
232 }
233
234
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.
239
240 int get_option_value_int(const std::string& optionName) {
241
242   return get_option_value<int>(optionName);
243 }
244
245
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".
248
249 const std::string get_option_value_string(const std::string& optionName) {
250
251    return get_option_value<std::string>(optionName);
252 }
253
254
255 /// button_was_pressed() tests whether a UCI parameter of type "button" has
256 /// been selected since the last time the function was called.
257
258 bool button_was_pressed(const std::string& buttonName) {
259
260   if (get_option_value<bool>(buttonName))
261   {
262     set_option_value(buttonName, "false");
263     return true;
264   }
265
266   return false;
267 }
268
269
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.
273
274 void set_option_value(const std::string& optionName,
275                       const std::string& newValue) {
276
277   Options::iterator it = option_from_name(optionName);
278
279   if (it != options.end())
280       it->currentValue = newValue;
281   else
282       std::cout << "No such option: " << optionName << std::endl;
283 }
284
285
286 /// push_button() is used to tell the engine that a UCI parameter of type
287 /// "button" has been selected:
288
289 void push_button(const std::string& buttonName) {
290
291   set_option_value(buttonName, "true");
292 }
293
294
295 namespace {
296
297     // methods and c'tors of Option class.
298
299     template<typename T>
300     std::string Option::stringify(const T& v)
301     {
302         std::ostringstream ss;
303         ss << v;
304
305         return ss.str();
306     }
307
308     Option::Option(const char* nm, const char* def, OptionType t)
309     : name(nm), defaultValue(def), currentValue(def), type(t), minValue(0), maxValue(0) {}
310
311     Option::Option(const char* nm, bool def)
312     : name(nm), defaultValue(stringify(def)), currentValue(stringify(def)), type(CHECK), minValue(0), maxValue(0) {}
313
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) {}
316
317
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.
321
322     template<typename T>
323     T get_option_value(const std::string& optionName) {
324
325         T ret;
326
327         Options::iterator it = option_from_name(optionName);
328
329         if (it != options.end())
330         {
331             std::istringstream ss(it->currentValue);
332             ss >> ret;
333         }
334         return ret;
335     }
336
337
338     // option_from_name returns an iterator to the option
339     // with name optionName.
340
341     Options::iterator option_from_name(const std::string& optionName) {
342
343         for (Options::iterator it = options.begin(); it != options.end(); ++it)
344             if (it->name == optionName)
345                 return it;
346
347         return options.end();
348     }
349
350 }