{"id":19389010,"url":"https://github.com/gorgonia/agogo","last_synced_at":"2025-08-01T15:36:27.599Z","repository":{"id":40625786,"uuid":"150810453","full_name":"gorgonia/agogo","owner":"gorgonia","description":"A reimplementation of AlphaGo in Go (specifically AlphaZero)","archived":false,"fork":false,"pushed_at":"2021-02-14T20:12:22.000Z","size":239,"stargazers_count":216,"open_issues_count":7,"forks_count":21,"subscribers_count":18,"default_branch":"master","last_synced_at":"2025-04-23T23:39:19.668Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gorgonia.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-09-29T01:18:17.000Z","updated_at":"2024-10-18T20:24:25.000Z","dependencies_parsed_at":"2022-09-20T12:43:24.452Z","dependency_job_id":null,"html_url":"https://github.com/gorgonia/agogo","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/gorgonia/agogo","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gorgonia%2Fagogo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gorgonia%2Fagogo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gorgonia%2Fagogo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gorgonia%2Fagogo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gorgonia","download_url":"https://codeload.github.com/gorgonia/agogo/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gorgonia%2Fagogo/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265500128,"owners_count":23777412,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-10T10:14:31.978Z","updated_at":"2025-07-16T09:34:41.908Z","avatar_url":"https://github.com/gorgonia.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"# agogo\n\nA reimplementation of AlphaGo in Go (specifically AlphaZero)\n\n## About\n\nThe algorithm is composed of:\n\n- a Monte-Carlo Tree Search (MCTS) implemented in the [`mcts`](https://pkg.go.dev/github.com/gorgonia/agogo/mcts) package;\n- a Dual Neural Network (DNN) implemented in the [`dualnet`](https://pkg.go.dev/github.com/gorgonia/agogo/dualnet) package.\n\nThe algorithm is wrapped into a top-level structure ([`AZ`](https://pkg.go.dev/github.com/gorgonia/agogo#AZ) for AlphaZero). The algorithm applies to any game able to fulfill a specified contract.\n\nThe contract specifies the description of a game state.\n\nIn this package, the contract is a Go interface declared in the `game` package: [`State`](https://pkg.go.dev/github.com/gorgonia/agogo/game#State).\n\n### Description of some concepts/ubiquitous language\n\n- In the `agogo` package, each player of the game is an [`Agent`](https://pkg.go.dev/github.com/gorgonia/agogo#Agent), and in a `game`, two `Agents` are playing in an [`Arena`](https://pkg.go.dev/github.com/gorgonia/agogo@v0.1.0#Arena)\n\n- The `game` package is loosely coupled with the AlphaZero algorithm and describes a game's behavior (and not what a game is). The behavior is expressed as a set of functions to operate on a [`State`](https://pkg.go.dev/github.com/gorgonia/agogo/game#State) of the game. A State is an interface that represents the current game state *as well* as the allowed interactions. The interaction is made by an object [`Player`](https://pkg.go.dev/github.com/gorgonia/agogo/game#Player) who is operating a [`PlayerMove`](https://pkg.go.dev/github.com/gorgonia/agogo/game#PlayerMove). The implementer's responsibility is to code the game's rules by creating an object that fulfills the State contract and implements the allowed moves.\n\n### Training process\n\n### Applying the Algo on a game\n\nThis package is designed to be extensible. Therefore you can train AlphaZero on any board game respecting the contract of the `game` package.\nThen, the model can be saved and used as a player.\n\nThe steps to train the algorithm are:\n\n- Creating a structure that is fulfilling the [`State`](https://pkg.go.dev/github.com/gorgonia/agogo/game#State) interface (aka a _game_).\n- Creating a _configuration_ for your AZ internal MCTS and NN.\n- Creating an `AZ` structure based on the _game_ and  the _configuration_\n- Executing the learning process (by calling the [`Learn`](https://pkg.go.dev/github.com/gorgonia/agogo#AZ.Learn) method)\n- Saving the trained model (by calling the [`Save`](https://pkg.go.dev/github.com/gorgonia/agogo#AZ.Save) method)\n\nThe steps to play against the algorithm are:\n\n- Creating an `AZ` object\n- Loading the trained model (by calling the [`Read`](https://pkg.go.dev/github.com/gorgonia/agogo#AZ.Read) method)\n- Switching the agent to inference mode via the [`SwitchToInference`](https://pkg.go.dev/github.com/gorgonia/agogo#Agent.SwitchToInference) method\n- Get the AI move by calling the [`Search`](https://pkg.go.dev/github.com/gorgonia/agogo#Agent.Search) method and applying the move to the game manually\n\n## Examples\n\nFour board games are implemented so far. Each of them is defined as a subpackage of `game`:\n\n- [`mnk`](https://pkg.go.dev/github.com/gorgonia/agogo/game/mnk) for [m,n,k](https://en.wikipedia.org/wiki/M,n,k-game) game.\n- [`wq`](https://pkg.go.dev/github.com/gorgonia/agogo/game/mnk) is the game of [Go](https://en.wikipedia.org/wiki/Go_(game)) (围碁)\n- `c4`\n- `komi`\n\n### tic-tac-toe\n\nTic-tac-toe is a m,n,k game where m=n=k=3.\n\n#### Training\n\nHere is a sample code that trains AlphaGo to play the game. The result is saved in a file `example.model`\n\n```go\n// encodeBoard is a GameEncoder (https://pkg.go.dev/github.com/gorgonia/agogo#GameEncoder) for the tic-tac-toe\nfunc encodeBoard(a game.State) []float32 {\n     board := agogo.EncodeTwoPlayerBoard(a.Board(), nil)\n     for i := range board {\n     if board[i] == 0 {\n          board[i] = 0.001\n     }\n     }\n     playerLayer := make([]float32, len(a.Board()))\n     next := a.ToMove()\n     if next == game.Player(game.Black) {\n     for i := range playerLayer {\n          playerLayer[i] = 1\n     }\n     } else if next == game.Player(game.White) {\n     // vecf32.Scale(board, -1)\n     for i := range playerLayer {\n          playerLayer[i] = -1\n     }\n     }\n     retVal := append(board, playerLayer...)\n     return retVal\n}\n\nfunc main() {\n    // Create the configuration of the neural network\n     conf := agogo.Config{\n         Name:            \"Tic Tac Toe\",\n         NNConf:          dual.DefaultConf(3, 3, 10),\n         MCTSConf:        mcts.DefaultConfig(3),\n         UpdateThreshold: 0.52,\n     }\n     conf.NNConf.BatchSize = 100\n     conf.NNConf.Features = 2 // write a better encoding of the board, and increase features (and that allows you to increase K as well)\n     conf.NNConf.K = 3\n     conf.NNConf.SharedLayers = 3\n     conf.MCTSConf = mcts.Config{\n         PUCT:           1.0,\n         M:              3,\n         N:              3,\n         Timeout:        100 * time.Millisecond,\n         PassPreference: mcts.DontPreferPass,\n         Budget:         1000,\n         DumbPass:       true,\n         RandomCount:    0,\n     }\n\n     conf.Encoder = encodeBoard\n\n    // Create a new game\n    g := mnk.TicTacToe()\n    // Create the AlphaZero structure \n    a := agogo.New(g, conf)\n    // Launch the learning process\n    err := a.Learn(5, 50, 100, 100) // 5 epochs, 50 episode, 100 NN iters, 100 games.\n    if err != nil {\n        log.Println(err)\n    }\n    // Save the model\n     a.Save(\"example.model\")\n}\n```\n\n#### Inference\n\n```go\nfunc encodeBoard(a game.State) []float32 {\n    board := agogo.EncodeTwoPlayerBoard(a.Board(), nil)\n    for i := range board {\n        if board[i] == 0 {\n            board[i] = 0.001\n        }\n    }\n    playerLayer := make([]float32, len(a.Board()))\n    next := a.ToMove()\n    if next == game.Player(game.Black) {\n        for i := range playerLayer {\n            playerLayer[i] = 1\n        }\n    } else if next == game.Player(game.White) {\n        // vecf32.Scale(board, -1)\n        for i := range playerLayer {\n            playerLayer[i] = -1\n        }\n    }\n    retVal := append(board, playerLayer...)\n    return retVal\n}\n\nfunc main() {\n    conf := agogo.Config{\n        Name:     \"Tic Tac Toe\",\n        NNConf:   dual.DefaultConf(3, 3, 10),\n        MCTSConf: mcts.DefaultConfig(3),\n    }\n    conf.Encoder = encodeBoard\n\n    g := mnk.TicTacToe()\n    a := agogo.New(g, conf)\n    a.Load(\"example.model\")\n    a.A.Player = mnk.Cross\n    a.B.Player = mnk.Nought\n    a.B.SwitchToInference(g)\n    a.A.SwitchToInference(g)\n    // Put x int the center\n    stateAfterFirstPlay := g.Apply(game.PlayerMove{\n        Player: mnk.Cross,\n        Single: 4,\n    })\n    fmt.Println(stateAfterFirstPlay)\n    // ⎢ · · · ⎥\n    // ⎢ · X · ⎥\n    // ⎢ · · · ⎥\n\n    // What to do next\n    move := a.B.Search(stateAfterFirstPlay)\n    fmt.Println(move)\n    // 1\n    g.Apply(game.PlayerMove{\n        Player: mnk.Nought,\n        Single: move,\n    })\n    fmt.Println(stateAfterFirstPlay)\n    // ⎢ · O · ⎥\n    // ⎢ · X · ⎥\n    // ⎢ · · · ⎥\n}\n```\n\n## Misc\n\n[A Funny Thing Happened On The Way To Reimplementing AlphaGo](https://www.youtube.com/watch?v=nk87zsxpF1A) -  A talk by @chewxy (one of the authors) about this specific implementation\n\n## Credits\n\nOriginal implementation credits to\n\n- [@cfgt](https://github.com/cfgt)\n- [@garethseneque](https://twitter.com/garethseneque)\n- [@ynqa](https://github.com/ynqa)\n- [@chewxy](https://github.com/chewxy)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgorgonia%2Fagogo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgorgonia%2Fagogo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgorgonia%2Fagogo/lists"}