io-chess
UCI chess engine
Loading...
Searching...
No Matches
WDLConverter.hpp
Go to the documentation of this file.
1#pragma once
2
26
27#include <algorithm>
28#include <cmath>
29
35public:
40 struct WDL {
41 float win, draw, loss;
42 };
43
44 // Score ranges
45 static constexpr int MAX_CP = 15000;
46
47 // Lichess constant: 1 / 0.00368208 (exact, from WDLNormalizer.hpp)
48 static constexpr float WDL_SCALE = 1.0f / 0.00368208f;
49
50 // Aggression parameter: -1.0 = defensive, 0 = neutral, +1.0 = aggressive
51 float aggression = 0.0f;
52
61 int convert(float win, float draw, float loss) const {
62 return convertWDL(win, draw, loss);
63 }
64
65private:
85 int convertWDL(float win, float draw, float loss) const {
86 // Aggression adjusts how draws affect win/loss perception
87 // Positive aggression: draws feel like partial losses (fight for wins)
88 // Negative aggression: draws feel like partial wins (play safe)
89 float draw_to_win = 0.5f - aggression * 0.3f;
90 draw_to_win = std::clamp(draw_to_win, 0.0f, 1.0f);
91
92 // Adjust win/loss with draw allocation
93 float win_adj = win + draw * draw_to_win;
94 float loss_adj = loss + draw * (1.0f - draw_to_win);
95
96 // The key ratio - this extracts the original sigmoid value
97 // regardless of the 0.3 factor used in training
98 float total = win_adj + loss_adj;
99 if (total < 1e-6f) {
100 return 0; // Draw-only position
101 }
102 float win_ratio = win_adj / total;
103
104 // Clamp to avoid log(0) or log(inf)
105 win_ratio = std::clamp(win_ratio, 1e-6f, 1.0f - 1e-6f);
106
107 // Inverse sigmoid: cp = SCALE * ln(win_ratio / (1 - win_ratio))
108 int cp =
109 static_cast<int>(WDL_SCALE * std::log(win_ratio / (1.0f - win_ratio)));
110
111 // Clamp to non-mate range
112 return std::clamp(cp, -MAX_CP, MAX_CP);
113 }
114};
Handles conversion between Win-Draw-Loss probabilities and centipawn scores.
Definition WDLConverter.hpp:34
static constexpr int MAX_CP
Definition WDLConverter.hpp:45
float aggression
Definition WDLConverter.hpp:51
int convertWDL(float win, float draw, float loss) const
Definition WDLConverter.hpp:85
static constexpr float WDL_SCALE
Definition WDLConverter.hpp:48
int convert(float win, float draw, float loss) const
Definition WDLConverter.hpp:61
Definition loss.py:1
Holds Win, Draw, and Loss probabilities.
Definition WDLConverter.hpp:40
float win
Definition WDLConverter.hpp:41
float draw
Definition WDLConverter.hpp:41