Skip to content

Commit c527c3a

Browse files
mcostalbavondele
authored andcommitted
Fishtest Tuning Framework
The purpose of the code is to allow developers to easily and flexibly setup SF for a tuning session. Mainly you have just to remove 'const' qualifiers from the variables you want to tune and flag them for tuning, so if you have: int myKing = 10; Score myBonus = S(5, 15); Value myValue[][2] = { { V(100), V(20) }, { V(7), V(78) } }; and at the end of the update you may want to call a post update function: void my_post_update(); If instead of default Option's min-max values, you prefer your custom ones, returned by: std::pair<int, int> my_range(int value) Or you jus want to set the range directly, you can simply add below: TUNE(SetRange(my_range), myKing, SetRange(-200, 200), myBonus, myValue, my_post_update); And all the magic happens :-) At startup all the parameters are printed in a format suitable to be copy-pasted in fishtest. In case the post update function is slow and you have many parameters to tune, you can add: UPDATE_ON_LAST(); And the values update, including post update function call, will be done only once, after the engine receives the last UCI option. The last option is the one defined and created as the last one, so this assumes that the GUI sends the options in the same order in which have been defined. closes #2654 No functional change.
1 parent eb4a124 commit c527c3a

File tree

5 files changed

+345
-1
lines changed

5 files changed

+345
-1
lines changed

src/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ PGOBENCH = ./$(EXE) bench
3838
### Object files
3939
OBJS = benchmark.o bitbase.o bitboard.o endgame.o evaluate.o main.o \
4040
material.o misc.o movegen.o movepick.o pawns.o position.o psqt.o \
41-
search.o thread.o timeman.o tt.o uci.o ucioption.o syzygy/tbprobe.o
41+
search.o thread.o timeman.o tt.o uci.o ucioption.o tune.o syzygy/tbprobe.o
4242

4343
### Establish the operating system name
4444
KERNEL = $(shell uname -s)

src/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ int main(int argc, char* argv[]) {
3838
std::cout << engine_info() << std::endl;
3939

4040
UCI::init(Options);
41+
Tune::init();
4142
PSQT::init();
4243
Bitboards::init();
4344
Position::init();

src/tune.cpp

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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 = 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(Options["m" + name], eg_value(value));
104+
105+
if (Options.count("e" + name))
106+
value = make_score(mg_value(value), 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+
}

src/tune.h

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
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-2017 Marco Costalba, Joona Kiiski, Tord Romstad
5+
Copyright (C) 2015-2018 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+
#ifndef TUNE_H_INCLUDED
22+
#define TUNE_H_INCLUDED
23+
24+
#include <memory>
25+
#include <string>
26+
#include <type_traits>
27+
#include <vector>
28+
29+
typedef std::pair<int, int> Range; // Option's min-max values
30+
typedef Range (RangeFun) (int);
31+
32+
// Default Range function, to calculate Option's min-max values
33+
inline Range default_range(int v) {
34+
return v > 0 ? Range(0, 2 * v) : Range(2 * v, 0);
35+
}
36+
37+
struct SetRange {
38+
explicit SetRange(RangeFun f) : fun(f) {}
39+
SetRange(int min, int max) : fun(nullptr), range(min, max) {}
40+
Range operator()(int v) const { return fun ? fun(v) : range; }
41+
42+
RangeFun* fun;
43+
Range range;
44+
};
45+
46+
#define SetDefaultRange SetRange(default_range)
47+
48+
49+
/// BoolConditions struct is used to tune boolean conditions in the
50+
/// code by toggling them on/off according to a probability that
51+
/// depends on the value of a tuned integer parameter: for high
52+
/// values of the parameter condition is always disabled, for low
53+
/// values is always enabled, otherwise it is enabled with a given
54+
/// probability that depnends on the parameter under tuning.
55+
56+
struct BoolConditions {
57+
void init(size_t size) { values.resize(size, defaultValue), binary.resize(size, 0); }
58+
void set();
59+
60+
std::vector<int> binary, values;
61+
int defaultValue = 465, variance = 40, threshold = 500;
62+
SetRange range = SetRange(0, 1000);
63+
};
64+
65+
extern BoolConditions Conditions;
66+
67+
inline void set_conditions() { Conditions.set(); }
68+
69+
70+
/// Tune class implements the 'magic' code that makes the setup of a fishtest
71+
/// tuning session as easy as it can be. Mainly you have just to remove const
72+
/// qualifiers from the variables you want to tune and flag them for tuning, so
73+
/// if you have:
74+
///
75+
/// const Score myScore = S(10, 15);
76+
/// const Value myValue[][2] = { { V(100), V(20) }, { V(7), V(78) } };
77+
///
78+
/// If you have a my_post_update() function to run after values have been updated,
79+
/// and a my_range() function to set custom Option's min-max values, then you just
80+
/// remove the 'const' qualifiers and write somewhere below in the file:
81+
///
82+
/// TUNE(SetRange(my_range), myScore, myValue, my_post_update);
83+
///
84+
/// You can also set the range directly, and restore the default at the end
85+
///
86+
/// TUNE(SetRange(-100, 100), myScore, SetDefaultRange);
87+
///
88+
/// In case update function is slow and you have many parameters, you can add:
89+
///
90+
/// UPDATE_ON_LAST();
91+
///
92+
/// And the values update, including post update function call, will be done only
93+
/// once, after the engine receives the last UCI option, that is the one defined
94+
/// and created as the last one, so the GUI should send the options in the same
95+
/// order in which have been defined.
96+
97+
class Tune {
98+
99+
typedef void (PostUpdate) (); // Post-update function
100+
101+
Tune() { read_results(); }
102+
Tune(const Tune&) = delete;
103+
void operator=(const Tune&) = delete;
104+
void read_results();
105+
106+
static Tune& instance() { static Tune t; return t; } // Singleton
107+
108+
// Use polymorphism to accomodate Entry of different types in the same vector
109+
struct EntryBase {
110+
virtual ~EntryBase() = default;
111+
virtual void init_option() = 0;
112+
virtual void read_option() = 0;
113+
};
114+
115+
template<typename T>
116+
struct Entry : public EntryBase {
117+
118+
static_assert(!std::is_const<T>::value, "Parameter cannot be const!");
119+
120+
static_assert( std::is_same<T, int>::value
121+
|| std::is_same<T, Value>::value
122+
|| std::is_same<T, Score>::value
123+
|| std::is_same<T, PostUpdate>::value, "Parameter type not supported!");
124+
125+
Entry(const std::string& n, T& v, const SetRange& r) : name(n), value(v), range(r) {}
126+
void operator=(const Entry&) = delete; // Because 'value' is a reference
127+
void init_option() override;
128+
void read_option() override;
129+
130+
std::string name;
131+
T& value;
132+
SetRange range;
133+
};
134+
135+
// Our facilty to fill the container, each Entry corresponds to a parameter to tune.
136+
// We use variadic templates to deal with an unspecified number of entries, each one
137+
// of a possible different type.
138+
static std::string next(std::string& names, bool pop = true);
139+
140+
int add(const SetRange&, std::string&&) { return 0; }
141+
142+
template<typename T, typename... Args>
143+
int add(const SetRange& range, std::string&& names, T& value, Args&&... args) {
144+
list.push_back(std::unique_ptr<EntryBase>(new Entry<T>(next(names), value, range)));
145+
return add(range, std::move(names), args...);
146+
}
147+
148+
// Template specialization for arrays: recursively handle multi-dimensional arrays
149+
template<typename T, size_t N, typename... Args>
150+
int add(const SetRange& range, std::string&& names, T (&value)[N], Args&&... args) {
151+
for (size_t i = 0; i < N; i++)
152+
add(range, next(names, i == N - 1) + "[" + std::to_string(i) + "]", value[i]);
153+
return add(range, std::move(names), args...);
154+
}
155+
156+
// Template specialization for SetRange
157+
template<typename... Args>
158+
int add(const SetRange&, std::string&& names, SetRange& value, Args&&... args) {
159+
return add(value, (next(names), std::move(names)), args...);
160+
}
161+
162+
// Template specialization for BoolConditions
163+
template<typename... Args>
164+
int add(const SetRange& range, std::string&& names, BoolConditions& cond, Args&&... args) {
165+
for (size_t size = cond.values.size(), i = 0; i < size; i++)
166+
add(cond.range, next(names, i == size - 1) + "_" + std::to_string(i), cond.values[i]);
167+
return add(range, std::move(names), args...);
168+
}
169+
170+
std::vector<std::unique_ptr<EntryBase>> list;
171+
172+
public:
173+
template<typename... Args>
174+
static int add(const std::string& names, Args&&... args) {
175+
return instance().add(SetDefaultRange, names.substr(1, names.size() - 2), args...); // Remove trailing parenthesis
176+
}
177+
static void init() { for (auto& e : instance().list) e->init_option(); read_options(); } // Deferred, due to UCI::Options access
178+
static void read_options() { for (auto& e : instance().list) e->read_option(); }
179+
static bool update_on_last;
180+
};
181+
182+
// Some macro magic :-) we define a dummy int variable that compiler initializes calling Tune::add()
183+
#define STRINGIFY(x) #x
184+
#define UNIQUE2(x, y) x ## y
185+
#define UNIQUE(x, y) UNIQUE2(x, y) // Two indirection levels to expand __LINE__
186+
#define TUNE(...) int UNIQUE(p, __LINE__) = Tune::add(STRINGIFY((__VA_ARGS__)), __VA_ARGS__)
187+
188+
#define UPDATE_ON_LAST() bool UNIQUE(p, __LINE__) = Tune::update_on_last = true
189+
190+
// Some macro to tune toggling of boolean conditions
191+
#define CONDITION(x) (Conditions.binary[__COUNTER__] || (x))
192+
#define TUNE_CONDITIONS() int UNIQUE(c, __LINE__) = (Conditions.init(__COUNTER__), 0); \
193+
TUNE(Conditions, set_conditions)
194+
195+
#endif // #ifndef TUNE_H_INCLUDED

src/types.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,3 +465,5 @@ constexpr bool is_ok(Move m) {
465465
}
466466

467467
#endif // #ifndef TYPES_H_INCLUDED
468+
469+
#include "tune.h" // Global visibility to tuning setup

0 commit comments

Comments
 (0)