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