]> git.sesse.net Git - stockfish/blob - src/tune.cpp
Update Top CPU Contributors
[stockfish] / src / tune.cpp
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
4
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.
9
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.
14
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/>.
17 */
18
19 #include <algorithm>
20 #include <iostream>
21 #include <sstream>
22
23 #include "types.h"
24 #include "misc.h"
25 #include "uci.h"
26
27 using std::string;
28
29 bool Tune::update_on_last;
30 const UCI::Option* LastOption = nullptr;
31 BoolConditions Conditions;
32 static std::map<std::string, int> TuneResults;
33
34 string Tune::next(string& names, bool pop) {
35
36   string name;
37
38   do {
39       string token = names.substr(0, names.find(','));
40
41       if (pop)
42           names.erase(0, token.size() + 1);
43
44       std::stringstream ws(token);
45       name += (ws >> token, token); // Remove trailing whitespace
46
47   } while (  std::count(name.begin(), name.end(), '(')
48            - std::count(name.begin(), name.end(), ')'));
49
50   return name;
51 }
52
53 static void on_tune(const UCI::Option& o) {
54
55   if (!Tune::update_on_last || LastOption == &o)
56       Tune::read_options();
57 }
58
59 static void make_option(const string& n, int v, const SetRange& r) {
60
61   // Do not generate option when there is nothing to tune (ie. min = max)
62   if (r(v).first == r(v).second)
63       return;
64
65   if (TuneResults.count(n))
66       v = TuneResults[n];
67
68   Options[n] << UCI::Option(v, r(v).first, r(v).second, on_tune);
69   LastOption = &Options[n];
70
71   // Print formatted parameters, ready to be copy-pasted in Fishtest
72   std::cout << n << ","
73             << v << ","
74             << r(v).first << "," << r(v).second << ","
75             << (r(v).second - r(v).first) / 20.0 << ","
76             << "0.0020"
77             << std::endl;
78 }
79
80 template<> void Tune::Entry<int>::init_option() { make_option(name, value, range); }
81
82 template<> void Tune::Entry<int>::read_option() {
83   if (Options.count(name))
84       value = int(Options[name]);
85 }
86
87 template<> void Tune::Entry<Value>::init_option() { make_option(name, value, range); }
88
89 template<> void Tune::Entry<Value>::read_option() {
90   if (Options.count(name))
91       value = Value(int(Options[name]));
92 }
93
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);
97 }
98
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));
102
103   if (Options.count("e" + name))
104       value = make_score(mg_value(value), int(Options["e" + name]));
105 }
106
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(); }
110
111
112 // Set binary conditions according to a probability that depends
113 // on the corresponding parameter value.
114
115 void BoolConditions::set() {
116
117   static PRNG rng(now());
118   static bool startup = true; // To workaround fishtest bench
119
120   for (size_t i = 0; i < binary.size(); i++)
121       binary[i] = !startup && (values[i] + int(rng.rand<unsigned>() % variance) > threshold);
122
123   startup = false;
124
125   for (size_t i = 0; i < binary.size(); i++)
126       sync_cout << binary[i] << sync_endl;
127 }
128
129
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
133 // values with:
134 //
135 // cat results.txt | sed 's/^param: \([^,]*\), best: \([^,]*\).*/  TuneResults["\1"] = int(round(\2));/'
136 //
137 // Then paste the output below, as the function body
138
139 #include <cmath>
140
141 void Tune::read_results() {
142
143   /* ...insert your values here... */
144 }