gomcts

package module
v0.0.0-...-35cc976 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2018 License: MIT Imports: 3 Imported by: 0

README

Build Status GoDoc Reference Go Report Card

Implementation of basic Monte Carlo Tree Search algorithm.

Installation

Install with

go get github.com/int8/gomcts
Usage

The central routine is MonteCarloTreeSearch(GameState, RolloutPolicy, int) which consumes GameState, RolloutPolicy and performs requested number of MCTS simulations

To use it for your perfect-information sum-zero strictly competitive two players game (board games such as go/chess/checkers/tictactoe) you need to provide implementation of GameState and Action interfaces

// Action - game action interface
type Action interface{
	ApplyTo(GameState) GameState
}

// GameState - state of the game interface
type GameState interface {
	EvaluateGame() (GameResult, bool)
	GetLegalActions() []Action
	IsGameEnded() bool
	NextToMove() int8
}

where GameResult is just float64 alias

type GameResult float64

As current implementation expects sum-zero two players game GameResult is supposed to reflect it. For the same reason NextToMove() is expected to return 1 or -1.

You can use DefaultRolloutPolicy (actions chosen randomly) or implement your own Rollout Policy as a function with the following signature:

func YourCustomRolloutPolicy(state GameState) Action {
	...
}
Examples
Tic-Tac-Toe

There is a built-in tic-tac-toe game implementation available through TicTacToeBoardGameAction and TicTacToeGameState types

To play with it go for something like:

package main 
import "github.com/int8/gomcts"

func main() {
	initialState := gomcts.CreateTicTacToeInitialGameState(3)
	chosenAction:= gomcts.MonteCarloTreeSearch(initialState, gomcts.DefaultRolloutPolicy, 100)
	// use chosenAction further
}   

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action interface {
	ApplyTo(GameState) GameState
}

Action - interface representing entity that can be applied to a game state (generating the next game state)

func DefaultRolloutPolicy

func DefaultRolloutPolicy(state GameState) Action

DefaultRolloutPolicy - default rollout policy, picks action randomly (w.r.t uniform random dist)

func MonteCarloTreeSearch

func MonteCarloTreeSearch(state GameState, rolloutPolicy RolloutPolicy, simulations int) Action

MonteCarloTreeSearch - function starting Monte Carlo Tree Search over provided GameState using RolloutPolicy of your choice, repeating simulation requested amount of time

type GameResult

type GameResult float64

GameResult - number representing a game result

type GameState

type GameState interface {
	EvaluateGame() (GameResult, bool)
	GetLegalActions() []Action
	IsGameEnded() bool
	NextToMove() int8
}

GameState - state of the game interface

type RolloutPolicy

type RolloutPolicy func(GameState) Action

RolloutPolicy - function signature determining the next action during Monte Carlo Tree Search rollout

type TicTacToeBoardGameAction

type TicTacToeBoardGameAction struct {
	// contains filtered or unexported fields
}

TicTacToeBoardGameAction - action on a tic tac toe board game

func (TicTacToeBoardGameAction) ApplyTo

ApplyTo - TicTacToeBoardGameAction implementation of ApplyTo method of Action interface

type TicTacToeGameState

type TicTacToeGameState struct {
	// contains filtered or unexported fields
}

TicTacToeGameState - tic tac toe game state

func CreateTicTacToeInitialGameState

func CreateTicTacToeInitialGameState(boardSize uint8) TicTacToeGameState

CreateTicTacToeInitialGameState - initializes tic tac toe game state

func (TicTacToeGameState) EvaluateGame

func (s TicTacToeGameState) EvaluateGame() (result GameResult, ended bool)

EvaluateGame - TicTacToeGameState implementation of EvaluateGame method of GameState interface

func (TicTacToeGameState) GetLegalActions

func (s TicTacToeGameState) GetLegalActions() []Action

GetLegalActions - TicTacToeGameState implementation of GetLegalActions method of GameState interface

func (TicTacToeGameState) IsGameEnded

func (s TicTacToeGameState) IsGameEnded() bool

IsGameEnded - TicTacToeGameState implementation of IsGameEnded method of GameState interface

func (TicTacToeGameState) NextToMove

func (s TicTacToeGameState) NextToMove() int8

NextToMove - TicTacToeGameState implementation of NextToMove method of GameState interface

Jump to

Keyboard shortcuts

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