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