]> git.sesse.net Git - stockfish/blob - src/ucioption.cpp
Finally remove UCIInputParser class altogether
[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 <cstdarg>
26 #include <cstdio>
27
28 #include "misc.h"
29 #include "thread.h"
30 #include "ucioption.h"
31
32
33 ////
34 //// Variables
35 ////
36
37 bool Chess960 = false;
38
39
40 ////
41 //// Local definitions
42 ////
43
44 namespace {
45
46   ///
47   /// Types
48   ///
49
50   enum OptionType { SPIN, COMBO, CHECK, STRING, BUTTON, OPTION_TYPE_NONE};
51
52   struct Option {
53     char name[50], defaultValue[300], currentValue[300];
54     OptionType type;
55     int minValue, maxValue;
56     char comboValues[8][64];
57   };
58     
59
60   ///
61   /// Variables
62   ///
63
64   Option Options[] = {
65     { "Use Search Log", "false", "false", CHECK, 0, 0, {""} },
66     { "Search Log Filename", "SearchLog.txt", "SearchLog.txt", STRING, 0, 0, {""} },
67     { "Book File", "book.bin", "book.bin", STRING, 0, 0, {""} },
68     { "Mobility (Middle Game)", "100", "100", SPIN, 0, 200, {""} },
69     { "Mobility (Endgame)", "100", "100", SPIN, 0, 200, {""} },
70     { "Pawn Structure (Middle Game)", "100", "100", SPIN, 0, 200, {""} },
71     { "Pawn Structure (Endgame)", "100", "100", SPIN, 0, 200, {""} },
72     { "Passed Pawns (Middle Game)", "100", "100", SPIN, 0, 200, {""} },
73     { "Passed Pawns (Endgame)", "100", "100", SPIN, 0, 200, {""} },
74     { "Aggressiveness", "100", "100", SPIN, 0, 200, {""} },
75     { "Cowardice", "100", "100", SPIN, 0, 200, {""} },
76     { "King Safety Curve", "Quadratic", "Quadratic", COMBO, 0, 0,
77       { "Quadratic", "Linear" /*, "From File"*/ } },
78     { "King Safety Coefficient", "40", "40", SPIN, 1, 100 , {""} },
79     { "King Safety X Intercept", "0", "0", SPIN, 0, 20, {""} },
80     { "King Safety Max Slope", "30", "30", SPIN, 10, 100, {""} },
81     { "King Safety Max Value", "500", "500", SPIN, 100, 1000, {""} },
82     { "Queen Contact Check Bonus", "4", "4", SPIN, 0, 8, {""} },
83     { "Rook Contact Check Bonus", "2", "2", SPIN, 0, 4, {""} },
84     { "Queen Check Bonus", "2", "2", SPIN, 0, 4, {""} },
85     { "Rook Check Bonus", "1", "1", SPIN, 0, 4, {""} },
86     { "Bishop Check Bonus", "1", "1", SPIN, 0, 4, {""} },
87     { "Knight Check Bonus", "1", "1", SPIN, 0, 4, {""} },
88     { "Discovered Check Bonus", "3", "3", SPIN, 0, 8, {""} },
89     { "Mate Threat Bonus", "3", "3", SPIN, 0, 8, {""} },
90     { "Check Extension (PV nodes)", "2", "2", SPIN, 0, 2, {""} },
91     { "Check Extension (non-PV nodes)", "1", "1", SPIN, 0, 2, {""} },
92     { "Single Reply Extension (PV nodes)", "2", "2", SPIN, 0, 2, {""} },
93     { "Single Reply Extension (non-PV nodes)", "2", "2", SPIN, 0, 2, {""} },
94     { "Mate Threat Extension (PV nodes)", "0", "0", SPIN, 0, 2, {""} },
95     { "Mate Threat Extension (non-PV nodes)", "0", "0", SPIN, 0, 2, {""} },
96     { "Pawn Push to 7th Extension (PV nodes)", "1", "1", SPIN, 0, 2, {""} },
97     { "Pawn Push to 7th Extension (non-PV nodes)", "1", "1", SPIN, 0, 2, {""} },
98     { "Passed Pawn Extension (PV nodes)", "1", "1", SPIN, 0, 2, {""} },
99     { "Passed Pawn Extension (non-PV nodes)", "0", "0", SPIN, 0, 2, {""} },
100     { "Pawn Endgame Extension (PV nodes)", "2", "2", SPIN, 0, 2, {""} },
101     { "Pawn Endgame Extension (non-PV nodes)", "2", "2", SPIN, 0, 2, {""} },
102     { "Full Depth Moves (PV nodes)", "14", "14", SPIN, 1, 100, {""} },
103     { "Full Depth Moves (non-PV nodes)", "3", "3", SPIN, 1, 100, {""} },
104     { "Threat Depth", "5", "5", SPIN, 0, 100, {""} },
105     { "Selective Plies", "7", "7", SPIN, 0, 10, {""} },
106     { "Futility Pruning (Main Search)", "true", "true", CHECK, 0, 0, {""} },
107     { "Futility Pruning (Quiescence Search)", "true", "true", CHECK, 0, 0, {""} },
108     { "Futility Margin 0", "50", "50", SPIN, 0, 1000, {""} },
109     { "Futility Margin 1", "100", "100", SPIN, 0, 1000, {""} },
110     { "Futility Margin 2", "300", "300", SPIN, 0, 1000, {""} },
111     { "Maximum Razoring Depth", "3", "3", SPIN, 0, 4, {""} },
112     { "Razoring Margin", "300", "300", SPIN, 150, 600, {""} },
113     { "Randomness", "0", "0", SPIN, 0, 10, {""} },
114     { "Minimum Split Depth", "4", "4", SPIN, 4, 7, {""} },
115     { "Maximum Number of Threads per Split Point", "5", "5", SPIN, 4, 8, {""} },
116     { "Threads", "1", "1", SPIN, 1, 8, {""} },
117     { "Hash", "32", "32", SPIN, 4, 4096, {""} },
118     { "Clear Hash", "false", "false", BUTTON, 0, 0, {""} },
119     { "Ponder", "true", "true", CHECK, 0, 0, {""} },
120     { "OwnBook", "true", "true", CHECK, 0, 0, {""} },
121     { "MultiPV", "1", "1", SPIN, 1, 500, {""} },
122     { "UCI_ShowCurrLine", "false", "false", CHECK, 0, 0, {""} },
123     { "UCI_Chess960", "false", "false", CHECK, 0, 0, {""} },
124     { "", "", "", OPTION_TYPE_NONE, 0, 0, {""}}
125   };
126     
127
128   ///
129   /// Functions
130   ///
131
132   Option *option_with_name(const char *optionName);
133
134 }
135
136
137 ////
138 //// Functions
139 ////
140
141 /// init_uci_options() initializes the UCI options.  Currently, the only
142 /// thing this function does is to initialize the default value of the
143 /// "Threads" parameter to the number of available CPU cores.
144
145 void init_uci_options() {
146   Option *o;
147
148   o = option_with_name("Threads");
149   assert(o != NULL);
150
151   // Limit the default value of "Threads" to 7 even if we have 8 CPU cores.
152   // According to Ken Dail's tests, Glaurung plays much better with 7 than
153   // with 8 threads.  This is weird, but it is probably difficult to find out
154   // why before I have a 8-core computer to experiment with myself.
155   sprintf(o->defaultValue, "%d", Min(cpu_count(), 7));
156   sprintf(o->currentValue, "%d", Min(cpu_count(), 7));
157
158   // Increase the minimum split depth when the number of CPUs is big.
159   // It would probably be better to let this depend on the number of threads
160   // instead.
161   o = option_with_name("Minimum Split Depth");
162   assert(o != NULL);
163   if(cpu_count() > 4) {
164     sprintf(o->defaultValue, "%d", 6);
165     sprintf(o->defaultValue, "%d", 6);
166   }
167 }
168
169
170 /// print_uci_options() prints all the UCI options to the standard output,
171 /// in the format defined by the UCI protocol.
172
173 void print_uci_options() {
174   static const char optionTypeName[][16] = {
175     "spin", "combo", "check", "string", "button"
176   };
177   for(Option *o = Options; o->type != OPTION_TYPE_NONE; o++) {
178     printf("option name %s type %s", o->name, optionTypeName[o->type]);
179     if(o->type != BUTTON) {
180       printf(" default %s", o->defaultValue);
181       if(o->type == SPIN)
182         printf(" min %d max %d", o->minValue, o->maxValue);
183       else if(o->type == COMBO)
184         for(int i = 0; strlen(o->comboValues[i]) > 0; i++)
185           printf(" var %s", o->comboValues[i]);
186     }
187     printf("\n");
188   }
189 }
190
191
192 /// get_option_value_bool() returns the current value of a UCI parameter of
193 /// type "check".
194
195 bool get_option_value_bool(const std::string &optionName) {
196   Option *o = option_with_name(optionName.c_str());
197   return o != NULL && strcmp(o->currentValue, "true") == 0;
198 }
199
200
201 /// get_option_value_int() returns the value of a UCI parameter as an integer.
202 /// Normally, this function will be used for a parameter of type "spin", but
203 /// it could also be used with a "combo" parameter, where all the available
204 /// values are integers.
205
206 int get_option_value_int(const std::string &optionName) {
207   Option *o = option_with_name(optionName.c_str());
208   return atoi(o->currentValue);
209 }
210
211
212 /// get_option_value_string() returns the current value of a UCI parameter as
213 /// a string.  It is used with parameters of type "combo" and "string".
214
215 const std::string get_option_value_string(const std::string &optionName) {
216   Option *o = option_with_name(optionName.c_str());
217   return o->currentValue;
218 }
219
220
221 /// button_was_pressed() tests whether a UCI parameter of type "button" has
222 /// been selected since the last time the function was called.
223
224 bool button_was_pressed(const std::string &buttonName) {
225   if(get_option_value_bool(buttonName)) {
226     set_option_value(buttonName, "false");
227     return true;
228   }
229   else
230     return false;
231 }
232
233
234 /// set_option_value() inserts a new value for a UCI parameter.  Note that
235 /// the function does not check that the new value is legal for the given
236 /// parameter:  This is assumed to be the responsibility of the GUI.
237
238 void set_option_value(const std::string &optionName,
239                       const std::string &newValue) {
240   Option *o = option_with_name(optionName.c_str());
241
242   if(o != NULL)
243     strcpy(o->currentValue, newValue.c_str());
244   else
245     std::cout << "No such option: " << optionName << std::endl;
246 }
247
248
249 /// push_button() is used to tell the engine that a UCI parameter of type
250 /// "button" has been selected:
251
252 void push_button(const std::string &buttonName) {
253   set_option_value(buttonName, "true");
254 }
255
256
257 namespace {
258
259   // option_with_name() tries to find a UCI option with a given
260   // name.  It returns a pointer to the UCI option or the null pointer,
261   // depending on whether an option with the given name exists.
262   
263   Option *option_with_name(const char *optionName) {
264     for(Option *o = Options; o->type != OPTION_TYPE_NONE; o++)
265       if(strcmp(o->name, optionName) == 0)
266         return o;
267     return NULL;
268   }
269
270 }