]> git.sesse.net Git - stockfish/blob - src/tune.cpp
Don't save excluded move eval in TT
[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 namespace Stockfish {
30
31 bool Tune::update_on_last;
32 const UCI::Option* LastOption = nullptr;
33 static std::map<std::string, int> TuneResults;
34
35 string Tune::next(string& names, bool pop) {
36
37   string name;
38
39   do {
40       string token = names.substr(0, names.find(','));
41
42       if (pop)
43           names.erase(0, token.size() + 1);
44
45       std::stringstream ws(token);
46       name += (ws >> token, token); // Remove trailing whitespace
47
48   } while (  std::count(name.begin(), name.end(), '(')
49            - std::count(name.begin(), name.end(), ')'));
50
51   return name;
52 }
53
54 static void on_tune(const UCI::Option& o) {
55
56   if (!Tune::update_on_last || LastOption == &o)
57       Tune::read_options();
58 }
59
60 static void make_option(const string& n, int v, const SetRange& r) {
61
62   // Do not generate option when there is nothing to tune (ie. min = max)
63   if (r(v).first == r(v).second)
64       return;
65
66   if (TuneResults.count(n))
67       v = TuneResults[n];
68
69   Options[n] << UCI::Option(v, r(v).first, r(v).second, on_tune);
70   LastOption = &Options[n];
71
72   // Print formatted parameters, ready to be copy-pasted in Fishtest
73   std::cout << n << ","
74             << v << ","
75             << r(v).first << "," << r(v).second << ","
76             << (r(v).second - r(v).first) / 20.0 << ","
77             << "0.0020"
78             << std::endl;
79 }
80
81 template<> void Tune::Entry<int>::init_option() { make_option(name, value, range); }
82
83 template<> void Tune::Entry<int>::read_option() {
84   if (Options.count(name))
85       value = int(Options[name]);
86 }
87
88 template<> void Tune::Entry<Value>::init_option() { make_option(name, value, range); }
89
90 template<> void Tune::Entry<Value>::read_option() {
91   if (Options.count(name))
92       value = Value(int(Options[name]));
93 }
94
95 template<> void Tune::Entry<Score>::init_option() {
96   make_option("m" + name, mg_value(value), range);
97   make_option("e" + name, eg_value(value), range);
98 }
99
100 template<> void Tune::Entry<Score>::read_option() {
101   if (Options.count("m" + name))
102       value = make_score(int(Options["m" + name]), eg_value(value));
103
104   if (Options.count("e" + name))
105       value = make_score(mg_value(value), int(Options["e" + name]));
106 }
107
108 // Instead of a variable here we have a PostUpdate function: just call it
109 template<> void Tune::Entry<Tune::PostUpdate>::init_option() {}
110 template<> void Tune::Entry<Tune::PostUpdate>::read_option() { value(); }
111
112 } // namespace Stockfish
113
114
115 // Init options with tuning session results instead of default values. Useful to
116 // get correct bench signature after a tuning session or to test tuned values.
117 // Just copy fishtest tuning results in a result.txt file and extract the
118 // values with:
119 //
120 // cat results.txt | sed 's/^param: \([^,]*\), best: \([^,]*\).*/  TuneResults["\1"] = int(round(\2));/'
121 //
122 // Then paste the output below, as the function body
123
124 #include <cmath>
125
126 namespace Stockfish {
127
128 void Tune::read_results() {
129
130   /* ...insert your values here... */
131 }
132
133 } // namespace Stockfish