diff --git a/src/evaluate.cpp b/src/evaluate.cpp index 25a6545564a..46ebbb49920 100644 --- a/src/evaluate.cpp +++ b/src/evaluate.cpp @@ -136,35 +136,54 @@ namespace Eval { } } -/// 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. -Value Eval::evaluate(const Position& pos) { - - assert(!pos.checkers()); - - Value v; +/// simple_eval() 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. - int nnueComplexity; - int npm = pos.non_pawn_material() / 64; +Value Eval::simple_eval(const Position& pos, Color c) { + return PawnValue * (pos.count(c) - pos.count(~c)) + + (pos.non_pawn_material(c) - pos.non_pawn_material(~c)); +} - Color stm = pos.side_to_move(); - Value optimism = pos.this_thread()->optimism[stm]; - Value nnue = NNUE::evaluate(pos, true, &nnueComplexity); +/// 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. - int material = pos.non_pawn_material(stm) - pos.non_pawn_material(~stm) - + 126 * (pos.count(stm) - pos.count(~stm)); +Value Eval::evaluate(const Position& pos) { - // Blend optimism and eval with nnue complexity and material imbalance - optimism += optimism * (nnueComplexity + abs(material - nnue)) / 512; - nnue -= nnue * (nnueComplexity + abs(material - nnue)) / 32768; + assert(!pos.checkers()); - v = ( nnue * (915 + npm + 9 * pos.count()) - + optimism * (154 + npm + pos.count())) / 1024; + Value v; + Color stm = pos.side_to_move(); + int shuffling = pos.rule50_count(); + int simpleEval = simple_eval(pos, stm) + (int(pos.key() & 7) - 3); + + bool lazy = abs(simpleEval) >= RookValue + KnightValue + + 16 * shuffling * shuffling + + abs(pos.this_thread()->bestValue) + + abs(pos.this_thread()->rootSimpleEval); + + if (lazy) + v = Value(simpleEval); + else + { + int nnueComplexity; + Value nnue = NNUE::evaluate(pos, true, &nnueComplexity); + + Value optimism = pos.this_thread()->optimism[stm]; + + // Blend optimism and eval with nnue complexity and material imbalance + optimism += optimism * (nnueComplexity + abs(simpleEval - nnue)) / 512; + nnue -= nnue * (nnueComplexity + abs(simpleEval - nnue)) / 32768; + + int npm = pos.non_pawn_material() / 64; + v = ( nnue * (915 + npm + 9 * pos.count()) + + optimism * (154 + npm + pos.count())) / 1024; + } // Damp down the evaluation linearly when shuffling - v = v * (200 - pos.rule50_count()) / 214; + v = v * (200 - shuffling) / 214; // Guarantee evaluation does not hit the tablebase range v = std::clamp(v, VALUE_TB_LOSS_IN_MAX_PLY + 1, VALUE_TB_WIN_IN_MAX_PLY - 1); @@ -184,6 +203,7 @@ std::string Eval::trace(Position& pos) { // Reset any global variable used in eval pos.this_thread()->bestValue = VALUE_ZERO; + pos.this_thread()->rootSimpleEval = VALUE_ZERO; pos.this_thread()->optimism[WHITE] = VALUE_ZERO; pos.this_thread()->optimism[BLACK] = VALUE_ZERO; diff --git a/src/evaluate.h b/src/evaluate.h index a222da7361a..7f4feedf0c6 100644 --- a/src/evaluate.h +++ b/src/evaluate.h @@ -21,6 +21,8 @@ #include +#include "types.h" + namespace Stockfish { class Position; @@ -29,6 +31,8 @@ enum Value : int; namespace Eval { std::string trace(Position& pos); + + Value simple_eval(const Position& pos, Color c); Value evaluate(const Position& pos); extern std::string currentEvalFileName; diff --git a/src/thread.cpp b/src/thread.cpp index 9cf85310c39..60f760ed46e 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -27,6 +27,7 @@ #include #include +#include "evaluate.h" #include "misc.h" #include "movegen.h" #include "search.h" @@ -212,6 +213,7 @@ void ThreadPool::start_thinking(Position& pos, StateListPtr& states, th->rootMoves = rootMoves; th->rootPos.set(pos.fen(), pos.is_chess960(), &th->rootState, th); th->rootState = setupStates->back(); + th->rootSimpleEval = Eval::simple_eval(pos, pos.side_to_move()); } main()->start_searching(); diff --git a/src/thread.h b/src/thread.h index a421af9e3bc..8d0adcf0340 100644 --- a/src/thread.h +++ b/src/thread.h @@ -67,6 +67,7 @@ class Thread { Search::RootMoves rootMoves; Depth rootDepth, completedDepth; Value rootDelta; + Value rootSimpleEval; CounterMoveHistory counterMoves; ButterflyHistory mainHistory; CapturePieceToHistory captureHistory;