- // Scale and shift NNUE for compatibility with search and classical evaluation
- auto adjusted_NNUE = [&]()
- {
- int material = pos.non_pawn_material() + 2 * PawnValueMg * pos.count<PAWN>();
- int scale = 641
- + material / 32
- - 4 * pos.rule50_count();
-
- Value nnue = NNUE::evaluate(pos) * scale / 1024 + Tempo;
-
- if (pos.is_chess960())
- nnue += fix_FRC(pos);
-
- return nnue;
- };
-
- // If there is PSQ imbalance use classical eval, with small probability if it is small
- Value psq = Value(abs(eg_value(pos.psq_score())));
- int r50 = 16 + pos.rule50_count();
- bool largePsq = psq * 16 > (NNUEThreshold1 + pos.non_pawn_material() / 64) * r50;
- bool classical = largePsq || (psq > PawnValueMg / 4 && !(pos.this_thread()->nodes & 0xB));
-
- // Use classical evaluation for really low piece endgames.
- // The most critical case is a bishop + A/H file pawn vs naked king draw.
- bool strongClassical = pos.non_pawn_material() < 2 * RookValueMg && pos.count<PAWN>() < 2;
-
- v = classical || strongClassical ? Evaluation<NO_TRACE>(pos).value() : adjusted_NNUE();
-
- // If the classical eval is small and imbalance large, use NNUE nevertheless.
- // For the case of opposite colored bishops, switch to NNUE eval with
- // small probability if the classical eval is less than the threshold.
- if ( largePsq
- && !strongClassical
- && ( abs(v) * 16 < NNUEThreshold2 * r50
- || ( pos.opposite_bishops()
- && abs(v) * 16 < (NNUEThreshold1 + pos.non_pawn_material() / 64) * r50
- && !(pos.this_thread()->nodes & 0xB))))
- v = adjusted_NNUE();
+ int nnueComplexity;
+ int scale = 1064 + 106 * pos.non_pawn_material() / 5120;
+ Value optimism = pos.this_thread()->optimism[stm];
+
+ Value nnue = NNUE::evaluate(pos, true, &nnueComplexity);
+ // Blend nnue complexity with (semi)classical complexity
+ nnueComplexity = (104 * nnueComplexity + 131 * abs(nnue - psq)) / 256;
+ if (complexity) // Return hybrid NNUE complexity to caller
+ *complexity = nnueComplexity;
+
+ optimism = optimism * (269 + nnueComplexity) / 256;
+ v = (nnue * scale + optimism * (scale - 754)) / 1024;