Skip to content

Commit

Permalink
Replace material to major_material
Browse files Browse the repository at this point in the history
Major material contains Rooks, Cannons and Knights
  • Loading branch information
PikaCat-OuO committed Oct 28, 2023
1 parent 811d464 commit 93e35eb
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 30 deletions.
11 changes: 8 additions & 3 deletions src/evaluate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,12 @@ void NNUE::verify() {
// Returns a static, purely materialistic evaluation of the position from
// the point of view of the given color. It can be divided by PawnValue to get
// an approximation of the material advantage on the board in terms of pawns.
Value Eval::simple_eval(const Position& pos, Color c) { return pos.material(c) - pos.material(~c); }
Value Eval::simple_eval(const Position& pos, Color c) {
return PawnValue * (pos.count<PAWN>(c) - pos.count<PAWN>(~c))
+ AdvisorValue * (pos.count<ADVISOR>(c) - pos.count<ADVISOR>(~c))
+ BishopValue * (pos.count<BISHOP>(c) - pos.count<BISHOP>(~c))
+ (pos.major_material(c) - pos.major_material(~c));
}

// Evaluate is the evaluator for the outer world. It returns a static
// evaluation of the position from the point of view of the side to move.
Expand All @@ -118,14 +123,14 @@ Value Eval::evaluate(const Position& pos) {
int nnueComplexity;
Value nnue = NNUE::evaluate(pos, true, &nnueComplexity);

int material = pos.material() / 42;
Value optimism = pos.this_thread()->optimism[stm];

// Blend optimism and eval with nnue complexity and material imbalance
optimism += optimism * (nnueComplexity + abs(simpleEval - nnue)) / 708;
nnue -= nnue * (nnueComplexity + abs(simpleEval - nnue)) / 32858;

v = (nnue * (545 + material) + optimism * (128 + material)) / 1312;
int mm = pos.major_material() / 42;
v = (nnue * (545 + mm) + optimism * (128 + mm)) / 1312;

// Damp down the evaluation linearly when shuffling
v = v * (263 - shuffling) / 192;
Expand Down
38 changes: 20 additions & 18 deletions src/position.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,26 +201,26 @@ void Position::set_check_info() const {
// The function is only used when a new position is set up
void Position::set_state() const {

st->key = 0;
st->pawnKey = Zobrist::noPawns;
st->material[WHITE] = st->material[BLACK] = VALUE_ZERO;
st->checkersBB = checkers_to(~sideToMove, square<KING>(sideToMove));
st->move = MOVE_NONE;
st->key = 0;
st->pawnKey = Zobrist::noPawns;
st->majorMaterial[WHITE] = st->majorMaterial[BLACK] = VALUE_ZERO;
st->checkersBB = checkers_to(~sideToMove, square<KING>(sideToMove));
st->move = MOVE_NONE;

set_check_info();

for (Bitboard b = pieces(); b;)
{
Square s = pop_lsb(b);
Piece pc = piece_on(s);
Square s = pop_lsb(b);
Piece pc = piece_on(s);
PieceType pt = type_of(pc);
st->key ^= Zobrist::psq[pc][s];

if (type_of(pc) != KING)
{
st->material[color_of(pc)] += PieceValue[pc];
if (type_of(pc) == PAWN)
st->pawnKey ^= Zobrist::psq[pc][s];
}
if (pt == PAWN)
st->pawnKey ^= Zobrist::psq[pc][s];

else if (pt & 1)
st->majorMaterial[color_of(pc)] += PieceValue[pc];
}

if (sideToMove == BLACK)
Expand Down Expand Up @@ -461,7 +461,12 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) {
{
Square capsq = to;

st->material[them] -= PieceValue[captured];
// If the captured piece is a pawn, update pawn hash key, otherwise
// update major material.
if (type_of(captured) == PAWN)
st->pawnKey ^= Zobrist::psq[captured][capsq];
else if (type_of(captured) & 1)
st->majorMaterial[them] -= PieceValue[captured];

dp.dirty_num = 2; // 1 piece moved, 1 piece captured
dp.piece[1] = captured;
Expand All @@ -476,9 +481,6 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) {

// Update hash key
k ^= Zobrist::psq[captured][capsq];
// If the captured piece is a pawn, update pawn hash key.
if (type_of(captured) == PAWN)
st->pawnKey ^= Zobrist::psq[captured][capsq];

// Reset rule 60 counter
st->check10[WHITE] = st->check10[BLACK] = st->rule60 = 0;
Expand All @@ -488,7 +490,7 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) {
k ^= Zobrist::psq[pc][from] ^ Zobrist::psq[pc][to];
// If the moving piece is a pawn, update pawn hash key.
if (type_of(pc) == PAWN)
st->pawnKey ^= Zobrist::psq[pc][to];
st->pawnKey ^= Zobrist::psq[pc][from] ^ Zobrist::psq[pc][to];

// Move the piece.
dp.piece[0] = pc;
Expand Down
12 changes: 7 additions & 5 deletions src/position.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct StateInfo {

// Copied when making a move
Key pawnKey;
Value material[COLOR_NB];
Value majorMaterial[COLOR_NB];
int16_t check10[COLOR_NB];
int rule60;
int pliesFromNull;
Expand Down Expand Up @@ -154,8 +154,8 @@ class Position {
int rule60_count() const;
bool has_mate_threat(Depth d = -1);
uint16_t chased(Color c);
Value material(Color c) const;
Value material() const;
Value major_material(Color c) const;
Value major_material() const;

// Position consistency check, for debugging
bool pos_is_ok() const;
Expand Down Expand Up @@ -277,9 +277,11 @@ inline Key Position::adjust_key60(Key k) const {

inline Key Position::pawn_key() const { return st->pawnKey; }

inline Value Position::material(Color c) const { return st->material[c]; }
inline Value Position::major_material(Color c) const { return st->majorMaterial[c]; }

inline Value Position::material() const { return material(WHITE) + material(BLACK); }
inline Value Position::major_material() const {
return major_material(WHITE) + major_material(BLACK);
}

inline int Position::game_ply() const { return gamePly; }

Expand Down
10 changes: 6 additions & 4 deletions src/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,8 @@ Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth, boo
// Step 8. Null move search with verification search (~35 Elo)
if (!PvNode && (ss - 1)->currentMove != MOVE_NULL && (ss - 1)->statScore < 12731 && eval >= beta
&& eval >= ss->staticEval && ss->staticEval >= beta - 14 * depth + 138 && !excludedMove
&& ss->ply >= thisThread->nmpMinPly && beta > VALUE_MATED_IN_MAX_PLY)
&& pos.major_material(us) && ss->ply >= thisThread->nmpMinPly
&& beta > VALUE_MATED_IN_MAX_PLY)
{
assert(eval - beta >= 0);

Expand Down Expand Up @@ -840,9 +841,9 @@ Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth, boo

Depth r = reduction(improving, depth, moveCount, delta, thisThread->rootDelta);

// Step 13. Pruning at shallow depth (~98 Elo).
// Step 13. Pruning at shallow depth (~120 Elo).
// Depth conditions are important for mate finding.
if (!rootNode && bestValue > VALUE_MATED_IN_MAX_PLY)
if (!rootNode && pos.major_material(us) && bestValue > VALUE_MATED_IN_MAX_PLY)
{
// Skip quiet moves if movecount exceeds our FutilityMoveCount threshold (~8 Elo)
if (!moveCountPruning)
Expand Down Expand Up @@ -1252,6 +1253,7 @@ Value qsearch(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth) {
Value bestValue, value, ttValue, futilityValue, futilityBase;
bool pvHit, givesCheck, capture;
int moveCount;
Color us = pos.side_to_move();

// Step 1. Initialize node
if (PvNode)
Expand Down Expand Up @@ -1358,7 +1360,7 @@ Value qsearch(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth) {
moveCount++;

// Step 6. Pruning.
if (bestValue > VALUE_MATED_IN_MAX_PLY)
if (bestValue > VALUE_MATED_IN_MAX_PLY && pos.major_material(us))
{
// Futility pruning and moveCount pruning (~5 Elo)
if (!givesCheck && to_sq(move) != prevSq && futilityBase > VALUE_MATED_IN_MAX_PLY)
Expand Down

0 comments on commit 93e35eb

Please sign in to comment.