{"id":16759562,"url":"https://github.com/cyrildever/reinforcement-learning-in-golang","last_synced_at":"2025-07-21T07:01:50.887Z","repository":{"id":64302169,"uuid":"298753402","full_name":"cyrildever/reinforcement-learning-in-golang","owner":"cyrildever","description":"Code for the algorithms of the \"Reinforcement Learning\" book","archived":false,"fork":false,"pushed_at":"2024-10-13T11:10:57.000Z","size":35,"stargazers_count":9,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-10T17:34:38.685Z","etag":null,"topics":["golang","machine-learning","reinforcement-learning-algorithms"],"latest_commit_sha":null,"homepage":"","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/cyrildever.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-09-26T06:35:36.000Z","updated_at":"2025-01-01T02:28:03.000Z","dependencies_parsed_at":"2025-02-17T16:46:40.409Z","dependency_job_id":null,"html_url":"https://github.com/cyrildever/reinforcement-learning-in-golang","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/cyrildever/reinforcement-learning-in-golang","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cyrildever%2Freinforcement-learning-in-golang","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cyrildever%2Freinforcement-learning-in-golang/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cyrildever%2Freinforcement-learning-in-golang/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cyrildever%2Freinforcement-learning-in-golang/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cyrildever","download_url":"https://codeload.github.com/cyrildever/reinforcement-learning-in-golang/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cyrildever%2Freinforcement-learning-in-golang/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266255243,"owners_count":23900097,"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":["golang","machine-learning","reinforcement-learning-algorithms"],"created_at":"2024-10-13T04:08:27.868Z","updated_at":"2025-07-21T07:01:50.838Z","avatar_url":"https://github.com/cyrildever.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# reinforcement-learning-in-golang\n\n![GitHub tag (latest by date)](https://img.shields.io/github/v/tag/cyrildever/reinforcement-learning-in-golang)\n![GitHub last commit](https://img.shields.io/github/last-commit/cyrildever/reinforcement-learning-in-golang)\n![GitHub issues](https://img.shields.io/github/issues/cyrildever/reinforcement-learning-in-golang)\n![GitHub](https://img.shields.io/github/license/cyrildever/reinforcement-learning-in-golang)\n\nCode freely created in Go from the [\"Reinforcement Learning - An Introduction\"](https://mitpress.mit.edu/books/reinforcement-learning-second-edition) book by Richard S. Sutton and Andrew G. Barto.\n\n\n### Motivation\n\nAfter attacking [deep neural networks in Go](https://github.com/cyrildever/neural-networks-and-deep-learning-in-golang), I kept on investigating machine learning algorithms, this time with reinforcement learning. For that, I decided to make my own adaptation of Richard S. Sutton and Andrew G. Barto's reference book on the subject.\n\nThe objective here was to transform some of the described algorithms found throughout the book in Go programming language. But, rest assured, I had no intention whatsoever to make it some kind of a reference. It was just simple practice. So don't see it for more than it is: I'm not claiming it's the best production way to implement each or any of these algorithms. But just a way to have fun while reading the book (I strongly advise you to read it, BTW).\n\n_NB: I mentioned the reference to the book boxes in the code according to the second edition paging._\n\n\n### Usage\n\n```console\n$ git clone https://github.com/cyrildever/reinforcement-learning-in-golang.git \u0026\u0026 cd reinforcement-learning-in-golang \u0026\u0026 go build\n```\n\n```\nUsage of ./rl-algo:\n  -test string\n        The test to launch (eg. simple-bandit)\n```\n\n##### k-armed bandit\n\n```golang\nimport (\n    \"rl-algo/agent\"\n    \"rl-algo/model\"\n)\n\n// DEFINE ACTIONS\nactions := []model.Action{FIRST_ACTION, SECOND_ACTION, [...]}\n\n// IMPLEMENT bandit() FUNCTION\nbandit := func(a model.Action) (r model.Reward) {\n    // DO THE ACTION AND BUILD THE REWARD\n    return\n}\n\n// START THE AGENT\nagent.SimpleBandit(bandit, actions, .05)\n```\n\n##### Dynamic programming\n\n```golang\nimport (\n    \"rl-algo/dp\"\n    \"rl-algo/model\"\n)\n\n// DEFINE ALL STATES\nvar states = []model.State{[...]}\n\n// DETERMINE ACTIONS\nvar (\n    LEFT  = gridworldAction{-1, 0}\n    RIGHT = gridworldAction{1, 0}\n    UP    = gridworldAction{0, -1}\n    DOWN  = gridworldAction{0, 1}\n)\nactions := []model.Action{LEFT, RIGHT, UP, DOWN}\nrandomStateActions := make(model.StateActions, len(grid))\nfor _, s := range grid {\n    if !s.IsTerminal() {\n        randomStateActions[s] = actions\n    } else {\n        randomStateActions[s] = []model.Action{}\n    }\n}\n\n// DESCRIBE POLICY\npolicy := model.Policy{\n    StateActions: randomStateActions,\n    Gamma:   1,\n    Pi:      func(a model.Action, s model.State) float64 { return 0.25 },\n}\n\n// WRAP-UP IN A MODEL\nmdp := model.Model{\n    Policy: policy,\n    States:  states,\n    Probability: func(sPrime model.State, r model.Reward, s model.State, a model.Action) float64 {\n        return 1 / float64(len(actions))\n    },\n}\n\n// DO SOME DYNAMIC PROGRAMMING\nstateValue := dp.IterativePolicyEvaluation(mdp, 0.001)\n\n// TRANSFORM TO POLICY\nfunctions := make(map[model.Action]model.ActionFunc, len(actions))\nfor _, a := range actions {\n    functions[a] = a.ValueFunc()\n}\nnewStateActions, display := stateValue.ToPolicy(functions, states, 4)\nlog.Println(display)\n\n// UPDATE MODEL\nmdp.Policy.StateActions = newStateActions\n```\n\n\n### License\n\nThe code in Go is distributed under a [MIT license](LICENSE). \\\nPlease check Sutton et al. book for credits on the algorithms.\n\n\n\u003chr /\u003e\n\u0026copy; 2020-2024 Cyril Dever. All rights reserved.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcyrildever%2Freinforcement-learning-in-golang","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcyrildever%2Freinforcement-learning-in-golang","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcyrildever%2Freinforcement-learning-in-golang/lists"}