Documentation ¶
Overview ¶
Package engine implements board, move generation and position searching.
The package can be used as a general library for chess tool writing and provides the core functionality for the zurichess chess engine.
Position (basic.go, position.go) uses:
- Bitboards for representation - https://chessprogramming.wikispaces.com/Bitboards
- Magic bitboards for sliding move generation - https://chessprogramming.wikispaces.com/Magic+Bitboards
Search (engine.go) features implemented are:
- Aspiration window - https://chessprogramming.wikispaces.com/Aspiration+Windows
- Check extension - https://chessprogramming.wikispaces.com/Check+Extensions
- Fail soft - https://chessprogramming.wikispaces.com/Fail-Soft
- Futility Pruning - https://chessprogramming.wikispaces.com/Futility+pruning
- History leaf pruning - https://chessprogramming.wikispaces.com/History+Leaf+Pruning
- Killer move heuristic - https://chessprogramming.wikispaces.com/Killer+Heuristic
- Late move redution (LMR) - https://chessprogramming.wikispaces.com/Late+Move+Reductions
- Mate distance pruning - https://chessprogramming.wikispaces.com/Mate+Distance+Pruning
- Negamax framework - http://chessprogramming.wikispaces.com/Alpha-Beta#Implementation-Negamax%20Framework
- Null move prunning (NMP) - https://chessprogramming.wikispaces.com/Null+Move+Pruning
- Principal variation search (PVS) - https://chessprogramming.wikispaces.com/Principal+Variation+Search
- Quiescence search - https://chessprogramming.wikispaces.com/Quiescence+Search
- Razoring - https://chessprogramming.wikispaces.com/Razoring
- Static Single Evaluation - https://chessprogramming.wikispaces.com/Static+Exchange+Evaluation
- Zobrist hashing - https://chessprogramming.wikispaces.com/Zobrist+Hashing
Move ordering (move_ordering.go) consists of:
- Hash move heuristic
- Captures sorted by MVVLVA - https://chessprogramming.wikispaces.com/MVV-LVA
- Killer moves - https://chessprogramming.wikispaces.com/Killer+Move
- History Heuristic - https://chessprogramming.wikispaces.com/History+Heuristic
- Countermove Heuristic - https://chessprogramming.wikispaces.com/Countermove+Heuristic
Evaluation (material.go) consists of
- Material and mobility.
- Piece square tables.
- King pawn shield - https://chessprogramming.wikispaces.com/King+Safety
- King safery ala Toga style - https://chessprogramming.wikispaces.com/King+Safety#Attacking%20King%20Zone
- Pawn structure: connected, isolated, double, passed, rammed. Evaluation is cached (see cache.go).
- Attacks on minors and majors.
- Rooks on open and semiopenfiles - https://chessprogramming.wikispaces.com/Rook+on+Open+File
- Tapered evaluation - https://chessprogramming.wikispaces.com/Tapered+Eval
Index ¶
- Constants
- Variables
- func Phase(pos *Position) int32
- type Accum
- type Engine
- func (eng *Engine) DoMove(move Move)
- func (eng *Engine) Play(tc *TimeControl) (score int32, moves []Move)
- func (eng *Engine) PlayMoves(tc *TimeControl, rootMoves []Move) (score int32, moves []Move)
- func (eng *Engine) Score() int32
- func (eng *Engine) SetPosition(pos *Position)
- func (eng *Engine) UndoMove()
- type Eval
- type HashTable
- type Logger
- type NulLogger
- type Options
- type Score
- type Stats
- type TimeControl
Constants ¶
const ( // KnownWinScore is strictly greater than all evaluation scores (mate not included). KnownWinScore = 25000 // KnownLossScore is strictly smaller than all evaluation scores (mated not included). KnownLossScore = -KnownWinScore // MateScore - N is mate in N plies. MateScore = 30000 // MatedScore + N is mated in N plies. MatedScore = -MateScore // InfinityScore is possible score. -InfinityScore is the minimum possible score. InfinityScore = 32000 )
Variables ¶
var Weights = [...]Score{}/* 202 elements not displayed */
Weights stores the network parameters. Network has train error 0.05679009 and validation error 0.05702872.
Functions ¶
Types ¶
type Engine ¶
type Engine struct { Options Options // engine options Log Logger // logger Stats Stats // search statistics Position *Position // current Position // contains filtered or unexported fields }
Engine implements the logic to search for the best move for a position.
func NewEngine ¶
NewEngine creates a new engine to search for pos. If pos is nil then the start position is used.
func (*Engine) Play ¶
func (eng *Engine) Play(tc *TimeControl) (score int32, moves []Move)
Play evaluates current position. See PlayMoves for the returned values.
func (*Engine) PlayMoves ¶
func (eng *Engine) PlayMoves(tc *TimeControl, rootMoves []Move) (score int32, moves []Move)
PlayMoves evaluates current position searching only moves specifid by rootMoves.
Returns the principal variation, that is
moves[0] is the best move found and moves[1] is the pondering move.
If rootMoves is nil searches all root moves.
Returns a nil pv if no move was found because the game is already finished. Returns empty pv array if it's valid position, but no pv was found (e.g. search depth is 0).
Time control, tc, should already be started.
func (*Engine) SetPosition ¶
func (eng *Engine) SetPosition(pos *Position)
SetPosition sets current position. If pos is nil, the starting position is set.
type Eval ¶
type Eval struct { // The scores. // - Accum[NoColor] is the combined score // - Accum[White] is White's score // - Accum[Black] is Black's score Accum [ColorArraySize]Accum // contains filtered or unexported fields }
Eval contains necessary information for evaluation.
func (Eval) GetCentipawnsScore ¶
GetCentipawnsScore returns the current position evalution in centipawns.
type HashTable ¶
type HashTable struct {
// contains filtered or unexported fields
}
HashTable is a transposition table. Engine uses this table to cache position scores so it doesn't have to research them again.
var ( // DefaultHashTableSizeMB is the default size in MB. DefaultHashTableSizeMB = 64 // GlobalHashTable is the global transposition table. GlobalHashTable *HashTable )
func NewHashTable ¶
NewHashTable builds transposition table that takes up to hashSizeMB megabytes.
type Logger ¶
type Logger interface { // BeginSearch signals a new search is started. BeginSearch() // EndSearch signals end of search. EndSearch() // PrintPV logs the principal variation after iterative deepening completed one depth. PrintPV(stats Stats, multiPV int, score int32, pv []Move) // CurrMove logs the current move. Current move index is 1-based. CurrMove(depth int, move Move, num int) }
Logger logs search progress.
type NulLogger ¶
type NulLogger struct{}
NulLogger is a logger that does nothing.
func (*NulLogger) BeginSearch ¶
func (nl *NulLogger) BeginSearch()
type Options ¶
type Options struct { AnalyseMode bool // true to display info strings MultiPV int // number of principal variation lines to compute HandicapLevel int }
Options keeps engine's options.
type Score ¶
type Score struct {
M, E int32 // mid game, end game
}
Score represents a pair of mid and end game scores.
type Stats ¶
type Stats struct { CacheHit uint64 // number of times the position was found transposition table CacheMiss uint64 // number of times the position was not found in the transposition table Nodes uint64 // number of nodes searched Depth int32 // depth search SelDepth int32 // maximum depth reached on PV (doesn't include the hash moves) }
Stats stores statistics about the search.
func (*Stats) CacheHitRatio ¶
CacheHitRatio returns the ratio of transposition table hits over total number of lookups.
type TimeControl ¶
type TimeControl struct {
WTime, WInc time.Duration // time and increment for white.
BTime, BInc time.Duration // time and increment for black
Depth int32 // maximum depth search (including)
MovesToGo int32 // number of remaining moves, defaults to defaultMovesToGo
// contains filtered or unexported fields
}
TimeControl is a time control that tries to split the remaining time over MovesToGo.
func NewDeadlineTimeControl ¶
func NewDeadlineTimeControl(pos *Position, deadline time.Duration) *TimeControl
NewDeadlineTimeControl returns a TimeControl corresponding to a single move before deadline.
func NewFixedDepthTimeControl ¶
func NewFixedDepthTimeControl(pos *Position, depth int32) *TimeControl
NewFixedDepthTimeControl returns a TimeControl which limits the search depth.
func NewTimeControl ¶
func NewTimeControl(pos *Position, predicted bool) *TimeControl
NewTimeControl returns a new time control with no time limit, no depth limit, zero time increment and zero moves to go.
func (*TimeControl) NextDepth ¶
func (tc *TimeControl) NextDepth(depth int32) bool
NextDepth returns true if search can start at depth. In any case Stopped() will return false.
func (*TimeControl) PonderHit ¶
func (tc *TimeControl) PonderHit()
PonderHit switch to our time control.
func (*TimeControl) Start ¶
func (tc *TimeControl) Start(ponder bool)
Start starts the timer. Should start as soon as possible to set the correct time.
func (*TimeControl) Stopped ¶
func (tc *TimeControl) Stopped() bool
Stopped returns true if the search has stopped because Stop() was called or the time has ran out.