Files
Rigby/src/types.h
T
rektdieandclaude 15258f952a feat: policy network inference
src/policy.cpp runs the network train/policy writes: 3072 threat
inputs (side to move's view, flipped horizontally when its king is on
files e-h, +768 attacked, +1536 defended), a hidden layer with CReLU
and pairwise product, and one output row per move, i8 weights scaled
by 128. The hidden width is read from the file's size, so any trained
width loads. When a network is loaded, eval::policy uses its logits
plus the butterfly-history bias instead of the move heuristics; the
temperature and softmax are unchanged.

A network is built in with make POLICYFILE=<net> (default: none, so
the engine plays as before) or loaded at runtime with the UCI option
PolicyFile. policyfens prints the priors of the trainer's --check
positions.

Checked: with a smoke-trained network policyfens matches the trainer's
float output to a few tenths of a percent in all six positions (black
to move, mirrored king, castling, promotion). Speed with a network:
-13% nps at 512 hidden, -41% at 2048. A zero network built in with
POLICYFILE gives uniform priors. Without a network the bench is
unchanged.

Bench: 529903

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 21:27:38 +01:00

116 lines
3.6 KiB
C++

#pragma once
#include <cstdint>
#include <string>
#include <string_view>
namespace rigby {
using u64 = std::uint64_t;
using u32 = std::uint32_t;
using u16 = std::uint16_t;
using u8 = std::uint8_t;
using i64 = std::int64_t;
using i8 = std::int8_t;
using i16 = std::int16_t;
constexpr std::string_view START_FEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
enum Color : int { WHITE = 0, BLACK = 1 };
enum PieceType : int { PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING, NO_PIECE_TYPE };
// A piece is color * 6 + type, NO_PIECE marks an empty square.
constexpr int NO_PIECE = 12;
constexpr int make_piece(int color, int type) { return color * 6 + type; }
constexpr int piece_type(int piece) { return piece % 6; }
constexpr int piece_color(int piece) { return piece / 6; }
constexpr std::string_view PIECE_CHARS = "PNBRQKpnbrqk";
// clang-format off
enum Square : int {
A1, B1, C1, D1, E1, F1, G1, H1,
A2, B2, C2, D2, E2, F2, G2, H2,
A3, B3, C3, D3, E3, F3, G3, H3,
A4, B4, C4, D4, E4, F4, G4, H4,
A5, B5, C5, D5, E5, F5, G5, H5,
A6, B6, C6, D6, E6, F6, G6, H6,
A7, B7, C7, D7, E7, F7, G7, H7,
A8, B8, C8, D8, E8, F8, G8, H8,
NO_SQUARE
};
// clang-format on
constexpr int file_of(int sq) { return sq & 7; }
constexpr int rank_of(int sq) { return sq >> 3; }
constexpr int make_square(int file, int rank) { return rank * 8 + file; }
// Mirrors the square vertically for black, so tables can be written from white's view.
constexpr int relative_square(int color, int sq) { return color == WHITE ? sq : sq ^ 56; }
enum CastlingRight : int { WHITE_OO = 1, WHITE_OOO = 2, BLACK_OO = 4, BLACK_OOO = 8 };
inline std::string square_name(int sq) {
return {char('a' + file_of(sq)), char('1' + rank_of(sq))};
}
// 16-bit move: from (6) | to (6) | flags (4).
class Move {
public:
enum Flag : u16 {
QUIET = 0,
DOUBLE_PUSH = 1,
KING_CASTLE = 2,
QUEEN_CASTLE = 3,
CAPTURE = 4,
EN_PASSANT = 5,
PROMO = 8, // + 0..3 for knight, bishop, rook, queen
PROMO_CAPTURE = 12, // + 0..3
};
constexpr Move() = default;
constexpr Move(int from, int to, int flag) : data_(u16(from | (to << 6) | (flag << 12))) {}
static constexpr Move from_raw(u16 raw) {
Move m;
m.data_ = raw;
return m;
}
constexpr int from() const { return data_ & 63; }
constexpr int to() const { return (data_ >> 6) & 63; }
constexpr int flag() const { return data_ >> 12; }
constexpr bool is_capture() const { return flag() & 4; }
constexpr bool is_promotion() const { return flag() & 8; }
constexpr bool is_castle() const { return flag() == KING_CASTLE || flag() == QUEEN_CASTLE; }
constexpr bool is_en_passant() const { return flag() == EN_PASSANT; }
constexpr int promo_type() const { return (flag() & 3) + KNIGHT; }
constexpr u16 raw() const { return data_; }
constexpr explicit operator bool() const { return data_ != 0; }
constexpr bool operator==(const Move&) const = default;
std::string to_uci() const {
if (!data_) return "0000";
std::string s = square_name(from()) + square_name(to());
if (is_promotion()) s += "nbrq"[promo_type() - KNIGHT];
return s;
}
private:
u16 data_ = 0;
};
struct MoveList {
Move moves[256];
int count = 0;
void add(Move m) { moves[count++] = m; }
void clear() { count = 0; }
int size() const { return count; }
bool empty() const { return count == 0; }
Move operator[](int i) const { return moves[i]; }
const Move* begin() const { return moves; }
const Move* end() const { return moves + count; }
};
} // namespace rigby