gmcts/README.md -rw-r--r-- 2.6 KiB
f7bd5ecabonbon move v2.0.0 changes to v2 folder 1 year, 4 months ago


#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:

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:

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

#How To Use

package pkg

import (

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

        //Add the searched tree into the mcts tree collection

        //Get the best action based off of the trees collected from mcts.AddTree()
        bestAction := mcts.BestAction()

        //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
for i := 0; i < concurrentTrees; i++ {
    go func(){
        tree := mcts.SpawnTree()
//Wait for the 4 trees to finish searching

bestAction := mcts.BestAction()

gameState, _ = gameState.ApplyAction(bestAction)


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 for this package can be found either at godoc.org or pkg.go.dev

#Bug Reports

Email me at bonbon@bonbon.moe :D