]> git.sesse.net Git - stockfish/blob - src/tune.cpp
Remove all references to Score type
[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 <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 // Instead of a variable here we have a PostUpdate function: just call it
96 template<> void Tune::Entry<Tune::PostUpdate>::init_option() {}
97 template<> void Tune::Entry<Tune::PostUpdate>::read_option() { value(); }
98
99 } // namespace Stockfish
100
101
102 // Init options with tuning session results instead of default values. Useful to
103 // get correct bench signature after a tuning session or to test tuned values.
104 // Just copy fishtest tuning results in a result.txt file and extract the
105 // values with:
106 //
107 // cat results.txt | sed 's/^param: \([^,]*\), best: \([^,]*\).*/  TuneResults["\1"] = int(round(\2));/'
108 //
109 // Then paste the output below, as the function body
110
111 #include <cmath>
112
113 namespace Stockfish {
114
115 void Tune::read_results() {
116
117   /* ...insert your values here... */
118 }
119
120 } // namespace Stockfish