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