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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
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.
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.
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/>.
27 #include "ucioption.h"
31 OptionsMap Options; // Global object
35 /// 'On change' actions, triggered by an option's value change
36 void on_logger(const UCIOption& opt) { start_logger(opt); }
37 void on_eval(const UCIOption&) { Eval::init(); }
38 void on_threads(const UCIOption&) { Threads.read_uci_options(); }
39 void on_hash_size(const UCIOption& opt) { TT.set_size(opt); }
40 void on_clear_hash(const UCIOption&) { TT.clear(); }
42 /// Our case insensitive less() function as required by UCI protocol
43 bool ci_less(char c1, char c2) { return tolower(c1) < tolower(c2); }
47 bool CaseInsensitiveLess::operator() (const string& s1, const string& s2) const {
48 return std::lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end(), ci_less);
52 /// UCIOptions::init() initializes the UCI options to their hard coded default
53 /// values and initializes the default value of "Threads" and "Min Split Depth"
54 /// parameters according to the number of CPU cores detected.
56 void UCIOptions::init(OptionsMap& o) {
58 int cpus = std::min(cpu_count(), MAX_THREADS);
59 int msd = cpus < 8 ? 4 : 7;
61 o["Use Debug Log"] = UCIOption(false, on_logger);
62 o["Use Search Log"] = UCIOption(false);
63 o["Search Log Filename"] = UCIOption("SearchLog.txt");
64 o["Book File"] = UCIOption("book.bin");
65 o["Best Book Move"] = UCIOption(false);
66 o["Mobility (Middle Game)"] = UCIOption(100, 0, 200, on_eval);
67 o["Mobility (Endgame)"] = UCIOption(100, 0, 200, on_eval);
68 o["Passed Pawns (Middle Game)"] = UCIOption(100, 0, 200, on_eval);
69 o["Passed Pawns (Endgame)"] = UCIOption(100, 0, 200, on_eval);
70 o["Space"] = UCIOption(100, 0, 200, on_eval);
71 o["Aggressiveness"] = UCIOption(100, 0, 200, on_eval);
72 o["Cowardice"] = UCIOption(100, 0, 200, on_eval);
73 o["Min Split Depth"] = UCIOption(msd, 4, 7, on_threads);
74 o["Max Threads per Split Point"] = UCIOption(5, 4, 8, on_threads);
75 o["Threads"] = UCIOption(cpus, 1, MAX_THREADS, on_threads);
76 o["Use Sleeping Threads"] = UCIOption(true, on_threads);
77 o["Hash"] = UCIOption(32, 4, 8192, on_hash_size);
78 o["Clear Hash"] = UCIOption(on_clear_hash);
79 o["Ponder"] = UCIOption(true);
80 o["OwnBook"] = UCIOption(false);
81 o["MultiPV"] = UCIOption(1, 1, 500);
82 o["Skill Level"] = UCIOption(20, 0, 20);
83 o["Emergency Move Horizon"] = UCIOption(40, 0, 50);
84 o["Emergency Base Time"] = UCIOption(200, 0, 30000);
85 o["Emergency Move Time"] = UCIOption(70, 0, 5000);
86 o["Minimum Thinking Time"] = UCIOption(20, 0, 5000);
87 o["Slow Mover"] = UCIOption(100, 10, 1000);
88 o["UCI_Chess960"] = UCIOption(false);
89 o["UCI_AnalyseMode"] = UCIOption(false, on_eval);
93 /// operator<<() is used to output all the UCI options in chronological insertion
94 /// order (the idx field) and in the format defined by the UCI protocol.
96 std::ostream& operator<<(std::ostream& os, const OptionsMap& om) {
98 for (size_t idx = 0; idx < om.size(); idx++)
99 for (OptionsMap::const_iterator it = om.begin(); it != om.end(); ++it)
100 if (it->second.idx == idx)
102 const UCIOption& o = it->second;
103 os << "\noption name " << it->first << " type " << o.type;
105 if (o.type != "button")
106 os << " default " << o.defaultValue;
108 if (o.type == "spin")
109 os << " min " << o.min << " max " << o.max;
117 /// UCIOption class c'tors
119 UCIOption::UCIOption(const char* v, Fn* f) : type("string"), min(0), max(0), idx(Options.size()), on_change(f)
120 { defaultValue = currentValue = v; }
122 UCIOption::UCIOption(bool v, Fn* f) : type("check"), min(0), max(0), idx(Options.size()), on_change(f)
123 { defaultValue = currentValue = (v ? "true" : "false"); }
125 UCIOption::UCIOption(Fn* f) : type("button"), min(0), max(0), idx(Options.size()), on_change(f)
128 UCIOption::UCIOption(int v, int minv, int maxv, Fn* f) : type("spin"), min(minv), max(maxv), idx(Options.size()), on_change(f)
129 { std::ostringstream ss; ss << v; defaultValue = currentValue = ss.str(); }
132 /// UCIOption::operator=() updates currentValue. Normally it's up to the GUI to
133 /// check for option's limits, but we could receive the new value directly from
134 /// the user by console window, so let's check the bounds anyway.
136 UCIOption& UCIOption::operator=(const string& v) {
138 assert(!type.empty());
140 if ( (type != "button" && v.empty())
141 || (type == "check" && v != "true" && v != "false")
142 || (type == "spin" && (atoi(v.c_str()) < min || atoi(v.c_str()) > max)))
145 if (type != "button")