engine

package
v0.0.0-...-ee20f16 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2018 License: BSD-3-Clause Imports: 5 Imported by: 6

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:

Search (engine.go) features implemented are:

Move ordering (move_ordering.go) consists of:

Evaluation (material.go) consists of

Index

Constants

View Source
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

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

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

Functions

func Phase

func Phase(pos *Position) int32

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

Types

type Accum

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

Accum accumulates scores.

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

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

func (eng *Engine) DoMove(move Move)

DoMove executes a move.

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

func (eng *Engine) Score() int32

Score evaluates current position from current player's POV.

func (*Engine) SetPosition

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

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

func (*Engine) UndoMove

func (eng *Engine) UndoMove()

UndoMove undoes the last move.

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 Evaluate

func Evaluate(pos *Position) Eval

Evaluate evaluates the position pos.

func (Eval) GetCentipawnsScore

func (e Eval) GetCentipawnsScore() int32

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

func NewHashTable(hashSizeMB int) *HashTable

NewHashTable builds transposition table that takes up to hashSizeMB megabytes.

func (*HashTable) Clear

func (ht *HashTable) Clear()

Clear removes all entries from hash.

func (*HashTable) Size

func (ht *HashTable) Size() int

Size returns the number of entries in the table.

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()

func (*NulLogger) CurrMove

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

func (*NulLogger) EndSearch

func (nl *NulLogger) EndSearch()

func (*NulLogger) PrintPV

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

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

func (s *Stats) CacheHitRatio() float32

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) Stop

func (tc *TimeControl) Stop()

Stop marks the search as stopped.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL