BenBot 1.7.5
A chess engine
Loading...
Searching...
No Matches
Bounds.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 <cstddef> // IWYU pragma: keep - for size_t
24#include <optional>
25
26namespace ben_bot::search {
27
28using eval::Score;
29using std::size_t;
30
36struct Bounds final {
39
42
46 [[nodiscard]] constexpr auto invert() const noexcept -> Bounds;
47
51 [[nodiscard]] constexpr auto null_window() const noexcept -> Bounds;
52
56 [[nodiscard]] constexpr auto null_window_beta() const noexcept -> Bounds;
57
59 [[nodiscard]] constexpr auto contains(Score score) const noexcept -> bool;
60
66 [[nodiscard]] constexpr auto mate_distance_pruning(size_t plyFromRoot) noexcept -> std::optional<Score>;
67};
68
69/*
70 ___ ,--,
71 ,---, ,--.'|_ ,--, ,--.'|
72 ,---.'| | | :,' ,--.'| | | :
73 | | : : : ' : | |, : : ' .--.--.
74 | | | ,---. .;__,' / ,--.--. `--'_ | ' | / / '
75 ,--.__| | / \ | | | / \ ,' ,'| ' | | | : /`./
76 / ,' | / / |:__,'| : .--. .-. | ' | | | | : | : ;_
77. ' / |. ' / | ' : |__ \__\/: . . | | : ' : |__ \ \ `.
78' ; |: |' ; /| | | '.'| ," .--.; | ' : |__ | | '.'| `----. \
79| | '/ '' | / | ; : ;/ / ,. | | | '.'|; : ;/ /`--' /__ ___ ___
80| : :|| : | | , /; : .' \; : ;| , /'--'. / .\/ .\/ .\
81 \ \ / \ \ / ---`-' | , .-./| , / ---`-' `--'---'\ ; \ ; \ ; |
82 `----' `----' `--`---' ---`-' `--" `--" `--"
83
84 */
85
86constexpr auto Bounds::invert() const noexcept -> Bounds
87{
88 return {
89 .alpha = -beta,
90 .beta = -alpha
91 };
92}
93
94constexpr auto Bounds::null_window() const noexcept -> Bounds
95{
96 return {
97 .alpha = Score { static_cast<eval::Value>(-alpha.value - UINT16_C(1)) },
98 .beta = -alpha
99 };
100}
101
102constexpr auto Bounds::null_window_beta() const noexcept -> Bounds
103{
104 return {
105 .alpha = -beta,
106 .beta = Score { static_cast<eval::Value>(-beta.value + UINT16_C(1)) }
107 };
108}
109
110constexpr auto Bounds::contains(const Score score) const noexcept -> bool
111{
112 return score > alpha and score < beta;
113}
114
115constexpr auto Bounds::mate_distance_pruning(const size_t plyFromRoot) noexcept -> std::optional<Score>
116{
117 // the worst we can do from here is be mated in this position,
118 // and the best we can do is deliver mate on our next move
119 const auto matedScore = Score::mate(plyFromRoot);
120 const auto mateScore = -Score::mate(plyFromRoot + 1uz);
121
122 if (alpha < matedScore)
123 alpha = matedScore;
124
125 if (beta > mateScore)
126 beta = mateScore;
127
128 if (alpha >= beta)
129 return alpha;
130
131 return std::nullopt;
132}
133
134} // namespace ben_bot::search
std::int16_t Value
Definition Score.hpp:38
constexpr Value MAX
Definition Score.hpp:46
constexpr auto null_window_beta() const noexcept -> Bounds
Definition Bounds.hpp:102
constexpr auto contains(Score score) const noexcept -> bool
Definition Bounds.hpp:110
constexpr auto mate_distance_pruning(size_t plyFromRoot) noexcept -> std::optional< Score >
Definition Bounds.hpp:115
constexpr auto null_window() const noexcept -> Bounds
Definition Bounds.hpp:94
constexpr auto invert() const noexcept -> Bounds
Definition Bounds.hpp:86
static constexpr auto mate(const size_t plyFromRoot) noexcept -> Score
Definition Score.hpp:122