BenBot 1.7.5
A chess engine
Loading...
Searching...
No Matches
Trace.hpp
Go to the documentation of this file.
1/*
2 * ======================================================================================
3 *
4 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓███████▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░▒▓████████▓▒░
5 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
6 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
7 * ░▒▓███████▓▒░░▒▓██████▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
8 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
9 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
10 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░ ░▒▓█▓▒░
11 *
12 * ======================================================================================
13 */
14
20
21#pragma once
22
23#include <array>
24#include <cassert>
25#include <cstddef>
26#include <cstdint>
32#include <utility>
33
34namespace chess::game {
35struct Position;
36} // namespace chess::game
37
38namespace ben_bot::eval {
39
40using chess::game::Position;
42
48namespace phase_weights {
49
52
54 inline constexpr std::int32_t KNIGHT { 1 };
55
57 inline constexpr std::int32_t BISHOP { 1 };
58
60 inline constexpr std::int32_t ROOK { 2 };
61
63 inline constexpr std::int32_t QUEEN { 4 };
64
66
67} // namespace phase_weights
68
77inline constexpr std::int32_t MAX_PHASE
79
80static_assert(MAX_PHASE == 24);
81
85struct NullTracer final {
87 static constexpr bool ENABLED = false;
88
90 static constexpr void add(
91 [[maybe_unused]] const Term term, [[maybe_unused]] const Color side,
92 [[maybe_unused]] const int count, [[maybe_unused]] const size_t index = 0uz) noexcept
93 {
94 }
95
97 static constexpr void add_base(
98 [[maybe_unused]] const Color side, [[maybe_unused]] const TaperedScore& part) noexcept
99 {
100 }
101};
102
112struct CoeffTracer final {
114 static constexpr bool ENABLED = true;
115
120 std::array<std::array<std::int16_t, 2uz>, NUM_WEIGHTS> coeffs { };
121
127
129 std::int32_t phase { MAX_PHASE };
130
132 Color sideToMove { Color::White };
133
135 constexpr void add(
136 const Term term, const Color side, const int count, const size_t index = 0uz) noexcept
137 {
138 assert(index < count_of(term));
139
140 auto& coeff = coeffs[offset_of(term) + index][std::to_underlying(side)];
141
142 coeff = static_cast<std::int16_t>(coeff + count);
143 }
144
146 constexpr void add_base(const Color side, const TaperedScore& part) noexcept
147 {
148 if (side == Color::White)
149 base += part;
150 else
151 base -= part;
152 }
153
157 [[nodiscard]] constexpr auto coefficient(const size_t index) const noexcept -> int
158 {
159 const auto& coeff = coeffs[index];
160
161 return coeff[std::to_underlying(Color::White)] - coeff[std::to_underlying(Color::Black)];
162 }
163
167 [[nodiscard]] constexpr auto white_score(const Weights& weights) const noexcept -> TaperedScore
168 {
169 auto total = base;
170
171 for (auto i = 0uz; i < NUM_WEIGHTS; ++i)
172 total += weights.values[i] * coefficient(i);
173
174 return total;
175 }
176
180 [[nodiscard]] constexpr auto score(const Weights& weights) const noexcept -> Score
181 {
182 const auto white = white_score(weights).blend(phase, MAX_PHASE);
183
184 return Score { static_cast<Value>(sideToMove == Color::White ? white : -white) };
185 }
186};
187
194[[nodiscard]] auto evaluate_traced(const Position& position) -> CoeffTracer;
195
196} // namespace ben_bot::eval
constexpr std::int32_t KNIGHT
Definition Trace.hpp:54
std::int16_t Value
Definition Score.hpp:38
constexpr std::int32_t ROOK
Definition Trace.hpp:60
constexpr auto NUM_WEIGHTS
Definition Weights.hpp:185
constexpr auto count_of(const Term term) noexcept -> size_t
Definition Weights.hpp:198
constexpr std::int32_t MAX_PHASE
Definition Trace.hpp:78
constexpr auto offset_of(const Term term) noexcept -> size_t
Definition Weights.hpp:190
auto evaluate_traced(const Position &position) -> CoeffTracer
constexpr std::int32_t QUEEN
Definition Trace.hpp:63
constexpr std::int32_t BISHOP
Definition Trace.hpp:57
constexpr void add_base(const Color side, const TaperedScore &part) noexcept
Definition Trace.hpp:146
static constexpr bool ENABLED
Definition Trace.hpp:114
constexpr auto white_score(const Weights &weights) const noexcept -> TaperedScore
Definition Trace.hpp:167
constexpr void add(const Term term, const Color side, const int count, const size_t index=0uz) noexcept
Definition Trace.hpp:135
constexpr auto score(const Weights &weights) const noexcept -> Score
Definition Trace.hpp:180
std::array< std::array< std::int16_t, 2uz >, NUM_WEIGHTS > coeffs
Definition Trace.hpp:120
constexpr auto coefficient(const size_t index) const noexcept -> int
Definition Trace.hpp:157
static constexpr bool ENABLED
Definition Trace.hpp:87
static constexpr void add(const Term term, const Color side, const int count, const size_t index=0uz) noexcept
Definition Trace.hpp:90
static constexpr void add_base(const Color side, const TaperedScore &part) noexcept
Definition Trace.hpp:97