BenBot 1.7.5
A chess engine
Loading...
Searching...
No Matches
Weights.hpp
Go to the documentation of this file.
1/*
2 * ======================================================================================
3 *
4 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓███████▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░▒▓████████▓▒░
5 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
6 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
7 * ░▒▓███████▓▒░░▒▓██████▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
8 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
9 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
10 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░ ░▒▓█▓▒░
11 *
12 * ======================================================================================
13 */
14
19
20#pragma once
21
22#include <array>
23#include <cassert>
24#include <cstddef>
25#include <cstdint>
29#include <span>
30#include <string>
31#include <string_view>
32#include <utility>
33
34namespace ben_bot::eval {
35
36using std::size_t;
38
82
86enum class Method : std::uint8_t {
88};
89
93struct TermInfo final {
95 std::string_view name;
96
99
101 size_t count { 1uz };
102
106 bool tunable { true };
107
110};
111
117inline constexpr std::array TERMS {
118 TermInfo { .name = "Material", .term = Term::Material, .count = 5uz },
119 TermInfo { .name = "Pst", .term = Term::Pst, .count = 6uz * chess::board::NUM_SQUARES },
120 TermInfo { .name = "PassedPawn", .term = Term::PassedPawn, .count = 6uz },
121 TermInfo { .name = "PasserProtectedByPasser", .term = Term::PasserProtectedByPasser },
122 TermInfo { .name = "PasserProtectedByPawn", .term = Term::PasserProtectedByPawn },
123 TermInfo { .name = "IsolatedPawn", .term = Term::IsolatedPawn },
124 TermInfo { .name = "DoubledPawn", .term = Term::DoubledPawn },
125 TermInfo { .name = "BackwardPawn", .term = Term::BackwardPawn },
126 TermInfo { .name = "PawnChain", .term = Term::PawnChain },
127 TermInfo { .name = "RookBehindPasser", .term = Term::RookBehindPasser },
128 TermInfo { .name = "KingEscortsPasser", .term = Term::KingEscortsPasser },
129 TermInfo { .name = "EnemyKingBlocksPasser", .term = Term::EnemyKingBlocksPasser },
130 TermInfo { .name = "RookHalfOpenFile", .term = Term::RookHalfOpenFile },
131 TermInfo { .name = "RookOpenFile", .term = Term::RookOpenFile },
132 TermInfo { .name = "ConnectedRooksOpenFile", .term = Term::ConnectedRooksOpenFile },
133 TermInfo { .name = "KingHalfOpenFile", .term = Term::KingHalfOpenFile },
134 TermInfo { .name = "KingOpenLine", .term = Term::KingOpenLine },
135 TermInfo { .name = "KingStranded", .term = Term::KingStranded },
136 TermInfo { .name = "KnightTropism", .term = Term::KnightTropism },
137 TermInfo { .name = "QueenTropism", .term = Term::QueenTropism },
138 TermInfo { .name = "KnightMobility", .term = Term::KnightMobility, .count = 9uz },
139 TermInfo { .name = "BishopMobility", .term = Term::BishopMobility, .count = 14uz },
140 TermInfo { .name = "RookMobility", .term = Term::RookMobility, .count = 15uz },
141 TermInfo { .name = "QueenMobility", .term = Term::QueenMobility, .count = 28uz },
142 TermInfo { .name = "KingAreaControl", .term = Term::KingAreaControl },
143 TermInfo { .name = "CenterControl", .term = Term::CenterControl },
144 TermInfo { .name = "Space", .term = Term::Space },
145 TermInfo { .name = "PawnThreat", .term = Term::PawnThreat },
146 TermInfo { .name = "MinorOnRook", .term = Term::MinorOnRook },
147 TermInfo { .name = "MinorOnQueen", .term = Term::MinorOnQueen },
148 TermInfo { .name = "RookOnQueen", .term = Term::RookOnQueen },
149 TermInfo { .name = "Hanging", .term = Term::Hanging },
150 TermInfo { .name = "PushThreat", .term = Term::PushThreat },
151 TermInfo { .name = "LastPiece", .term = Term::LastPiece, .tunable = false },
152 TermInfo { .name = "MopUpEdge", .term = Term::MopUpEdge, .tunable = false },
153 TermInfo { .name = "MopUpKingDistance", .term = Term::MopUpKingDistance, .tunable = false }
154};
155
159inline constexpr auto NUM_TERMS = TERMS.size();
160
161namespace detail {
162
163 [[nodiscard]] consteval auto compute_term_offsets() -> std::array<size_t, NUM_TERMS + 1uz>
164 {
165 std::array<size_t, NUM_TERMS + 1uz> offsets { };
166
167 for (auto i = 0uz; i < NUM_TERMS; ++i) {
168 // the registry must list the terms in the same order as the enum
169 if (std::to_underlying(TERMS.at(i).term) != i)
170 throw "TERMS must be in the same order as the Term enum";
171
172 offsets.at(i + 1uz) = offsets.at(i) + TERMS.at(i).count;
173 }
174
175 return offsets;
176 }
177
178 inline constexpr auto TERM_OFFSETS = compute_term_offsets();
179
180} // namespace detail
181
185inline constexpr auto NUM_WEIGHTS = detail::TERM_OFFSETS.back();
186
190[[nodiscard, gnu::const]] constexpr auto offset_of(const Term term) noexcept -> size_t
191{
192 return detail::TERM_OFFSETS[std::to_underlying(term)];
193}
194
198[[nodiscard, gnu::const]] constexpr auto count_of(const Term term) noexcept -> size_t
199{
200 return TERMS[std::to_underlying(term)].count;
201}
202
209struct Weights final {
211 std::array<TaperedScore, NUM_WEIGHTS> values { };
212
214 [[nodiscard]] constexpr auto get(const Term term, const size_t index = 0uz) const noexcept -> TaperedScore
215 {
216 assert(index < count_of(term));
217
218 return values[offset_of(term) + index];
219 }
220
222 [[nodiscard]] constexpr auto get(const Term term, const size_t index = 0uz) noexcept -> TaperedScore&
223 {
224 assert(index < count_of(term));
225
226 return values[offset_of(term) + index];
227 }
228
230 [[nodiscard]] constexpr auto of_term(const Term term) const noexcept -> std::span<const TaperedScore>
231 {
232 return std::span { values }.subspan(offset_of(term), count_of(term));
233 }
234
236 [[nodiscard]] constexpr auto material(const PieceType type) const noexcept -> TaperedScore
237 {
238 return get(Term::Material, static_cast<size_t>(type));
239 }
240
244 [[nodiscard]] constexpr auto pst(const PieceType type) const noexcept
245 -> std::span<const TaperedScore, chess::board::NUM_SQUARES>
246 {
247 return std::span { values }
248 .subspan(offset_of(Term::Pst) + (static_cast<size_t>(type) * chess::board::NUM_SQUARES))
250 }
251
255 [[nodiscard]] constexpr auto pst(const PieceType type) noexcept
256 -> std::span<TaperedScore, chess::board::NUM_SQUARES>
257 {
258 return std::span { values }
259 .subspan(offset_of(Term::Pst) + (static_cast<size_t>(type) * chess::board::NUM_SQUARES))
261 }
262
266 [[nodiscard]] auto to_cpp() const -> std::string;
267
269 [[nodiscard]] constexpr auto operator==(const Weights&) const noexcept -> bool = default;
270};
271
272} // namespace ben_bot::eval
constexpr BitboardIndex NUM_SQUARES
constexpr auto NUM_TERMS
Definition Weights.hpp:159
constexpr std::array TERMS
Definition Weights.hpp:117
constexpr auto NUM_WEIGHTS
Definition Weights.hpp:185
constexpr auto count_of(const Term term) noexcept -> size_t
Definition Weights.hpp:198
constexpr auto offset_of(const Term term) noexcept -> size_t
Definition Weights.hpp:190
@ BackwardPawn
Bonus (penalty) for each backward pawn.
Definition Weights.hpp:52
@ KnightTropism
Bonus (penalty) per square that an enemy knight is closer than 6 to the king.
Definition Weights.hpp:63
@ KingStranded
Bonus (penalty) for the king on the d/e file with no castling rights.
Definition Weights.hpp:62
@ KingEscortsPasser
Bonus per square that our king is closer than 7 to our passer.
Definition Weights.hpp:55
@ MopUpEdge
Mop-up bonus per square that the enemy king is from the center.
Definition Weights.hpp:79
@ RookHalfOpenFile
Bonus for each rook on a file with an enemy pawn but none of ours.
Definition Weights.hpp:57
@ RookOnQueen
Bonus for each enemy queen attacked by one of our rooks.
Definition Weights.hpp:75
@ Pst
Piece square tables: [piece type][square], from White's point of view.
Definition Weights.hpp:46
@ MinorOnQueen
Bonus for each enemy queen attacked by one of our knights or bishops.
Definition Weights.hpp:74
@ IsolatedPawn
Bonus (penalty) for each isolated pawn.
Definition Weights.hpp:50
@ MinorOnRook
Bonus for each enemy rook attacked by one of our knights or bishops.
Definition Weights.hpp:73
@ PawnThreat
Bonus for each enemy piece attacked by one of our pawns.
Definition Weights.hpp:72
@ KingOpenLine
Bonus (penalty) for the king on an open file or a pawnless diagonal.
Definition Weights.hpp:61
@ BishopMobility
Bishop mobility, indexed by the number of safe squares.
Definition Weights.hpp:66
@ QueenMobility
Queen mobility, indexed by the number of safe squares.
Definition Weights.hpp:68
@ PassedPawn
Passed pawn bonus, indexed by squares from promoting minus 1.
Definition Weights.hpp:47
@ DoubledPawn
Bonus (penalty) for each file with more than one of our pawns.
Definition Weights.hpp:51
@ PawnChain
Bonus for each pawn defended by a friendly pawn (counted per defender).
Definition Weights.hpp:53
@ RookMobility
Rook mobility, indexed by the number of safe squares.
Definition Weights.hpp:67
@ QueenTropism
Bonus (penalty) per square that an enemy queen is closer than 7 to the king.
Definition Weights.hpp:64
@ PasserProtectedByPasser
Bonus for each passed pawn defending a passed pawn.
Definition Weights.hpp:48
@ PushThreat
Bonus for each enemy piece that one of our pawns could attack with a safe push.
Definition Weights.hpp:77
@ PasserProtectedByPawn
Bonus for each non-passed pawn defending a passed pawn.
Definition Weights.hpp:49
@ KingHalfOpenFile
Bonus (penalty) for the king on a file with an enemy pawn but none of ours.
Definition Weights.hpp:60
@ Hanging
Bonus for each hanging enemy piece or pawn.
Definition Weights.hpp:76
@ EnemyKingBlocksPasser
Bonus (penalty) for the enemy king on our passer's promotion square.
Definition Weights.hpp:56
@ KnightMobility
Knight mobility, indexed by the number of safe squares.
Definition Weights.hpp:65
@ CenterControl
Bonus per center square controlled.
Definition Weights.hpp:70
@ KingAreaControl
Bonus per square around each king that we control more of than they do.
Definition Weights.hpp:69
@ RookOpenFile
Bonus for each rook on an open file.
Definition Weights.hpp:58
@ Space
Bonus per square behind our pawns that we control more of than they do.
Definition Weights.hpp:71
@ MopUpKingDistance
Mop-up bonus per square that the kings are closer than 14.
Definition Weights.hpp:80
@ Material
Pawn, knight, bishop, rook & queen values.
Definition Weights.hpp:45
@ ConnectedRooksOpenFile
Bonus for each open file with two of our rooks on it.
Definition Weights.hpp:59
@ LastPiece
Bonus for having a non-pawn piece left when the opponent has none.
Definition Weights.hpp:78
@ RookBehindPasser
Bonus for our rook behind our passer (penalty for their rook behind it).
Definition Weights.hpp:54
@ Linear
The eval is the sum of coefficient times weight.
Definition Weights.hpp:87
constexpr auto TERM_OFFSETS
Definition Weights.hpp:178
consteval auto compute_term_offsets() -> std::array< size_t, NUM_TERMS+1uz >
Definition Weights.hpp:163
chess::pieces::Type PieceType
Definition Material.hpp:31
std::string_view name
Definition Weights.hpp:95
constexpr auto pst(const PieceType type) const noexcept -> std::span< const TaperedScore, chess::board::NUM_SQUARES >
Definition Weights.hpp:244
constexpr auto get(const Term term, const size_t index=0uz) noexcept -> TaperedScore &
Definition Weights.hpp:222
constexpr auto get(const Term term, const size_t index=0uz) const noexcept -> TaperedScore
Definition Weights.hpp:214
constexpr auto of_term(const Term term) const noexcept -> std::span< const TaperedScore >
Definition Weights.hpp:230
constexpr auto pst(const PieceType type) noexcept -> std::span< TaperedScore, chess::board::NUM_SQUARES >
Definition Weights.hpp:255
std::array< TaperedScore, NUM_WEIGHTS > values
Definition Weights.hpp:211
constexpr auto material(const PieceType type) const noexcept -> TaperedScore
Definition Weights.hpp:236
auto to_cpp() const -> std::string