io-chess
UCI chess engine
Loading...
Searching...
No Matches
EvalNormalizer.hpp
Go to the documentation of this file.
1
5#pragma once
6#include <string>
7#include <cmath>
8#include <algorithm>
9#include <iostream>
10
12public:
13 // Constants used for normalization
14 static constexpr double MATE_SCORE = 20000.0;
15 static constexpr double CLIP_LIMIT = 8000.0;
16 static constexpr double GAMMA = 350.0;
17
22 static float normalize(const std::string& eval_str, bool is_white_to_move) {
23 double cp_val = 0.0;
24
25 // --- 1. Handle Mate Scores ("#5", "#-2") ---
26 if (eval_str.find('#') != std::string::npos) {
27 // Remove the '#' character to parse the number
28 std::string moves_str = eval_str;
29 moves_str.erase(std::remove(moves_str.begin(), moves_str.end(), '#'), moves_str.end());
30
31 int moves = std::stoi(moves_str);
32
33 int sign = (moves > 0) ? 1 : -1;
34 cp_val = sign * (MATE_SCORE - (2000.0 * std::log(std::abs(moves))));
35 }
36 // --- 2. Handle Centipawn Scores ("35", "-400") ---
37 else {
38 cp_val = std::stod(eval_str);
39
40 // Clamp to keep range stable [-8000, 8000]
41 cp_val = std::clamp(cp_val, -CLIP_LIMIT, CLIP_LIMIT);
42 }
43
44 // --- 3. Apply Arctangent Normalization ---
45 float normalized = static_cast<float>((2.0 / M_PI) * std::atan(cp_val / GAMMA));
46
47
48 // --- 4. Adjust for Side to Move ---
49 if (!is_white_to_move) {
50 normalized = -normalized;
51 }
52
53 return normalized;
54 }
55
59 static float to_centipawns(float normalized) {
60
61 // Inverse of the arctangent normalization
62 float cp_val = GAMMA * std::tan((normalized * M_PI) / 2.0f);
63 // clamp to mate range
64 cp_val = std::clamp(cp_val, static_cast<float>(-MATE_SCORE), static_cast<float>(MATE_SCORE));
65 return cp_val;
66
67 }
68};
69
Definition EvalNormalizer.hpp:11
static constexpr double MATE_SCORE
Definition EvalNormalizer.hpp:14
static constexpr double CLIP_LIMIT
Definition EvalNormalizer.hpp:15
static float to_centipawns(float normalized)
Definition EvalNormalizer.hpp:59
static constexpr double GAMMA
Definition EvalNormalizer.hpp:16
static float normalize(const std::string &eval_str, bool is_white_to_move)
Definition EvalNormalizer.hpp:22