BenBot 1.7.5
A chess engine
Loading...
Searching...
No Matches
Evaluation
Collaboration diagram for Evaluation:

Files

file  DefaultWeights.hpp
file  Evaluation.hpp
file  Material.hpp
file  Score.hpp
file  SEE.hpp
file  TaperedScore.hpp
file  Trace.hpp
file  Weights.hpp

Namespaces

namespace  ben_bot
namespace  ben_bot::eval
namespace  ben_bot::eval::phase_weights
namespace  ben_bot::eval::piece_values

Classes

struct  ben_bot::eval::CoeffTracer
struct  std::formatter< ben_bot::eval::Score >
struct  ben_bot::eval::NullTracer
struct  ben_bot::eval::Score
struct  ben_bot::eval::TaperedScore
struct  ben_bot::eval::TermInfo
struct  ben_bot::eval::Weights

Typedefs

using ben_bot::eval::Value = std::int16_t

Enumerations

enum class  ben_bot::eval::Method : std::uint8_t { ben_bot::eval::Method::Linear }
enum class  ben_bot::eval::Term : std::uint8_t {
  ben_bot::eval::Term::Material , ben_bot::eval::Term::Pst , ben_bot::eval::Term::PassedPawn , ben_bot::eval::Term::PasserProtectedByPasser ,
  ben_bot::eval::Term::PasserProtectedByPawn , ben_bot::eval::Term::IsolatedPawn , ben_bot::eval::Term::DoubledPawn , ben_bot::eval::Term::BackwardPawn ,
  ben_bot::eval::Term::PawnChain , ben_bot::eval::Term::RookBehindPasser , ben_bot::eval::Term::KingEscortsPasser , ben_bot::eval::Term::EnemyKingBlocksPasser ,
  ben_bot::eval::Term::RookHalfOpenFile , ben_bot::eval::Term::RookOpenFile , ben_bot::eval::Term::ConnectedRooksOpenFile , ben_bot::eval::Term::KingHalfOpenFile ,
  ben_bot::eval::Term::KingOpenLine , ben_bot::eval::Term::KingStranded , ben_bot::eval::Term::KnightTropism , ben_bot::eval::Term::QueenTropism ,
  ben_bot::eval::Term::KnightMobility , ben_bot::eval::Term::BishopMobility , ben_bot::eval::Term::RookMobility , ben_bot::eval::Term::QueenMobility ,
  ben_bot::eval::Term::KingAreaControl , ben_bot::eval::Term::CenterControl , ben_bot::eval::Term::Space , ben_bot::eval::Term::PawnThreat ,
  ben_bot::eval::Term::MinorOnRook , ben_bot::eval::Term::MinorOnQueen , ben_bot::eval::Term::RookOnQueen , ben_bot::eval::Term::Hanging ,
  ben_bot::eval::Term::PushThreat , ben_bot::eval::Term::LastPiece , ben_bot::eval::Term::MopUpEdge , ben_bot::eval::Term::MopUpKingDistance
}

Functions

constexpr auto ben_bot::eval::count_of (const Term term) noexcept -> size_t
auto ben_bot::eval::evaluate (const Position &position, PawnTable *pawnTable=nullptr) -> Score
auto ben_bot::eval::evaluate_traced (const Position &position) -> CoeffTracer
constexpr auto ben_bot::eval::piece_values::get (PieceType type) noexcept -> Value
constexpr auto ben_bot::eval::offset_of (const Term term) noexcept -> size_t
constexpr auto ben_bot::eval::score_material (const Position &position) noexcept -> Value
auto ben_bot::eval::see (const Position &position, Move move) -> int

Variables

constexpr Weights ben_bot::eval::DEFAULT_WEIGHTS
constexpr Value ben_bot::eval::DRAW { UINT16_C(0) }
constexpr Value ben_bot::eval::MATE { MAX / UINT16_C(2) }
constexpr Value ben_bot::eval::MAX { std::numeric_limits<Value>::max() - UINT16_C(5) }
constexpr std::int32_t ben_bot::eval::MAX_PHASE = (phase_weights::QUEEN + (phase_weights::ROOK * 2) + (phase_weights::BISHOP * 2) + (phase_weights::KNIGHT * 2)) * 2
constexpr auto ben_bot::eval::NUM_TERMS = TERMS.size()
constexpr auto ben_bot::eval::NUM_WEIGHTS = detail::TERM_OFFSETS.back()
constexpr std::array ben_bot::eval::TERMS
constexpr Value ben_bot::eval::piece_values::PAWN { UINT16_C(100) }
constexpr Value ben_bot::eval::piece_values::KNIGHT { UINT16_C(320) }
constexpr Value ben_bot::eval::piece_values::BISHOP { UINT16_C(330) }
constexpr Value ben_bot::eval::piece_values::ROOK { UINT16_C(500) }
constexpr Value ben_bot::eval::piece_values::QUEEN { UINT16_C(900) }
constexpr Value ben_bot::eval::piece_values::KING { UINT16_C(10000) }
constexpr std::int32_t ben_bot::eval::phase_weights::KNIGHT { 1 }
constexpr std::int32_t ben_bot::eval::phase_weights::BISHOP { 1 }
constexpr std::int32_t ben_bot::eval::phase_weights::ROOK { 2 }
constexpr std::int32_t ben_bot::eval::phase_weights::QUEEN { 4 }

Detailed Description

Functions for evaluating positions.

Typedef Documentation

◆ Value

using ben_bot::eval::Value = std::int16_t

Signed integer type used to represent evaluation values.

Definition at line 38 of file Score.hpp.

Enumeration Type Documentation

◆ Method

enum class ben_bot::eval::Method : std::uint8_t
strong

How a term's weights enter the eval.

Enumerator
Linear 

The eval is the sum of coefficient times weight.

Definition at line 86 of file Weights.hpp.

◆ Term

enum class ben_bot::eval::Term : std::uint8_t
strong

Identifies a group of eval weights. The order of this enum must match the order of TERMS.

Enumerator
Material 

Pawn, knight, bishop, rook & queen values.

Pst 

Piece square tables: [piece type][square], from White's point of view.

PassedPawn 

Passed pawn bonus, indexed by squares from promoting minus 1.

PasserProtectedByPasser 

Bonus for each passed pawn defending a passed pawn.

PasserProtectedByPawn 

Bonus for each non-passed pawn defending a passed pawn.

IsolatedPawn 

Bonus (penalty) for each isolated pawn.

DoubledPawn 

Bonus (penalty) for each file with more than one of our pawns.

BackwardPawn 

Bonus (penalty) for each backward pawn.

PawnChain 

Bonus for each pawn defended by a friendly pawn (counted per defender).

RookBehindPasser 

Bonus for our rook behind our passer (penalty for their rook behind it).

KingEscortsPasser 

Bonus per square that our king is closer than 7 to our passer.

EnemyKingBlocksPasser 

Bonus (penalty) for the enemy king on our passer's promotion square.

RookHalfOpenFile 

Bonus for each rook on a file with an enemy pawn but none of ours.

RookOpenFile 

Bonus for each rook on an open file.

ConnectedRooksOpenFile 

Bonus for each open file with two of our rooks on it.

KingHalfOpenFile 

Bonus (penalty) for the king on a file with an enemy pawn but none of ours.

KingOpenLine 

Bonus (penalty) for the king on an open file or a pawnless diagonal.

KingStranded 

Bonus (penalty) for the king on the d/e file with no castling rights.

KnightTropism 

Bonus (penalty) per square that an enemy knight is closer than 6 to the king.

QueenTropism 

Bonus (penalty) per square that an enemy queen is closer than 7 to the king.

KnightMobility 

Knight mobility, indexed by the number of safe squares.

BishopMobility 

Bishop mobility, indexed by the number of safe squares.

RookMobility 

Rook mobility, indexed by the number of safe squares.

QueenMobility 

Queen mobility, indexed by the number of safe squares.

KingAreaControl 

Bonus per square around each king that we control more of than they do.

CenterControl 

Bonus per center square controlled.

Space 

Bonus per square behind our pawns that we control more of than they do.

PawnThreat 

Bonus for each enemy piece attacked by one of our pawns.

MinorOnRook 

Bonus for each enemy rook attacked by one of our knights or bishops.

MinorOnQueen 

Bonus for each enemy queen attacked by one of our knights or bishops.

RookOnQueen 

Bonus for each enemy queen attacked by one of our rooks.

Hanging 

Bonus for each hanging enemy piece or pawn.

PushThreat 

Bonus for each enemy piece that one of our pawns could attack with a safe push.

LastPiece 

Bonus for having a non-pawn piece left when the opponent has none.

MopUpEdge 

Mop-up bonus per square that the enemy king is from the center.

MopUpKingDistance 

Mop-up bonus per square that the kings are closer than 14.

Definition at line 44 of file Weights.hpp.

Function Documentation

◆ count_of()

auto ben_bot::eval::count_of ( const Term term) -> size_t
nodiscardconstexprnoexcept

Returns the number of weights that the given term owns.

Definition at line 198 of file Weights.hpp.

◆ evaluate()

auto ben_bot::eval::evaluate ( const Position & position,
PawnTable * pawnTable = nullptr ) -> Score
nodiscard

Returns a numerical score representing the evaluation of the give position from the perspective of the side to move. Higher numbers are better for the side to move.

If pawnTable isn't null, the pawn structure terms that only depend on the pawns are cached in it. The result is the same either way.

◆ evaluate_traced()

auto ben_bot::eval::evaluate_traced ( const Position & position) -> CoeffTracer
nodiscard

Evaluates the given position, and returns the trace of the eval. The traced eval never uses a pawn table.

See also
CoeffTracer

◆ get()

auto ben_bot::eval::piece_values::get ( PieceType type) -> Value
nodiscardconstexprnoexcept

Returns the value of the given piece type.

Definition at line 97 of file Material.hpp.

◆ offset_of()

auto ben_bot::eval::offset_of ( const Term term) -> size_t
nodiscardconstexprnoexcept

Returns the index of the given term's first weight in the flat weight array.

Definition at line 190 of file Weights.hpp.

◆ score_material()

auto ben_bot::eval::score_material ( const Position & position) -> Value
nodiscardconstexprnoexcept

Returns an aggregate score for the material on the board, relative to the side to move.

Definition at line 133 of file Material.hpp.

◆ see()

auto ben_bot::eval::see ( const Position & position,
Move move ) -> int
nodiscard

Static exchange evaluation (SEE).

Returns the net material change for the side to move if it plays the given move, and then both sides continue recapturing on the move's destination square with their least valuable attacker, each side being free to stop recapturing whenever continuing would lose material. A positive value means the side to move wins material; a negative value means it loses material.

This is computed using the swap algorithm described at https://www.chessprogramming.org/SEE_-_The_Swap_Algorithm, and includes X-ray attackers (sliding pieces hidden behind other attackers of the same square). Promotions (including recaptures that promote) are assumed to be to a queen unless the move itself specifies otherwise. Pins and checks are not considered, so the result is an approximation.

The move may also be a quiet move, in which case the result is zero if the moved piece can safely stand on its destination square, or negative if it can be won by the opponent. Castling moves always return zero.

Variable Documentation

◆ BISHOP [1/2]

std::int32_t ben_bot::eval::phase_weights::BISHOP { 1 }
inlineconstexpr

The phase weight of a bishop.

Definition at line 57 of file Trace.hpp.

◆ BISHOP [2/2]

Value ben_bot::eval::piece_values::BISHOP { UINT16_C(330) }
inlineconstexpr

The value of a bishop.

Definition at line 48 of file Material.hpp.

◆ DEFAULT_WEIGHTS

Weights ben_bot::eval::DEFAULT_WEIGHTS
inlineconstexpr

The default eval weights. The eval reads these as compile-time constants.

Definition at line 47 of file DefaultWeights.hpp.

◆ DRAW

Value ben_bot::eval::DRAW { UINT16_C(0) }
inlineconstexpr

A neutral, or draw, score.

Definition at line 59 of file Score.hpp.

◆ KING

Value ben_bot::eval::piece_values::KING { UINT16_C(10000) }
inlineconstexpr

The value of the king. This value isn't really used in counting material, it's just an arbitrarily large placeholder value.

Definition at line 60 of file Material.hpp.

◆ KNIGHT [1/2]

std::int32_t ben_bot::eval::phase_weights::KNIGHT { 1 }
inlineconstexpr

The phase weight of a knight.

Definition at line 54 of file Trace.hpp.

◆ KNIGHT [2/2]

Value ben_bot::eval::piece_values::KNIGHT { UINT16_C(320) }
inlineconstexpr

The value of a knight

Definition at line 45 of file Material.hpp.

◆ MATE

Value ben_bot::eval::MATE { MAX / UINT16_C(2) }
inlineconstexpr

The maximum possible evaluation score, i.e., if the side to move has mate-in-1. If the side to move is in checkmate, the evaluation is -MATE.

Definition at line 54 of file Score.hpp.

◆ MAX

Value ben_bot::eval::MAX { std::numeric_limits<Value>::max() - UINT16_C(5) }
inlineconstexpr

Arbitrary value used as the starting beta value for alpha/beta search. This should be larger than mate, but smaller than the data type's max (to avoid issues with sign flipping).

Definition at line 46 of file Score.hpp.

◆ MAX_PHASE

std::int32_t ben_bot::eval::MAX_PHASE = (phase_weights::QUEEN + (phase_weights::ROOK * 2) + (phase_weights::BISHOP * 2) + (phase_weights::KNIGHT * 2)) * 2
inlineconstexpr

The game phase of a pure middlegame. The eval blends its middlegame & endgame scores with a phase in [0, MAX_PHASE], which is the sum of the phase weights of the pieces on the board, capped at this value (which is the phase of the starting position), so that promotions don't push it past a pure middlegame.

Definition at line 77 of file Trace.hpp.

◆ NUM_TERMS

auto ben_bot::eval::NUM_TERMS = TERMS.size()
inlineconstexpr

The number of eval terms.

Definition at line 159 of file Weights.hpp.

◆ NUM_WEIGHTS

auto ben_bot::eval::NUM_WEIGHTS = detail::TERM_OFFSETS.back()
inlineconstexpr

The total number of eval weights.

Definition at line 185 of file Weights.hpp.

◆ PAWN

Value ben_bot::eval::piece_values::PAWN { UINT16_C(100) }
inlineconstexpr

The value of a pawn.

Definition at line 42 of file Material.hpp.

◆ QUEEN [1/2]

std::int32_t ben_bot::eval::phase_weights::QUEEN { 4 }
inlineconstexpr

The phase weight of a queen.

Definition at line 63 of file Trace.hpp.

◆ QUEEN [2/2]

Value ben_bot::eval::piece_values::QUEEN { UINT16_C(900) }
inlineconstexpr

The value of a queen.

Definition at line 54 of file Material.hpp.

◆ ROOK [1/2]

std::int32_t ben_bot::eval::phase_weights::ROOK { 2 }
inlineconstexpr

The phase weight of a rook.

Definition at line 60 of file Trace.hpp.

◆ ROOK [2/2]

Value ben_bot::eval::piece_values::ROOK { UINT16_C(500) }
inlineconstexpr

The value of a rook.

Definition at line 51 of file Material.hpp.

◆ TERMS

std::array ben_bot::eval::TERMS
inlineconstexpr

The registry of all eval terms, in the same order as the Term enum. The weights are stored in this order in one flat array.

Definition at line 117 of file Weights.hpp.