{"id":16499187,"url":"https://github.com/hardbyte/sorting-gym","last_synced_at":"2025-07-23T04:34:12.297Z","repository":{"id":57469397,"uuid":"279834576","full_name":"hardbyte/sorting-gym","owner":"hardbyte","description":"OpenAI Gym environments for sorting with a \"Neural interface\" based on the paper \"Strong Generalization and Efficiency in Neural Programs\"","archived":false,"fork":false,"pushed_at":"2020-09-09T00:24:56.000Z","size":73,"stargazers_count":8,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-07-14T19:28:58.398Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hardbyte.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-07-15T10:11:16.000Z","updated_at":"2023-05-15T05:36:56.000Z","dependencies_parsed_at":"2022-09-19T10:11:14.679Z","dependency_job_id":null,"html_url":"https://github.com/hardbyte/sorting-gym","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/hardbyte/sorting-gym","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hardbyte%2Fsorting-gym","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hardbyte%2Fsorting-gym/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hardbyte%2Fsorting-gym/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hardbyte%2Fsorting-gym/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hardbyte","download_url":"https://codeload.github.com/hardbyte/sorting-gym/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hardbyte%2Fsorting-gym/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266618842,"owners_count":23957273,"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","status":"online","status_checked_at":"2025-07-23T02:00:09.312Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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-10-11T14:51:26.631Z","updated_at":"2025-07-23T04:34:12.270Z","avatar_url":"https://github.com/hardbyte.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Sorting Gym\n\nOpenAI Gym Environments for Sorting based on the 2020 paper\n[_Strong Generalization and Efficiency in Neural Programs_](https://arxiv.org/abs/2007.03629) by \n_Yujia Li, Felix Gimeno, Pushmeet Kohli, Oriol Vinyals_.\n\nThis repository includes implementations of the neural interface environments for sorting.\n\nInstall from pypi (recommended) with:\n```\npip install sorting-gym\n```\n\nImporting the Python package `sorting_gym` will expose the following Gym environments:\n\n- `SortTapeAlgorithmicEnv-v0` - Tape based environment based on [Gym's algorithmic environment](https://github.com/openai/gym/blob/master/gym/envs/algorithmic/algorithmic_env.py#L242))\n- `BasicNeuralSortInterfaceEnv-v0` - an interface where agents can implement simple algorithms such as bubble sort and insertion sort.\n- `FunctionalNeuralSortInterfaceEnv-v0` - extends the `BasicNeuralSortInterfaceEnv-v0` interface to include instructions for entering and exiting functions.\n\nTo define the parametric action space we introduce the `DiscreteParametric(Space)` type,\nallowing environments to describe disjoint output spaces, conditioned on a discrete parameter space.\nFor example:\n\n```python\nfrom gym.spaces import Discrete, Tuple, MultiBinary\nfrom sorting_gym import DiscreteParametric\naction_space = DiscreteParametric(2, ([Discrete(2), Tuple([Discrete(3), MultiBinary(3)])]))\naction_space.sample()\n(1, 2, array([0, 1, 0], dtype=int8))\naction_space.sample()\n(0, 1)\n```\n\nFor agents that don't support a parametric action space, we provide two wrappers (`BoxActionSpaceWrapper` and \n`MultiDiscreteActionSpaceWrapper`) that flatten the `DiscreteParametric` action space down to a `Box` and a \n`MultiDiscrete` respectively. \n\nIn the `sorting_gym.agents.scripted` module we implement the scripted agents from the paper directly using the \nunwrapped environment.\n\nRL Agents may want to consider supporting parametric/auto-regressive actions:\n- https://docs.ray.io/en/master/rllib-models.html#autoregressive-action-distributions\n- https://arxiv.org/abs/1502.03509\n\n\n### Goals:\n\n- [x] Implement bubblesort/insertion sort environment.\n- [x] Implement bubblesort/insertion sort agents as tests.\n- [x] Implement function environment.\n- [x] Implement quick sort scripted agent to test function environment.\n- [x] Wrap the environment to expose a box action space.\n- [x] Wrap the environment to expose a single MultiDiscrete action space.\n- [ ] Wrap the environment to expose a Parametric action space where each disjoint space is a\n      MultiDiscrete action space. WIP in `DisjointMultiDiscreteActionSpaceWrapper`\n- [ ] Include an example solution to train an agent via RL\n- [ ] Environment rendering (at least text based, optional dependency for rendering graphically with e.g. pygame)\n- [ ] Remove the open ai gym tape environment from base env (used to generate longer data as agent levels up)\n\n\n### Ideas to take it further:\n\n- Accelerate environment with cython (if required)\n- Open PR to `gym` for a discrete parametric space\n- Abstract out a Neural Controller Mixin/Environment Wrapper?\n- Consider a different/enhanced instruction set. \n  Instead of always comparing every pointer and data element in the view (and neighbours), \n  have explicit comparison instructions. Could extend to other math instructions, including\n  accounting for variable cost of the instructions.\n- Instead of passing previous arguments, consider passing in the number of instructions\n  executed in the current scope as a cheap program counter.\n\n\n## Run test with pytest\n\n```\npytest\n```\n\n## Building/Packaging\n\n```\npoetry update\npoetry version patch\npoetry lock\npoetry build\npoetry publish\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhardbyte%2Fsorting-gym","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhardbyte%2Fsorting-gym","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhardbyte%2Fsorting-gym/lists"}