mcts

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2021 License: MIT Imports: 7 Imported by: 0

README

mcts

GitHub Workflow Status codecov Go Report Card PkgGoDev Release

Package mcts provides parallel monte-carlo tree search for your Go applications.

Installation

Go latest is recommended.

go get -u github.com/go-mcts/mcts

Getting started

See examples directory.

Implements mcts.Move and mcts.State:

// examples/nim/nim.go
type Move int

type State struct {
	playerToMove int
	chips        int
}

func (s *State) PlayerToMove() int {
	return s.playerToMove
}

func (s *State) HasMoves() bool {
	s.checkInvariant()
	return s.chips > 0
}

func (s *State) GetMoves() []mcts.Move {
	s.checkInvariant()

	var moves []mcts.Move
	for i := 1; i <= min(3, s.chips); i++ {
		moves = append(moves, i)
	}
	return moves
}

func (s *State) DoMove(move mcts.Move) {
	m := move.(int)
	if m < 1 || m > 3 {
		panic("illegal move")
	}
	s.checkInvariant()

	s.chips -= m
	s.playerToMove = 3 - s.playerToMove

	s.checkInvariant()
}

func (s *State) DoRandomMove(rd *rand.Rand) {
	if s.chips <= 0 {
		panic("invalid chips")
	}
	s.checkInvariant()

	max := min(3, s.chips)
	s.DoMove(rd.Intn(max) + 1)

	s.checkInvariant()
}

func (s *State) GetResult(currentPlayerToMove int) float64 {
	if s.chips != 0 {
		panic("game not over")
	}
	s.checkInvariant()

	if s.playerToMove == currentPlayerToMove {
		return 1.0
	}
	return 0.0
}

func (s *State) Clone() mcts.State {
	return &State{
		playerToMove: s.playerToMove,
		chips:        s.chips,
	}
}

Run mcts.ComputeMove:

state := &State{
	playerToMove: 1,
	chips:        chips,
}
move := mcts.ComputeMove(state, mcts.MaxIterations(100000), mcts.Verbose(true))

By default, runtime.NumCPU() goroutines are started for compute:

2021-10-16T16:13:33+08:00       DEBUG   100000 games played (493618.37 / second).
2021-10-16T16:13:33+08:00       DEBUG   100000 games played (487598.00 / second).
2021-10-16T16:13:33+08:00       DEBUG   100000 games played (474579.62 / second).
2021-10-16T16:13:33+08:00       DEBUG   100000 games played (470514.17 / second).
2021-10-16T16:13:33+08:00       DEBUG   Move: 2 (16% visits) (48% wins)
2021-10-16T16:13:33+08:00       DEBUG   Move: 1 (72% visits) (67% wins)
2021-10-16T16:13:33+08:00       DEBUG   Move: 3 (13% visits) (48% wins)
2021-10-16T16:13:33+08:00       DEBUG   Best: 1 (72% visits) (67% wins)
2021-10-16T16:13:33+08:00       DEBUG   400000 games played in 0.21 s. (1881366.23 / second, 4 parallel jobs).

License

This project is under the MIT License. See the LICENSE file for the full license text.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Move

type Move interface{}

Move must be implemented for different games

func ComputeMove

func ComputeMove(rootState State, opts ...Option) Move

ComputeMove start multi goroutines to compute best move

type Option

type Option func(*Options)

func Goroutines

func Goroutines(number int) Option

Goroutines number of goroutines, default is runtime.NumCPU()

func MaxIterations

func MaxIterations(iter int) Option

MaxIterations maximum number of iterations, default is 10000

iter < 0: not limit

func MaxTime

func MaxTime(d time.Duration) Option

MaxTime search timeout, default is not limit

type Options

type Options struct {
	Goroutines    int
	MaxIterations int
	MaxTime       time.Duration
}

Options use functional-option to customize mcts

type State

type State interface {
	// PlayerToMove is who next to play
	PlayerToMove() int
	// HasMoves return whether the game is over
	HasMoves() bool
	// GetMoves get all legal moves
	GetMoves() []Move
	// DoMove modify state with the given move
	DoMove(move Move)
	// DoRandomMove do random move with the given random engine
	DoRandomMove(rd *rand.Rand)
	// GetResult return game result
	GetResult(currentPlayerToMove int) float64
	// Clone is deep copy
	Clone() State
}

State must be implemented for different games

Directories

Path Synopsis
examples
nim
internal
log

Jump to

Keyboard shortcuts

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