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