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