~bonbon/gmcts

Monte-Carlo Tree Search Implementation in Golang. The g stands for whatever you want it to mean.
move v2.0.0 changes to v2 folder
separate math.Log and math.Sqrt calls
update README to reflect v2.0 changes

clone

read-only
https://git.sr.ht/~bonbon/gmcts
read/write
git@git.sr.ht:~bonbon/gmcts

You can also use your local clone with git send-email.

Documentation

GMCTS - Monte-Carlo Tree Search (the g stands for whatever you want it to mean :^) )

GMCTS is an implementation of the Monte-Carlo Tree Search algorithm with support for any deterministic game.

How To Install

This project requires Go 1.7+ to run. To install, use go get:

bash go get git.sr.ht/~bonbon/gmcts

Alternatively, you can clone it yourself into your $GOPATH/src/git.sr.ht/~bonbon/ folder to get the latest dev build:

bash git clone https://git.sr.ht/~bonbon/gmcts

How To Use

Version Imports

Version 1 package is available by importing "git.sr.ht/~bonbon/gmcts"

Version 2 package is available by importing "git.sr.ht/~bonbon/gmcts/v2"

Both packages use "gmcts" as the package name. The code shown in this README assumes usage of Version 2.

Example Code


package pkg

import (
    "git.sr.ht/~bonbon/gmcts/v2"
)

func NewGame() gmcts.Game {
    var game gmcts.Game
    //...
    //Setup a new game
    //...
    return game
}

func runGame() {
    gameState := NewGame()

    //MCTS algorithm will play against itself
    //until a terminal state has been reached
    for !gameState.IsTerminal() {
        mcts := gmcts.NewMCTS(gameState)

        //Spawn a new tree and play 1000 game simulations
        tree := mcts.SpawnTree()
        tree.SearchRounds(1000)

        //Add the searched tree into the mcts tree collection
        mcts.AddTree(tree)

        //Get the best action based off of the trees collected from mcts.AddTree()
        bestAction, err := mcts.BestAction()
        if err != nil {
            //...
            //handle error
            //...
        }

        //Update the game state using the tree's best action
        gameState, _ = gameState.ApplyAction(bestAction)
    }
}

If you choose to, you can run multiple trees concurrently.


concurrentTrees := 4

mcts := gmcts.NewMCTS(gameState)

//Run 4 trees concurrently
var wait sync.WaitGroup
wait.Add(concurrentTrees)
for i := 0; i < concurrentTrees; i++ {
    go func(){
        tree := mcts.SpawnTree()
        tree.SearchRounds(1000)
        mcts.AddTree(tree)
        wait.Done()
    }()
}
//Wait for the 4 trees to finish searching
wait.Wait()

bestAction, err := mcts.BestAction()
if err != nil {
    //...
    //handle error
    //...
}

gameState, _ = gameState.ApplyAction(bestAction)

Testing

You can test this package with go test. The test plays a game of tic-tac-toe against itself. The test should:

  1. Start the game by placing an x piece in the middle, and
  2. Finish in a draw.

If either of these fail, the test fails. It's a rather neat way to make sure everything works as intended!

Documentation

Documentation for this package can be found either at godoc.org or pkg.go.dev

Bug Reports

Email me at bonbon@bonbon.moe :D