feat: value network trained on Lc0 data
The static evaluation behind the quiescence search is now a (768 -> 128)x2 -> 1 SCReLU network instead of the hand-written evaluation. It was trained with bullet on linrock's Lc0-derived test80 2024-02 binpack, 40 superbatches (10 min on an RTX 3070), final loss 0.0220. The quiescence search itself is unchanged. The network is embedded with incbin (EVALFILE, default networks/rigby-value-v1.bin). Q is tanh(cp / 800), the same scale the network was trained on (sigmoid(cp / 400) = expected score). Checked: bullet's float evaluation of fixed positions (trainer --check) and the engine's quantised inference (evalfens) agree within 6 cp; an integer re-implementation in Python matches the engine exactly. Perft passes, bench is deterministic. Bench: 545914 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
530c4d67ed
commit
30b0c36cd5
14 files changed
+700
-68
No files matched your search
+17
-53
@@ -4,26 +4,24 @@
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
|
||||
#include "nnue.h"
|
||||
|
||||
namespace rigby::eval {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr int MATE = 30000;
|
||||
constexpr int QS_MAX_PLY = 32;
|
||||
constexpr float Q_SCALE = 300.0f;
|
||||
// The network is trained so that sigmoid(cp / 400) is the expected score, so
|
||||
// Q = 2 * sigmoid(cp / 400) - 1 = tanh(cp / 800).
|
||||
constexpr float Q_SCALE = 2.0f * float(nnue::SCALE);
|
||||
|
||||
constexpr int MG_VALUE[6] = {90, 320, 335, 480, 980, 0};
|
||||
constexpr int EG_VALUE[6] = {110, 290, 305, 520, 940, 0};
|
||||
constexpr int SEE_VALUE[6] = {100, 300, 310, 500, 900, 0};
|
||||
constexpr int PHASE_WEIGHT[6] = {0, 1, 1, 2, 4, 0};
|
||||
|
||||
// Material folded into piece-square tables, indexed [piece][square].
|
||||
int mg_table[12][64];
|
||||
int eg_table[12][64];
|
||||
// Positional part only (no material), white's view, used by the policy.
|
||||
// Positional piece-square bonuses, white's view, used by the policy.
|
||||
int mg_pst[6][64];
|
||||
|
||||
void pst_for(int type, int sq, int& mg, int& eg) {
|
||||
int pst_mg(int type, int sq) {
|
||||
const int f = file_of(sq);
|
||||
const int r = rank_of(sq);
|
||||
const int center = std::abs(2 * f - 7) + std::abs(2 * r - 7); // 2..14
|
||||
@@ -31,29 +29,17 @@ void pst_for(int type, int sq, int& mg, int& eg) {
|
||||
|
||||
switch (type) {
|
||||
case PAWN:
|
||||
mg = (r >= 1 && r <= 6) ? (r - 1) * 6 + (central_file ? (r - 1) * 4 : 0) : 0;
|
||||
eg = (r >= 1 && r <= 6) ? (r - 1) * 14 : 0;
|
||||
break;
|
||||
return (r >= 1 && r <= 6) ? (r - 1) * 6 + (central_file ? (r - 1) * 4 : 0) : 0;
|
||||
case KNIGHT:
|
||||
mg = 24 - center * 4;
|
||||
eg = 18 - center * 3;
|
||||
break;
|
||||
return 24 - center * 4;
|
||||
case BISHOP:
|
||||
mg = 12 - center * 2;
|
||||
eg = 10 - center * 2;
|
||||
break;
|
||||
return 12 - center * 2;
|
||||
case ROOK:
|
||||
mg = (r == 6 ? 20 : 0) + (central_file ? 6 : 0);
|
||||
eg = r == 6 ? 15 : 0;
|
||||
break;
|
||||
return (r == 6 ? 20 : 0) + (central_file ? 6 : 0);
|
||||
case QUEEN:
|
||||
mg = 6 - center;
|
||||
eg = 20 - center * 2;
|
||||
break;
|
||||
default: // KING: shelter in the middlegame, centralise in the endgame
|
||||
mg = -std::min(r, 3) * 25 + ((f <= 2 || f >= 6) ? 15 : -15);
|
||||
eg = 30 - center * 4;
|
||||
break;
|
||||
return 6 - center;
|
||||
default: // KING: stay sheltered
|
||||
return -std::min(r, 3) * 25 + ((f <= 2 || f >= 6) ? 15 : -15);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,35 +50,13 @@ int victim_type(const Position& pos, Move m) { return m.is_en_passant() ? PAWN :
|
||||
void init() {
|
||||
for (int type = PAWN; type <= KING; type++) {
|
||||
for (int sq = 0; sq < 64; sq++) {
|
||||
int mg, eg;
|
||||
pst_for(type, sq, mg, eg);
|
||||
mg_pst[type][sq] = mg;
|
||||
mg_table[make_piece(WHITE, type)][sq] = MG_VALUE[type] + mg;
|
||||
eg_table[make_piece(WHITE, type)][sq] = EG_VALUE[type] + eg;
|
||||
mg_table[make_piece(BLACK, type)][sq ^ 56] = -(MG_VALUE[type] + mg);
|
||||
eg_table[make_piece(BLACK, type)][sq ^ 56] = -(EG_VALUE[type] + eg);
|
||||
mg_pst[type][sq] = pst_mg(type, sq);
|
||||
}
|
||||
}
|
||||
nnue::init();
|
||||
}
|
||||
|
||||
int static_eval(const Position& pos) {
|
||||
int mg = 0, eg = 0, phase = 0;
|
||||
u64 occ = pos.occupancy();
|
||||
while (occ) {
|
||||
const int sq = pop_lsb(occ);
|
||||
const int piece = pos.piece_on(sq);
|
||||
mg += mg_table[piece][sq];
|
||||
eg += eg_table[piece][sq];
|
||||
phase += PHASE_WEIGHT[piece_type(piece)];
|
||||
}
|
||||
phase = std::min(phase, 24);
|
||||
|
||||
int score = (mg * phase + eg * (24 - phase)) / 24;
|
||||
if (popcount(pos.pieces_of(WHITE, BISHOP)) >= 2) score += 30;
|
||||
if (popcount(pos.pieces_of(BLACK, BISHOP)) >= 2) score -= 30;
|
||||
|
||||
return (pos.stm == WHITE ? score : -score) + 10;
|
||||
}
|
||||
int static_eval(const Position& pos) { return nnue::evaluate(pos); }
|
||||
|
||||
int quiescence(const Position& pos, int alpha, int beta, int ply) {
|
||||
const bool in_check = pos.in_check();
|
||||
|
||||
Reference in new issue
Block a user