]> git.sesse.net Git - stockfish/blob - src/ucioption.cpp
Revert previous patch
[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-2010 Marco Costalba, Joona Kiiski, Tord Romstad
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 using std::string;
37
38 ////
39 //// Local definitions
40 ////
41
42 namespace {
43
44   ///
45   /// Types
46   ///
47
48   enum OptionType { SPIN, COMBO, CHECK, STRING, BUTTON };
49
50   typedef std::vector<string> ComboValues;
51
52   struct Option {
53
54     string name, defaultValue, currentValue;
55     OptionType type;
56     size_t idx;
57     int minValue, maxValue;
58     ComboValues comboValues;
59
60     Option();
61     Option(const char* defaultValue, OptionType = STRING);
62     Option(bool defaultValue, OptionType = CHECK);
63     Option(int defaultValue, int minValue, int maxValue);
64
65     bool operator<(const Option& o) const { return this->idx < o.idx; }
66   };
67
68   typedef std::map<string, Option> Options;
69
70   ///
71   /// Constants
72   ///
73
74   // load_defaults populates the options map with the hard
75   // coded names and default values.
76
77   void load_defaults(Options& o) {
78
79     o["Use Search Log"] = Option(false);
80     o["Search Log Filename"] = Option("SearchLog.txt");
81     o["Book File"] = Option("book.bin");
82     o["Best Book Move"] = Option(false);
83     o["Mobility (Middle Game)"] = Option(100, 0, 200);
84     o["Mobility (Endgame)"] = Option(100, 0, 200);
85     o["Pawn Structure (Middle Game)"] = Option(100, 0, 200);
86     o["Pawn Structure (Endgame)"] = Option(100, 0, 200);
87     o["Passed Pawns (Middle Game)"] = Option(100, 0, 200);
88     o["Passed Pawns (Endgame)"] = Option(100, 0, 200);
89     o["Space"] = Option(100, 0, 200);
90     o["Aggressiveness"] = Option(100, 0, 200);
91     o["Cowardice"] = Option(100, 0, 200);
92     o["Check Extension (PV nodes)"] = Option(2, 0, 2);
93     o["Check Extension (non-PV nodes)"] = Option(1, 0, 2);
94     o["Single Evasion Extension (PV nodes)"] = Option(2, 0, 2);
95     o["Single Evasion Extension (non-PV nodes)"] = Option(2, 0, 2);
96     o["Mate Threat Extension (PV nodes)"] = Option(2, 0, 2);
97     o["Mate Threat Extension (non-PV nodes)"] = Option(2, 0, 2);
98     o["Pawn Push to 7th Extension (PV nodes)"] = Option(1, 0, 2);
99     o["Pawn Push to 7th Extension (non-PV nodes)"] = Option(1, 0, 2);
100     o["Passed Pawn Extension (PV nodes)"] = Option(1, 0, 2);
101     o["Passed Pawn Extension (non-PV nodes)"] = Option(0, 0, 2);
102     o["Pawn Endgame Extension (PV nodes)"] = Option(2, 0, 2);
103     o["Pawn Endgame Extension (non-PV nodes)"] = Option(2, 0, 2);
104     o["Randomness"] = Option(0, 0, 10);
105     o["Minimum Split Depth"] = Option(4, 4, 7);
106     o["Maximum Number of Threads per Split Point"] = Option(5, 4, 8);
107     o["Threads"] = Option(1, 1, MAX_THREADS);
108     o["Hash"] = Option(32, 4, 8192);
109     o["Clear Hash"] = Option(false, BUTTON);
110     o["New Game"] = Option(false, BUTTON);
111     o["Ponder"] = Option(true);
112     o["OwnBook"] = Option(true);
113     o["MultiPV"] = Option(1, 1, 500);
114     o["UCI_Chess960"] = Option(false);
115     o["UCI_AnalyseMode"] = Option(false);
116
117     // Any option should know its name so to be easily printed
118     for (Options::iterator it = o.begin(); it != o.end(); ++it)
119         it->second.name = it->first;
120   }
121
122   ///
123   /// Variables
124   ///
125
126   Options options;
127
128   // stringify converts a value of type T to a std::string
129   template<typename T>
130   string stringify(const T& v) {
131
132      std::ostringstream ss;
133      ss << v;
134      return ss.str();
135   }
136
137
138   // get_option_value implements the various get_option_value_<type>
139   // functions defined later, because only the option value
140   // type changes a template seems a proper solution.
141
142   template<typename T>
143   T get_option_value(const string& optionName) {
144
145       T ret = T();
146       if (options.find(optionName) == options.end())
147           return ret;
148
149       std::istringstream ss(options[optionName].currentValue);
150       ss >> ret;
151       return ret;
152   }
153
154   // Specialization for std::string where instruction 'ss >> ret;'
155   // would erroneusly tokenize a string with spaces.
156
157   template<>
158   string get_option_value<string>(const string& optionName) {
159
160       if (options.find(optionName) == options.end())
161           return string();
162
163       return options[optionName].currentValue;
164   }
165
166 }
167
168 ////
169 //// Functions
170 ////
171
172 /// init_uci_options() initializes the UCI options.  Currently, the only
173 /// thing this function does is to initialize the default value of the
174 /// "Threads" parameter to the number of available CPU cores.
175
176 void init_uci_options() {
177
178   load_defaults(options);
179
180   // Set optimal value for parameter "Minimum Split Depth"
181   // according to number of available cores.
182   assert(options.find("Threads") != options.end());
183   assert(options.find("Minimum Split Depth") != options.end());
184
185   Option& thr = options["Threads"];
186   Option& msd = options["Minimum Split Depth"];
187
188   thr.defaultValue = thr.currentValue = stringify(cpu_count());
189
190   if (cpu_count() >= 8)
191       msd.defaultValue = msd.currentValue = stringify(7);
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   // Build up a vector out of the options map and sort it according to idx
205   // field, that is the chronological insertion order in options map.
206   std::vector<Option> vec;
207   for (Options::const_iterator it = options.begin(); it != options.end(); ++it)
208       vec.push_back(it->second);
209
210   std::sort(vec.begin(), vec.end());
211
212   for (std::vector<Option>::const_iterator it = vec.begin(); it != vec.end(); ++it)
213   {
214       std::cout << "\noption name " << it->name
215                 << " type "         << optionTypeName[it->type];
216
217       if (it->type == BUTTON)
218           continue;
219
220       if (it->type == CHECK)
221           std::cout << " default " << (it->defaultValue == "1" ? "true" : "false");
222       else
223           std::cout << " default " << it->defaultValue;
224
225       if (it->type == SPIN)
226           std::cout << " min " << it->minValue << " max " << it->maxValue;
227       else if (it->type == COMBO)
228           for (ComboValues::const_iterator itc = it->comboValues.begin();
229               itc != it->comboValues.end(); ++itc)
230               std::cout << " var " << *itc;
231   }
232   std::cout << std::endl;
233 }
234
235
236 /// get_option_value_bool() returns the current value of a UCI parameter of
237 /// type "check".
238
239 bool get_option_value_bool(const string& optionName) {
240
241   return get_option_value<bool>(optionName);
242 }
243
244
245 /// get_option_value_int() returns the value of a UCI parameter as an integer.
246 /// Normally, this function will be used for a parameter of type "spin", but
247 /// it could also be used with a "combo" parameter, where all the available
248 /// values are integers.
249
250 int get_option_value_int(const string& optionName) {
251
252   return get_option_value<int>(optionName);
253 }
254
255
256 /// get_option_value_string() returns the current value of a UCI parameter as
257 /// a string. It is used with parameters of type "combo" and "string".
258
259 string get_option_value_string(const string& optionName) {
260
261    return get_option_value<string>(optionName);
262 }
263
264
265 /// set_option_value() inserts a new value for a UCI parameter. Note that
266 /// the function does not check that the new value is legal for the given
267 /// parameter: This is assumed to be the responsibility of the GUI.
268
269 void set_option_value(const string& name, const string& value) {
270
271   // UCI protocol uses "true" and "false" instead of "1" and "0", so convert
272   // value according to standard C++ convention before to store it.
273   string v(value);
274   if (v == "true")
275       v = "1";
276   else if (v == "false")
277       v = "0";
278
279   if (options.find(name) == options.end())
280   {
281       std::cout << "No such option: " << name << std::endl;
282       return;
283   }
284
285   // Normally it's up to the GUI to check for option's limits,
286   // but we could receive the new value directly from the user
287   // by teminal window. So let's check the bounds anyway.
288   Option& opt = options[name];
289
290   if (opt.type == CHECK && v != "0" && v != "1")
291       return;
292
293   else if (opt.type == SPIN)
294   {
295       int val = atoi(v.c_str());
296       if (val < opt.minValue || val > opt.maxValue)
297           return;
298   }
299
300   opt.currentValue = v;
301 }
302
303
304 /// push_button() is used to tell the engine that a UCI parameter of type
305 /// "button" has been selected:
306
307 void push_button(const string& buttonName) {
308
309   set_option_value(buttonName, "true");
310 }
311
312
313 /// button_was_pressed() tests whether a UCI parameter of type "button" has
314 /// been selected since the last time the function was called, in this case
315 /// it also resets the button.
316
317 bool button_was_pressed(const string& buttonName) {
318
319   if (!get_option_value<bool>(buttonName))
320       return false;
321
322   set_option_value(buttonName, "false");
323   return true;
324 }
325
326
327 namespace {
328
329   // Define constructors of Option class.
330
331   Option::Option() {} // To allow insertion in a std::map
332
333   Option::Option(const char* def, OptionType t)
334   : defaultValue(def), currentValue(def), type(t), idx(options.size()), minValue(0), maxValue(0) {}
335
336   Option::Option(bool def, OptionType t)
337   : defaultValue(stringify(def)), currentValue(stringify(def)), type(t), idx(options.size()), minValue(0), maxValue(0) {}
338
339   Option::Option(int def, int minv, int maxv)
340   : defaultValue(stringify(def)), currentValue(stringify(def)), type(SPIN), idx(options.size()), minValue(minv), maxValue(maxv) {}
341
342 }