125 float middlegame_factor = 1.0f - endgame_factor;
129 float tactical_strength =
136 middlegame_factor * (1.0f - tactical_strength) *
SCORE_MAX;
146 for (
int i = 0; i <
NUM_BASE; ++i) {
147 raw_base[i] = std::max(raw_base[i],
SCORE_MIN);
154 for (
int i = 0; i <
NUM_BASE; ++i) {
192 float middlegame_factor = 1.0f - endgame_factor;
194 float tactical_strength =
202 middlegame_factor * (1.0f - tactical_strength) *
SCORE_MAX;
206 for (
int i = 0; i <
NUM_BASE; ++i) {
207 raw_base[i] = std::max(raw_base[i],
SCORE_MIN);
212 for (
int i = 0; i <
NUM_BASE; ++i) {
250 [&weights](
int a,
int b) {
255 for (
int i = 0; i < k; ++i) {
261 for (
int i = 0; i < k; ++i) {
262 sum += weights.
weights[idx[i]];
264 for (
int i = 0; i < k; ++i) {
265 probs[i] = weights.
weights[idx[i]] / sum;
330 bool us_major_threatened =
333 bool them_major_threatened =
344 int attacker_group_begin,
345 int attacker_group_end,
346 int target_presence_group) {
348 for (
int g = attacker_group_begin; g <= attacker_group_end; ++g) {
349 for (
int sq = 0; sq < 64; ++sq) {
350 if (input.
branches[g][FE::ATTACKS][sq] > 0.5f &&
351 input.
branches[target_presence_group][FE::PRESENCE][sq] > 0.5f) {
364 const int us_begin = FE::US_PAWN;
365 const int us_end = FE::US_KING;
366 const int them_begin = FE::THEM_PAWN;
367 const int them_end = FE::THEM_KING;
404 bool us_major_threatened =
408 bool them_major_threatened =
424 return input.
global[G::US_MAT_ROOK] > 0.0f ||
425 input.
global[G::THEM_MAT_ROOK] > 0.0f ||
426 input.
global[G::US_MAT_QUEEN] > 0.0f ||
427 input.
global[G::THEM_MAT_QUEEN] > 0.0f;
443 float major_mat = 0.0f;
445 (input.
global[G::US_MAT_ROOK] + input.
global[G::THEM_MAT_ROOK]) * 2.0f *
448 (input.
global[G::US_MAT_QUEEN] + input.
global[G::THEM_MAT_QUEEN]) *
452 float minor_mat = 0.0f;
454 (input.
global[G::US_MAT_KNIGHT] + input.
global[G::THEM_MAT_KNIGHT]) *
457 (input.
global[G::US_MAT_BISHOP] + input.
global[G::THEM_MAT_BISHOP]) *
460 float total = major_mat + minor_mat;
465 return major_mat / total;
471 float major_mat = 0.0f;
473 (input.
global[G::US_MAT_ROOK] + input.
global[G::THEM_MAT_ROOK]) * 2.0f *
476 (input.
global[G::US_MAT_QUEEN] + input.
global[G::THEM_MAT_QUEEN]) *
479 float minor_mat = 0.0f;
481 (input.
global[G::US_MAT_KNIGHT] + input.
global[G::THEM_MAT_KNIGHT]) *
484 (input.
global[G::US_MAT_BISHOP] + input.
global[G::THEM_MAT_BISHOP]) *
487 float total = major_mat + minor_mat;
492 return major_mat / total;
502 us_mat += input.
global[G::US_MAT_PAWN] * 8.0f * 1.0f;
503 us_mat += input.
global[G::US_MAT_KNIGHT] * 2.0f * 3.0f;
504 us_mat += input.
global[G::US_MAT_BISHOP] * 2.0f * 3.0f;
505 us_mat += input.
global[G::US_MAT_ROOK] * 2.0f * 5.0f;
506 us_mat += input.
global[G::US_MAT_QUEEN] * 1.0f * 9.0f;
508 float them_mat = 0.0f;
509 them_mat += input.
global[G::THEM_MAT_PAWN] * 8.0f * 1.0f;
510 them_mat += input.
global[G::THEM_MAT_KNIGHT] * 2.0f * 3.0f;
511 them_mat += input.
global[G::THEM_MAT_BISHOP] * 2.0f * 3.0f;
512 them_mat += input.
global[G::THEM_MAT_ROOK] * 2.0f * 5.0f;
513 them_mat += input.
global[G::THEM_MAT_QUEEN] * 1.0f * 9.0f;
515 return std::abs(us_mat - them_mat);
527 const uint64_t *ptr =
reinterpret_cast<const uint64_t *
>(layer);
529 for (
int i = 0; i < 8; ++i) {
530 uint64_t val = ptr[i];
535 const uint8_t *b =
reinterpret_cast<const uint8_t *
>(&val);
536 for (
int j = 0; j < 8; ++j)
548 static bool has_overlap(
const uint8_t a[64],
const uint8_t b[64]) {
549 const uint64_t *a64 =
reinterpret_cast<const uint64_t *
>(a);
550 const uint64_t *b64 =
reinterpret_cast<const uint64_t *
>(b);
577 int piece_layer,
float piece_value) {
601 x = 1.0f + x / 256.0f;
623 return 1.0f / (1.0f +
fast_exp(-x));
630 static void softmax(
const float *scores,
float *probs,
int n) {
632 float max_score = scores[0];
633 for (
int i = 1; i < n; ++i) {
634 if (scores[i] > max_score)
635 max_score = scores[i];
640 for (
int i = 0; i < n; ++i) {
642 float e =
fast_exp(scores[i] - max_score);
650 if (sum < 1e-9f || std::isnan(sum)) {
652 for (
int i = 0; i < n; ++i)
667 float inv_sum = 1.0f / sum;
668 for (
int i = 0; i < n; ++i) {
Definition ExpertRouter.hpp:30
static constexpr float KILLER_CP_THRESHOLD
Definition ExpertRouter.hpp:67
static constexpr float AUX_RAMP_RANGE
Definition ExpertRouter.hpp:71
static float compute_phase(const ChessInput &input)
Definition ExpertRouter.hpp:279
static constexpr int NUM_EXPERTS
Definition ExpertRouter.hpp:32
static constexpr float SCORE_MAX
Definition ExpertRouter.hpp:87
static void compute_weights(const FactorizedInput &input, float eval_cp, ExpertWeights &out)
Definition ExpertRouter.hpp:183
static constexpr int NUM_BASE
Definition ExpertRouter.hpp:33
static void compute_weights(const ChessInput &input, float eval_cp, ExpertWeights &out)
Definition ExpertRouter.hpp:106
static float fast_exp(float x)
Definition ExpertRouter.hpp:593
static constexpr float SURVIVOR_CP_THRESHOLD
Definition ExpertRouter.hpp:69
static constexpr float CCT_WEIGHT_CHECK
Definition ExpertRouter.hpp:74
static constexpr float PHASE_ENDGAME_THRESHOLD
Definition ExpertRouter.hpp:60
static constexpr float PIECE_VALUE_ROOK
Definition ExpertRouter.hpp:81
static float compute_major_ratio(const FactorizedInput &input)
Definition ExpertRouter.hpp:468
static constexpr float CCT_STRATEGIC_THRESHOLD
Definition ExpertRouter.hpp:64
static constexpr float SCORE_MIN
Definition ExpertRouter.hpp:88
static constexpr int NUM_AUX
Definition ExpertRouter.hpp:34
static bool has_major_pieces(const ChessInput &input)
Definition ExpertRouter.hpp:422
static float compute_material_imbalance(const ChessInput &input)
Definition ExpertRouter.hpp:498
static float compute_phase(const FactorizedInput &input)
Definition ExpertRouter.hpp:283
static constexpr float PIECE_VALUE_PAWN
Definition ExpertRouter.hpp:78
static float piece_threat_value(const ChessInput &input, int threat_layer, int piece_layer, float piece_value)
Definition ExpertRouter.hpp:576
static float sigmoid(float x)
Definition ExpertRouter.hpp:618
static constexpr float CCT_WEIGHT_MAJOR_THREAT
Definition ExpertRouter.hpp:75
static constexpr float PIECE_VALUE_KNIGHT
Definition ExpertRouter.hpp:79
static float compute_major_ratio(const ChessInput &input)
Definition ExpertRouter.hpp:439
static float compute_cct(const ChessInput &input)
Definition ExpertRouter.hpp:291
static void softmax(const float *scores, float *probs, int n)
Definition ExpertRouter.hpp:630
static void get_top_k(const ExpertWeights &weights, int k, int *indices, float *probs)
Definition ExpertRouter.hpp:243
static float compute_cct(const FactorizedInput &input)
Definition ExpertRouter.hpp:359
static constexpr float PIECE_VALUE_QUEEN
Definition ExpertRouter.hpp:82
static int count_layer(const uint8_t layer[64])
Definition ExpertRouter.hpp:526
static constexpr float CCT_TACTICAL_THRESHOLD
Definition ExpertRouter.hpp:63
static bool has_overlap(const uint8_t a[64], const uint8_t b[64])
Definition ExpertRouter.hpp:548
Expert
Definition ExpertRouter.hpp:37
@ STRATEGIC
Definition ExpertRouter.hpp:40
@ KILLER
Definition ExpertRouter.hpp:45
@ TACTICAL
Definition ExpertRouter.hpp:39
@ MAJOR_END
Definition ExpertRouter.hpp:41
@ MINOR_END
Definition ExpertRouter.hpp:42
@ SURVIVOR
Definition ExpertRouter.hpp:44
static bool has_overlap_factorized(const FactorizedInput &input, int attacker_group_begin, int attacker_group_end, int target_presence_group)
Definition ExpertRouter.hpp:343
static constexpr float PIECE_VALUE_BISHOP
Definition ExpertRouter.hpp:80
Definition ExpertRouter.hpp:52
float weights[NUM_EXPERTS]
Definition ExpertRouter.hpp:53
float raw_scores[NUM_EXPERTS]
Definition ExpertRouter.hpp:54