feat: minimal MCTS engine with UCI

Bitboard board with fancy magics and fully legal move generation, checked
by a perft suite. PUCT search over a node arena with FPU reduction and
in-tree draw detection. Hand-written value (eval + quiescence) and
heuristic policy stand in until the network is trained. UCI loop with a
search thread, OpenBench-compatible bench and Makefile.

Bench: 546127

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rektdieandclaude committed 2026-11-15 00:31:20 +01:00
commit d3044c0b88
19 files changed
+1997

No files matched your search

+215
View File
@@ -0,0 +1,215 @@
#include "eval.h"
#include <algorithm>
#include <cmath>
#include <cstdlib>
namespace rigby::eval {
namespace {
constexpr int MATE = 30000;
constexpr int QS_MAX_PLY = 32;
constexpr float Q_SCALE = 300.0f;
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.
int mg_pst[6][64];
void pst_for(int type, int sq, int& mg, int& eg) {
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
const bool central_file = f == 3 || f == 4;
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;
case KNIGHT:
mg = 24 - center * 4;
eg = 18 - center * 3;
break;
case BISHOP:
mg = 12 - center * 2;
eg = 10 - center * 2;
break;
case ROOK:
mg = (r == 6 ? 20 : 0) + (central_file ? 6 : 0);
eg = r == 6 ? 15 : 0;
break;
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;
}
}
int victim_type(const Position& pos, Move m) { return m.is_en_passant() ? PAWN : piece_type(pos.piece_on(m.to())); }
} // namespace
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);
}
}
}
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 quiescence(const Position& pos, int alpha, int beta, int ply) {
const bool in_check = pos.in_check();
int best = -MATE + ply;
int stand_pat = 0;
if (!in_check) {
stand_pat = static_eval(pos);
if (stand_pat >= beta || ply >= QS_MAX_PLY) return stand_pat;
best = stand_pat;
alpha = std::max(alpha, stand_pat);
}
MoveList list;
generate_legal(pos, list);
if (list.empty()) return in_check ? -MATE + ply : 0;
if (in_check && ply >= QS_MAX_PLY) return static_eval(pos);
Move moves[256];
int scores[256];
int count = 0;
for (Move m : list) {
const bool tactical = m.is_capture() || (m.is_promotion() && m.promo_type() == QUEEN);
if (!in_check && !tactical) continue;
int score = 0;
if (m.is_capture()) {
const int victim = victim_type(pos, m);
// Delta pruning: even winning the piece for free cannot reach alpha.
if (!in_check && !m.is_promotion() && stand_pat + SEE_VALUE[victim] + 200 <= alpha) continue;
score = SEE_VALUE[victim] * 16 - piece_type(pos.piece_on(m.from()));
}
if (m.is_promotion()) score += SEE_VALUE[m.promo_type()] * 16;
moves[count] = m;
scores[count++] = score;
}
for (int i = 0; i < count; i++) {
int best_idx = i;
for (int j = i + 1; j < count; j++)
if (scores[j] > scores[best_idx]) best_idx = j;
std::swap(moves[i], moves[best_idx]);
std::swap(scores[i], scores[best_idx]);
Position child = pos;
child.make_move(moves[i]);
const int score = -quiescence(child, -beta, -alpha, ply + 1);
if (score > best) {
best = score;
if (score > alpha) {
alpha = score;
if (alpha >= beta) break;
}
}
}
return best;
}
float value(const Position& pos) { return cp_to_q(quiescence(pos, -MATE, MATE, 0)); }
void policy(const Position& pos, const MoveList& moves, float temperature, float* out) {
const int us = pos.stm;
const int them = us ^ 1;
const u64 occ = pos.occupancy();
const u64 their_pawns = pos.pieces_of(them, PAWN);
const u64 pawn_threats = them == WHITE ? (((their_pawns << 7) & ~FILE_H_BB) | ((their_pawns << 9) & ~FILE_A_BB))
: (((their_pawns >> 7) & ~FILE_A_BB) | ((their_pawns >> 9) & ~FILE_H_BB));
// Squares from which each piece type would give a direct check.
const int eksq = pos.king_square(them);
u64 check_squares[6];
check_squares[PAWN] = attacks::pawn[them][eksq];
check_squares[KNIGHT] = attacks::knight[eksq];
check_squares[BISHOP] = attacks::bishop(eksq, occ);
check_squares[ROOK] = attacks::rook(eksq, occ);
check_squares[QUEEN] = check_squares[BISHOP] | check_squares[ROOK];
check_squares[KING] = 0;
const float inv_temp = 1.0f / std::max(temperature, 0.01f);
float max_logit = -1e30f;
for (int i = 0; i < moves.size(); i++) {
const Move m = moves[i];
const int from = m.from();
const int to = m.to();
const int type = piece_type(pos.piece_on(from));
float s = 0.0f;
if (m.is_capture()) s += 1.0f + 0.004f * SEE_VALUE[victim_type(pos, m)] - 0.0015f * SEE_VALUE[type];
if (m.is_promotion()) s += m.promo_type() == QUEEN ? 2.5f : -2.5f;
if (m.is_castle()) s += 0.8f;
s += 0.01f * float(mg_pst[type][relative_square(us, to)] - mg_pst[type][relative_square(us, from)]);
if (type != PAWN && type != KING) {
if (pawn_threats & bb(to)) s -= 0.004f * SEE_VALUE[type];
if (pawn_threats & bb(from)) s += 0.003f * SEE_VALUE[type];
}
if (check_squares[m.is_promotion() ? m.promo_type() : type] & bb(to)) s += 0.5f;
out[i] = s * inv_temp;
max_logit = std::max(max_logit, out[i]);
}
float total = 0.0f;
for (int i = 0; i < moves.size(); i++) {
out[i] = std::exp(out[i] - max_logit);
total += out[i];
}
for (int i = 0; i < moves.size(); i++) out[i] /= total;
}
float cp_to_q(int cp) { return std::tanh(float(cp) / Q_SCALE); }
int q_to_cp(float q) {
q = std::clamp(q, -0.9999f, 0.9999f);
return int(std::lround(Q_SCALE * std::atanh(q)));
}
} // namespace rigby::eval