Skip to content

Commit

Permalink
Retire MixedScore class
Browse files Browse the repository at this point in the history
Since the bucketed NNUE, this class is no longer used.
Also introduce a scaling factor to convert NNUE scores to cp.

Non-regression STC:
LLR:  2.98/2.94<-6.00, 0.00> Elo diff: 5.04 [0.56, 9.52] (95%)
Games: 4000 W: 459 L: 401 D: 3140 Draw ratio: 78.5%
Pntl: [13, 265, 1387, 321, 14]

No functional change
  • Loading branch information
ruicoelhopedro committed May 9, 2024
1 parent 2ee09c1 commit c05147d
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 103 deletions.
2 changes: 1 addition & 1 deletion src/Evaluation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void eval_table(const Board& board)
<< std::endl;
}
std::cout << "--------------------------------------" << std::endl;
std::cout << "Final evaluation: " << 100 * nnue / PawnValue.endgame() << " cp (White)" << std::endl;
std::cout << "Final evaluation: " << 100 * nnue / ScoreToCp << " cp (White)" << std::endl;
std::cout << std::endl;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Evaluation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace Evaluation
: m_score(score)
{}

static inline double adjust(Score s) { return double(s) / PawnValue.endgame(); }
static inline double adjust(Score s) { return double(s) / ScoreToCp; }

friend std::ostream& operator<<(std::ostream& out, const Term& term);
};
Expand Down
2 changes: 1 addition & 1 deletion src/MoveOrder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ int MoveOrder::capture_score(Move move) const
{
// MVV
PieceType to = move.is_ep_capture() ? PAWN : m_position.board().get_piece_at(move.to());
return piece_value_mg[to];
return piece_value[to];
}


Expand Down
21 changes: 2 additions & 19 deletions src/Position.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -500,13 +500,11 @@ bool Board::is_valid() const

// Material and phase evaluation
uint8_t phase = Phases::Total;
MixedScore material(0, 0);
NNUE::Accumulator acc[2];
for (PieceType piece : { PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING })
for (Turn turn : { WHITE, BLACK })
{
Bitboard bb = get_pieces(turn, piece);
material += piece_value[piece] * bb.count() * turn_to_color(turn);
phase -= bb.count() * Phases::Pieces[piece];
while (bb)
{
Expand All @@ -515,11 +513,8 @@ bool Board::is_valid() const
acc[BLACK].push(piece, s, m_king_sq[BLACK], turn, BLACK);
}
}
MixedScore total_material = m_material[WHITE] - m_material[BLACK];
if (phase != m_phase)
return false;
if (material.middlegame() != total_material.middlegame() || material.endgame() != total_material.endgame())
return false;
if (acc[WHITE] != m_acc[WHITE] || acc[BLACK] != m_acc[BLACK])
return false;

Expand Down Expand Up @@ -631,7 +626,7 @@ Score Board::see(Move move, Score threshold) const

// Make the initial capture
PieceType last_attacker = get_piece_at(move.from());
Score gain = piece_value_mg[move.is_ep_capture() ? PAWN : get_piece_at(target)] - threshold;
Score gain = piece_value[move.is_ep_capture() ? PAWN : get_piece_at(target)] - threshold;
Bitboard from_bb = Bitboard::from_square(move.from());
Bitboard occupancy = get_pieces() ^ from_bb;
Turn side_to_move = ~m_turn;
Expand All @@ -651,7 +646,7 @@ Score Board::see(Move move, Score threshold) const
Bitboard attacker_bb = Bitboard::from_square(attacker);

// Make the capture
gain += color * piece_value_mg[last_attacker];
gain += color * piece_value[last_attacker];
last_attacker = get_piece_at(attacker);
occupancy ^= attacker_bb;
side_to_move = ~side_to_move;
Expand All @@ -665,18 +660,6 @@ Score Board::see(Move move, Score threshold) const
}


MixedScore Board::material() const
{
return m_material[WHITE] - m_material[BLACK];
}


MixedScore Board::material(Turn turn) const
{
return m_material[turn] - KingValue;
}


uint8_t Board::phase() const
{
return m_phase;
Expand Down
9 changes: 0 additions & 9 deletions src/Position.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class Board
Square m_king_sq[NUM_COLORS];
Hash m_hash;
Bitboard m_checkers;
MixedScore m_material[NUM_COLORS];
NNUE::Accumulator m_acc[NUM_COLORS];
uint8_t m_phase;
Piece m_board_pieces[NUM_SQUARES];
Expand Down Expand Up @@ -485,7 +484,6 @@ class Board
m_board_pieces[square] = get_piece(piece, turn);
m_acc[WHITE].push(piece, square, m_king_sq[WHITE], turn, WHITE);
m_acc[BLACK].push(piece, square, m_king_sq[BLACK], turn, BLACK);
m_material[turn] += piece_value[piece];
m_phase -= Phases::Pieces[piece];
}

Expand All @@ -497,7 +495,6 @@ class Board
m_board_pieces[square] = NO_PIECE;
m_acc[WHITE].pop(piece, square, m_king_sq[WHITE], turn, WHITE);
m_acc[BLACK].pop(piece, square, m_king_sq[BLACK], turn, BLACK);
m_material[turn] -= piece_value[piece];
m_phase += Phases::Pieces[piece];
}

Expand Down Expand Up @@ -657,12 +654,6 @@ class Board
Score see(Move move, Score threshold = 0) const;


MixedScore material() const;


MixedScore material(Turn turn) const;


uint8_t phase() const;


Expand Down
2 changes: 1 addition & 1 deletion src/Search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ namespace Search
if (is_mate(this->score()))
std::cout << " score mate " << mate_in(this->score());
else
std::cout << " score cp " << 100 * int(this->score()) / PawnValue.endgame();
std::cout << " score cp " << 100 * int(this->score()) / ScoreToCp;

// Score bound (if any)
if (this->bound() != BoundType::EXACT)
Expand Down
81 changes: 10 additions & 71 deletions src/Types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,87 +156,26 @@ namespace Phases
constexpr int EgLimit = 19;
}

constexpr Score ScoreToCp = 200;

class MixedScore
{
Score mg;
Score eg;

public:
inline constexpr MixedScore() : mg(0), eg(0) {}
inline constexpr MixedScore(Score smg, Score seg) : mg(smg), eg(seg) {}

inline constexpr Score middlegame() const { return mg; }
inline constexpr Score endgame() const { return eg; }
constexpr Score PawnValue( 125);
constexpr Score KnightValue( 750);
constexpr Score BishopValue( 800);
constexpr Score RookValue( 1200);
constexpr Score QueenValue( 2500);
constexpr Score KingValue(10000); // Needed for SEE and MVV-LVA

inline constexpr Score tapered(int phase_entry) const
{
int phase = std::clamp(256 * (phase_entry - Phases::MgLimit) / (Phases::EgLimit - Phases::MgLimit), 0, 256);
return ((mg * (256 - phase)) + (eg * phase)) / 256;
}

inline constexpr MixedScore& operator+=(const MixedScore& other) { mg += other.mg; eg += other.eg; return *this; }
inline constexpr MixedScore& operator-=(const MixedScore& other) { mg -= other.mg; eg -= other.eg; return *this; }
inline constexpr MixedScore& operator*=(const MixedScore& other) { mg *= other.mg; eg *= other.eg; return *this; }
inline constexpr MixedScore& operator/=(const MixedScore& other) { mg /= other.mg; eg /= other.eg; return *this; }

inline constexpr MixedScore& operator+=(int other) { mg += other; eg += other; return *this; }
inline constexpr MixedScore& operator-=(int other) { mg -= other; eg -= other; return *this; }
inline constexpr MixedScore& operator*=(int other) { mg *= other; eg *= other; return *this; }
inline constexpr MixedScore& operator/=(int other) { mg /= other; eg /= other; return *this; }

inline constexpr MixedScore operator+(const MixedScore& other) const { return MixedScore(mg + other.mg, eg + other.eg); }
inline constexpr MixedScore operator-(const MixedScore& other) const { return MixedScore(mg - other.mg, eg - other.eg); }
inline constexpr MixedScore operator*(const MixedScore& other) const { return MixedScore(mg * other.mg, eg * other.eg); }
inline constexpr MixedScore operator/(const MixedScore& other) const { return MixedScore(mg / other.mg, eg / other.eg); }

inline constexpr MixedScore operator+(int other) const { return MixedScore(mg + other, eg + other); }
inline constexpr MixedScore operator-(int other) const { return MixedScore(mg - other, eg - other); }
inline constexpr MixedScore operator*(int other) const { return MixedScore(mg * other, eg * other); }
inline constexpr MixedScore operator/(int other) const { return MixedScore(mg / other, eg / other); }
};


constexpr MixedScore PawnValue( 125, 200);
constexpr MixedScore KnightValue( 750, 850);
constexpr MixedScore BishopValue( 800, 900);
constexpr MixedScore RookValue( 1200, 1400);
constexpr MixedScore QueenValue( 2500, 2600);
constexpr MixedScore KingValue(10000, 10000); // Needed for SEE and MVV-LVA

constexpr MixedScore piece_value[] = {
constexpr Score piece_value[] = {
PawnValue,
KnightValue,
BishopValue,
RookValue,
QueenValue,
KingValue,
MixedScore(0, 0), // Empty
MixedScore(0, 0) // PIECE_NONE
Score(0), // Empty
Score(0) // PIECE_NONE
};

constexpr Score piece_value_mg[] = {
PawnValue.middlegame(),
KnightValue.middlegame(),
BishopValue.middlegame(),
RookValue.middlegame(),
QueenValue.middlegame(),
KingValue.middlegame(),
0, // Empty
0 // PIECE_NONE
};

constexpr Score piece_value_eg[] = {
PawnValue.endgame(),
KnightValue.endgame(),
BishopValue.endgame(),
RookValue.endgame(),
QueenValue.endgame(),
KingValue.endgame(),
0, // Empty
0 // PIECE_NONE
};


constexpr uint64_t uint64(int i) { return static_cast<uint64_t>(i); }

Expand Down

0 comments on commit c05147d

Please sign in to comment.