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