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