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-2015 Marco Costalba, Joona Kiiski, Tord Romstad
5 Copyright (C) 2015-2020 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
7 Stockfish is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Stockfish is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
31 bool Tune::update_on_last;
32 const UCI::Option* LastOption = nullptr;
33 BoolConditions Conditions;
34 static std::map<std::string, int> TuneResults;
36 string Tune::next(string& names, bool pop) {
41 string token = names.substr(0, names.find(','));
44 names.erase(0, token.size() + 1);
46 std::stringstream ws(token);
47 name += (ws >> token, token); // Remove trailing whitespace
49 } while ( std::count(name.begin(), name.end(), '(')
50 - std::count(name.begin(), name.end(), ')'));
55 static void on_tune(const UCI::Option& o) {
57 if (!Tune::update_on_last || LastOption == &o)
61 static void make_option(const string& n, int v, const SetRange& r) {
63 // Do not generate option when there is nothing to tune (ie. min = max)
64 if (r(v).first == r(v).second)
67 if (TuneResults.count(n))
70 Options[n] << UCI::Option(v, r(v).first, r(v).second, on_tune);
71 LastOption = &Options[n];
73 // Print formatted parameters, ready to be copy-pasted in fishtest
76 << r(v).first << "," << r(v).second << ","
77 << (r(v).second - r(v).first) / 20.0 << ","
82 template<> void Tune::Entry<int>::init_option() { make_option(name, value, range); }
84 template<> void Tune::Entry<int>::read_option() {
85 if (Options.count(name))
86 value = int(Options[name]);
89 template<> void Tune::Entry<Value>::init_option() { make_option(name, value, range); }
91 template<> void Tune::Entry<Value>::read_option() {
92 if (Options.count(name))
93 value = Value(int(Options[name]));
96 template<> void Tune::Entry<Score>::init_option() {
97 make_option("m" + name, mg_value(value), range);
98 make_option("e" + name, eg_value(value), range);
101 template<> void Tune::Entry<Score>::read_option() {
102 if (Options.count("m" + name))
103 value = make_score(int(Options["m" + name]), eg_value(value));
105 if (Options.count("e" + name))
106 value = make_score(mg_value(value), int(Options["e" + name]));
109 // Instead of a variable here we have a PostUpdate function: just call it
110 template<> void Tune::Entry<Tune::PostUpdate>::init_option() {}
111 template<> void Tune::Entry<Tune::PostUpdate>::read_option() { value(); }
114 // Set binary conditions according to a probability that depends
115 // on the corresponding parameter value.
117 void BoolConditions::set() {
119 static PRNG rng(now());
120 static bool startup = true; // To workaround fishtest bench
122 for (size_t i = 0; i < binary.size(); i++)
123 binary[i] = !startup && (values[i] + int(rng.rand<unsigned>() % variance) > threshold);
127 for (size_t i = 0; i < binary.size(); i++)
128 sync_cout << binary[i] << sync_endl;
132 // Init options with tuning session results instead of default values. Useful to
133 // get correct bench signature after a tuning session or to test tuned values.
134 // Just copy fishtest tuning results in a result.txt file and extract the
137 // cat results.txt | sed 's/^param: \([^,]*\), best: \([^,]*\).*/ TuneResults["\1"] = int(round(\2));/'
139 // Then paste the output below, as the function body
143 void Tune::read_results() {
145 /* ...insert your values here... */