2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2020 The Stockfish developers (see AUTHORS file)
5 Stockfish is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 Stockfish is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 bool Tune::update_on_last;
30 const UCI::Option* LastOption = nullptr;
31 BoolConditions Conditions;
32 static std::map<std::string, int> TuneResults;
34 string Tune::next(string& names, bool pop) {
39 string token = names.substr(0, names.find(','));
42 names.erase(0, token.size() + 1);
44 std::stringstream ws(token);
45 name += (ws >> token, token); // Remove trailing whitespace
47 } while ( std::count(name.begin(), name.end(), '(')
48 - std::count(name.begin(), name.end(), ')'));
53 static void on_tune(const UCI::Option& o) {
55 if (!Tune::update_on_last || LastOption == &o)
59 static void make_option(const string& n, int v, const SetRange& r) {
61 // Do not generate option when there is nothing to tune (ie. min = max)
62 if (r(v).first == r(v).second)
65 if (TuneResults.count(n))
68 Options[n] << UCI::Option(v, r(v).first, r(v).second, on_tune);
69 LastOption = &Options[n];
71 // Print formatted parameters, ready to be copy-pasted in Fishtest
74 << r(v).first << "," << r(v).second << ","
75 << (r(v).second - r(v).first) / 20.0 << ","
80 template<> void Tune::Entry<int>::init_option() { make_option(name, value, range); }
82 template<> void Tune::Entry<int>::read_option() {
83 if (Options.count(name))
84 value = int(Options[name]);
87 template<> void Tune::Entry<Value>::init_option() { make_option(name, value, range); }
89 template<> void Tune::Entry<Value>::read_option() {
90 if (Options.count(name))
91 value = Value(int(Options[name]));
94 template<> void Tune::Entry<Score>::init_option() {
95 make_option("m" + name, mg_value(value), range);
96 make_option("e" + name, eg_value(value), range);
99 template<> void Tune::Entry<Score>::read_option() {
100 if (Options.count("m" + name))
101 value = make_score(int(Options["m" + name]), eg_value(value));
103 if (Options.count("e" + name))
104 value = make_score(mg_value(value), int(Options["e" + name]));
107 // Instead of a variable here we have a PostUpdate function: just call it
108 template<> void Tune::Entry<Tune::PostUpdate>::init_option() {}
109 template<> void Tune::Entry<Tune::PostUpdate>::read_option() { value(); }
112 // Set binary conditions according to a probability that depends
113 // on the corresponding parameter value.
115 void BoolConditions::set() {
117 static PRNG rng(now());
118 static bool startup = true; // To workaround fishtest bench
120 for (size_t i = 0; i < binary.size(); i++)
121 binary[i] = !startup && (values[i] + int(rng.rand<unsigned>() % variance) > threshold);
125 for (size_t i = 0; i < binary.size(); i++)
126 sync_cout << binary[i] << sync_endl;
130 // Init options with tuning session results instead of default values. Useful to
131 // get correct bench signature after a tuning session or to test tuned values.
132 // Just copy fishtest tuning results in a result.txt file and extract the
135 // cat results.txt | sed 's/^param: \([^,]*\), best: \([^,]*\).*/ TuneResults["\1"] = int(round(\2));/'
137 // Then paste the output below, as the function body
141 void Tune::read_results() {
143 /* ...insert your values here... */