]> git.sesse.net Git - stockfish/blob - src/ucioption.cpp
Retire direction.h
[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 #include <cctype>
21 #include <iostream>
22 #include <sstream>
23
24 #include "misc.h"
25 #include "thread.h"
26 #include "ucioption.h"
27
28 using std::string;
29 using std::cout;
30 using std::endl;
31
32 OptionsMap Options; // Global object
33
34
35 // Our case insensitive less() function as required by UCI protocol
36 bool CaseInsensitiveLess::operator() (const string& s1, const string& s2) const {
37
38   int c1, c2;
39   size_t i = 0;
40
41   while (i < s1.size() && i < s2.size())
42   {
43       c1 = tolower(s1[i]);
44       c2 = tolower(s2[i++]);
45
46       if (c1 != c2)
47           return c1 < c2;
48   }
49   return s1.size() < s2.size();
50 }
51
52
53 // stringify() converts a numeric value of type T to a std::string
54 template<typename T>
55 static string stringify(const T& v) {
56
57   std::ostringstream ss;
58   ss << v;
59   return ss.str();
60 }
61
62
63 /// init_uci_options() initializes the UCI options to their hard coded default
64 /// values and initializes the default value of "Threads" and "Minimum Split Depth"
65 /// parameters according to the number of CPU cores.
66
67 void init_uci_options() {
68
69   Options["Use Search Log"] = Option(false);
70   Options["Search Log Filename"] = Option("SearchLog.txt");
71   Options["Book File"] = Option("book.bin");
72   Options["Best Book Move"] = Option(false);
73   Options["Mobility (Middle Game)"] = Option(100, 0, 200);
74   Options["Mobility (Endgame)"] = Option(100, 0, 200);
75   Options["Pawn Structure (Middle Game)"] = Option(100, 0, 200);
76   Options["Pawn Structure (Endgame)"] = Option(100, 0, 200);
77   Options["Passed Pawns (Middle Game)"] = Option(100, 0, 200);
78   Options["Passed Pawns (Endgame)"] = Option(100, 0, 200);
79   Options["Space"] = Option(100, 0, 200);
80   Options["Aggressiveness"] = Option(100, 0, 200);
81   Options["Cowardice"] = Option(100, 0, 200);
82   Options["Check Extension (PV nodes)"] = Option(2, 0, 2);
83   Options["Check Extension (non-PV nodes)"] = Option(1, 0, 2);
84   Options["Single Evasion Extension (PV nodes)"] = Option(2, 0, 2);
85   Options["Single Evasion Extension (non-PV nodes)"] = Option(2, 0, 2);
86   Options["Mate Threat Extension (PV nodes)"] = Option(2, 0, 2);
87   Options["Mate Threat Extension (non-PV nodes)"] = Option(2, 0, 2);
88   Options["Pawn Push to 7th Extension (PV nodes)"] = Option(1, 0, 2);
89   Options["Pawn Push to 7th Extension (non-PV nodes)"] = Option(1, 0, 2);
90   Options["Passed Pawn Extension (PV nodes)"] = Option(1, 0, 2);
91   Options["Passed Pawn Extension (non-PV nodes)"] = Option(0, 0, 2);
92   Options["Pawn Endgame Extension (PV nodes)"] = Option(2, 0, 2);
93   Options["Pawn Endgame Extension (non-PV nodes)"] = Option(2, 0, 2);
94   Options["Minimum Split Depth"] = Option(4, 4, 7);
95   Options["Maximum Number of Threads per Split Point"] = Option(5, 4, 8);
96   Options["Threads"] = Option(1, 1, MAX_THREADS);
97   Options["Use Sleeping Threads"] = Option(false);
98   Options["Hash"] = Option(32, 4, 8192);
99   Options["Clear Hash"] = Option(false, "button");
100   Options["Ponder"] = Option(true);
101   Options["OwnBook"] = Option(true);
102   Options["MultiPV"] = Option(1, 1, 500);
103   Options["Emergency Move Horizon"] = Option(40, 0, 50);
104   Options["Emergency Base Time"] = Option(200, 0, 60000);
105   Options["Emergency Move Time"] = Option(70, 0, 5000);
106   Options["Minimum Thinking Time"] = Option(20, 0, 5000);
107   Options["UCI_Chess960"] = Option(false); // Just a dummy but needed by GUIs
108   Options["UCI_AnalyseMode"] = Option(false);
109
110   // Set some SMP parameters accordingly to the detected CPU count
111   Option& thr = Options["Threads"];
112   Option& msd = Options["Minimum Split Depth"];
113
114   thr.defaultValue = thr.currentValue = stringify(cpu_count());
115
116   if (cpu_count() >= 8)
117       msd.defaultValue = msd.currentValue = stringify(7);
118 }
119
120
121 /// print_uci_options() prints all the UCI options to the standard output,
122 /// in chronological insertion order (the idx field) and in the format
123 /// defined by the UCI protocol.
124
125 void print_uci_options() {
126
127   for (size_t i = 0; i <= Options.size(); i++)
128       for (OptionsMap::const_iterator it = Options.begin(); it != Options.end(); ++it)
129           if (it->second.idx == i)
130           {
131               const Option& o = it->second;
132               cout << "\noption name " << it->first << " type " << o.type;
133
134               if (o.type != "button")
135                   cout << " default " << o.defaultValue;
136
137               if (o.type == "spin")
138                   cout << " min " << o.minValue << " max " << o.maxValue;
139
140               break;
141           }
142   cout << endl;
143 }
144
145
146 /// Option class c'tors
147
148 Option::Option(const char* def) : type("string"), idx(Options.size()), minValue(0), maxValue(0)
149 { defaultValue = currentValue = def; }
150
151 Option::Option(bool def, string t) : type(t), idx(Options.size()), minValue(0), maxValue(0)
152 { defaultValue = currentValue = (def ? "true" : "false"); }
153
154 Option::Option(int def, int minv, int maxv) : type("spin"), idx(Options.size()), minValue(minv), maxValue(maxv)
155 { defaultValue = currentValue = stringify(def); }
156
157
158 /// set_value() updates currentValue of the Option object. Normally it's up to
159 /// the GUI to check for option's limits, but we could receive the new value
160 /// directly from the user by teminal window. So let's check the bounds anyway.
161
162 void Option::set_value(const string& value) {
163
164   assert(!type.empty());
165
166   if (    (type == "check" || type == "button")
167       && !(value == "true" || value == "false"))
168       return;
169
170   if (type == "spin")
171   {
172       int v = atoi(value.c_str());
173       if (v < minValue || v > maxValue)
174           return;
175   }
176
177   currentValue = value;
178 }