]> git.sesse.net Git - stockfish/blob - src/ucioption.cpp
Simplify search results update
[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-2013 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 <cassert>
22 #include <cstdlib>
23 #include <sstream>
24
25 #include "evaluate.h"
26 #include "misc.h"
27 #include "thread.h"
28 #include "tt.h"
29 #include "ucioption.h"
30
31 using std::string;
32
33 UCI::OptionsMap Options; // Global object
34
35 namespace UCI {
36
37 /// 'On change' actions, triggered by an option's value change
38 void on_logger(const Option& o) { start_logger(o); }
39 void on_eval(const Option&) { Eval::init(); }
40 void on_threads(const Option&) { Threads.read_uci_options(); }
41 void on_hash_size(const Option& o) { TT.set_size(o); }
42 void on_clear_hash(const Option&) { TT.clear(); }
43
44
45 /// Our case insensitive less() function as required by UCI protocol
46 bool ci_less(char c1, char c2) { return tolower(c1) < tolower(c2); }
47
48 bool CaseInsensitiveLess::operator() (const string& s1, const string& s2) const {
49   return std::lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end(), ci_less);
50 }
51
52
53 /// init() initializes the UCI options to their hard coded default values
54 /// and initializes the default value of "Threads" and "Min Split Depth"
55 /// parameters according to the number of CPU cores detected.
56
57 void init(OptionsMap& o) {
58
59   int cpus = std::min(cpu_count(), MAX_THREADS);
60   int msd = cpus < 8 ? 4 : 7;
61
62   o["Use Debug Log"]               = Option(false, on_logger);
63   o["Use Search Log"]              = Option(false);
64   o["Search Log Filename"]         = Option("SearchLog.txt");
65   o["Book File"]                   = Option("book.bin");
66   o["Best Book Move"]              = Option(false);
67   o["Contempt Factor"]             = Option(0, -50,  50);
68   o["Mobility (Midgame)"]          = Option(100, 0, 200, on_eval);
69   o["Mobility (Endgame)"]          = Option(100, 0, 200, on_eval);
70   o["Pawn Structure (Midgame)"]    = Option(100, 0, 200, on_eval);
71   o["Pawn Structure (Endgame)"]    = Option(100, 0, 200, on_eval);
72   o["Passed Pawns (Midgame)"]      = Option(100, 0, 200, on_eval);
73   o["Passed Pawns (Endgame)"]      = Option(100, 0, 200, on_eval);
74   o["Space"]                       = Option(100, 0, 200, on_eval);
75   o["Aggressiveness"]              = Option(100, 0, 200, on_eval);
76   o["Cowardice"]                   = Option(100, 0, 200, on_eval);
77   o["Min Split Depth"]             = Option(msd, 4, 12, on_threads);
78   o["Max Threads per Split Point"] = Option(5, 4, 8, on_threads);
79   o["Threads"]                     = Option(cpus, 1, MAX_THREADS, on_threads);
80   o["Use Sleeping Threads"]        = Option(true);
81   o["Hash"]                        = Option(32, 1, 8192, on_hash_size);
82   o["Clear Hash"]                  = Option(on_clear_hash);
83   o["Ponder"]                      = Option(true);
84   o["OwnBook"]                     = Option(false);
85   o["MultiPV"]                     = Option(1, 1, 500);
86   o["Skill Level"]                 = Option(20, 0, 20);
87   o["Emergency Move Horizon"]      = Option(40, 0, 50);
88   o["Emergency Base Time"]         = Option(200, 0, 30000);
89   o["Emergency Move Time"]         = Option(70, 0, 5000);
90   o["Minimum Thinking Time"]       = Option(20, 0, 5000);
91   o["Slow Mover"]                  = Option(100, 10, 1000);
92   o["UCI_Chess960"]                = Option(false);
93   o["UCI_AnalyseMode"]             = Option(false, on_eval);
94 }
95
96
97 /// operator<<() is used to print all the options default values in chronological
98 /// insertion order (the idx field) and in the format defined by the UCI protocol.
99
100 std::ostream& operator<<(std::ostream& os, const OptionsMap& om) {
101
102   for (size_t idx = 0; idx < om.size(); idx++)
103       for (OptionsMap::const_iterator it = om.begin(); it != om.end(); ++it)
104           if (it->second.idx == idx)
105           {
106               const Option& o = it->second;
107               os << "\noption name " << it->first << " type " << o.type;
108
109               if (o.type != "button")
110                   os << " default " << o.defaultValue;
111
112               if (o.type == "spin")
113                   os << " min " << o.min << " max " << o.max;
114
115               break;
116           }
117   return os;
118 }
119
120
121 /// Option c'tors and conversion operators
122
123 Option::Option(const char* v, Fn* f) : type("string"), min(0), max(0), idx(Options.size()), on_change(f)
124 { defaultValue = currentValue = v; }
125
126 Option::Option(bool v, Fn* f) : type("check"), min(0), max(0), idx(Options.size()), on_change(f)
127 { defaultValue = currentValue = (v ? "true" : "false"); }
128
129 Option::Option(Fn* f) : type("button"), min(0), max(0), idx(Options.size()), on_change(f)
130 {}
131
132 Option::Option(int v, int minv, int maxv, Fn* f) : type("spin"), min(minv), max(maxv), idx(Options.size()), on_change(f)
133 { std::ostringstream ss; ss << v; defaultValue = currentValue = ss.str(); }
134
135
136 Option::operator int() const {
137   assert(type == "check" || type == "spin");
138   return (type == "spin" ? atoi(currentValue.c_str()) : currentValue == "true");
139 }
140
141 Option::operator std::string() const {
142   assert(type == "string");
143   return currentValue;
144 }
145
146
147 /// operator=() updates currentValue and triggers on_change() action. It's up to
148 /// the GUI to check for option's limits, but we could receive the new value from
149 /// the user by console window, so let's check the bounds anyway.
150
151 Option& Option::operator=(const string& v) {
152
153   assert(!type.empty());
154
155   if (   (type != "button" && v.empty())
156       || (type == "check" && v != "true" && v != "false")
157       || (type == "spin" && (atoi(v.c_str()) < min || atoi(v.c_str()) > max)))
158       return *this;
159
160   if (type != "button")
161       currentValue = v;
162
163   if (on_change)
164       (*on_change)(*this);
165
166   return *this;
167 }
168
169 } // namespace UCI