zurichess: bitbucket.org/zurichess/zurichess/engine Index | Files

package engine

import "bitbucket.org/zurichess/zurichess/engine"

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


Package Files

asm_stubs.go engine.go features.go hash_table.go lib.go material.go misc.go move_ordering.go pawns.go pv.go score.go see.go time_control.go weights.go


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


var Weights = [...]Score{ /* 202 elements not displayed */


Weights stores the network parameters. Network has train error 0.05679009 and validation error 0.05702872.

func Phase Uses

func Phase(pos *Position) int32

Phase computes the progress of the game. 0 is opening, 256 is late end game.

type Accum Uses

type Accum struct {
    M, E int32 // mid game, end game

Accum accumulates scores.

type Engine Uses

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 Uses

func NewEngine(pos *Position, log Logger, options Options) *Engine

NewEngine creates a new engine to search for pos. If pos is nil then the start position is used.

func (*Engine) DoMove Uses

func (eng *Engine) DoMove(move Move)

DoMove executes a move.

func (*Engine) Play Uses

func (eng *Engine) Play(tc *TimeControl) (score int32, moves []Move)

Play evaluates current position. See PlayMoves for the returned values.

func (*Engine) PlayMoves Uses

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) Score Uses

func (eng *Engine) Score() int32

Score evaluates current position from current player's POV.

func (*Engine) SetPosition Uses

func (eng *Engine) SetPosition(pos *Position)

SetPosition sets current position. If pos is nil, the starting position is set.

func (*Engine) UndoMove Uses

func (eng *Engine) UndoMove()

UndoMove undoes the last move.

type Eval Uses

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 Evaluate Uses

func Evaluate(pos *Position) Eval

Evaluate evaluates the position pos.

func (Eval) GetCentipawnsScore Uses

func (e Eval) GetCentipawnsScore() int32

GetCentipawnsScore returns the current position evalution in centipawns.

type HashTable Uses

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 Uses

func NewHashTable(hashSizeMB int) *HashTable

NewHashTable builds transposition table that takes up to hashSizeMB megabytes.

func (*HashTable) Clear Uses

func (ht *HashTable) Clear()

Clear removes all entries from hash.

func (*HashTable) Size Uses

func (ht *HashTable) Size() int

Size returns the number of entries in the table.

type Logger Uses

type Logger interface {
    // BeginSearch signals a new search is started.
    // EndSearch signals end of search.
    // 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 Uses

type NulLogger struct{}

NulLogger is a logger that does nothing.

func (*NulLogger) BeginSearch Uses

func (nl *NulLogger) BeginSearch()

func (*NulLogger) CurrMove Uses

func (nl *NulLogger) CurrMove(depth int, move Move, num int)

func (*NulLogger) EndSearch Uses

func (nl *NulLogger) EndSearch()

func (*NulLogger) PrintPV Uses

func (nl *NulLogger) PrintPV(stats Stats, multiPV int, score int32, pv []Move)

type Options Uses

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 Uses

type Score struct {
    M, E int32 // mid game, end game

Score represents a pair of mid and end game scores.

type Stats Uses

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 Uses

func (s *Stats) CacheHitRatio() float32

CacheHitRatio returns the ratio of transposition table hits over total number of lookups.

type TimeControl Uses

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 Uses

func NewDeadlineTimeControl(pos *Position, deadline time.Duration) *TimeControl

NewDeadlineTimeControl returns a TimeControl corresponding to a single move before deadline.

func NewFixedDepthTimeControl Uses

func NewFixedDepthTimeControl(pos *Position, depth int32) *TimeControl

NewFixedDepthTimeControl returns a TimeControl which limits the search depth.

func NewTimeControl Uses

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 Uses

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 Uses

func (tc *TimeControl) PonderHit()

PonderHit switch to our time control.

func (*TimeControl) Start Uses

func (tc *TimeControl) Start(ponder bool)

Start starts the timer. Should start as soon as possible to set the correct time.

func (*TimeControl) Stop Uses

func (tc *TimeControl) Stop()

Stop marks the search as stopped.

func (*TimeControl) Stopped Uses

func (tc *TimeControl) Stopped() bool

Stopped returns true if the search has stopped because Stop() was called or the time has ran out.

Package engine imports 5 packages (graph) and is imported by 8 packages. Updated 2019-04-20. Refresh now. Tools for package owners.