├── .dockerignore ├── MANIFEST.in ├── benchmark ├── .gitignore ├── requirements.txt ├── numa_test.sh ├── test_gym.py └── test_dmc.py ├── examples ├── cleanrl_examples │ └── .gitignore ├── acme_examples │ ├── requirements.txt │ └── README.md ├── list_all_envs.py └── make_env.py ├── docs ├── requirements.txt ├── _static │ └── images │ │ ├── async.png │ │ ├── favicon.png │ │ ├── envpool-logo.png │ │ └── throughput │ │ ├── throughput.png │ │ ├── Atari_Laptop.png │ │ ├── Atari_TPU-VM.png │ │ ├── Mujoco_Laptop.png │ │ ├── Mujoco_TPU-VM.png │ │ ├── Atari_DGX-A100.png │ │ ├── Mujoco_DGX-A100.png │ │ ├── Atari_Workstation.png │ │ └── Mujoco_Workstation.png ├── content │ ├── slides.rst │ └── contributing.rst ├── env │ ├── minigrid.rst │ └── classic_control.rst ├── Makefile └── spelling_wordlist.txt ├── third_party ├── pip_requirements │ ├── .gitignore │ ├── requirements-release.txt │ ├── requirements-dev.txt │ └── BUILD ├── freedoom │ ├── freedoom.BUILD │ └── BUILD ├── gym3_libenv │ ├── gym3_libenv.BUILD │ └── BUILD ├── mujoco_gym_xml │ ├── mujoco_gym_xml.BUILD │ └── BUILD ├── threadpool │ ├── threadpool.BUILD │ └── BUILD ├── glibc_version_header │ ├── glibc_version_header.BUILD │ └── BUILD ├── mujoco_dmc_xml │ ├── mujoco_dmc_xml.BUILD │ └── BUILD ├── vizdoom_extra_maps │ ├── vizdoom_extra_maps.BUILD │ └── BUILD ├── pugixml │ ├── pugixml.BUILD │ └── BUILD ├── pretrain_weight │ ├── pretrain_weight.BUILD │ └── BUILD ├── vizdoom │ ├── sdl_thread.patch │ └── BUILD ├── concurrentqueue │ ├── concurrentqueue.BUILD │ └── BUILD ├── atari_roms │ ├── atari_roms.BUILD │ └── BUILD ├── mujoco │ ├── mujoco.BUILD │ └── BUILD ├── BUILD ├── ale │ ├── BUILD │ └── ale.BUILD ├── box2d │ ├── BUILD │ └── box2d.BUILD ├── cuda │ ├── BUILD │ └── cuda.bzl ├── jpeg │ └── BUILD ├── nasm │ └── BUILD ├── sdl2 │ ├── BUILD │ └── sdl2.BUILD ├── zlib │ ├── BUILD │ └── zlib.BUILD ├── opencv │ ├── BUILD │ └── opencv.BUILD ├── procgen │ ├── BUILD │ └── procgen.BUILD ├── vizdoom_lib │ ├── BUILD │ └── vizdoom_lib.BUILD └── common.bzl ├── CPPLINT.cfg ├── .clang-format ├── CONTRIBUTING.md ├── .github ├── workflows │ ├── test.yml │ ├── lint.yml │ └── release.yml ├── ISSUE_TEMPLATE │ ├── feature_request.md │ └── bug_report.md └── PULL_REQUEST_TEMPLATE.md ├── BUILD ├── .readthedocs.yaml ├── envpool ├── mujoco │ ├── __init__.py │ ├── gym │ │ ├── mujoco_gym_envpool_test.cc │ │ ├── registration.py │ │ ├── mujoco_gym_deterministic_test.py │ │ └── mujoco_env.h │ └── dmc │ │ ├── utils.h │ │ ├── registration.py │ │ └── mujoco_dmc_suite_ext_deterministic_test.py ├── python │ ├── __init__.py │ ├── utils.py │ ├── api.py │ └── lax.py ├── atari │ ├── atari_envpool.cc │ ├── __init__.py │ └── registration.py ├── minigrid │ ├── minigrid.cc │ ├── __init__.py │ ├── impl │ │ ├── minigrid_empty_env.h │ │ ├── minigrid_env.h │ │ └── minigrid_empty_env.cc │ ├── minigrid_deterministic_test.py │ ├── BUILD │ └── registration.py ├── procgen │ ├── procgen_envpool.cc │ ├── __init__.py │ ├── registration.py │ ├── BUILD │ └── procgen_env_test.cc ├── vizdoom │ ├── vizdoom_envpool.cc │ ├── __init__.py │ ├── bin │ │ └── BUILD │ └── registration.py ├── dummy │ ├── __init__.py │ ├── dummy_envpool.cc │ └── BUILD ├── utils │ ├── BUILD │ ├── image_process.h │ └── image_process_test.cc ├── __init__.py ├── core │ ├── circular_buffer_test.cc │ ├── envpool.h │ ├── type_utils.h │ ├── circular_buffer.h │ ├── dict_test.cc │ ├── action_buffer_queue_test.cc │ ├── tuple_utils.h │ └── action_buffer_queue.h ├── workspace1.bzl ├── pip.bzl ├── box2d │ ├── utils.h │ ├── utils.cc │ ├── box2d_envpool.cc │ ├── registration.py │ ├── box2d_deterministic_test.py │ ├── __init__.py │ ├── lunar_lander_discrete.h │ ├── lunar_lander_continuous.h │ └── BUILD ├── entry.py ├── toy_text │ ├── BUILD │ ├── toy_text.cc │ ├── __init__.py │ ├── registration.py │ └── nchain.h ├── classic_control │ ├── classic_control.cc │ ├── BUILD │ ├── __init__.py │ └── registration.py └── BUILD ├── WORKSPACE ├── setup.py ├── .bazelrc ├── docker ├── dev.dockerfile └── dev-cn.dockerfile ├── setup.cfg ├── .clang-tidy └── .gitignore /.dockerignore: -------------------------------------------------------------------------------- 1 | .gitignore -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include LICENSE 2 | -------------------------------------------------------------------------------- /benchmark/.gitignore: -------------------------------------------------------------------------------- 1 | *.pdf 2 | *.png 3 | -------------------------------------------------------------------------------- /examples/cleanrl_examples/.gitignore: -------------------------------------------------------------------------------- 1 | runs 2 | -------------------------------------------------------------------------------- /docs/requirements.txt: -------------------------------------------------------------------------------- 1 | sphinx 2 | sphinx_rtd_theme 3 | -------------------------------------------------------------------------------- /third_party/pip_requirements/.gitignore: -------------------------------------------------------------------------------- 1 | requirements.txt 2 | -------------------------------------------------------------------------------- /CPPLINT.cfg: -------------------------------------------------------------------------------- 1 | filter=-build/c++11,+build/c++17,-build/include_subdir 2 | -------------------------------------------------------------------------------- /.clang-format: -------------------------------------------------------------------------------- 1 | BasedOnStyle: Google 2 | DerivePointerAlignment: false 3 | PointerAlignment: Left 4 | -------------------------------------------------------------------------------- /docs/_static/images/async.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sail-sg/envpool/HEAD/docs/_static/images/async.png -------------------------------------------------------------------------------- /docs/_static/images/favicon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sail-sg/envpool/HEAD/docs/_static/images/favicon.png -------------------------------------------------------------------------------- /docs/_static/images/envpool-logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sail-sg/envpool/HEAD/docs/_static/images/envpool-logo.png -------------------------------------------------------------------------------- /examples/acme_examples/requirements.txt: -------------------------------------------------------------------------------- 1 | jax==0.3.6 2 | jaxlib==0.3.5 3 | libtpu-nightly==0.1.dev20220412 4 | wandb==0.12.17 5 | -------------------------------------------------------------------------------- /docs/_static/images/throughput/throughput.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sail-sg/envpool/HEAD/docs/_static/images/throughput/throughput.png -------------------------------------------------------------------------------- /docs/_static/images/throughput/Atari_Laptop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sail-sg/envpool/HEAD/docs/_static/images/throughput/Atari_Laptop.png -------------------------------------------------------------------------------- /docs/_static/images/throughput/Atari_TPU-VM.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sail-sg/envpool/HEAD/docs/_static/images/throughput/Atari_TPU-VM.png -------------------------------------------------------------------------------- /docs/_static/images/throughput/Mujoco_Laptop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sail-sg/envpool/HEAD/docs/_static/images/throughput/Mujoco_Laptop.png -------------------------------------------------------------------------------- /docs/_static/images/throughput/Mujoco_TPU-VM.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sail-sg/envpool/HEAD/docs/_static/images/throughput/Mujoco_TPU-VM.png -------------------------------------------------------------------------------- /docs/_static/images/throughput/Atari_DGX-A100.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sail-sg/envpool/HEAD/docs/_static/images/throughput/Atari_DGX-A100.png -------------------------------------------------------------------------------- /docs/_static/images/throughput/Mujoco_DGX-A100.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sail-sg/envpool/HEAD/docs/_static/images/throughput/Mujoco_DGX-A100.png -------------------------------------------------------------------------------- /docs/_static/images/throughput/Atari_Workstation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sail-sg/envpool/HEAD/docs/_static/images/throughput/Atari_Workstation.png -------------------------------------------------------------------------------- /docs/_static/images/throughput/Mujoco_Workstation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sail-sg/envpool/HEAD/docs/_static/images/throughput/Mujoco_Workstation.png -------------------------------------------------------------------------------- /third_party/freedoom/freedoom.BUILD: -------------------------------------------------------------------------------- 1 | filegroup( 2 | name = "freedoom", 3 | srcs = ["freedoom2.wad"], 4 | visibility = ["//visibility:public"], 5 | ) 6 | -------------------------------------------------------------------------------- /third_party/gym3_libenv/gym3_libenv.BUILD: -------------------------------------------------------------------------------- 1 | cc_library( 2 | name = "gym3_libenv_header", 3 | srcs = ["libenv.h"], 4 | visibility = ["//visibility:public"], 5 | ) 6 | -------------------------------------------------------------------------------- /third_party/mujoco_gym_xml/mujoco_gym_xml.BUILD: -------------------------------------------------------------------------------- 1 | filegroup( 2 | name = "mujoco_gym_xml", 3 | srcs = glob(["assets/*.xml"]), 4 | visibility = ["//visibility:public"], 5 | ) 6 | -------------------------------------------------------------------------------- /third_party/threadpool/threadpool.BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | cc_library( 4 | name = "threadpool", 5 | hdrs = ["ThreadPool.h"], 6 | ) 7 | -------------------------------------------------------------------------------- /third_party/pip_requirements/requirements-release.txt: -------------------------------------------------------------------------------- 1 | setuptools 2 | wheel 3 | numpy 4 | dm-env 5 | gym>=0.26 6 | gymnasium>=0.26,!=0.27.0 7 | optree>=0.6.0 8 | jax[cpu] 9 | packaging 10 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to EnvPool 2 | 3 | Please refer to [envpool.readthedocs.io/en/latest/content/contributing.html](https://envpool.readthedocs.io/en/latest/content/contributing.html) 4 | -------------------------------------------------------------------------------- /third_party/glibc_version_header/glibc_version_header.BUILD: -------------------------------------------------------------------------------- 1 | filegroup( 2 | name = "glibc_2_17", 3 | srcs = ["force_link_glibc_2.17.h"], 4 | visibility = ["//visibility:public"], 5 | ) 6 | -------------------------------------------------------------------------------- /third_party/mujoco_dmc_xml/mujoco_dmc_xml.BUILD: -------------------------------------------------------------------------------- 1 | filegroup( 2 | name = "mujoco_dmc_xml", 3 | srcs = glob(["suite/*.xml"]) + ["suite/common"], 4 | visibility = ["//visibility:public"], 5 | ) 6 | -------------------------------------------------------------------------------- /third_party/vizdoom_extra_maps/vizdoom_extra_maps.BUILD: -------------------------------------------------------------------------------- 1 | filegroup( 2 | name = "vizdoom_maps", 3 | srcs = glob( 4 | ["maps/*"], 5 | exclude = ["maps/*99maps.*"], 6 | ), 7 | visibility = ["//visibility:public"], 8 | ) 9 | -------------------------------------------------------------------------------- /benchmark/requirements.txt: -------------------------------------------------------------------------------- 1 | gym[accept-rom-license]==0.23.1 2 | ale-py==0.7.5 3 | mujoco==2.2.0 4 | envpool==0.6.1.post1 5 | sample-factory==1.123.0 6 | mujoco_py==2.1.2.14 7 | tqdm 8 | opencv-python-headless 9 | dm_control==1.0.3.post1 10 | packaging 11 | -------------------------------------------------------------------------------- /docs/content/slides.rst: -------------------------------------------------------------------------------- 1 | Slides 2 | ====== 3 | 4 | - [Invited talk (in Chinese)] 5 | `2021/11/17 PDF `_ 6 | - [Invited talk (in English)] 7 | `2021/12/05 PDF `_ 8 | -------------------------------------------------------------------------------- /third_party/pugixml/pugixml.BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | cc_library( 4 | name = "pugixml", 5 | srcs = ["pugixml.cpp"], 6 | hdrs = [ 7 | "pugiconfig.hpp", 8 | "pugixml.hpp", 9 | ], 10 | ) 11 | -------------------------------------------------------------------------------- /third_party/pretrain_weight/pretrain_weight.BUILD: -------------------------------------------------------------------------------- 1 | filegroup( 2 | name = "pth", 3 | srcs = [ 4 | "policy-breakout.pth", 5 | "policy-d1.pth", 6 | "policy-d3.pth", 7 | "policy-pong.pth", 8 | ], 9 | visibility = ["//visibility:public"], 10 | ) 11 | -------------------------------------------------------------------------------- /third_party/vizdoom/sdl_thread.patch: -------------------------------------------------------------------------------- 1 | --- src/posix/sdl/critsec.h 2 | +++ src/posix/sdl/critsec.h 3 | @@ -6,7 +6,7 @@ 4 | #define CRITSEC_H 5 | 6 | #include "SDL2/SDL.h" 7 | -#include "SDL_thread.h" 8 | +#include "SDL2/SDL_thread.h" 9 | #include "i_system.h" 10 | 11 | class FCriticalSection 12 | -------------------------------------------------------------------------------- /third_party/concurrentqueue/concurrentqueue.BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | cc_library( 4 | name = "concurrentqueue", 5 | hdrs = [ 6 | "blockingconcurrentqueue.h", 7 | "concurrentqueue.h", 8 | "lightweightsemaphore.h", 9 | ], 10 | ) 11 | -------------------------------------------------------------------------------- /third_party/atari_roms/atari_roms.BUILD: -------------------------------------------------------------------------------- 1 | filegroup( 2 | name = "roms", 3 | srcs = glob( 4 | ["ROM/*/*.bin"], 5 | exclude = [ 6 | "ROM/combat/combat.bin", 7 | "ROM/joust/joust.bin", 8 | "ROM/maze_craze/maze_craze.bin", 9 | "ROM/warlords/warlords.bin", 10 | ], 11 | ), 12 | visibility = ["//visibility:public"], 13 | ) 14 | -------------------------------------------------------------------------------- /third_party/pip_requirements/requirements-dev.txt: -------------------------------------------------------------------------------- 1 | setuptools 2 | wheel 3 | numpy 4 | dm-env 5 | gym>=0.26 6 | gymnasium>=0.26,!=0.27.0 7 | optree>=0.6.0 8 | jax[cpu] 9 | absl-py 10 | packaging 11 | tqdm 12 | protobuf<=4.20.0 13 | torch!=2.0.0,!=2.0.1 14 | tianshou>=0.4.10,<1.0.0 15 | opencv-python-headless 16 | box2d-py 17 | dm-control>=1.0.5 18 | mujoco>=2.2.1,<2.3 19 | mujoco_py>=2.1.2.14 20 | pygame 21 | minigrid 22 | -------------------------------------------------------------------------------- /docs/env/minigrid.rst: -------------------------------------------------------------------------------- 1 | Minigrid 2 | ======== 3 | 4 | We use ``minigrid==2.1.0`` as the codebase. 5 | See https://github.com/Farama-Foundation/Minigrid/tree/v2.1.0 6 | 7 | 8 | Empty 9 | ----- 10 | 11 | Registered Configurations 12 | 13 | - `MiniGrid-Empty-5x5-v0` 14 | - `MiniGrid-Empty-Random-5x5-v0` 15 | - `MiniGrid-Empty-6x6-v0` 16 | - `MiniGrid-Empty-Random-6x6-v0` 17 | - `MiniGrid-Empty-8x8-v0` 18 | - `MiniGrid-Empty-16x16-v0` 19 | -------------------------------------------------------------------------------- /third_party/mujoco/mujoco.BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | cc_library( 4 | name = "mujoco_lib", 5 | srcs = glob(["lib/*"]), 6 | hdrs = glob(["include/mujoco/*.h"]), 7 | includes = [ 8 | "include", 9 | "include/mujoco", 10 | ], 11 | linkopts = ["-Wl,-rpath,'$$ORIGIN'"], 12 | linkstatic = 0, 13 | ) 14 | 15 | filegroup( 16 | name = "mujoco_so", 17 | srcs = ["lib/libmujoco.so.2.2.1"], 18 | ) 19 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Bazel Build and Test 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | test: 7 | runs-on: [self-hosted, Linux, X64, Test] 8 | steps: 9 | - name: Cancel previous run 10 | uses: styfle/cancel-workflow-action@0.11.0 11 | with: 12 | access_token: ${{ github.token }} 13 | - uses: actions/checkout@v3 14 | - name: Test 15 | run: | 16 | make bazel-test 17 | - name: Run clang-tidy 18 | run: | 19 | make clang-tidy 20 | -------------------------------------------------------------------------------- /BUILD: -------------------------------------------------------------------------------- 1 | load("@pip_requirements//:requirements.bzl", "requirement") 2 | 3 | filegroup( 4 | name = "clang_tidy_config", 5 | data = [".clang-tidy"], 6 | ) 7 | 8 | py_binary( 9 | name = "setup", 10 | srcs = [ 11 | "setup.py", 12 | ], 13 | data = [ 14 | "README.md", 15 | "setup.cfg", 16 | "//envpool", 17 | ], 18 | main = "setup.py", 19 | python_version = "PY3", 20 | deps = [ 21 | requirement("setuptools"), 22 | requirement("wheel"), 23 | ], 24 | ) 25 | -------------------------------------------------------------------------------- /third_party/BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /third_party/ale/BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /third_party/box2d/BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /third_party/cuda/BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /third_party/jpeg/BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /third_party/nasm/BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /third_party/sdl2/BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /third_party/zlib/BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /third_party/atari_roms/BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /third_party/freedoom/BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /third_party/mujoco/BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /third_party/opencv/BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /third_party/procgen/BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /third_party/pugixml/BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /third_party/threadpool/BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /third_party/vizdoom/BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /third_party/concurrentqueue/BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /third_party/gym3_libenv/BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /third_party/mujoco_dmc_xml/BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /third_party/mujoco_gym_xml/BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /third_party/pretrain_weight/BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /third_party/vizdoom_lib/BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /.readthedocs.yaml: -------------------------------------------------------------------------------- 1 | # .readthedocs.yaml 2 | # Read the Docs configuration file 3 | # See https://docs.readthedocs.io/en/stable/config-file/v2.html for details 4 | 5 | # Required 6 | version: 2 7 | 8 | # Set the version of Python and other tools you might need 9 | build: 10 | os: ubuntu-22.04 11 | tools: 12 | python: "3.11" 13 | 14 | # Build documentation in the docs/ directory with Sphinx 15 | sphinx: 16 | configuration: docs/conf.py 17 | # We recommend specifying your dependencies to enable reproducible builds: 18 | # https://docs.readthedocs.io/en/stable/guides/reproducible-builds.html 19 | python: 20 | install: 21 | - requirements: docs/requirements.txt 22 | -------------------------------------------------------------------------------- /third_party/glibc_version_header/BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /third_party/vizdoom_extra_maps/BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /examples/acme_examples/README.md: -------------------------------------------------------------------------------- 1 | # [Acme](https://github.com/deepmind/acme) 2 | 3 | Acme has only released v0.4.0 on PyPI for now (22/06/03), which is far behind 4 | the master codes, where APIs for constructing experiments were added. 5 | 6 | We are using a specific master version (84e0923), so please make sure you 7 | install acme using [method 4](https://github.com/deepmind/acme#installation). 8 | 9 | To setup the environment: `pip install -r requirements.txt` 10 | 11 | To run the experiments: 12 | 13 | ```bash 14 | python examples/acme_examples/ppo_continuous.py --use-envpool 15 | ``` 16 | 17 | The benchmark training curve: see [openrlbenchmark](https://wandb.ai/openrlbenchmark/acme). 18 | -------------------------------------------------------------------------------- /envpool/mujoco/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | """Mujoco env in EnvPool.""" 15 | -------------------------------------------------------------------------------- /envpool/python/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | """Python interface for EnvPool.""" 15 | -------------------------------------------------------------------------------- /docs/Makefile: -------------------------------------------------------------------------------- 1 | # Minimal makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line, and also 5 | # from the environment for the first two. 6 | SPHINXOPTS ?= 7 | SPHINXBUILD ?= sphinx-build 8 | SOURCEDIR = . 9 | BUILDDIR = _build 10 | 11 | # Put it first so that "make" without argument is like "make help". 12 | help: 13 | @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) 14 | 15 | .PHONY: help Makefile 16 | 17 | # Catch-all target: route all unknown targets to Sphinx using the new 18 | # "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). 19 | %: Makefile 20 | @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) 21 | -------------------------------------------------------------------------------- /WORKSPACE: -------------------------------------------------------------------------------- 1 | workspace(name = "envpool") 2 | 3 | load("//envpool:workspace0.bzl", workspace0 = "workspace") 4 | 5 | workspace0() 6 | 7 | load("//envpool:workspace1.bzl", workspace1 = "workspace") 8 | 9 | workspace1() 10 | 11 | # QT special, cannot move to workspace2.bzl, not sure why 12 | 13 | load("@local_config_qt//:local_qt.bzl", "local_qt_path") 14 | 15 | new_local_repository( 16 | name = "qt", 17 | build_file = "@com_justbuchanan_rules_qt//:qt.BUILD", 18 | path = local_qt_path(), 19 | ) 20 | 21 | load("@com_justbuchanan_rules_qt//tools:qt_toolchain.bzl", "register_qt_toolchains") 22 | 23 | register_qt_toolchains() 24 | 25 | load("//envpool:pip.bzl", pip_workspace = "workspace") 26 | 27 | pip_workspace() 28 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | from setuptools import setup 4 | from setuptools.command.install import install 5 | from setuptools.dist import Distribution 6 | 7 | 8 | class InstallPlatlib(install): 9 | """Fix auditwheel error, https://github.com/google/or-tools/issues/616""" 10 | 11 | def finalize_options(self) -> None: 12 | install.finalize_options(self) 13 | if self.distribution.has_ext_modules(): 14 | self.install_lib = self.install_platlib 15 | 16 | 17 | class BinaryDistribution(Distribution): 18 | 19 | def is_pure(self) -> bool: 20 | return False 21 | 22 | def has_ext_modules(foo) -> bool: 23 | return True 24 | 25 | 26 | if __name__ == '__main__': 27 | setup(distclass=BinaryDistribution, cmdclass={'install': InstallPlatlib}) 28 | -------------------------------------------------------------------------------- /third_party/pip_requirements/BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | package(default_visibility = ["//visibility:public"]) 16 | 17 | exports_files(["requirements.txt"]) 18 | -------------------------------------------------------------------------------- /examples/list_all_envs.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import pprint 16 | 17 | import envpool 18 | 19 | if __name__ == "__main__": 20 | pprint.pprint(envpool.list_all_envs()) 21 | -------------------------------------------------------------------------------- /benchmark/numa_test.sh: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | total=`seq 0 $(expr $1 - 1)` 16 | shift 17 | for i in $total 18 | do 19 | numactl --cpunodebind=$i --membind=$i -- $@ & 20 | done 21 | wait 22 | -------------------------------------------------------------------------------- /.bazelrc: -------------------------------------------------------------------------------- 1 | build --action_env=BAZEL_LINKLIBS=-l%:libstdc++.a:-lm 2 | build --action_env=BAZEL_LINKOPTS=-static-libgcc 3 | build --action_env=CUDA_DIR=/usr/local/cuda 4 | build --action_env=LD_LIBRARY_PATH=/usr/local/lib:/usr/lib/nvidia 5 | build --incompatible_strict_action_env --cxxopt=-std=c++17 --host_cxxopt=-std=c++17 --client_env=BAZEL_CXXOPTS=-std=c++17 6 | build:debug --cxxopt=-DENVPOOL_TEST --compilation_mode=dbg -s 7 | build:test --cxxopt=-DENVPOOL_TEST --copt=-g0 --copt=-O3 --copt=-DNDEBUG --copt=-msse --copt=-msse2 --copt=-mmmx 8 | build:release --copt=-g0 --copt=-O3 --copt=-DNDEBUG --copt=-msse --copt=-msse2 --copt=-mmmx 9 | 10 | build:clang-tidy --aspects @bazel_clang_tidy//clang_tidy:clang_tidy.bzl%clang_tidy_aspect 11 | build:clang-tidy --@bazel_clang_tidy//:clang_tidy_config=//:clang_tidy_config 12 | build:clang-tidy --output_groups=report 13 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: "[Feature Request]" 5 | labels: '' 6 | assignees: Trinkle23897 7 | 8 | --- 9 | 10 | ## Motivation 11 | 12 | Please outline the motivation for the proposal. 13 | Is your feature request related to a problem? e.g., "I'm always frustrated when [...]". 14 | If this is related to another issue, please link here too. 15 | 16 | ## Solution 17 | 18 | A clear and concise description of what you want to happen. 19 | 20 | ## Alternatives 21 | 22 | A clear and concise description of any alternative solutions or features you've considered. 23 | 24 | ## Additional context 25 | 26 | Add any other context or screenshots about the feature request here. 27 | 28 | ## Checklist 29 | 30 | - [ ] I have checked that there is no similar issue in the repo (**required**) 31 | -------------------------------------------------------------------------------- /docs/spelling_wordlist.txt: -------------------------------------------------------------------------------- 1 | threadpool 2 | async 3 | dm 4 | env 5 | pybind 6 | cpplint 7 | envs 8 | subprocess 9 | codebase 10 | TimeStep 11 | numa 12 | bazel 13 | bazelisk 14 | buildifier 15 | addlicense 16 | envpool 17 | th 18 | rew 19 | parallelization 20 | parallelly 21 | mypy 22 | numpy 23 | openai 24 | gcc 25 | repo 26 | CMake 27 | dockerfile 28 | config 29 | cartpole 30 | pre 31 | boolean 32 | uint 33 | runtime 34 | http 35 | deps 36 | namespace 37 | un 38 | Acrobot 39 | frictionless 40 | effector 41 | walkable 42 | NChain 43 | facedown 44 | vizdoom 45 | args 46 | lmp 47 | timestep 48 | RGB 49 | py 50 | Mujoco 51 | tran 52 | Reacher 53 | golang 54 | uncomment 55 | Brax 56 | Procgen 57 | Minigrid 58 | Garena 59 | Tianshou 60 | namedtuple 61 | playfield 62 | Pontryagin 63 | booleans 64 | viewport 65 | Tensorflow 66 | Jax 67 | jitting 68 | api 69 | jit 70 | mins 71 | lidar 72 | procgen 73 | -------------------------------------------------------------------------------- /third_party/procgen/procgen.BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | filegroup( 4 | name = "procgen_assets", 5 | srcs = [ 6 | "data/assets/kenney", 7 | "data/assets/kenney-abstract", 8 | "data/assets/misc_assets", 9 | "data/assets/platform_backgrounds", 10 | "data/assets/platform_backgrounds_2", 11 | "data/assets/platformer", 12 | "data/assets/space_backgrounds", 13 | "data/assets/topdown_backgrounds", 14 | "data/assets/water_backgrounds", 15 | ], 16 | ) 17 | 18 | cc_library( 19 | name = "procgen", 20 | srcs = glob(["src/**/*.cpp"]) + glob(["src/*.h"]), 21 | hdrs = glob(["src/*.h"]), 22 | copts = [ 23 | "-fpic", 24 | ], 25 | strip_include_prefix = "src", 26 | deps = [ 27 | "@gym3_libenv//:gym3_libenv_header", 28 | "@qt//:qt_core", 29 | "@qt//:qt_gui", 30 | ], 31 | ) 32 | -------------------------------------------------------------------------------- /envpool/atari/atari_envpool.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Garena Online Private Limited 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "envpool/atari/atari_env.h" 16 | #include "envpool/core/py_envpool.h" 17 | 18 | using AtariEnvSpec = PyEnvSpec; 19 | using AtariEnvPool = PyEnvPool; 20 | 21 | PYBIND11_MODULE(atari_envpool, m) { REGISTER(m, AtariEnvSpec, AtariEnvPool) } 22 | -------------------------------------------------------------------------------- /envpool/minigrid/minigrid.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Garena Online Private Limited 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "envpool/core/py_envpool.h" 16 | #include "envpool/minigrid/empty.h" 17 | 18 | using EmptyEnvSpec = PyEnvSpec; 19 | using EmptyEnvPool = PyEnvPool; 20 | 21 | PYBIND11_MODULE(minigrid_envpool, m) { REGISTER(m, EmptyEnvSpec, EmptyEnvPool) } 22 | -------------------------------------------------------------------------------- /third_party/zlib/zlib.BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | licenses(["notice"]) # BSD/MIT-like license (for zlib) 4 | 5 | cc_library( 6 | name = "zlib", 7 | srcs = [ 8 | "adler32.c", 9 | "compress.c", 10 | "crc32.c", 11 | "crc32.h", 12 | "deflate.c", 13 | "deflate.h", 14 | "gzclose.c", 15 | "gzguts.h", 16 | "gzlib.c", 17 | "gzread.c", 18 | "gzwrite.c", 19 | "infback.c", 20 | "inffast.c", 21 | "inffast.h", 22 | "inffixed.h", 23 | "inflate.c", 24 | "inflate.h", 25 | "inftrees.c", 26 | "inftrees.h", 27 | "trees.c", 28 | "trees.h", 29 | "uncompr.c", 30 | "zconf.h", 31 | "zutil.c", 32 | "zutil.h", 33 | ], 34 | hdrs = ["zlib.h"], 35 | copts = [ 36 | "-Wno-shift-negative-value", 37 | "-DZ_HAVE_UNISTD_H", 38 | ], 39 | includes = ["."], 40 | ) 41 | -------------------------------------------------------------------------------- /envpool/procgen/procgen_envpool.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Garena Online Private Limited 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "envpool/core/py_envpool.h" 16 | #include "envpool/procgen/procgen_env.h" 17 | 18 | using ProcgenEnvSpec = PyEnvSpec; 19 | using ProcgenEnvPool = PyEnvPool; 20 | 21 | PYBIND11_MODULE(procgen_envpool, m) { 22 | REGISTER(m, ProcgenEnvSpec, ProcgenEnvPool) 23 | } 24 | -------------------------------------------------------------------------------- /envpool/vizdoom/vizdoom_envpool.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Garena Online Private Limited 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "envpool/core/py_envpool.h" 16 | #include "envpool/vizdoom/vizdoom_env.h" 17 | 18 | using VizdoomEnvSpec = PyEnvSpec; 19 | using VizdoomEnvPool = PyEnvPool; 20 | 21 | PYBIND11_MODULE(vizdoom_envpool, m) { 22 | REGISTER(m, VizdoomEnvSpec, VizdoomEnvPool) 23 | } 24 | -------------------------------------------------------------------------------- /third_party/sdl2/sdl2.BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake") 2 | 3 | filegroup( 4 | name = "srcs", 5 | srcs = glob(["**"]), 6 | visibility = ["//visibility:public"], 7 | ) 8 | 9 | # https://aur.archlinux.org/cgit/aur.git/tree/PKGBUILD?h=sdl2-static 10 | cmake( 11 | name = "sdl2", 12 | generate_args = [ 13 | "-GNinja", 14 | "-DCMAKE_BUILD_TYPE=Release", # always compile for release 15 | "-DSDL_STATIC=ON", 16 | "-DSDL_STATIC_LIB=ON", 17 | "-DSDL_DLOPEN=ON", 18 | "-DARTS=OFF", 19 | "-DESD=OFF", 20 | "-DNAS=OFF", 21 | "-DALSA=ON", 22 | "-DHIDAPI=ON", 23 | "-DPULSEAUDIO_SHARED=ON", 24 | "-DVIDEO_WAYLAND=ON", 25 | "-DRPATH=OFF", 26 | "-DCLOCK_GETTIME=ON", 27 | "-DJACK_SHARED=ON", 28 | "-DSDL_STATIC_PIC=ON", 29 | ], 30 | lib_source = ":srcs", 31 | out_include_dir = "include", 32 | out_static_libs = ["libSDL2.a"], 33 | visibility = ["//visibility:public"], 34 | ) 35 | -------------------------------------------------------------------------------- /envpool/minigrid/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | """Minigrid env in EnvPool.""" 15 | 16 | from envpool.python.api import py_env 17 | 18 | from .minigrid_envpool import _EmptyEnvPool, _EmptyEnvSpec 19 | 20 | (EmptyEnvSpec, EmptyDMEnvPool, EmptyGymEnvPool, 21 | EmptyGymnasiumEnvPool) = py_env(_EmptyEnvSpec, _EmptyEnvPool) 22 | 23 | __all__ = [ 24 | "EmptyEmvSpec", 25 | "EmptyDMEnvPool", 26 | "EmptyGymEnvPool", 27 | "EmptyGymnasiumEnvPool", 28 | ] 29 | -------------------------------------------------------------------------------- /envpool/vizdoom/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | """Vizdoom env in EnvPool.""" 15 | 16 | from envpool.python.api import py_env 17 | 18 | from .vizdoom_envpool import _VizdoomEnvPool, _VizdoomEnvSpec 19 | 20 | (VizdoomEnvSpec, VizdoomDMEnvPool, VizdoomGymEnvPool, 21 | VizdoomGymnasiumEnvPool) = py_env(_VizdoomEnvSpec, _VizdoomEnvPool) 22 | 23 | __all__ = [ 24 | "VizdoomEnvSpec", 25 | "VizdoomDMEnvPool", 26 | "VizdoomGymEnvPool", 27 | "VizdoomGymnasiumEnvPool", 28 | ] 29 | -------------------------------------------------------------------------------- /envpool/atari/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # Copyright 2021 Garena Online Private Limited 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | """Atari env in EnvPool.""" 16 | 17 | from envpool.python.api import py_env 18 | 19 | from .atari_envpool import _AtariEnvPool, _AtariEnvSpec 20 | 21 | AtariEnvSpec, AtariDMEnvPool, AtariGymEnvPool, AtariGymnasiumEnvPool = py_env( 22 | _AtariEnvSpec, _AtariEnvPool 23 | ) 24 | 25 | __all__ = [ 26 | "AtariEnvSpec", 27 | "AtariDMEnvPool", 28 | "AtariGymEnvPool", 29 | "AtariGymnasiumEnvPool", 30 | ] 31 | -------------------------------------------------------------------------------- /envpool/dummy/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # Copyright 2021 Garena Online Private Limited 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | """Dummy Env in EnvPool.""" 16 | 17 | from envpool.python.api import py_env 18 | 19 | from .dummy_envpool import _DummyEnvPool, _DummyEnvSpec 20 | 21 | DummyEnvSpec, DummyDMEnvPool, DummyGymEnvPool, DummyGymnasiumEnvPool = py_env( 22 | _DummyEnvSpec, _DummyEnvPool 23 | ) 24 | 25 | __all__ = [ 26 | "DummyEnvSpec", 27 | "DummyDMEnvPool", 28 | "DummyGymEnvPool", 29 | "DummyGymnasiumEnvPool", 30 | ] 31 | -------------------------------------------------------------------------------- /envpool/procgen/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | """Procgen env Init.""" 15 | from envpool.python.api import py_env 16 | 17 | from .procgen_envpool import _ProcgenEnvPool, _ProcgenEnvSpec 18 | 19 | ( 20 | ProcgenEnvSpec, 21 | ProcgenDMEnvPool, 22 | ProcgenGymEnvPool, 23 | ProcgenGymnasiumEnvPool, 24 | ) = py_env(_ProcgenEnvSpec, _ProcgenEnvPool) 25 | 26 | __all__ = [ 27 | "ProcgenEnvSpec", 28 | "ProcgenDMEnvPool", 29 | "ProcgenGymEnvPool", 30 | "ProcgenGymnasiumEnvPool", 31 | ] 32 | -------------------------------------------------------------------------------- /envpool/utils/BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | package(default_visibility = ["//visibility:public"]) 16 | 17 | cc_library( 18 | name = "image_process", 19 | hdrs = ["image_process.h"], 20 | deps = [ 21 | "//envpool/core:array", 22 | "@opencv", 23 | ], 24 | ) 25 | 26 | cc_test( 27 | name = "image_process_test", 28 | srcs = ["image_process_test.cc"], 29 | deps = [ 30 | ":image_process", 31 | "@com_google_googletest//:gtest_main", 32 | ], 33 | ) 34 | -------------------------------------------------------------------------------- /envpool/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | """EnvPool package for efficient RL environment simulation.""" 15 | 16 | import envpool.entry # noqa: F401 17 | from envpool.registration import ( 18 | list_all_envs, 19 | make, 20 | make_dm, 21 | make_gym, 22 | make_gymnasium, 23 | make_spec, 24 | register, 25 | ) 26 | 27 | __version__ = "0.8.4" 28 | __all__ = [ 29 | "register", 30 | "make", 31 | "make_dm", 32 | "make_gym", 33 | "make_gymnasium", 34 | "make_spec", 35 | "list_all_envs", 36 | ] 37 | -------------------------------------------------------------------------------- /third_party/vizdoom_lib/vizdoom_lib.BUILD: -------------------------------------------------------------------------------- 1 | load("@envpool//third_party:common.bzl", "template_rule") 2 | 3 | package(default_visibility = ["//visibility:public"]) 4 | 5 | template_rule( 6 | name = "vizdoom_version", 7 | src = "src/lib/ViZDoomVersion.h.in", 8 | out = "ViZDoomVersion.h", 9 | substitutions = { 10 | "@ViZDoom_VERSION_ID@": "1113", 11 | "@ViZDoom_VERSION_STR@": "1.1.13", 12 | }, 13 | ) 14 | 15 | cc_library( 16 | name = "vizdoom_lib", 17 | srcs = glob([ 18 | "include/*.h", 19 | "src/lib/*.h", 20 | "src/lib/*.cpp", 21 | "src/lib/boost/**/*.hpp", 22 | ]) + [":vizdoom_version"], 23 | hdrs = ["include/ViZDoom.h"], 24 | includes = [ 25 | "include", 26 | "src/lib", 27 | ], 28 | linkopts = ["-ldl"], 29 | deps = [ 30 | "@boost//:asio", 31 | "@boost//:filesystem", 32 | "@boost//:interprocess", 33 | "@boost//:iostreams", 34 | "@boost//:process", 35 | "@boost//:random", 36 | "@boost//:thread", 37 | ], 38 | ) 39 | 40 | filegroup( 41 | name = "vizdoom_maps", 42 | srcs = glob(["scenarios/*"]), 43 | ) 44 | -------------------------------------------------------------------------------- /envpool/dummy/dummy_envpool.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Garena Online Private Limited 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "envpool/dummy/dummy_envpool.h" 18 | 19 | #include "envpool/core/py_envpool.h" 20 | 21 | /** 22 | * Wrap the `DummyEnvSpec` and `DummyEnvPool` with the corresponding `PyEnvSpec` 23 | * and `PyEnvPool` template. 24 | */ 25 | using DummyEnvSpec = PyEnvSpec; 26 | using DummyEnvPool = PyEnvPool; 27 | 28 | /** 29 | * Finally, call the REGISTER macro to expose them to python 30 | */ 31 | PYBIND11_MODULE(dummy_envpool, m) { REGISTER(m, DummyEnvSpec, DummyEnvPool) } 32 | -------------------------------------------------------------------------------- /envpool/python/utils.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | """Helper function for Python API.""" 15 | 16 | from typing import Any, List 17 | 18 | import numpy as np 19 | 20 | 21 | def check_key_duplication(cls: Any, keytype: str, keys: List[str]) -> None: 22 | """Check if there's any duplicated keys in ``keys``.""" 23 | ukeys, counts = np.unique(keys, return_counts=True) 24 | if not np.all(counts == 1): 25 | dup_keys = ukeys[counts > 1] 26 | raise SystemError( 27 | f"{cls} c++ code error. {keytype} keys {list(dup_keys)} are duplicated. " 28 | f"Please report to the author of {cls}." 29 | ) 30 | -------------------------------------------------------------------------------- /envpool/vizdoom/bin/BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | package(default_visibility = ["//visibility:public"]) 16 | 17 | genrule( 18 | name = "vizdoom_bin", 19 | srcs = ["@vizdoom"], 20 | outs = ["vizdoom"], 21 | cmd = "cp $< $@", 22 | executable = True, 23 | ) 24 | 25 | genrule( 26 | name = "vizdoom_pk3", 27 | srcs = ["@vizdoom//:vizdoom_pk3"], 28 | outs = ["vizdoom.pk3"], 29 | cmd = "cp $< $@", 30 | executable = True, 31 | ) 32 | 33 | genrule( 34 | name = "freedoom", 35 | srcs = ["@freedoom"], 36 | outs = ["freedoom2.wad"], 37 | cmd = "cp $< $@", 38 | executable = True, 39 | ) 40 | -------------------------------------------------------------------------------- /envpool/core/circular_buffer_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Garena Online Private Limited 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "circular_buffer.h" 16 | 17 | #include 18 | #include 19 | 20 | #include 21 | #include 22 | 23 | TEST(CircularBufferTest, Basic) { 24 | CircularBuffer cb(100); 25 | std::vector number(100000); 26 | for (auto& n : number) { 27 | n = std::rand(); 28 | } 29 | std::thread t_put([&]() { 30 | for (auto& n : number) { 31 | cb.Put(n); 32 | } 33 | }); 34 | 35 | for (auto& n : number) { 36 | int r = cb.Get(); 37 | EXPECT_EQ(r, n); 38 | } 39 | t_put.join(); 40 | } 41 | -------------------------------------------------------------------------------- /envpool/minigrid/impl/minigrid_empty_env.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2023 Garena Online Private Limited 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef ENVPOOL_MINIGRID_IMPL_MINIGRID_EMPTY_ENV_H_ 18 | #define ENVPOOL_MINIGRID_IMPL_MINIGRID_EMPTY_ENV_H_ 19 | 20 | #include 21 | 22 | #include "envpool/minigrid/impl/minigrid_env.h" 23 | 24 | namespace minigrid { 25 | 26 | class MiniGridEmptyEnv : public MiniGridEnv { 27 | public: 28 | MiniGridEmptyEnv(int size, std::pair agent_start_pos, 29 | int agent_start_dir, int max_steps, int agent_view_size); 30 | void GenGrid() override; 31 | }; 32 | 33 | } // namespace minigrid 34 | 35 | #endif // ENVPOOL_MINIGRID_IMPL_MINIGRID_EMPTY_ENV_H_ 36 | -------------------------------------------------------------------------------- /envpool/atari/registration.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | """Atari env registration.""" 15 | 16 | import os 17 | 18 | from envpool.registration import base_path, register 19 | 20 | atari_rom_path = os.path.join(base_path, "atari", "roms") 21 | atari_game_list = sorted( 22 | [i.replace(".bin", "") for i in os.listdir(atari_rom_path)] 23 | ) 24 | 25 | for game in atari_game_list: 26 | name = "".join([g.capitalize() for g in game.split("_")]) 27 | register( 28 | task_id=name + "-v5", 29 | import_path="envpool.atari", 30 | spec_cls="AtariEnvSpec", 31 | dm_cls="AtariDMEnvPool", 32 | gym_cls="AtariGymEnvPool", 33 | gymnasium_cls="AtariGymnasiumEnvPool", 34 | task=game, 35 | max_episode_steps=27000, 36 | ) 37 | -------------------------------------------------------------------------------- /.github/workflows/lint.yml: -------------------------------------------------------------------------------- 1 | name: Lint 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | lint: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - name: Cancel previous run 10 | uses: styfle/cancel-workflow-action@0.11.0 11 | with: 12 | access_token: ${{ github.token }} 13 | - uses: actions/checkout@v3 14 | - uses: actions/setup-go@v3 15 | with: 16 | go-version: ">=1.16.0" 17 | - name: Set up Python 3.11 18 | uses: actions/setup-python@v4 19 | with: 20 | python-version: "3.11" 21 | - name: Upgrade pip 22 | run: | 23 | python -m pip install --upgrade pip 24 | - name: flake8 25 | run: | 26 | make flake8 27 | - name: isort and yapf 28 | run: | 29 | make py-format 30 | - name: cpplint 31 | run: | 32 | make cpplint 33 | - name: clang-format 34 | run: | 35 | make clang-format 36 | - name: buildifier 37 | run: | 38 | make buildifier 39 | - name: addlicense 40 | run: | 41 | make addlicense 42 | - name: mypy 43 | run: | 44 | make mypy 45 | - name: docstyle 46 | run: | 47 | make docstyle 48 | - name: spelling 49 | run: | 50 | make spelling 51 | -------------------------------------------------------------------------------- /envpool/workspace1.bzl: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | """EnvPool workspace initialization, load after workspace0.""" 16 | 17 | load("@com_github_nelhage_rules_boost//:boost/boost.bzl", "boost_deps") 18 | load("@com_justbuchanan_rules_qt//:qt_configure.bzl", "qt_configure") 19 | load("@pybind11_bazel//:python_configure.bzl", "python_configure") 20 | load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies") 21 | 22 | def workspace(): 23 | """Configure pip requirements.""" 24 | python_configure( 25 | name = "local_config_python", 26 | python_version = "3", 27 | ) 28 | 29 | rules_foreign_cc_dependencies() 30 | 31 | boost_deps() 32 | 33 | qt_configure() 34 | 35 | workspace1 = workspace 36 | -------------------------------------------------------------------------------- /envpool/pip.bzl: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | """EnvPool pip requirements initialization, this is loaded in WORKSPACE.""" 16 | 17 | load("@rules_python//python:pip.bzl", "pip_install") 18 | 19 | def workspace(): 20 | """Configure pip requirements.""" 21 | 22 | if "pip_requirements" not in native.existing_rules().keys(): 23 | pip_install( 24 | name = "pip_requirements", 25 | python_interpreter = "python3", 26 | # default timeout value is 600, change it if you failed. 27 | # timeout = 3600, 28 | quiet = False, 29 | requirements = "@envpool//third_party/pip_requirements:requirements.txt", 30 | # extra_pip_args = ["--extra-index-url", "https://mirrors.aliyun.com/pypi/simple"], 31 | ) 32 | -------------------------------------------------------------------------------- /envpool/box2d/utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Garena Online Private Limited 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef ENVPOOL_BOX2D_UTILS_H_ 18 | #define ENVPOOL_BOX2D_UTILS_H_ 19 | 20 | #include 21 | 22 | #include 23 | #include 24 | 25 | namespace box2d { 26 | 27 | using RandInt = std::uniform_int_distribution<>; 28 | using RandUniform = std::uniform_real_distribution<>; 29 | 30 | // this function is to pass clang-tidy conversion check 31 | b2Vec2 Vec2(double x, double y); 32 | 33 | float Sign(double val, double eps = 1e-8); 34 | 35 | std::array RotateRad(const std::array& vec, float angle); 36 | 37 | b2Vec2 RotateRad(const b2Vec2& v, float angle); 38 | 39 | b2Vec2 Multiply(const b2Transform& trans, const b2Vec2& v); 40 | 41 | } // namespace box2d 42 | 43 | #endif // ENVPOOL_BOX2D_UTILS_H_ 44 | -------------------------------------------------------------------------------- /docker/dev.dockerfile: -------------------------------------------------------------------------------- 1 | # Need docker >= 20.10.9, see https://stackoverflow.com/questions/71941032/why-i-cannot-run-apt-update-inside-a-fresh-ubuntu22-04 2 | 3 | FROM nvidia/cuda:12.2.2-cudnn8-devel-ubuntu22.04 4 | 5 | ARG DEBIAN_FRONTEND=noninteractive 6 | ARG HOME=/root 7 | ARG PATH=$PATH:$HOME/go/bin 8 | 9 | RUN apt-get update \ 10 | && apt-get install -y python3-pip python3-dev golang-1.18 git wget curl zsh tmux vim \ 11 | && rm -rf /var/lib/apt/lists/* 12 | RUN ln -s /usr/bin/python3 /usr/bin/python 13 | RUN ln -sf /usr/lib/go-1.18/bin/go /usr/bin/go 14 | RUN sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" 15 | WORKDIR $HOME 16 | RUN git clone https://github.com/gpakosz/.tmux.git 17 | RUN ln -s -f .tmux/.tmux.conf 18 | RUN cp .tmux/.tmux.conf.local . 19 | RUN echo "set-option -g default-shell /bin/zsh" >> .tmux.conf.local 20 | RUN echo "set-option -g history-limit 10000" >> .tmux.conf.local 21 | RUN echo "export PATH=$PATH:$HOME/go/bin" >> .zshrc 22 | 23 | RUN go install github.com/bazelbuild/bazelisk@latest && ln -sf $HOME/go/bin/bazelisk $HOME/go/bin/bazel 24 | RUN go install github.com/bazelbuild/buildtools/buildifier@latest 25 | RUN $HOME/go/bin/bazel version 26 | 27 | RUN useradd -ms /bin/zsh github-action 28 | 29 | RUN apt-get update \ 30 | && apt-get install -y clang-format clang-tidy swig qtdeclarative5-dev \ 31 | && rm -rf /var/lib/apt/lists/* 32 | 33 | WORKDIR /app 34 | -------------------------------------------------------------------------------- /third_party/cuda/cuda.bzl: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | """This is loaded in workspace0.bzl to provide cuda library.""" 16 | 17 | _CUDA_DIR = "CUDA_DIR" 18 | 19 | def _impl(rctx): 20 | cuda_dir = rctx.os.environ.get(_CUDA_DIR, default = "/usr/local/cuda") 21 | rctx.symlink("{}/include".format(cuda_dir), "include") 22 | rctx.symlink("{}/lib64".format(cuda_dir), "lib64") 23 | rctx.file("WORKSPACE") 24 | rctx.file("BUILD", content = """ 25 | package(default_visibility = ["//visibility:public"]) 26 | 27 | cc_library( 28 | name = "cudart_static", 29 | srcs = ["lib64/libcudart_static.a"], 30 | hdrs = glob([ 31 | "include/*.h", 32 | "include/**/*.h", 33 | ]), 34 | strip_include_prefix = "include", 35 | ) 36 | """) 37 | 38 | cuda_configure = repository_rule( 39 | implementation = _impl, 40 | environ = [ 41 | _CUDA_DIR, 42 | ], 43 | ) 44 | -------------------------------------------------------------------------------- /third_party/ale/ale.BUILD: -------------------------------------------------------------------------------- 1 | load("@envpool//third_party:common.bzl", "template_rule") 2 | 3 | package(default_visibility = ["//visibility:public"]) 4 | 5 | cc_library( 6 | name = "irregular_files", 7 | hdrs = glob([ 8 | "src/**/*.def", 9 | "src/**/*.ins", 10 | ]), 11 | ) 12 | 13 | template_rule( 14 | name = "ale_version", 15 | src = "src/version.hpp.in", 16 | out = "version.hpp", 17 | substitutions = { 18 | "@ALE_VERSION@": "0.7.5", 19 | "@ALE_VERSION_MAJOR@": "0", 20 | "@ALE_VERSION_MINOR@": "7", 21 | "@ALE_VERSION_PATCH@": "5", 22 | "@ALE_VERSION_GIT_SHA@": "978d2ce25665338ba71e45d32fff853b17c15f2e", 23 | }, 24 | ) 25 | 26 | cc_library( 27 | name = "ale_interface", 28 | srcs = glob( 29 | [ 30 | "src/**/*.h", 31 | "src/**/*.hpp", 32 | "src/**/*.hxx", 33 | "src/**/*.c", 34 | "src/**/*.cpp", 35 | "src/**/*.cxx", 36 | ], 37 | exclude = [ 38 | "src/python/*", 39 | ], 40 | ) + [ 41 | ":ale_version", 42 | ], 43 | hdrs = ["src/ale_interface.hpp"], 44 | includes = [ 45 | "src", 46 | "src/common", 47 | "src/emucore", 48 | "src/environment", 49 | "src/games", 50 | "src/games/supported", 51 | ], 52 | linkopts = [ 53 | "-ldl", 54 | ], 55 | linkstatic = 0, 56 | deps = [ 57 | ":irregular_files", 58 | "@zlib", 59 | ], 60 | ) 61 | -------------------------------------------------------------------------------- /envpool/entry.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | """Entry point for all envs' registration.""" 15 | 16 | try: 17 | import envpool.atari.registration # noqa: F401 18 | except ImportError: 19 | pass 20 | 21 | try: 22 | import envpool.box2d.registration # noqa: F401 23 | except ImportError: 24 | pass 25 | 26 | try: 27 | import envpool.classic_control.registration # noqa: F401 28 | except ImportError: 29 | pass 30 | 31 | try: 32 | import envpool.mujoco.dmc.registration # noqa: F401 33 | except ImportError: 34 | pass 35 | 36 | try: 37 | import envpool.mujoco.gym.registration # noqa: F401 38 | except ImportError: 39 | pass 40 | 41 | try: 42 | import envpool.procgen.registration # noqa: F401 43 | except ImportError: 44 | pass 45 | 46 | try: 47 | import envpool.toy_text.registration # noqa: F401 48 | except ImportError: 49 | pass 50 | 51 | try: 52 | import envpool.vizdoom.registration # noqa: F401 53 | except ImportError: 54 | pass 55 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release PyPI Wheel 2 | 3 | # on: [push, pull_request] 4 | on: 5 | push: 6 | branches: 7 | - main 8 | tags: 9 | - v* 10 | 11 | jobs: 12 | release: 13 | runs-on: ubuntu-latest 14 | container: trinkle23897/envpool-release:2023-01-02-5f1a5fd 15 | strategy: 16 | matrix: 17 | python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"] 18 | steps: 19 | - uses: actions/checkout@v3 20 | - name: Set up Python ${{ matrix.python-version }} 21 | run: | 22 | ln -sf /root/.cache $HOME/.cache 23 | ln -sf /root/.pyenv $HOME/.pyenv 24 | pyenv global ${{ matrix.python-version }}-dev 25 | - name: Build 26 | run: | 27 | make pypi-wheel 28 | pip3 install dist/*.whl --force-reinstall 29 | - name: Test 30 | run: | 31 | make release-test 32 | - name: Upload artifact 33 | uses: actions/upload-artifact@main 34 | with: 35 | name: wheel 36 | path: wheelhouse/ 37 | 38 | publish: 39 | runs-on: ubuntu-latest 40 | needs: [release] 41 | steps: 42 | - uses: actions/download-artifact@v3 43 | with: 44 | path: artifact 45 | - name: Move files so the next action can find them 46 | run: | 47 | mkdir dist && mv artifact/wheel/* dist/ 48 | ls dist/ 49 | - name: Publish distribution to PyPI 50 | if: startsWith(github.ref, 'refs/tags') 51 | uses: pypa/gh-action-pypi-publish@release/v1 52 | with: 53 | password: ${{ secrets.PYPI_API_TOKEN }} 54 | -------------------------------------------------------------------------------- /envpool/python/api.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | """Api wrapper layer for EnvPool.""" 15 | 16 | from typing import Tuple, Type 17 | 18 | from .dm_envpool import DMEnvPoolMeta 19 | from .env_spec import EnvSpecMeta 20 | from .gym_envpool import GymEnvPoolMeta 21 | from .gymnasium_envpool import GymnasiumEnvPoolMeta 22 | from .protocol import EnvPool, EnvSpec 23 | 24 | 25 | def py_env( 26 | envspec: Type[EnvSpec], envpool: Type[EnvPool] 27 | ) -> Tuple[Type[EnvSpec], Type[EnvPool], Type[EnvPool], Type[EnvPool]]: 28 | """Initialize EnvPool for users.""" 29 | # remove the _ prefix added when registering cpp class via pybind 30 | spec_name = envspec.__name__[1:] 31 | pool_name = envpool.__name__[1:] 32 | return ( 33 | EnvSpecMeta(spec_name, (envspec,), {}), # type: ignore[return-value] 34 | DMEnvPoolMeta(pool_name.replace("EnvPool", "DMEnvPool"), (envpool,), {}), 35 | GymEnvPoolMeta(pool_name.replace("EnvPool", "GymEnvPool"), (envpool,), {}), 36 | GymnasiumEnvPoolMeta( 37 | pool_name.replace("EnvPool", "GymnasiumEnvPool"), (envpool,), {} 38 | ), 39 | ) 40 | -------------------------------------------------------------------------------- /envpool/box2d/utils.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Garena Online Private Limited 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "envpool/box2d/utils.h" 16 | 17 | namespace box2d { 18 | 19 | b2Vec2 Vec2(double x, double y) { 20 | return {static_cast(x), static_cast(y)}; 21 | } 22 | 23 | float Sign(double val, double eps) { 24 | if (val > eps) { 25 | return 1; 26 | } 27 | if (val < -eps) { 28 | return -1; 29 | } 30 | return 0; 31 | } 32 | 33 | std::array RotateRad(const std::array& vec, float angle) { 34 | return {std::cos(angle) * vec[0] - std::sin(angle) * vec[1], 35 | std::sin(angle) * vec[0] + std::cos(angle) * vec[1]}; 36 | } 37 | 38 | b2Vec2 RotateRad(const b2Vec2& v, float angle) { 39 | return {std::cos(angle) * v.x - std::sin(angle) * v.y, 40 | std::sin(angle) * v.x + std::cos(angle) * v.y}; 41 | } 42 | 43 | b2Vec2 Multiply(const b2Transform& trans, const b2Vec2& v) { 44 | float x = (trans.q.c * v.x - trans.q.s * v.y) + trans.p.x; 45 | float y = (trans.q.s * v.x + trans.q.c * v.y) + trans.p.y; 46 | return {x, y}; 47 | } 48 | 49 | } // namespace box2d 50 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: "[BUG]" 5 | labels: '' 6 | assignees: Trinkle23897 7 | 8 | --- 9 | 10 | ## Describe the bug 11 | 12 | A clear and concise description of what the bug is. 13 | 14 | ## To Reproduce 15 | 16 | Steps to reproduce the behavior. 17 | 18 | Please try to provide a minimal example to reproduce the bug. Error messages and stack traces are also helpful. 19 | 20 | Please use the markdown code blocks for both code and stack traces. 21 | 22 | ```python 23 | import envpool 24 | ``` 25 | 26 | ```bash 27 | Traceback (most recent call last): 28 | File ... 29 | ``` 30 | 31 | ## Expected behavior 32 | 33 | A clear and concise description of what you expected to happen. 34 | 35 | ## Screenshots 36 | If applicable, add screenshots to help explain your problem. 37 | 38 | ## System info 39 | 40 | Describe the characteristic of your environment: 41 | * Describe how the library was installed (pip, source, ...) 42 | * Python version 43 | * Versions of any other relevant libraries 44 | 45 | ```python 46 | import envpool, numpy, sys 47 | print(envpool.__version__, numpy.__version__, sys.version, sys.platform) 48 | ``` 49 | 50 | ## Additional context 51 | 52 | Add any other context about the problem here. 53 | 54 | ## Reason and Possible fixes 55 | 56 | If you know or suspect the reason for this bug, paste the code lines and suggest modifications. 57 | 58 | ## Checklist 59 | 60 | - [ ] I have checked that there is no similar issue in the repo (**required**) 61 | - [ ] I have read the [documentation](https://envpool.readthedocs.io/) (**required**) 62 | - [ ] I have provided a minimal working example to reproduce the bug (**required**) 63 | -------------------------------------------------------------------------------- /envpool/utils/image_process.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Garena Online Private Limited 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef ENVPOOL_UTILS_IMAGE_PROCESS_H_ 18 | #define ENVPOOL_UTILS_IMAGE_PROCESS_H_ 19 | 20 | #include 21 | 22 | #include "envpool/core/array.h" 23 | 24 | /** 25 | * Resize `src` image to `tgt`. Use inplace modification to reduce overhead. 26 | */ 27 | void Resize(const Array& src, Array* tgt, bool use_inter_area = true) { 28 | int channel = src.Shape(2); 29 | cv::Mat src_img(src.Shape(0), src.Shape(1), CV_8UC(channel), src.Data()); 30 | cv::Mat tgt_img(tgt->Shape(0), tgt->Shape(1), CV_8UC(channel), tgt->Data()); 31 | if (use_inter_area) { 32 | cv::resize(src_img, tgt_img, tgt_img.size(), 0, 0, cv::INTER_AREA); 33 | } else { 34 | cv::resize(src_img, tgt_img, tgt_img.size()); 35 | } 36 | } 37 | 38 | /** 39 | * Change src (with RGB format) to grayscale image. 40 | */ 41 | void GrayScale(const Array& src, Array* tgt) { 42 | cv::Mat src_img(src.Shape(0), src.Shape(1), CV_8UC3, src.Data()); 43 | cv::Mat tgt_img(tgt->Shape(0), tgt->Shape(1), CV_8UC1, tgt->Data()); 44 | cv::cvtColor(src_img, tgt_img, cv::COLOR_RGB2GRAY); 45 | } 46 | 47 | #endif // ENVPOOL_UTILS_IMAGE_PROCESS_H_ 48 | -------------------------------------------------------------------------------- /envpool/utils/image_process_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Garena Online Private Limited 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "envpool/utils/image_process.h" 16 | 17 | #include 18 | #include 19 | 20 | #include 21 | 22 | TEST(ImageProcessTest, Resize) { 23 | // shape 24 | TArray a(Spec({4, 3, 1})); 25 | TArray b(Spec({6, 7, 1})); 26 | Resize(a, &b); 27 | EXPECT_EQ(b.Shape(), std::vector({6, 7, 1})); 28 | EXPECT_NE(a.Data(), b.Data()); 29 | // same shape, no reference 30 | TArray a2(Spec({4, 3, 2})); 31 | a2(1, 0, 1) = 6; 32 | a2(3, 1, 0) = 4; 33 | TArray b2(Spec({4, 3, 2})); 34 | Resize(a2, &b2); 35 | EXPECT_EQ(a2.Shape(), b2.Shape()); 36 | EXPECT_NE(a2.Data(), b2.Data()); 37 | EXPECT_EQ(6, static_cast(b2(1, 0, 1))); 38 | EXPECT_EQ(4, static_cast(b2(3, 1, 0))); 39 | } 40 | 41 | TEST(ImageProcessTest, GrayScale) { 42 | // shape 43 | TArray a(Spec({4, 3, 3})); 44 | TArray b(Spec({4, 3, 1})); 45 | a(3, 2, 2) = 255; 46 | GrayScale(a, &b); 47 | EXPECT_EQ(static_cast(b(3, 1)), 0); 48 | EXPECT_EQ(static_cast(b(2, 2)), 0); 49 | EXPECT_NE(static_cast(b(3, 2)), 0); 50 | } 51 | -------------------------------------------------------------------------------- /envpool/vizdoom/registration.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | """Vizdoom env registration.""" 15 | 16 | import os 17 | from typing import List 18 | 19 | from envpool.registration import base_path, register 20 | 21 | maps_path = os.path.join(base_path, "vizdoom", "maps") 22 | 23 | 24 | def _vizdoom_game_list() -> List[str]: 25 | return [ 26 | game.replace(".cfg", "") 27 | for game in sorted(os.listdir(maps_path)) 28 | if game.endswith(".cfg") and 29 | os.path.exists(os.path.join(maps_path, game.replace(".cfg", ".wad"))) 30 | ] 31 | 32 | 33 | for game in _vizdoom_game_list() + ["vizdoom_custom"]: 34 | name = "".join([g.capitalize() for g in game.split("_")]) 35 | if game == "vizdoom_custom": 36 | cfg_path = wad_path = "" 37 | else: 38 | cfg_path = os.path.join(maps_path, f"{game}.cfg") 39 | wad_path = os.path.join(maps_path, f"{game}.wad") 40 | register( 41 | task_id=f"{name}-v1", 42 | import_path="envpool.vizdoom", 43 | spec_cls="VizdoomEnvSpec", 44 | dm_cls="VizdoomDMEnvPool", 45 | gym_cls="VizdoomGymEnvPool", 46 | gymnasium_cls="VizdoomGymnasiumEnvPool", 47 | cfg_path=cfg_path, 48 | wad_path=wad_path, 49 | max_episode_steps=525, 50 | ) 51 | -------------------------------------------------------------------------------- /docker/dev-cn.dockerfile: -------------------------------------------------------------------------------- 1 | # Need docker >= 20.10.9, see https://stackoverflow.com/questions/71941032/why-i-cannot-run-apt-update-inside-a-fresh-ubuntu22-04 2 | 3 | FROM nvidia/cuda:12.2.2-cudnn8-devel-ubuntu22.04 4 | 5 | ARG DEBIAN_FRONTEND=noninteractive 6 | ARG HOME=/root 7 | ARG PATH=$PATH:$HOME/go/bin 8 | 9 | RUN apt-get update \ 10 | && apt-get install -y python3-pip python3-dev golang-1.18 git wget curl zsh tmux vim \ 11 | && rm -rf /var/lib/apt/lists/* 12 | RUN ln -s /usr/bin/python3 /usr/bin/python 13 | RUN ln -sf /usr/lib/go-1.18/bin/go /usr/bin/go 14 | RUN sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" 15 | WORKDIR $HOME 16 | RUN git clone https://github.com/gpakosz/.tmux.git 17 | RUN ln -s -f .tmux/.tmux.conf 18 | RUN cp .tmux/.tmux.conf.local . 19 | RUN echo "set-option -g default-shell /bin/zsh" >> .tmux.conf.local 20 | RUN echo "set-option -g history-limit 10000" >> .tmux.conf.local 21 | RUN go env -w GOPROXY=https://goproxy.cn 22 | 23 | RUN wget https://mirrors.huaweicloud.com/bazel/6.0.0/bazel-6.0.0-linux-x86_64 24 | RUN chmod +x bazel-6.0.0-linux-x86_64 25 | RUN mkdir -p $HOME/go/bin 26 | RUN mv bazel-6.0.0-linux-x86_64 $HOME/go/bin/bazel 27 | RUN go install github.com/bazelbuild/buildtools/buildifier@latest 28 | RUN $HOME/go/bin/bazel version 29 | 30 | RUN useradd -ms /bin/zsh github-action 31 | 32 | RUN apt-get update \ 33 | && apt-get install -y clang-format clang-tidy swig qtdeclarative5-dev \ 34 | && rm -rf /var/lib/apt/lists/* 35 | 36 | RUN pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple 37 | RUN sed -i "s@http://.*archive.ubuntu.com@https://mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list 38 | RUN sed -i "s@http://.*security.ubuntu.com@https://mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list 39 | 40 | WORKDIR /app 41 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ## Description 2 | 3 | Describe your changes in detail. 4 | 5 | ## Motivation and Context 6 | 7 | Why is this change required? What problem does it solve? 8 | If it fixes an open issue, please link to the issue here. 9 | You can use the syntax `close #233` if this solves the issue #233 10 | 11 | - [ ] I have raised an issue to propose this change ([required](https://envpool.readthedocs.io/en/latest/pages/contributing.html) for new features and bug fixes) 12 | 13 | ## Types of changes 14 | 15 | What types of changes does your code introduce? Put an `x` in all the boxes that apply: 16 | 17 | - [ ] Bug fix (non-breaking change which fixes an issue) 18 | - [ ] New feature (non-breaking change which adds core functionality) 19 | - [ ] New environment (non-breaking change which adds 3rd-party environment) 20 | - [ ] Breaking change (fix or feature that would cause existing functionality to change) 21 | - [ ] Documentation (update in the documentation) 22 | - [ ] Example (update in the folder of example) 23 | 24 | ## Implemented Tasks 25 | 26 | - [ ] Subtask 1 27 | - [ ] Subtask 2 28 | - [ ] Subtask 3 29 | 30 | ## Checklist 31 | 32 | Go over all the following points, and put an `x` in all the boxes that apply. 33 | If you are unsure about any of these, don't hesitate to ask. We are here to help! 34 | 35 | - [ ] I have read the [CONTRIBUTION](https://envpool.readthedocs.io/en/latest/pages/contributing.html) guide (**required**) 36 | - [ ] My change requires a change to the documentation. 37 | - [ ] I have updated the tests accordingly (*required for a bug fix or a new feature*). 38 | - [ ] I have updated the documentation accordingly. 39 | - [ ] I have reformatted the code using `make format` (**required**) 40 | - [ ] I have checked the code using `make lint` (**required**) 41 | - [ ] I have ensured `make bazel-test` pass. (**required**) 42 | -------------------------------------------------------------------------------- /envpool/core/envpool.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Garena Online Private Limited 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef ENVPOOL_CORE_ENVPOOL_H_ 18 | #define ENVPOOL_CORE_ENVPOOL_H_ 19 | 20 | #include 21 | #include 22 | 23 | #include "envpool/core/env_spec.h" 24 | 25 | /** 26 | * Templated subclass of EnvPool, to be overrided by the real EnvPool. 27 | */ 28 | template 29 | class EnvPool { 30 | public: 31 | EnvSpec spec; 32 | using Spec = EnvSpec; 33 | using State = NamedVector>; 34 | using Action = NamedVector>; 35 | explicit EnvPool(EnvSpec spec) : spec(std::move(spec)) {} 36 | virtual ~EnvPool() = default; 37 | 38 | protected: 39 | virtual void Send(const std::vector& action) { 40 | throw std::runtime_error("send not implemented"); 41 | } 42 | virtual void Send(std::vector&& action) { 43 | throw std::runtime_error("send not implemented"); 44 | } 45 | virtual std::vector Recv() { 46 | throw std::runtime_error("recv not implemented"); 47 | } 48 | virtual void Reset(const Array& env_ids) { 49 | throw std::runtime_error("reset not implemented"); 50 | } 51 | }; 52 | 53 | #endif // ENVPOOL_CORE_ENVPOOL_H_ 54 | -------------------------------------------------------------------------------- /envpool/minigrid/impl/minigrid_env.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2023 Garena Online Private Limited 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef ENVPOOL_MINIGRID_IMPL_MINIGRID_ENV_H_ 18 | #define ENVPOOL_MINIGRID_IMPL_MINIGRID_ENV_H_ 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | #include "envpool/core/array.h" 25 | #include "envpool/minigrid/impl/utils.h" 26 | 27 | namespace minigrid { 28 | 29 | class MiniGridEnv { 30 | protected: 31 | int width_; 32 | int height_; 33 | int max_steps_{100}; 34 | int step_count_{0}; 35 | int agent_view_size_{7}; 36 | bool see_through_walls_{false}; 37 | bool done_{true}; 38 | std::pair agent_start_pos_; 39 | std::pair agent_pos_; 40 | int agent_start_dir_; 41 | int agent_dir_; 42 | std::mt19937* gen_ref_; 43 | std::vector> grid_; 44 | WorldObj carrying_; 45 | 46 | public: 47 | MiniGridEnv() { carrying_ = WorldObj(kEmpty); } 48 | void MiniGridReset(); 49 | float MiniGridStep(Act act); 50 | void PlaceAgent(int start_x = 0, int start_y = 0, int end_x = -1, 51 | int end_y = -1); 52 | void GenImage(const Array& obs); 53 | virtual void GenGrid() {} 54 | }; 55 | 56 | } // namespace minigrid 57 | 58 | #endif // ENVPOOL_MINIGRID_IMPL_MINIGRID_ENV_H_ 59 | -------------------------------------------------------------------------------- /envpool/box2d/box2d_envpool.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Garena Online Private Limited 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "envpool/box2d/bipedal_walker.h" 16 | #include "envpool/box2d/car_racing.h" 17 | #include "envpool/box2d/lunar_lander_continuous.h" 18 | #include "envpool/box2d/lunar_lander_discrete.h" 19 | #include "envpool/core/py_envpool.h" 20 | 21 | using CarRacingEnvSpec = PyEnvSpec; 22 | using CarRacingEnvPool = PyEnvPool; 23 | 24 | using BipedalWalkerEnvSpec = PyEnvSpec; 25 | using BipedalWalkerEnvPool = PyEnvPool; 26 | 27 | using LunarLanderContinuousEnvSpec = 28 | PyEnvSpec; 29 | using LunarLanderContinuousEnvPool = 30 | PyEnvPool; 31 | 32 | using LunarLanderDiscreteEnvSpec = PyEnvSpec; 33 | using LunarLanderDiscreteEnvPool = PyEnvPool; 34 | 35 | PYBIND11_MODULE(box2d_envpool, m) { 36 | REGISTER(m, CarRacingEnvSpec, CarRacingEnvPool) 37 | REGISTER(m, BipedalWalkerEnvSpec, BipedalWalkerEnvPool) 38 | REGISTER(m, LunarLanderContinuousEnvSpec, LunarLanderContinuousEnvPool) 39 | REGISTER(m, LunarLanderDiscreteEnvSpec, LunarLanderDiscreteEnvPool) 40 | } 41 | -------------------------------------------------------------------------------- /envpool/dummy/BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | load("@pip_requirements//:requirements.bzl", "requirement") 16 | load("@pybind11_bazel//:build_defs.bzl", "pybind_extension") 17 | 18 | py_library( 19 | name = "dummy", 20 | srcs = ["__init__.py"], 21 | data = [":dummy_envpool.so"], 22 | deps = ["//envpool/python:api"], 23 | ) 24 | 25 | cc_library( 26 | name = "dummy_envpool_h", 27 | hdrs = ["dummy_envpool.h"], 28 | deps = [ 29 | "//envpool/core:async_envpool", 30 | "//envpool/core:env", 31 | "//envpool/core:env_spec", 32 | ], 33 | ) 34 | 35 | cc_test( 36 | name = "dummy_envpool_test", 37 | size = "enormous", 38 | srcs = ["dummy_envpool_test.cc"], 39 | deps = [ 40 | ":dummy_envpool_h", 41 | "@com_google_googletest//:gtest_main", 42 | ], 43 | ) 44 | 45 | py_test( 46 | name = "dummy_py_envpool_test", 47 | srcs = ["dummy_py_envpool_test.py"], 48 | data = ["dummy_envpool.so"], 49 | deps = [ 50 | requirement("numpy"), 51 | requirement("absl-py"), 52 | ], 53 | ) 54 | 55 | pybind_extension( 56 | name = "dummy_envpool", 57 | srcs = [ 58 | "dummy_envpool.cc", 59 | ], 60 | linkopts = [ 61 | "-ldl", 62 | ], 63 | deps = [ 64 | ":dummy_envpool_h", 65 | "//envpool/core:py_envpool", 66 | ], 67 | ) 68 | -------------------------------------------------------------------------------- /envpool/python/lax.py: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | """Provide xla mixin for envpool.""" 15 | 16 | from abc import ABC 17 | from typing import Any, Callable, Dict, Optional, Tuple, Union 18 | 19 | from dm_env import TimeStep 20 | from jax import numpy as jnp 21 | 22 | from .xla_template import make_xla 23 | 24 | 25 | class XlaMixin(ABC): 26 | """Mixin to provide XLA for envpool class.""" 27 | 28 | def xla(self: Any) -> Tuple[Any, Callable, Callable, Callable]: 29 | """Return the XLA version of send/recv/step functions.""" 30 | _handle, _recv, _send = make_xla(self) 31 | 32 | def recv(handle: jnp.ndarray) -> Union[TimeStep, Tuple]: 33 | ret = _recv(handle) 34 | new_handle = ret[0] 35 | state_list = ret[1:] 36 | return new_handle, self._to(state_list, reset=False, return_info=True) 37 | 38 | def send( 39 | handle: jnp.ndarray, 40 | action: Union[Dict[str, Any], jnp.ndarray], 41 | env_id: Optional[jnp.ndarray] = None 42 | ) -> Any: 43 | action = self._from(action, env_id) 44 | self._check_action(action) 45 | return _send(handle, *action) 46 | 47 | def step( 48 | handle: jnp.ndarray, 49 | action: Union[Dict[str, Any], jnp.ndarray], 50 | env_id: Optional[jnp.ndarray] = None 51 | ) -> Any: 52 | return recv(send(handle, action, env_id)) 53 | 54 | return _handle, recv, send, step 55 | -------------------------------------------------------------------------------- /envpool/core/type_utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Garena Online Private Limited 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef ENVPOOL_CORE_TYPE_UTILS_H_ 18 | #define ENVPOOL_CORE_TYPE_UTILS_H_ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | template 26 | struct any_match; 27 | 28 | template class Tuple, typename... Ts> 29 | struct any_match> : std::disjunction...> {}; 30 | 31 | template 32 | struct all_match; 33 | 34 | template class Tuple, typename... Ts> 35 | struct all_match> : std::conjunction...> {}; 36 | 37 | template 38 | struct all_convertible; 39 | 40 | template class Tuple, typename... Fs> 41 | struct all_convertible> 42 | : std::conjunction...> {}; 43 | 44 | template 45 | constexpr bool is_tuple_v = false; // NOLINT 46 | template 47 | constexpr bool is_tuple_v> = true; // NOLINT 48 | 49 | template 50 | constexpr bool is_vector_v = false; // NOLINT 51 | template 52 | constexpr bool is_vector_v> = true; // NOLINT 53 | 54 | #endif // ENVPOOL_CORE_TYPE_UTILS_H_ 55 | -------------------------------------------------------------------------------- /envpool/mujoco/gym/mujoco_gym_envpool_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Garena Online Private Limited 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | #include 17 | 18 | #include 19 | #include 20 | 21 | #include "envpool/mujoco/gym/half_cheetah.h" 22 | 23 | using MjcAction = typename mujoco_gym::HalfCheetahEnv::Action; 24 | using MjcState = typename mujoco_gym::HalfCheetahEnv::State; 25 | 26 | TEST(MjcEnvPoolTest, CheckAction) { 27 | auto config = mujoco_gym::HalfCheetahEnvSpec::kDefaultConfig; 28 | int num_envs = 128; 29 | config["num_envs"_] = num_envs; 30 | mujoco_gym::HalfCheetahEnvSpec spec(config); 31 | mujoco_gym::HalfCheetahEnvPool envpool(spec); 32 | Array all_env_ids(Spec({num_envs})); 33 | for (int i = 0; i < num_envs; ++i) { 34 | all_env_ids[i] = i; 35 | } 36 | envpool.Reset(all_env_ids); 37 | auto state_vec = envpool.Recv(); 38 | // construct action 39 | std::vector raw_action({Array(Spec({num_envs})), 40 | Array(Spec({num_envs})), 41 | Array(Spec({num_envs, 6}))}); 42 | MjcAction action(raw_action); 43 | for (int i = 0; i < num_envs; ++i) { 44 | action["env_id"_][i] = i; 45 | action["players.env_id"_][i] = i; 46 | for (int j = 0; j < 6; ++j) { 47 | action["action"_][i][j] = (i + j + 1) / 100.0; 48 | } 49 | } 50 | // send 51 | envpool.Send(action); 52 | state_vec = envpool.Recv(); 53 | } 54 | -------------------------------------------------------------------------------- /envpool/procgen/registration.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | """Procgen env registration.""" 15 | from envpool.registration import register 16 | 17 | # 16 games in Procgen 18 | # https://github.com/openai/procgen/blob/0.10.7/procgen/src/game.cpp#L56-L66 19 | procgen_game_config = [ 20 | ("bigfish", 6000, [0, 1]), 21 | ("bossfight", 4000, [0, 1]), 22 | ("caveflyer", 1000, [0, 1, 10]), 23 | ("chaser", 1000, [0, 1, 2]), 24 | ("climber", 1000, [0, 1]), 25 | ("coinrun", 1000, [0, 1]), 26 | ("dodgeball", 1000, [0, 1, 2, 10]), 27 | ("fruitbot", 1000, [0, 1]), 28 | ("heist", 1000, [0, 1, 10]), 29 | ("jumper", 1000, [0, 1, 10]), 30 | ("leaper", 500, [0, 1, 2]), 31 | ("maze", 500, [0, 1, 10]), 32 | ("miner", 1000, [0, 1, 10]), 33 | ("ninja", 1000, [0, 1]), 34 | ("plunder", 4000, [0, 1]), 35 | ("starpilot", 1000, [0, 1, 2]), 36 | ] 37 | 38 | distribution = { 39 | 0: "Easy", 40 | 1: "Hard", 41 | 2: "Extreme", 42 | 10: "Memory", 43 | } 44 | 45 | for env_name, timeout, dist_mode in procgen_game_config: 46 | for dist_value in dist_mode: 47 | dist_name = distribution[dist_value] 48 | register( 49 | task_id=f"{env_name.capitalize()}{dist_name}-v0", 50 | import_path="envpool.procgen", 51 | spec_cls="ProcgenEnvSpec", 52 | dm_cls="ProcgenDMEnvPool", 53 | gym_cls="ProcgenGymEnvPool", 54 | gymnasium_cls="ProcgenGymnasiumEnvPool", 55 | env_name=env_name, 56 | distribution_mode=dist_value, 57 | max_episode_steps=timeout, 58 | ) 59 | -------------------------------------------------------------------------------- /envpool/core/circular_buffer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Garena Online Private Limited 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef ENVPOOL_CORE_CIRCULAR_BUFFER_H_ 18 | #define ENVPOOL_CORE_CIRCULAR_BUFFER_H_ 19 | 20 | #ifndef MOODYCAMEL_DELETE_FUNCTION 21 | #define MOODYCAMEL_DELETE_FUNCTION = delete 22 | #endif 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #include "lightweightsemaphore.h" 33 | 34 | template 35 | class CircularBuffer { 36 | protected: 37 | std::size_t size_; 38 | moodycamel::LightweightSemaphore sem_get_; 39 | moodycamel::LightweightSemaphore sem_put_; 40 | std::vector buffer_; 41 | std::atomic head_; 42 | std::atomic tail_; 43 | 44 | public: 45 | explicit CircularBuffer(std::size_t size) 46 | : size_(size), sem_put_(size), buffer_(size), head_(0), tail_(0) {} 47 | 48 | template 49 | void Put(T&& v) { 50 | while (!sem_put_.wait()) { 51 | } 52 | uint64_t tail = tail_.fetch_add(1); 53 | auto offset = tail % size_; 54 | buffer_[offset] = std::forward(v); 55 | sem_get_.signal(); 56 | } 57 | 58 | V Get() { 59 | while (!sem_get_.wait()) { 60 | } 61 | uint64_t head = head_.fetch_add(1); 62 | auto offset = head % size_; 63 | V v = std::move(buffer_[offset]); 64 | sem_put_.signal(); 65 | return v; 66 | } 67 | }; 68 | 69 | #endif // ENVPOOL_CORE_CIRCULAR_BUFFER_H_ 70 | -------------------------------------------------------------------------------- /envpool/toy_text/BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | load("@pip_requirements//:requirements.bzl", "requirement") 16 | load("@pybind11_bazel//:build_defs.bzl", "pybind_extension") 17 | 18 | package(default_visibility = ["//visibility:public"]) 19 | 20 | cc_library( 21 | name = "toy_text_env", 22 | hdrs = [ 23 | "blackjack.h", 24 | "catch.h", 25 | "cliffwalking.h", 26 | "frozen_lake.h", 27 | "nchain.h", 28 | "taxi.h", 29 | ], 30 | deps = [ 31 | "//envpool/core:async_envpool", 32 | ], 33 | ) 34 | 35 | pybind_extension( 36 | name = "toy_text_envpool", 37 | srcs = [ 38 | "toy_text.cc", 39 | ], 40 | deps = [ 41 | ":toy_text_env", 42 | "//envpool/core:py_envpool", 43 | ], 44 | ) 45 | 46 | py_library( 47 | name = "toy_text", 48 | srcs = ["__init__.py"], 49 | data = [":toy_text_envpool.so"], 50 | deps = ["//envpool/python:api"], 51 | ) 52 | 53 | py_library( 54 | name = "toy_text_registration", 55 | srcs = ["registration.py"], 56 | deps = [ 57 | "//envpool:registration", 58 | ], 59 | ) 60 | 61 | py_test( 62 | name = "toy_text_test", 63 | srcs = ["toy_text_test.py"], 64 | deps = [ 65 | ":toy_text", 66 | ":toy_text_registration", 67 | requirement("absl-py"), 68 | requirement("dm_env"), 69 | requirement("gym"), 70 | requirement("numpy"), 71 | requirement("scipy"), 72 | requirement("pygame"), 73 | ], 74 | ) 75 | -------------------------------------------------------------------------------- /envpool/toy_text/toy_text.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Garena Online Private Limited 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "envpool/core/py_envpool.h" 16 | #include "envpool/toy_text/blackjack.h" 17 | #include "envpool/toy_text/catch.h" 18 | #include "envpool/toy_text/cliffwalking.h" 19 | #include "envpool/toy_text/frozen_lake.h" 20 | #include "envpool/toy_text/nchain.h" 21 | #include "envpool/toy_text/taxi.h" 22 | 23 | using CatchEnvSpec = PyEnvSpec; 24 | using CatchEnvPool = PyEnvPool; 25 | 26 | using FrozenLakeEnvSpec = PyEnvSpec; 27 | using FrozenLakeEnvPool = PyEnvPool; 28 | 29 | using TaxiEnvSpec = PyEnvSpec; 30 | using TaxiEnvPool = PyEnvPool; 31 | 32 | using NChainEnvSpec = PyEnvSpec; 33 | using NChainEnvPool = PyEnvPool; 34 | 35 | using CliffWalkingEnvSpec = PyEnvSpec; 36 | using CliffWalkingEnvPool = PyEnvPool; 37 | 38 | using BlackjackEnvSpec = PyEnvSpec; 39 | using BlackjackEnvPool = PyEnvPool; 40 | 41 | PYBIND11_MODULE(toy_text_envpool, m) { 42 | REGISTER(m, CatchEnvSpec, CatchEnvPool) 43 | REGISTER(m, FrozenLakeEnvSpec, FrozenLakeEnvPool) 44 | REGISTER(m, TaxiEnvSpec, TaxiEnvPool) 45 | REGISTER(m, NChainEnvSpec, NChainEnvPool) 46 | REGISTER(m, CliffWalkingEnvSpec, CliffWalkingEnvPool) 47 | REGISTER(m, BlackjackEnvSpec, BlackjackEnvPool) 48 | } 49 | -------------------------------------------------------------------------------- /third_party/common.bzl: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | """Rule for simple expansion of template files. 16 | 17 | This performs a simple search over the template file for the keys in 18 | substitutions, and replaces them with the corresponding values. 19 | 20 | Typical usage: 21 | :: 22 | 23 | load("/tools/build_rules/template_rule", "expand_header_template") 24 | template_rule( 25 | name = "ExpandMyTemplate", 26 | src = "my.template", 27 | out = "my.txt", 28 | substitutions = { 29 | "$VAR1": "foo", 30 | "$VAR2": "bar", 31 | } 32 | ) 33 | 34 | Args: 35 | name: The name of the rule. 36 | template: The template file to expand. 37 | out: The destination of the expanded file. 38 | substitutions: A dictionary mapping strings to their substitutions. 39 | """ 40 | 41 | def template_rule_impl(ctx): 42 | """Helper function for template_rule.""" 43 | ctx.actions.expand_template( 44 | template = ctx.file.src, 45 | output = ctx.outputs.out, 46 | substitutions = ctx.attr.substitutions, 47 | ) 48 | 49 | template_rule = rule( 50 | attrs = { 51 | "src": attr.label( 52 | mandatory = True, 53 | allow_single_file = True, 54 | ), 55 | "substitutions": attr.string_dict(mandatory = True), 56 | "out": attr.output(mandatory = True), 57 | }, 58 | # output_to_genfiles is required for header files. 59 | output_to_genfiles = True, 60 | implementation = template_rule_impl, 61 | ) 62 | -------------------------------------------------------------------------------- /envpool/classic_control/classic_control.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Garena Online Private Limited 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "envpool/classic_control/acrobot.h" 16 | #include "envpool/classic_control/cartpole.h" 17 | #include "envpool/classic_control/mountain_car.h" 18 | #include "envpool/classic_control/mountain_car_continuous.h" 19 | #include "envpool/classic_control/pendulum.h" 20 | #include "envpool/core/py_envpool.h" 21 | 22 | using CartPoleEnvSpec = PyEnvSpec; 23 | using CartPoleEnvPool = PyEnvPool; 24 | 25 | using PendulumEnvSpec = PyEnvSpec; 26 | using PendulumEnvPool = PyEnvPool; 27 | 28 | using MountainCarEnvSpec = PyEnvSpec; 29 | using MountainCarEnvPool = PyEnvPool; 30 | 31 | using MountainCarContinuousEnvSpec = 32 | PyEnvSpec; 33 | using MountainCarContinuousEnvPool = 34 | PyEnvPool; 35 | 36 | using AcrobotEnvSpec = PyEnvSpec; 37 | using AcrobotEnvPool = PyEnvPool; 38 | 39 | PYBIND11_MODULE(classic_control_envpool, m) { 40 | REGISTER(m, CartPoleEnvSpec, CartPoleEnvPool) 41 | REGISTER(m, PendulumEnvSpec, PendulumEnvPool) 42 | REGISTER(m, MountainCarEnvSpec, MountainCarEnvPool) 43 | REGISTER(m, MountainCarContinuousEnvSpec, MountainCarContinuousEnvPool) 44 | REGISTER(m, AcrobotEnvSpec, AcrobotEnvPool) 45 | } 46 | -------------------------------------------------------------------------------- /envpool/classic_control/BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | load("@pip_requirements//:requirements.bzl", "requirement") 16 | load("@pybind11_bazel//:build_defs.bzl", "pybind_extension") 17 | 18 | package(default_visibility = ["//visibility:public"]) 19 | 20 | cc_library( 21 | name = "classic_control_env", 22 | hdrs = [ 23 | "acrobot.h", 24 | "cartpole.h", 25 | "mountain_car.h", 26 | "mountain_car_continuous.h", 27 | "pendulum.h", 28 | ], 29 | deps = [ 30 | "//envpool/core:async_envpool", 31 | ], 32 | ) 33 | 34 | pybind_extension( 35 | name = "classic_control_envpool", 36 | srcs = ["classic_control.cc"], 37 | deps = [ 38 | ":classic_control_env", 39 | "//envpool/core:py_envpool", 40 | ], 41 | ) 42 | 43 | py_library( 44 | name = "classic_control", 45 | srcs = ["__init__.py"], 46 | data = [":classic_control_envpool.so"], 47 | deps = ["//envpool/python:api"], 48 | ) 49 | 50 | py_library( 51 | name = "classic_control_registration", 52 | srcs = ["registration.py"], 53 | deps = [ 54 | "//envpool:registration", 55 | ], 56 | ) 57 | 58 | py_test( 59 | name = "classic_control_test", 60 | size = "enormous", 61 | srcs = ["classic_control_test.py"], 62 | deps = [ 63 | ":classic_control", 64 | ":classic_control_registration", 65 | requirement("absl-py"), 66 | requirement("dm_env"), 67 | requirement("gym"), 68 | requirement("numpy"), 69 | requirement("pygame"), 70 | ], 71 | ) 72 | -------------------------------------------------------------------------------- /envpool/BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | load("@pip_requirements//:requirements.bzl", "requirement") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | exports_files([ 20 | "workspace0.bzl", 21 | "workspace1.bzl", 22 | ]) 23 | 24 | py_library( 25 | name = "registration", 26 | srcs = ["registration.py"], 27 | ) 28 | 29 | py_library( 30 | name = "entry", 31 | srcs = ["entry.py"], 32 | deps = [ 33 | "//envpool/atari:atari_registration", 34 | "//envpool/box2d:box2d_registration", 35 | "//envpool/classic_control:classic_control_registration", 36 | "//envpool/mujoco:mujoco_dmc_registration", 37 | "//envpool/mujoco:mujoco_gym_registration", 38 | "//envpool/procgen:procgen_registration", 39 | "//envpool/toy_text:toy_text_registration", 40 | "//envpool/vizdoom:vizdoom_registration", 41 | ], 42 | ) 43 | 44 | py_library( 45 | name = "envpool", 46 | srcs = ["__init__.py"], 47 | deps = [ 48 | ":entry", 49 | ":registration", 50 | "//envpool/atari", 51 | "//envpool/box2d", 52 | "//envpool/classic_control", 53 | "//envpool/mujoco:mujoco_dmc", 54 | "//envpool/mujoco:mujoco_gym", 55 | "//envpool/procgen", 56 | "//envpool/python", 57 | "//envpool/toy_text", 58 | "//envpool/vizdoom", 59 | ], 60 | ) 61 | 62 | py_test( 63 | name = "make_test", 64 | srcs = ["make_test.py"], 65 | deps = [ 66 | ":envpool", 67 | requirement("absl-py"), 68 | ], 69 | ) 70 | -------------------------------------------------------------------------------- /envpool/mujoco/gym/registration.py: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | """Mujoco gym env registration.""" 15 | 16 | from envpool.registration import register 17 | 18 | gym_mujoco_envs = [ 19 | ("Ant", "v3", False, 1000), 20 | ("Ant", "v4", True, 1000), 21 | ("HalfCheetah", "v3", False, 1000), 22 | ("HalfCheetah", "v4", True, 1000), 23 | ("Hopper", "v3", False, 1000), 24 | ("Hopper", "v4", True, 1000), 25 | ("Humanoid", "v3", False, 1000), 26 | ("Humanoid", "v4", True, 1000), 27 | ("HumanoidStandup", "v2", False, 1000), 28 | ("HumanoidStandup", "v4", True, 1000), 29 | ("InvertedDoublePendulum", "v2", False, 1000), 30 | ("InvertedDoublePendulum", "v4", True, 1000), 31 | ("InvertedPendulum", "v2", False, 1000), 32 | ("InvertedPendulum", "v4", True, 1000), 33 | ("Pusher", "v2", False, 100), 34 | ("Pusher", "v4", True, 100), 35 | ("Reacher", "v2", False, 50), 36 | ("Reacher", "v4", True, 50), 37 | ("Swimmer", "v3", False, 1000), 38 | ("Swimmer", "v4", True, 1000), 39 | ("Walker2d", "v3", False, 1000), 40 | ("Walker2d", "v4", True, 1000), 41 | ] 42 | 43 | for task, version, post_constraint, max_episode_steps in gym_mujoco_envs: 44 | extra_args = {} 45 | if task in ["Ant", "Humanoid"] and version == "v3": 46 | extra_args["use_contact_force"] = True 47 | register( 48 | task_id=f"{task}-{version}", 49 | import_path="envpool.mujoco.gym", 50 | spec_cls=f"Gym{task}EnvSpec", 51 | dm_cls=f"Gym{task}DMEnvPool", 52 | gym_cls=f"Gym{task}GymEnvPool", 53 | gymnasium_cls=f"Gym{task}GymnasiumEnvPool", 54 | post_constraint=post_constraint, 55 | max_episode_steps=max_episode_steps, 56 | **extra_args, 57 | ) 58 | -------------------------------------------------------------------------------- /envpool/mujoco/dmc/utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Garena Online Private Limited 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef ENVPOOL_MUJOCO_DMC_UTILS_H_ 18 | #define ENVPOOL_MUJOCO_DMC_UTILS_H_ 19 | 20 | #include 21 | #include 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | namespace mujoco_dmc { 28 | 29 | using RandInt = std::uniform_int_distribution<>; 30 | using RandUniform = std::uniform_real_distribution<>; 31 | using RandNormal = std::normal_distribution<>; 32 | 33 | // xml related 34 | std::string GetFileContent(const std::string& base_path, 35 | const std::string& asset_name); 36 | std::string XMLRemoveByBodyName(const std::string& content, 37 | const std::vector& body_names); 38 | std::string XMLAddPoles(const std::string& content, int n_poles); 39 | std::string XMLMakeSwimmer(const std::string& content, int n_bodies); 40 | 41 | // the following id is not 1 on 1 mapping 42 | int GetQposId(mjModel* model, const std::string& name); 43 | int GetQvelId(mjModel* model, const std::string& name); 44 | int GetSensorId(mjModel* model, const std::string& name); 45 | 46 | // rewards 47 | enum class SigmoidType { 48 | kGaussian, 49 | kHyperbolic, 50 | kLongTail, 51 | kReciprocal, 52 | kCosine, 53 | kLinear, 54 | kQuadratic, 55 | kTanhSquared, 56 | }; 57 | 58 | double RewardTolerance(double x, double bound_min = 0.0, double bound_max = 0.0, 59 | double margin = 0.0, double value_at_margin = 0.1, 60 | SigmoidType sigmoid_type = SigmoidType::kGaussian); 61 | 62 | } // namespace mujoco_dmc 63 | 64 | #endif // ENVPOOL_MUJOCO_DMC_UTILS_H_ 65 | -------------------------------------------------------------------------------- /envpool/box2d/registration.py: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | """Box2D env registration.""" 15 | 16 | from envpool.registration import register 17 | 18 | register( 19 | task_id="CarRacing-v2", 20 | import_path="envpool.box2d", 21 | spec_cls="CarRacingEnvSpec", 22 | dm_cls="CarRacingDMEnvPool", 23 | gym_cls="CarRacingGymEnvPool", 24 | gymnasium_cls="CarRacingGymnasiumEnvPool", 25 | max_episode_steps=1000, 26 | ) 27 | 28 | register( 29 | task_id="BipedalWalker-v3", 30 | import_path="envpool.box2d", 31 | spec_cls="BipedalWalkerEnvSpec", 32 | dm_cls="BipedalWalkerDMEnvPool", 33 | gym_cls="BipedalWalkerGymEnvPool", 34 | gymnasium_cls="BipedalWalkerGymnasiumEnvPool", 35 | hardcore=False, 36 | max_episode_steps=1600, 37 | ) 38 | 39 | register( 40 | task_id="BipedalWalkerHardcore-v3", 41 | import_path="envpool.box2d", 42 | spec_cls="BipedalWalkerEnvSpec", 43 | dm_cls="BipedalWalkerDMEnvPool", 44 | gym_cls="BipedalWalkerGymEnvPool", 45 | gymnasium_cls="BipedalWalkerGymnasiumEnvPool", 46 | hardcore=True, 47 | max_episode_steps=2000, 48 | ) 49 | 50 | register( 51 | task_id="LunarLander-v2", 52 | import_path="envpool.box2d", 53 | spec_cls="LunarLanderDiscreteEnvSpec", 54 | dm_cls="LunarLanderDiscreteDMEnvPool", 55 | gym_cls="LunarLanderDiscreteGymEnvPool", 56 | gymnasium_cls="LunarLanderDiscreteGymnasiumEnvPool", 57 | max_episode_steps=1000, 58 | ) 59 | 60 | register( 61 | task_id="LunarLanderContinuous-v2", 62 | import_path="envpool.box2d", 63 | spec_cls="LunarLanderContinuousEnvSpec", 64 | dm_cls="LunarLanderContinuousDMEnvPool", 65 | gym_cls="LunarLanderContinuousGymEnvPool", 66 | gymnasium_cls="LunarLanderContinuousGymnasiumEnvPool", 67 | max_episode_steps=1000, 68 | ) 69 | -------------------------------------------------------------------------------- /envpool/core/dict_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Garena Online Private Limited 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "envpool/core/dict.h" 16 | 17 | #include 18 | #include 19 | 20 | #include 21 | #include 22 | 23 | TEST(DictTest, Keys) { 24 | auto d = MakeDict("abc"_.Bind(0.), "xyz"_.Bind(0.), "ijk"_.Bind(1)); 25 | auto keys = decltype(d)::StaticKeys(); 26 | auto dkeys = d.AllKeys(); 27 | EXPECT_EQ(dkeys[0], "abc"); 28 | EXPECT_EQ(dkeys[1], "xyz"); 29 | EXPECT_EQ(dkeys[2], "ijk"); 30 | EXPECT_EQ(std::get<0>(keys).Str(), "abc"); 31 | EXPECT_EQ(std::get<1>(keys).Str(), "xyz"); 32 | EXPECT_EQ(std::get<2>(keys).Str(), "ijk"); 33 | } 34 | 35 | TEST(DictTest, Values) { 36 | auto d = MakeDict("abc"_.Bind(0.), "xyz"_.Bind(0.), "ijk"_.Bind(1)); 37 | auto values = d.AllValues(); 38 | auto int_vector = d.AllValues(); 39 | EXPECT_EQ(std::get<0>(values), 0.); 40 | EXPECT_EQ(std::get<1>(values), 0.); 41 | EXPECT_EQ(std::get<2>(values), 1); 42 | EXPECT_EQ(int_vector[0], 0); 43 | EXPECT_EQ(int_vector[1], 0); 44 | EXPECT_EQ(int_vector[2], 1); 45 | } 46 | 47 | TEST(DictTest, Lookup) { 48 | auto d = MakeDict("abc"_.Bind(0.), "xyz"_.Bind(0.), "ijk"_.Bind(1)); 49 | EXPECT_EQ(d["abc"_], 0.); 50 | EXPECT_EQ(d["xyz"_], 0.); 51 | EXPECT_EQ(d["ijk"_], 1); 52 | } 53 | 54 | TEST(DictTest, Modification) { 55 | auto d = MakeDict("abc"_.Bind(0.), "xyz"_.Bind("123"), "ijk"_.Bind(1)); 56 | EXPECT_EQ(d["abc"_], 0.); 57 | EXPECT_EQ(d["xyz"_], "123"); 58 | EXPECT_EQ(d["ijk"_], 1); 59 | d["abc"_] = 1; 60 | d["xyz"_] = "456"; 61 | // force convert to int 62 | d["ijk"_] = 0.5; // NOLINT 63 | EXPECT_EQ(d["abc"_], 1); 64 | EXPECT_EQ(d["xyz"_], "456"); 65 | EXPECT_EQ(d["ijk"_], 0); 66 | } 67 | -------------------------------------------------------------------------------- /envpool/minigrid/impl/minigrid_empty_env.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Garena Online Private Limited 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "envpool/minigrid/impl/minigrid_empty_env.h" 16 | 17 | #include 18 | #include 19 | 20 | namespace minigrid { 21 | 22 | MiniGridEmptyEnv::MiniGridEmptyEnv(int size, 23 | std::pair agent_start_pos, 24 | int agent_start_dir, int max_steps, 25 | int agent_view_size) { 26 | width_ = size; 27 | height_ = size; 28 | agent_start_pos_ = agent_start_pos; 29 | agent_start_dir_ = agent_start_dir; 30 | see_through_walls_ = true; 31 | max_steps_ = max_steps; 32 | agent_view_size_ = agent_view_size; 33 | } 34 | 35 | void MiniGridEmptyEnv::GenGrid() { 36 | grid_.clear(); 37 | for (int i = 0; i < height_; ++i) { 38 | std::vector temp_vec(width_); 39 | for (int j = 0; j < width_; ++j) { 40 | temp_vec[j] = WorldObj(kEmpty); 41 | } 42 | grid_.emplace_back(temp_vec); 43 | } 44 | // generate the surrounding walls 45 | for (int i = 0; i < width_; ++i) { 46 | grid_[0][i] = WorldObj(kWall, kGrey); 47 | grid_[height_ - 1][i] = WorldObj(kWall, kGrey); 48 | } 49 | for (int i = 0; i < height_; ++i) { 50 | grid_[i][0] = WorldObj(kWall, kGrey); 51 | grid_[i][width_ - 1] = WorldObj(kWall, kGrey); 52 | } 53 | // place a goal square in the bottom-right corner 54 | grid_[height_ - 2][width_ - 2] = WorldObj(kGoal, kGreen); 55 | // place the agent 56 | if (agent_start_pos_.first == -1) { 57 | PlaceAgent(1, 1, width_ - 2, height_ - 2); 58 | } else { 59 | agent_pos_ = agent_start_pos_; 60 | agent_dir_ = agent_start_dir_; 61 | } 62 | } 63 | 64 | } // namespace minigrid 65 | -------------------------------------------------------------------------------- /docs/content/contributing.rst: -------------------------------------------------------------------------------- 1 | Contributing to EnvPool 2 | ======================= 3 | 4 | 5 | Build From Source 6 | ----------------- 7 | 8 | See :doc:`/content/build`. 9 | 10 | 11 | Adding A New Environment 12 | ------------------------ 13 | 14 | See :doc:`/content/new_env`. 15 | 16 | 17 | Lint Check 18 | ---------- 19 | 20 | We use several tools to secure code quality, including 21 | 22 | - PEP8 code style: flake8, yapf, isort; 23 | - Type check: mypy; 24 | - C++ Google-style: cpplint, clang-format, clang-tidy; 25 | - Bazel build file: buildifier; 26 | - License: addlicense; 27 | - Documentation: pydocstyle, doc8. 28 | 29 | To make things easier, we create several shortcuts as follows. 30 | 31 | To automatically format the code, run: 32 | 33 | .. code-block:: bash 34 | 35 | make format 36 | 37 | To check if everything conforms to the specification, run: 38 | 39 | .. code-block:: bash 40 | 41 | make lint 42 | 43 | 44 | Test Locally 45 | ------------ 46 | 47 | This command will run automatic tests in the main directory: 48 | 49 | .. code-block:: bash 50 | 51 | make bazel-test 52 | 53 | If you only want to debug for Bazel build: 54 | 55 | .. code-block:: bash 56 | 57 | # this is for general use case 58 | make bazel-debug 59 | # this is for a special folder "envpool/classic_control" 60 | bazel build //envpool/classic_control --config=debug 61 | 62 | 63 | If you'd like to run only a single test, for example, testing Mujoco 64 | integration; however, you don't want to build other stuff such as OpenCV: 65 | 66 | .. code-block:: bash 67 | 68 | bazel test --test_output=all //envpool/mujoco:mujoco_gym_align_test --config=test 69 | # or alternatively 70 | cd bazel-bin/envpool/mujoco/mujoco_gym_align_test.runfiles/envpool/ 71 | ./envpool/mujoco/mujoco_gym_align_test 72 | 73 | Feel free to customize the command in ``Makefile``! 74 | 75 | 76 | Documentation 77 | ------------- 78 | 79 | Documentations are written under the ``docs/`` directory as ReStructuredText 80 | (``.rst``) files. ``index.rst`` is the main page. A Tutorial on 81 | ReStructuredText can be found `here 82 | `_. 83 | 84 | To compile documentation into the web page, run: 85 | 86 | .. code-block:: bash 87 | 88 | make doc 89 | 90 | And the website is in `http://localhost:8000 `_ 91 | -------------------------------------------------------------------------------- /envpool/box2d/box2d_deterministic_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | """Unit tests for box2d environments deterministic check.""" 15 | 16 | from typing import Any 17 | 18 | import numpy as np 19 | from absl.testing import absltest 20 | 21 | import envpool.box2d.registration # noqa: F401 22 | from envpool.registration import make_gym 23 | 24 | 25 | class _Box2dEnvPoolDeterministicTest(absltest.TestCase): 26 | 27 | def run_deterministic_check( 28 | self, 29 | task_id: str, 30 | num_envs: int = 4, 31 | **kwargs: Any, 32 | ) -> None: 33 | env0 = make_gym(task_id, num_envs=num_envs, seed=0, **kwargs) 34 | env1 = make_gym(task_id, num_envs=num_envs, seed=0, **kwargs) 35 | env2 = make_gym(task_id, num_envs=num_envs, seed=1, **kwargs) 36 | act_space = env0.action_space 37 | for _ in range(5000): 38 | action = np.array([act_space.sample() for _ in range(num_envs)]) 39 | obs0, rew0, terminated, truncated, info0 = env0.step(action) 40 | obs1, rew1, terminated, truncated, info1 = env1.step(action) 41 | obs2, rew2, terminated, truncated, info2 = env2.step(action) 42 | np.testing.assert_allclose(obs0, obs1) 43 | self.assertFalse(np.allclose(obs0, obs2)) 44 | 45 | def test_car_racing(self) -> None: 46 | self.run_deterministic_check("CarRacing-v2") 47 | self.run_deterministic_check("CarRacing-v2", max_episode_steps=3) 48 | 49 | def test_bipedal_walker(self) -> None: 50 | self.run_deterministic_check("BipedalWalker-v3") 51 | self.run_deterministic_check("BipedalWalkerHardcore-v3") 52 | 53 | def test_lunar_lander(self) -> None: 54 | self.run_deterministic_check("LunarLanderContinuous-v2") 55 | self.run_deterministic_check("LunarLander-v2") 56 | 57 | 58 | if __name__ == "__main__": 59 | absltest.main() 60 | -------------------------------------------------------------------------------- /envpool/procgen/BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | load("@pip_requirements//:requirements.bzl", "requirement") 16 | load("@pybind11_bazel//:build_defs.bzl", "pybind_extension") 17 | 18 | package(default_visibility = ["//visibility:public"]) 19 | 20 | genrule( 21 | name = "gen_procgen_assets", 22 | srcs = ["@procgen//:procgen_assets"], 23 | outs = ["assets"], 24 | cmd = "mkdir -p $(OUTS) && cp -r $(SRCS) $(OUTS)", 25 | ) 26 | 27 | cc_library( 28 | name = "procgen_env", 29 | hdrs = ["procgen_env.h"], 30 | data = [ 31 | ":gen_procgen_assets", 32 | ], 33 | deps = [ 34 | "//envpool/core:async_envpool", 35 | "@procgen", 36 | ], 37 | ) 38 | 39 | cc_test( 40 | name = "procgen_env_test", 41 | srcs = ["procgen_env_test.cc"], 42 | deps = [ 43 | ":procgen_env", 44 | "@com_google_googletest//:gtest_main", 45 | ], 46 | ) 47 | 48 | pybind_extension( 49 | name = "procgen_envpool", 50 | srcs = ["procgen_envpool.cc"], 51 | linkopts = [ 52 | "-ldl", 53 | ], 54 | deps = [ 55 | ":procgen_env", 56 | "//envpool/core:py_envpool", 57 | ], 58 | ) 59 | 60 | py_library( 61 | name = "procgen", 62 | srcs = ["__init__.py"], 63 | data = [":procgen_envpool.so"], 64 | deps = ["//envpool/python:api"], 65 | ) 66 | 67 | py_library( 68 | name = "procgen_registration", 69 | srcs = ["registration.py"], 70 | deps = [ 71 | "//envpool:registration", 72 | ], 73 | ) 74 | 75 | py_test( 76 | name = "procgen_test", 77 | srcs = ["procgen_test.py"], 78 | deps = [ 79 | ":procgen", 80 | ":procgen_registration", 81 | requirement("numpy"), 82 | requirement("absl-py"), 83 | requirement("gym"), 84 | ], 85 | ) 86 | -------------------------------------------------------------------------------- /envpool/box2d/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | """Box2D env in EnvPool.""" 15 | 16 | from envpool.python.api import py_env 17 | 18 | from .box2d_envpool import ( 19 | _BipedalWalkerEnvPool, 20 | _BipedalWalkerEnvSpec, 21 | _CarRacingEnvPool, 22 | _CarRacingEnvSpec, 23 | _LunarLanderContinuousEnvPool, 24 | _LunarLanderContinuousEnvSpec, 25 | _LunarLanderDiscreteEnvPool, 26 | _LunarLanderDiscreteEnvSpec, 27 | ) 28 | 29 | ( 30 | BipedalWalkerEnvSpec, BipedalWalkerDMEnvPool, BipedalWalkerGymEnvPool, 31 | BipedalWalkerGymnasiumEnvPool 32 | ) = py_env(_BipedalWalkerEnvSpec, _BipedalWalkerEnvPool) 33 | 34 | ( 35 | CarRacingEnvSpec, CarRacingDMEnvPool, CarRacingGymEnvPool, 36 | CarRacingGymnasiumEnvPool 37 | ) = py_env(_CarRacingEnvSpec, _CarRacingEnvPool) 38 | 39 | ( 40 | LunarLanderContinuousEnvSpec, 41 | LunarLanderContinuousDMEnvPool, 42 | LunarLanderContinuousGymEnvPool, 43 | LunarLanderContinuousGymnasiumEnvPool, 44 | ) = py_env(_LunarLanderContinuousEnvSpec, _LunarLanderContinuousEnvPool) 45 | 46 | ( 47 | LunarLanderDiscreteEnvSpec, 48 | LunarLanderDiscreteDMEnvPool, 49 | LunarLanderDiscreteGymEnvPool, 50 | LunarLanderDiscreteGymnasiumEnvPool, 51 | ) = py_env(_LunarLanderDiscreteEnvSpec, _LunarLanderDiscreteEnvPool) 52 | 53 | __all__ = [ 54 | "CarRacingEnvSpec", 55 | "CarRacingDMEnvPool", 56 | "CarRacingGymEnvPool", 57 | "BipedalWalkerEnvSpec", 58 | "BipedalWalkerDMEnvPool", 59 | "BipedalWalkerGymEnvPool", 60 | "BipedalWalkerGymnasiumEnvPool", 61 | "LunarLanderContinuousEnvSpec", 62 | "LunarLanderContinuousDMEnvPool", 63 | "LunarLanderContinuousGymEnvPool", 64 | "LunarLanderContinuousGymnasiumEnvPool", 65 | "LunarLanderDiscreteEnvSpec", 66 | "LunarLanderDiscreteDMEnvPool", 67 | "LunarLanderDiscreteGymEnvPool", 68 | "LunarLanderDiscreteGymnasiumEnvPool", 69 | ] 70 | -------------------------------------------------------------------------------- /docs/env/classic_control.rst: -------------------------------------------------------------------------------- 1 | Classic Control 2 | =============== 3 | 4 | All of the environments in classic control borrow from `Gym 5 | `_. 6 | 7 | 8 | CartPole-v0/1 9 | ------------- 10 | 11 | `gym cartpole source code 12 | `_ 13 | 14 | A pole is attached by an un-actuated joint to a cart, which moves along a 15 | frictionless track. The pendulum starts upright, and the goal is to prevent it 16 | from falling over by increasing and reducing the cart's velocity. 17 | 18 | The difference between ``CartPole-v0`` and ``CartPole-v1`` is that the former 19 | has ``max_episode_steps`` 200 with 195 reward threshold, while the latter has 20 | ``max_episode_steps`` 500 with 475 reward threshold. 21 | 22 | 23 | Pendulum-v0, Pendulum-v1 24 | ------------------------ 25 | 26 | `gym pendulum source code 27 | `_ 28 | 29 | The inverted pendulum swing-up problem is a classic problem in the control 30 | literature. In this version of the problem, the pendulum starts in a random 31 | position, and the goal is to swing it up to stay upright. 32 | 33 | You can find the difference between ``Pendulum-v0`` and ``Pendulum-v1`` in 34 | `this pull request `_. 35 | 36 | 37 | MountainCar-v0, MountainCarContinuous-v0 38 | ---------------------------------------- 39 | 40 | `gym mountain_car source code 41 | `_ 42 | and `gym mountain_car continuous source code 43 | `_ 44 | 45 | The agent (a car) is started at the bottom of a valley. For any given state the 46 | agent may choose to accelerate to the left, right or cease any acceleration. 47 | 48 | 49 | Acrobot-v1 50 | ---------- 51 | 52 | `gym acrobot source code 53 | `_ 54 | 55 | Acrobot is a 2-link pendulum with only the second joint actuated. Initially, 56 | both links point downwards. The goal is to swing the end-effector at a height 57 | at least the length of one link above the base. Both links can swing freely and 58 | can pass by each other, i.e., they don't collide when they have the same angle. 59 | -------------------------------------------------------------------------------- /envpool/procgen/procgen_env_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Garena Online Private Limited 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "envpool/procgen/procgen_env.h" 16 | 17 | #include 18 | #include 19 | 20 | #include 21 | 22 | using ProcgenState = procgen::ProcgenEnv::State; 23 | using ProcgenAction = procgen::ProcgenEnv::Action; 24 | 25 | TEST(PRocgenEnvTest, BasicStep) { 26 | std::srand(std::time(nullptr)); 27 | auto config = procgen::ProcgenEnvSpec::kDefaultConfig; 28 | std::size_t batch = 4; 29 | config["num_envs"_] = batch; 30 | config["batch_size"_] = batch; 31 | config["seed"_] = 0; 32 | config["env_name"_] = "coinrun"; 33 | int total_iter = 10000; 34 | procgen::ProcgenEnvSpec spec(config); 35 | procgen::ProcgenEnvPool envpool(spec); 36 | TArray all_env_ids(Spec({static_cast(batch)})); 37 | for (std::size_t i = 0; i < batch; ++i) { 38 | all_env_ids[i] = i; 39 | } 40 | envpool.Reset(all_env_ids); 41 | ProcgenAction action; 42 | for (int i = 0; i < total_iter; ++i) { 43 | ProcgenState state(envpool.Recv()); 44 | EXPECT_EQ(state["obs"_].Shape(), 45 | std::vector({batch, 3, 64, 64})); 46 | uint8_t* data = static_cast(state["obs"_].Data()); 47 | int index = 0; 48 | for (std::size_t j = 0; j < batch; ++j) { 49 | // ensure there's no black screen in each frame 50 | int sum = 0; 51 | for (int k = 0; k < 64 * 64 * 3; ++k) { 52 | sum += data[index++]; 53 | } 54 | EXPECT_NE(sum, 0) << i << " " << j; 55 | } 56 | action["env_id"_] = state["info:env_id"_]; 57 | action["players.env_id"_] = state["info:env_id"_]; 58 | action["action"_] = TArray(Spec({static_cast(batch)})); 59 | for (std::size_t j = 0; j < batch; ++j) { 60 | action["action"_][j] = std::rand() % 15; 61 | } 62 | envpool.Send(action); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [metadata] 2 | name = envpool 3 | version = 0.8.4 4 | author = "EnvPool Contributors" 5 | author_email = "sail@sea.com" 6 | description = "C++-based high-performance parallel environment execution engine (vectorized env) for general RL environments." 7 | long_description = file: README.md 8 | long_description_content_type = text/markdown 9 | url = https://github.com/sail-sg/envpool 10 | project_urls = 11 | Bug Tracker = https://github.com/sail-sg/envpool/issues 12 | classifiers = 13 | Programming Language :: Python :: 3 14 | Programming Language :: Python :: 3.7 15 | Programming Language :: Python :: 3.8 16 | Programming Language :: Python :: 3.9 17 | Programming Language :: Python :: 3.10 18 | Programming Language :: Python :: 3.11 19 | License :: OSI Approved :: Apache Software License 20 | Operating System :: POSIX :: Linux 21 | Topic :: Scientific/Engineering :: Artificial Intelligence 22 | 23 | [options] 24 | packages = find: 25 | python_requires = >=3.7 26 | install_requires = 27 | dm-env>=1.4 28 | gym>=0.18 29 | gymnasium>=0.26 30 | numpy>=1.19 31 | types-protobuf>=3.17.3 32 | typing-extensions 33 | packaging 34 | optree>=0.6.0 35 | 36 | [options.packages.find] 37 | include = envpool* 38 | 39 | [options.package_data] 40 | envpool = 41 | **/*_envpool.so 42 | atari/roms/*.bin 43 | mujoco/*.so.* 44 | mujoco/assets*/**/*.xml 45 | procgen/assets/**/*.png 46 | vizdoom/bin/* 47 | vizdoom/maps/* 48 | 49 | [yapf] 50 | based_on_style = yapf 51 | spaces_before_comment = 2 52 | dedent_closing_brackets = true 53 | column_limit = 80 54 | continuation_indent_width = 2 55 | 56 | [flake8] 57 | exclude = 58 | .git 59 | indent_size = 2 60 | extend-ignore = B024 61 | max-line-length = 80 62 | 63 | [pydocstyle] 64 | convention = google 65 | 66 | [isort] 67 | profile = black 68 | multi_line_output = 3 69 | indent = 2 70 | line_length = 80 71 | 72 | [mypy] 73 | allow_redefinition = True 74 | check_untyped_defs = True 75 | disallow_incomplete_defs = True 76 | disallow_untyped_defs = True 77 | ignore_missing_imports = True 78 | no_implicit_optional = True 79 | pretty = True 80 | show_error_codes = True 81 | show_error_context = True 82 | show_traceback = True 83 | strict_equality = True 84 | strict_optional = True 85 | warn_no_return = True 86 | warn_redundant_casts = True 87 | warn_unreachable = True 88 | warn_unused_configs = True 89 | warn_unused_ignores = True 90 | 91 | [doc8] 92 | max-line-length = 250 93 | -------------------------------------------------------------------------------- /envpool/core/action_buffer_queue_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Garena Online Private Limited 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "envpool/core/action_buffer_queue.h" 16 | 17 | #include 18 | #include 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include "ThreadPool.h" 26 | #include "envpool/core/dict.h" 27 | #include "envpool/core/spec.h" 28 | 29 | using ActionSlice = typename ActionBufferQueue::ActionSlice; 30 | 31 | TEST(ActionBufferQueueTest, Concurrent) { 32 | std::size_t num_envs = 1000; 33 | ActionBufferQueue queue(num_envs); 34 | std::srand(std::time(nullptr)); 35 | std::size_t mul = 2000; 36 | std::vector actions; 37 | // enqueue all envs 38 | for (std::size_t i = 0; i < num_envs; ++i) { 39 | actions.push_back(ActionSlice{ 40 | .env_id = static_cast(i), .order = -1, .force_reset = false}); 41 | } 42 | queue.EnqueueBulk(actions); 43 | std::vector> flag(mul); 44 | std::vector env_num(mul); 45 | for (std::size_t m = 0; m < mul; ++m) { 46 | flag[m] = 1; 47 | env_num[m] = std::rand() % (num_envs - 1) + 1; 48 | } 49 | 50 | std::thread send([&] { 51 | for (std::size_t m = 0; m < mul; ++m) { 52 | while (flag[m] == 1) { 53 | } 54 | actions.clear(); 55 | for (std::size_t i = 0; i < env_num[m]; ++i) { 56 | actions.push_back(ActionSlice{ 57 | .env_id = static_cast(i), .order = -1, .force_reset = false}); 58 | } 59 | queue.EnqueueBulk(actions); 60 | } 61 | }); 62 | std::thread recv([&] { 63 | for (std::size_t m = 0; m < mul; ++m) { 64 | for (std::size_t i = 0; i < env_num[m]; ++i) { 65 | queue.Dequeue(); 66 | } 67 | flag[m] = 0; 68 | } 69 | }); 70 | recv.join(); 71 | send.join(); 72 | EXPECT_EQ(queue.SizeApprox(), num_envs); 73 | } 74 | -------------------------------------------------------------------------------- /envpool/minigrid/minigrid_deterministic_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | """Unit tests for minigrid environments check.""" 15 | 16 | from typing import Any 17 | 18 | import numpy as np 19 | from absl.testing import absltest 20 | 21 | import envpool.minigrid.registration # noqa: F401 22 | from envpool.registration import make_gym 23 | 24 | 25 | class _MiniGridEnvPoolDeterministicTest(absltest.TestCase): 26 | 27 | def run_deterministic_check( 28 | self, 29 | task_id: str, 30 | num_envs: int = 4, 31 | total: int = 5000, 32 | seed: int = 1, 33 | **kwargs: Any, 34 | ) -> None: 35 | env0 = make_gym(task_id, num_envs=num_envs, seed=0, **kwargs) 36 | env1 = make_gym(task_id, num_envs=num_envs, seed=0, **kwargs) 37 | env2 = make_gym(task_id, num_envs=num_envs, seed=1, **kwargs) 38 | act_space = env0.action_space 39 | act_space.seed(seed) 40 | same_count = 0 41 | for _ in range(total): 42 | action = np.array([act_space.sample() for _ in range(num_envs)]) 43 | obs0, rew0, terminated, truncated, info0 = env0.step(action) 44 | obs1, rew1, terminated, truncated, info1 = env1.step(action) 45 | obs2, rew2, terminated, truncated, info2 = env2.step(action) 46 | np.testing.assert_allclose(obs0["image"], obs1["image"]) 47 | np.testing.assert_allclose(obs0["direction"], obs1["direction"]) 48 | # TODO: this may fail because the available state in minigrid env 49 | # is limited 50 | same_count += np.allclose(obs0["image"], obs2["image"]) and np.allclose( 51 | obs0["direction"], obs2["direction"] 52 | ) 53 | assert same_count == 0, f"{same_count=}" 54 | 55 | def test_empty(self) -> None: 56 | self.run_deterministic_check("MiniGrid-Empty-Random-5x5-v0") 57 | self.run_deterministic_check("MiniGrid-Empty-Random-6x6-v0") 58 | 59 | 60 | if __name__ == "__main__": 61 | absltest.main() 62 | -------------------------------------------------------------------------------- /envpool/minigrid/BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | load("@pip_requirements//:requirements.bzl", "requirement") 16 | load("@pybind11_bazel//:build_defs.bzl", "pybind_extension") 17 | 18 | package(default_visibility = ["//visibility:public"]) 19 | 20 | cc_library( 21 | name = "minigrid_env", 22 | srcs = [ 23 | "impl/minigrid_empty_env.cc", 24 | "impl/minigrid_env.cc", 25 | ], 26 | hdrs = [ 27 | "empty.h", 28 | "impl/minigrid_empty_env.h", 29 | "impl/minigrid_env.h", 30 | "impl/utils.h", 31 | ], 32 | deps = [ 33 | "//envpool/core:async_envpool", 34 | ], 35 | ) 36 | 37 | pybind_extension( 38 | name = "minigrid_envpool", 39 | srcs = ["minigrid.cc"], 40 | deps = [ 41 | ":minigrid_env", 42 | "//envpool/core:py_envpool", 43 | ], 44 | ) 45 | 46 | py_library( 47 | name = "minigrid", 48 | srcs = ["__init__.py"], 49 | data = [":minigrid_envpool.so"], 50 | deps = ["//envpool/python:api"], 51 | ) 52 | 53 | py_library( 54 | name = "minigrid_registration", 55 | srcs = ["registration.py"], 56 | deps = [ 57 | "//envpool:registration", 58 | ], 59 | ) 60 | 61 | py_test( 62 | name = "minigrid_align_test", 63 | size = "enormous", 64 | srcs = ["minigrid_align_test.py"], 65 | deps = [ 66 | ":minigrid", 67 | ":minigrid_registration", 68 | requirement("absl-py"), 69 | requirement("gym"), 70 | requirement("numpy"), 71 | requirement("minigrid"), 72 | ], 73 | ) 74 | 75 | py_test( 76 | name = "minigrid_deterministic_test", 77 | size = "enormous", 78 | srcs = ["minigrid_deterministic_test.py"], 79 | deps = [ 80 | ":minigrid", 81 | ":minigrid_registration", 82 | requirement("absl-py"), 83 | requirement("gym"), 84 | requirement("numpy"), 85 | ], 86 | ) 87 | -------------------------------------------------------------------------------- /third_party/opencv/opencv.BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake") 2 | 3 | filegroup( 4 | name = "srcs", 5 | srcs = glob(["**"]), 6 | visibility = ["//visibility:public"], 7 | ) 8 | 9 | # Shows a standard library using the Ninja generator 10 | cmake( 11 | name = "opencv", 12 | generate_args = [ 13 | "-GNinja", 14 | "-DBUILD_SHARED_LIBS=OFF", 15 | "-DBUILD_EXAMPLES=OFF", 16 | "-DBUILD_opencv_apps=OFF", 17 | "-DBUILD_opencv_calib3d=OFF", 18 | "-DBUILD_opencv_core=ON", 19 | "-DBUILD_opencv_features2d=ON", 20 | "-DBUILD_opencv_flann=ON", 21 | "-DBUILD_opencv_gapi=ON", 22 | "-DBUILD_opencv_highgui=OFF", 23 | "-DBUILD_opencv_imgcodecs=OFF", 24 | "-DBUILD_opencv_imgproc=ON", 25 | "-DBUILD_opencv_java_bindings_generator=OFF", 26 | "-DBUILD_opencv_js=OFF", 27 | "-DBUILD_opencv_js_bindings_generator=OFF", 28 | "-DBUILD_opencv_ml=OFF", 29 | "-DBUILD_opencv_objc_bindings_generator=OFF", 30 | "-DBUILD_opencv_objdetect=OFF", 31 | "-DBUILD_opencv_photo=OFF", 32 | "-DBUILD_opencv_python3=OFF", 33 | "-DBUILD_opencv_python_bindings_generator=OFF", 34 | "-DBUILD_opencv_python_tests=OFF", 35 | "-DBUILD_opencv_stitching=OFF", 36 | "-DBUILD_opencv_ts=OFF", 37 | "-DBUILD_opencv_video=OFF", 38 | "-DBUILD_opencv_videoio=OFF", 39 | "-DBUILD_opencv_world=OFF", 40 | "-DWITH_CUDA=OFF", 41 | "-DWITH_EIGEN=ON", 42 | "-DWITH_FFMPEG=ON", 43 | "-DWITH_GTK=OFF", 44 | "-DWITH_GTK_2_X=OFF", 45 | "-DWITH_IPP=OFF", 46 | "-DWITH_ITT=OFF", 47 | "-DWITH_JASPER=ON", 48 | "-DWITH_JPEG=ON", 49 | "-DWITH_LAPACK=ON", 50 | "-DWITH_ONNX=OFF", 51 | "-DWITH_OPENCL=OFF", 52 | "-DWITH_OPENGL=OFF", 53 | "-DWITH_OPENJPEG=OFF", 54 | "-DWITH_PLAIDML=OFF", 55 | "-DWITH_PNG=OFF", 56 | "-DWITH_PROTOBUF=OFF", 57 | "-DWITH_PTHREADS_PF=ON", 58 | "-DWITH_QT=OFF", 59 | "-DWITH_TBB=OFF", 60 | "-DWITH_TIFF=OFF", 61 | ], 62 | lib_source = ":srcs", 63 | linkopts = [ 64 | "-ldl", 65 | ], 66 | out_include_dir = "include/opencv4", 67 | out_static_libs = [ 68 | "libopencv_imgproc.a", 69 | "libopencv_features2d.a", 70 | "libopencv_flann.a", 71 | "libopencv_core.a", 72 | ], 73 | visibility = ["//visibility:public"], 74 | ) 75 | -------------------------------------------------------------------------------- /envpool/box2d/lunar_lander_discrete.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Garena Online Private Limited 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef ENVPOOL_BOX2D_LUNAR_LANDER_DISCRETE_H_ 18 | #define ENVPOOL_BOX2D_LUNAR_LANDER_DISCRETE_H_ 19 | 20 | #include "envpool/box2d/lunar_lander_env.h" 21 | #include "envpool/core/async_envpool.h" 22 | #include "envpool/core/env.h" 23 | 24 | namespace box2d { 25 | 26 | class LunarLanderDiscreteEnvFns { 27 | public: 28 | static decltype(auto) DefaultConfig() { 29 | return MakeDict("reward_threshold"_.Bind(200.0)); 30 | } 31 | template 32 | static decltype(auto) StateSpec(const Config& conf) { 33 | return MakeDict("obs"_.Bind(Spec({8}))); 34 | } 35 | template 36 | static decltype(auto) ActionSpec(const Config& conf) { 37 | return MakeDict("action"_.Bind(Spec({-1}, {0, 3}))); 38 | } 39 | }; 40 | 41 | using LunarLanderDiscreteEnvSpec = EnvSpec; 42 | 43 | class LunarLanderDiscreteEnv : public Env, 44 | public LunarLanderBox2dEnv { 45 | public: 46 | LunarLanderDiscreteEnv(const Spec& spec, int env_id) 47 | : Env(spec, env_id), 48 | LunarLanderBox2dEnv(false, spec.config["max_episode_steps"_]) {} 49 | 50 | bool IsDone() override { return done_; } 51 | 52 | void Reset() override { 53 | LunarLanderReset(&gen_); 54 | WriteState(); 55 | } 56 | 57 | void Step(const Action& action) override { 58 | int act = action["action"_]; 59 | LunarLanderStep(&gen_, act, 0, 0); 60 | WriteState(); 61 | } 62 | 63 | private: 64 | void WriteState() { 65 | State state = Allocate(); 66 | state["reward"_] = reward_; 67 | state["obs"_].Assign(obs_.begin(), obs_.size()); 68 | } 69 | }; 70 | 71 | using LunarLanderDiscreteEnvPool = AsyncEnvPool; 72 | 73 | } // namespace box2d 74 | 75 | #endif // ENVPOOL_BOX2D_LUNAR_LANDER_DISCRETE_H_ 76 | -------------------------------------------------------------------------------- /envpool/minigrid/registration.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | """Minigrid env registration.""" 15 | 16 | from envpool.registration import register 17 | 18 | register( 19 | task_id="MiniGrid-Empty-5x5-v0", 20 | import_path="envpool.minigrid", 21 | spec_cls="EmptyEnvSpec", 22 | dm_cls="EmptyDMEnvPool", 23 | gym_cls="EmptyGymEnvPool", 24 | gymnasium_cls="EmptyGymnasiumEnvPool", 25 | max_episode_steps=100, 26 | size=5, 27 | ) 28 | 29 | register( 30 | task_id="MiniGrid-Empty-Random-5x5-v0", 31 | import_path="envpool.minigrid", 32 | spec_cls="EmptyEnvSpec", 33 | dm_cls="EmptyDMEnvPool", 34 | gym_cls="EmptyGymEnvPool", 35 | gymnasium_cls="EmptyGymnasiumEnvPool", 36 | max_episode_steps=100, 37 | size=5, 38 | agent_start_pos=(-1, -1), 39 | agent_start_dir=-1, 40 | ) 41 | 42 | register( 43 | task_id="MiniGrid-Empty-6x6-v0", 44 | import_path="envpool.minigrid", 45 | spec_cls="EmptyEnvSpec", 46 | dm_cls="EmptyDMEnvPool", 47 | gym_cls="EmptyGymEnvPool", 48 | gymnasium_cls="EmptyGymnasiumEnvPool", 49 | max_episode_steps=144, 50 | size=6, 51 | ) 52 | 53 | register( 54 | task_id="MiniGrid-Empty-Random-6x6-v0", 55 | import_path="envpool.minigrid", 56 | spec_cls="EmptyEnvSpec", 57 | dm_cls="EmptyDMEnvPool", 58 | gym_cls="EmptyGymEnvPool", 59 | gymnasium_cls="EmptyGymnasiumEnvPool", 60 | max_episode_steps=144, 61 | size=6, 62 | agent_start_pos=(-1, -1), 63 | agent_start_dir=-1, 64 | ) 65 | 66 | register( 67 | task_id="MiniGrid-Empty-8x8-v0", 68 | import_path="envpool.minigrid", 69 | spec_cls="EmptyEnvSpec", 70 | dm_cls="EmptyDMEnvPool", 71 | gym_cls="EmptyGymEnvPool", 72 | gymnasium_cls="EmptyGymnasiumEnvPool", 73 | max_episode_steps=256, 74 | size=8, 75 | ) 76 | 77 | register( 78 | task_id="MiniGrid-Empty-16x16-v0", 79 | import_path="envpool.minigrid", 80 | spec_cls="EmptyEnvSpec", 81 | dm_cls="EmptyDMEnvPool", 82 | gym_cls="EmptyGymEnvPool", 83 | gymnasium_cls="EmptyGymnasiumEnvPool", 84 | max_episode_steps=1024, 85 | size=16, 86 | ) 87 | -------------------------------------------------------------------------------- /envpool/core/tuple_utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Garena Online Private Limited 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef ENVPOOL_CORE_TUPLE_UTILS_H_ 18 | #define ENVPOOL_CORE_TUPLE_UTILS_H_ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | template 27 | struct Index; 28 | 29 | template 30 | struct Index> { 31 | static constexpr std::size_t kValue = 0; 32 | }; 33 | 34 | template 35 | struct Index> { 36 | static constexpr std::size_t kValue = 37 | 1 + Index>::kValue; 38 | }; 39 | 40 | template 41 | decltype(auto) ApplyZip(F&& f, K&& k, V&& v, 42 | std::index_sequence /*unused*/) { 43 | return std::invoke(std::forward(f), 44 | std::make_tuple(I, std::get(std::forward(k)), 45 | std::get(std::forward(v)))...); 46 | } 47 | 48 | template 49 | using tuple_cat_t = decltype(std::tuple_cat(std::declval()...)); // NOLINT 50 | 51 | template 52 | decltype(auto) TupleFromVectorImpl(std::index_sequence /*unused*/, 53 | const std::vector& arguments) { 54 | return TupleType(arguments[Is]...); 55 | } 56 | 57 | template 58 | decltype(auto) TupleFromVectorImpl(std::index_sequence /*unused*/, 59 | std::vector&& arguments) { 60 | return TupleType(std::move(arguments[Is])...); 61 | } 62 | 63 | template 64 | decltype(auto) TupleFromVector(V&& arguments) { 65 | return TupleFromVectorImpl( 66 | std::make_index_sequence>{}, 67 | std::forward(arguments)); 68 | } 69 | 70 | #endif // ENVPOOL_CORE_TUPLE_UTILS_H_ 71 | -------------------------------------------------------------------------------- /envpool/toy_text/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | """Toy text env in EnvPool.""" 15 | 16 | from envpool.python.api import py_env 17 | 18 | from .toy_text_envpool import ( 19 | _BlackjackEnvPool, 20 | _BlackjackEnvSpec, 21 | _CatchEnvPool, 22 | _CatchEnvSpec, 23 | _CliffWalkingEnvPool, 24 | _CliffWalkingEnvSpec, 25 | _FrozenLakeEnvPool, 26 | _FrozenLakeEnvSpec, 27 | _NChainEnvPool, 28 | _NChainEnvSpec, 29 | _TaxiEnvPool, 30 | _TaxiEnvSpec, 31 | ) 32 | 33 | (CatchEnvSpec, CatchDMEnvPool, CatchGymEnvPool, 34 | CatchGymnasiumEnvPool) = py_env(_CatchEnvSpec, _CatchEnvPool) 35 | 36 | ( 37 | FrozenLakeEnvSpec, FrozenLakeDMEnvPool, FrozenLakeGymEnvPool, 38 | FrozenLakeGymnasiumEnvPool 39 | ) = py_env(_FrozenLakeEnvSpec, _FrozenLakeEnvPool) 40 | 41 | (TaxiEnvSpec, TaxiDMEnvPool, TaxiGymEnvPool, 42 | TaxiGymnasiumEnvPool) = py_env(_TaxiEnvSpec, _TaxiEnvPool) 43 | 44 | (NChainEnvSpec, NChainDMEnvPool, NChainGymEnvPool, 45 | NChainGymnasiumEnvPool) = py_env(_NChainEnvSpec, _NChainEnvPool) 46 | 47 | ( 48 | CliffWalkingEnvSpec, CliffWalkingDMEnvPool, CliffWalkingGymEnvPool, 49 | CliffWalkingGymnasiumEnvPool 50 | ) = py_env(_CliffWalkingEnvSpec, _CliffWalkingEnvPool) 51 | 52 | ( 53 | BlackjackEnvSpec, BlackjackDMEnvPool, BlackjackGymEnvPool, 54 | BlackjackGymnasiumEnvPool 55 | ) = py_env(_BlackjackEnvSpec, _BlackjackEnvPool) 56 | 57 | __all__ = [ 58 | "CatchEnvSpec", 59 | "CatchDMEnvPool", 60 | "CatchGymEnvPool", 61 | "CatchGymnasiumEnvPool", 62 | "FrozenLakeEnvSpec", 63 | "FrozenLakeDMEnvPool", 64 | "FrozenLakeGymEnvPool", 65 | "FrozenLakeGymnasiumEnvPool", 66 | "TaxiEnvSpec", 67 | "TaxiDMEnvPool", 68 | "TaxiGymEnvPool", 69 | "TaxiGymnasiumEnvPool", 70 | "NChainEnvSpec", 71 | "NChainDMEnvPool", 72 | "NChainGymEnvPool", 73 | "NChainGymnasiumEnvPool", 74 | "CliffWalkingEnvSpec", 75 | "CliffWalkingDMEnvPool", 76 | "CliffWalkingGymEnvPool", 77 | "CliffWalkingGymnasiumEnvPool", 78 | "BlackjackEnvSpec", 79 | "BlackjackDMEnvPool", 80 | "BlackjackGymEnvPool", 81 | "BlackjackGymnasiumEnvPool", 82 | ] 83 | -------------------------------------------------------------------------------- /envpool/box2d/lunar_lander_continuous.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Garena Online Private Limited 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef ENVPOOL_BOX2D_LUNAR_LANDER_CONTINUOUS_H_ 18 | #define ENVPOOL_BOX2D_LUNAR_LANDER_CONTINUOUS_H_ 19 | 20 | #include "envpool/box2d/lunar_lander_env.h" 21 | #include "envpool/core/async_envpool.h" 22 | #include "envpool/core/env.h" 23 | 24 | namespace box2d { 25 | 26 | class LunarLanderContinuousEnvFns { 27 | public: 28 | static decltype(auto) DefaultConfig() { 29 | return MakeDict("reward_threshold"_.Bind(200.0)); 30 | } 31 | template 32 | static decltype(auto) StateSpec(const Config& conf) { 33 | return MakeDict("obs"_.Bind(Spec({8}))); 34 | } 35 | template 36 | static decltype(auto) ActionSpec(const Config& conf) { 37 | return MakeDict("action"_.Bind(Spec({2}, {-1.0, 1.0}))); 38 | } 39 | }; 40 | 41 | using LunarLanderContinuousEnvSpec = EnvSpec; 42 | 43 | class LunarLanderContinuousEnv : public Env, 44 | public LunarLanderBox2dEnv { 45 | public: 46 | LunarLanderContinuousEnv(const Spec& spec, int env_id) 47 | : Env(spec, env_id), 48 | LunarLanderBox2dEnv(true, spec.config["max_episode_steps"_]) {} 49 | 50 | bool IsDone() override { return done_; } 51 | 52 | void Reset() override { 53 | LunarLanderReset(&gen_); 54 | WriteState(); 55 | } 56 | 57 | void Step(const Action& action) override { 58 | float action0 = action["action"_][0]; 59 | float action1 = action["action"_][1]; 60 | LunarLanderStep(&gen_, 0, action0, action1); 61 | WriteState(); 62 | } 63 | 64 | private: 65 | void WriteState() { 66 | State state = Allocate(); 67 | state["reward"_] = reward_; 68 | state["obs"_].Assign(obs_.begin(), obs_.size()); 69 | } 70 | }; 71 | 72 | using LunarLanderContinuousEnvPool = AsyncEnvPool; 73 | 74 | } // namespace box2d 75 | 76 | #endif // ENVPOOL_BOX2D_LUNAR_LANDER_CONTINUOUS_H_ 77 | -------------------------------------------------------------------------------- /envpool/classic_control/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | """Classic control env in EnvPool.""" 15 | 16 | from envpool.python.api import py_env 17 | 18 | from .classic_control_envpool import ( 19 | _AcrobotEnvPool, 20 | _AcrobotEnvSpec, 21 | _CartPoleEnvPool, 22 | _CartPoleEnvSpec, 23 | _MountainCarContinuousEnvPool, 24 | _MountainCarContinuousEnvSpec, 25 | _MountainCarEnvPool, 26 | _MountainCarEnvSpec, 27 | _PendulumEnvPool, 28 | _PendulumEnvSpec, 29 | ) 30 | 31 | ( 32 | CartPoleEnvSpec, 33 | CartPoleDMEnvPool, 34 | CartPoleGymEnvPool, 35 | CartPoleGymnasiumEnvPool, 36 | ) = py_env(_CartPoleEnvSpec, _CartPoleEnvPool) 37 | 38 | ( 39 | PendulumEnvSpec, 40 | PendulumDMEnvPool, 41 | PendulumGymEnvPool, 42 | PendulumGymnasiumEnvPool, 43 | ) = py_env(_PendulumEnvSpec, _PendulumEnvPool) 44 | 45 | ( 46 | MountainCarEnvSpec, 47 | MountainCarDMEnvPool, 48 | MountainCarGymEnvPool, 49 | MountainCarGymnasiumEnvPool, 50 | ) = py_env(_MountainCarEnvSpec, _MountainCarEnvPool) 51 | 52 | ( 53 | MountainCarContinuousEnvSpec, MountainCarContinuousDMEnvPool, 54 | MountainCarContinuousGymEnvPool, MountainCarContinuousGymnasiumEnvPool 55 | ) = py_env(_MountainCarContinuousEnvSpec, _MountainCarContinuousEnvPool) 56 | 57 | ( 58 | AcrobotEnvSpec, 59 | AcrobotDMEnvPool, 60 | AcrobotGymEnvPool, 61 | AcrobotGymnasiumEnvPool, 62 | ) = py_env(_AcrobotEnvSpec, _AcrobotEnvPool) 63 | 64 | __all__ = [ 65 | "CartPoleEnvSpec", 66 | "CartPoleDMEnvPool", 67 | "CartPoleGymEnvPool", 68 | "CartPoleGymnasiumEnvPool", 69 | "PendulumEnvSpec", 70 | "PendulumDMEnvPool", 71 | "PendulumGymEnvPool", 72 | "PendulumGymnasiumEnvPool", 73 | "MountainCarEnvSpec", 74 | "MountainCarDMEnvPool", 75 | "MountainCarGymEnvPool", 76 | "MountainCarGymnasiumEnvPool", 77 | "MountainCarContinuousEnvSpec", 78 | "MountainCarContinuousDMEnvPool", 79 | "MountainCarContinuousGymEnvPool", 80 | "MountainCarContinuousGymnasiumEnvPool", 81 | "AcrobotEnvSpec", 82 | "AcrobotDMEnvPool", 83 | "AcrobotGymEnvPool", 84 | "AcrobotGymnasiumEnvPool", 85 | ] 86 | -------------------------------------------------------------------------------- /envpool/core/action_buffer_queue.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Garena Online Private Limited 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef ENVPOOL_CORE_ACTION_BUFFER_QUEUE_H_ 18 | #define ENVPOOL_CORE_ACTION_BUFFER_QUEUE_H_ 19 | 20 | #ifndef MOODYCAMEL_DELETE_FUNCTION 21 | #define MOODYCAMEL_DELETE_FUNCTION = delete 22 | #endif 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | #include "envpool/core/array.h" 30 | #include "lightweightsemaphore.h" 31 | 32 | /** 33 | * Lock-free action buffer queue. 34 | */ 35 | class ActionBufferQueue { 36 | public: 37 | struct ActionSlice { 38 | int env_id; 39 | int order; 40 | bool force_reset; 41 | }; 42 | 43 | protected: 44 | std::atomic alloc_ptr_, done_ptr_; 45 | std::size_t queue_size_; 46 | std::vector queue_; 47 | moodycamel::LightweightSemaphore sem_, sem_enqueue_, sem_dequeue_; 48 | 49 | public: 50 | explicit ActionBufferQueue(std::size_t num_envs) 51 | : alloc_ptr_(0), 52 | done_ptr_(0), 53 | queue_size_(num_envs * 2), 54 | queue_(queue_size_), 55 | sem_(0), 56 | sem_enqueue_(1), 57 | sem_dequeue_(1) {} 58 | 59 | void EnqueueBulk(const std::vector& action) { 60 | // ensure only one enqueue_bulk happens at any time 61 | while (!sem_enqueue_.wait()) { 62 | } 63 | uint64_t pos = alloc_ptr_.fetch_add(action.size()); 64 | for (std::size_t i = 0; i < action.size(); ++i) { 65 | queue_[(pos + i) % queue_size_] = action[i]; 66 | } 67 | sem_.signal(action.size()); 68 | sem_enqueue_.signal(1); 69 | } 70 | 71 | ActionSlice Dequeue() { 72 | while (!sem_.wait()) { 73 | } 74 | while (!sem_dequeue_.wait()) { 75 | } 76 | auto ptr = done_ptr_.fetch_add(1); 77 | auto ret = queue_[ptr % queue_size_]; 78 | sem_dequeue_.signal(1); 79 | return ret; 80 | } 81 | 82 | std::size_t SizeApprox() { 83 | return static_cast(alloc_ptr_ - done_ptr_); 84 | } 85 | }; 86 | 87 | #endif // ENVPOOL_CORE_ACTION_BUFFER_QUEUE_H_ 88 | -------------------------------------------------------------------------------- /examples/make_env.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import numpy as np 16 | 17 | import envpool 18 | 19 | 20 | def make_env() -> None: 21 | # EnvPool now supports gym and dm_env API. 22 | # You need to manually specify which API you want to make. 23 | # For example, make dm_env: 24 | env_dm = envpool.make("Pong-v5", env_type="dm") 25 | # it is the same as 26 | env_dm0 = envpool.make_dm("Pong-v5") 27 | # and gym: 28 | env_gym = envpool.make("Pong-v5", env_type="gym") 29 | # it is the same as 30 | env_gym0 = envpool.make_gym("Pong-v5") 31 | # For easier debugging, you can directly print env, 32 | # and it includes all available configurations to this env 33 | print(env_dm) 34 | print(env_gym) 35 | assert str(env_dm) == str(env_dm0) 36 | assert str(env_gym) == str(env_gym0) 37 | # To use this configuration, just add these kwargs into `make` function. 38 | # For example, open an envpool that contains 4 envs: 39 | env = envpool.make_gym("Pong-v5", num_envs=4) 40 | print(env) 41 | 42 | 43 | def make_spec() -> None: 44 | # In the past, we need to make a fake env to get the actual observation 45 | # and action space. 46 | # But in envpool, you can do this by `make_spec`. 47 | # It can accept the same kwargs as `make`. 48 | spec = envpool.make_spec("Pong-v5", num_envs=4) 49 | print(spec) 50 | # You can get both observation and action space from spec 51 | gym_obs_space = spec.observation_space 52 | gym_act_space = spec.action_space 53 | dm_obs_spec = spec.observation_spec() 54 | dm_act_spec = spec.action_spec() 55 | np.testing.assert_allclose(gym_obs_space.high, 255) 56 | assert gym_act_space.n == 6 # 6 action in Pong 57 | np.testing.assert_allclose(dm_obs_spec.obs.maximum, 255) 58 | assert dm_act_spec.num_values, 6 59 | 60 | 61 | def check_info_optim() -> None: 62 | env = envpool.make_gym("Ant-v3") 63 | info = env.step(np.array([env.action_space.sample()]), np.array([0]))[-1] 64 | assert "qpos0" not in info and "qvel0" not in info 65 | 66 | 67 | if __name__ == "__main__": 68 | make_env() 69 | make_spec() 70 | check_info_optim() 71 | -------------------------------------------------------------------------------- /benchmark/test_gym.py: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import argparse 16 | import time 17 | 18 | import gym 19 | import tqdm 20 | from atari_wrappers import wrap_deepmind 21 | 22 | 23 | def run(env, num_envs, total_step, async_): 24 | if env == "atari": 25 | task_id = "PongNoFrameskip-v4" 26 | frame_skip = 4 27 | if num_envs == 1: 28 | env = wrap_deepmind( 29 | gym.make(task_id), 30 | episode_life=False, 31 | clip_rewards=False, 32 | frame_stack=4, 33 | ) 34 | else: 35 | env = gym.vector.make( 36 | task_id, num_envs, async_, lambda e: 37 | wrap_deepmind(e, episode_life=False, clip_rewards=False, frame_stack=4) 38 | ) 39 | elif env == "mujoco": 40 | task_id = "Ant-v3" 41 | frame_skip = 5 42 | if num_envs == 1: 43 | env = gym.make(task_id) 44 | else: 45 | env = gym.vector.make(task_id, num_envs, async_) 46 | elif env == "box2d": 47 | task_id = "LunarLander-v2" 48 | frame_skip = 1 49 | if num_envs == 1: 50 | env = gym.make(task_id) 51 | else: 52 | env = gym.vector.make(task_id, num_envs, async_) 53 | else: 54 | raise NotImplementedError(f"Unknown env {env}") 55 | env.seed(0) 56 | env.reset() 57 | action = env.action_space.sample() 58 | done = False 59 | t = time.time() 60 | for _ in tqdm.trange(total_step): 61 | if num_envs == 1: 62 | if done: 63 | done = False 64 | env.reset() 65 | else: 66 | done = env.step(action)[2] 67 | else: 68 | env.step(action) 69 | print(f"FPS = {frame_skip * total_step * num_envs / (time.time() - t):.2f}") 70 | 71 | 72 | if __name__ == "__main__": 73 | parser = argparse.ArgumentParser() 74 | parser.add_argument( 75 | "--env", type=str, default="atari", choices=["atari", "mujoco", "box2d"] 76 | ) 77 | parser.add_argument("--async_", action="store_true") 78 | parser.add_argument("--num-envs", type=int, default=10) 79 | parser.add_argument("--total-step", type=int, default=5000) 80 | args = parser.parse_args() 81 | print(args) 82 | run(args.env, args.num_envs, args.total_step, args.async_) 83 | -------------------------------------------------------------------------------- /envpool/toy_text/registration.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | """Classic control env registration.""" 15 | 16 | from envpool.registration import register 17 | 18 | register( 19 | task_id="Catch-v0", 20 | import_path="envpool.toy_text", 21 | spec_cls="CatchEnvSpec", 22 | dm_cls="CatchDMEnvPool", 23 | gym_cls="CatchGymEnvPool", 24 | gymnasium_cls="CatchGymnasiumEnvPool", 25 | height=10, 26 | width=5, 27 | ) 28 | 29 | register( 30 | task_id="FrozenLake-v1", 31 | import_path="envpool.toy_text", 32 | spec_cls="FrozenLakeEnvSpec", 33 | dm_cls="FrozenLakeDMEnvPool", 34 | gym_cls="FrozenLakeGymEnvPool", 35 | gymnasium_cls="FrozenLakeGymnasiumEnvPool", 36 | size=4, 37 | max_episode_steps=100, 38 | reward_threshold=0.7, 39 | ) 40 | 41 | register( 42 | task_id="FrozenLake8x8-v1", 43 | import_path="envpool.toy_text", 44 | spec_cls="FrozenLakeEnvSpec", 45 | dm_cls="FrozenLakeDMEnvPool", 46 | gym_cls="FrozenLakeGymEnvPool", 47 | gymnasium_cls="FrozenLakeGymnasiumEnvPool", 48 | size=8, 49 | max_episode_steps=200, 50 | reward_threshold=0.85, 51 | ) 52 | 53 | register( 54 | task_id="Taxi-v3", 55 | import_path="envpool.toy_text", 56 | spec_cls="TaxiEnvSpec", 57 | dm_cls="TaxiDMEnvPool", 58 | gym_cls="TaxiGymEnvPool", 59 | gymnasium_cls="TaxiGymnasiumEnvPool", 60 | max_episode_steps=200, 61 | reward_threshold=8.0, 62 | ) 63 | 64 | register( 65 | task_id="NChain-v0", 66 | import_path="envpool.toy_text", 67 | spec_cls="NChainEnvSpec", 68 | dm_cls="NChainDMEnvPool", 69 | gym_cls="NChainGymEnvPool", 70 | gymnasium_cls="NChainGymnasiumEnvPool", 71 | max_episode_steps=1000, 72 | ) 73 | 74 | register( 75 | task_id="CliffWalking-v0", 76 | import_path="envpool.toy_text", 77 | spec_cls="CliffWalkingEnvSpec", 78 | dm_cls="CliffWalkingDMEnvPool", 79 | gym_cls="CliffWalkingGymEnvPool", 80 | gymnasium_cls="CliffWalkingGymnasiumEnvPool", 81 | ) 82 | 83 | register( 84 | task_id="Blackjack-v1", 85 | import_path="envpool.toy_text", 86 | spec_cls="BlackjackEnvSpec", 87 | dm_cls="BlackjackDMEnvPool", 88 | gym_cls="BlackjackGymEnvPool", 89 | gymnasium_cls="BlackjackGymnasiumEnvPool", 90 | sab=True, 91 | natural=False, 92 | ) 93 | -------------------------------------------------------------------------------- /envpool/mujoco/dmc/registration.py: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | """Mujoco dm_control suite env registration.""" 15 | 16 | from envpool.registration import register 17 | 18 | # from suite.BENCHMARKING 19 | dmc_mujoco_envs = [ 20 | ("acrobot", "swingup", 1000), 21 | ("acrobot", "swingup_sparse", 1000), 22 | ("ball_in_cup", "catch", 1000), 23 | ("cartpole", "balance", 1000), 24 | ("cartpole", "balance_sparse", 1000), 25 | ("cartpole", "swingup", 1000), 26 | ("cartpole", "swingup_sparse", 1000), 27 | ("cartpole", "three_poles", 1000), 28 | ("cartpole", "two_poles", 1000), 29 | ("cheetah", "run", 1000), 30 | ("finger", "spin", 1000), 31 | ("finger", "turn_easy", 1000), 32 | ("finger", "turn_hard", 1000), 33 | ("fish", "swim", 1000), 34 | ("fish", "upright", 1000), 35 | ("hopper", "hop", 1000), 36 | ("hopper", "stand", 1000), 37 | ("humanoid", "run", 1000), 38 | ("humanoid", "run_pure_state", 1000), 39 | ("humanoid", "stand", 1000), 40 | ("humanoid", "walk", 1000), 41 | ("humanoid_CMU", "run", 1000), 42 | ("humanoid_CMU", "stand", 1000), 43 | ("manipulator", "bring_ball", 1000), 44 | ("manipulator", "bring_peg", 1000), 45 | ("manipulator", "insert_ball", 1000), 46 | ("manipulator", "insert_peg", 1000), 47 | ("pendulum", "swingup", 1000), 48 | ("point_mass", "easy", 1000), 49 | ("point_mass", "hard", 1000), 50 | ("reacher", "easy", 1000), 51 | ("reacher", "hard", 1000), 52 | ("swimmer", "swimmer6", 1000), 53 | ("swimmer", "swimmer15", 1000), 54 | ("walker", "run", 1000), 55 | ("walker", "stand", 1000), 56 | ("walker", "walk", 1000), 57 | ] 58 | 59 | for domain, task, max_episode_steps in dmc_mujoco_envs: 60 | domain_name = "".join([g[:1].upper() + g[1:] for g in domain.split("_")]) 61 | task_name = "".join([g[:1].upper() + g[1:] for g in task.split("_")]) 62 | register( 63 | task_id=f"{domain_name}{task_name}-v1", 64 | import_path="envpool.mujoco.dmc", 65 | spec_cls=f"Dmc{domain_name}EnvSpec", 66 | dm_cls=f"Dmc{domain_name}DMEnvPool", 67 | gym_cls=f"Dmc{domain_name}GymEnvPool", 68 | gymnasium_cls=f"Dmc{domain_name}GymnasiumEnvPool", 69 | task_name=task, 70 | max_episode_steps=max_episode_steps, 71 | ) 72 | -------------------------------------------------------------------------------- /envpool/box2d/BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | load("@pip_requirements//:requirements.bzl", "requirement") 16 | load("@pybind11_bazel//:build_defs.bzl", "pybind_extension") 17 | 18 | package(default_visibility = ["//visibility:public"]) 19 | 20 | cc_library( 21 | name = "box2d_env", 22 | srcs = [ 23 | "bipedal_walker_env.cc", 24 | "car_dynamics.cc", 25 | "car_racing_env.cc", 26 | "lunar_lander_env.cc", 27 | "utils.cc", 28 | ], 29 | hdrs = [ 30 | "bipedal_walker.h", 31 | "bipedal_walker_env.h", 32 | "car_dynamics.h", 33 | "car_racing.h", 34 | "car_racing_env.h", 35 | "lunar_lander_continuous.h", 36 | "lunar_lander_discrete.h", 37 | "lunar_lander_env.h", 38 | "utils.h", 39 | ], 40 | deps = [ 41 | "//envpool/core:async_envpool", 42 | "@box2d", 43 | "@opencv", 44 | ], 45 | ) 46 | 47 | pybind_extension( 48 | name = "box2d_envpool", 49 | srcs = ["box2d_envpool.cc"], 50 | deps = [ 51 | ":box2d_env", 52 | "//envpool/core:py_envpool", 53 | ], 54 | ) 55 | 56 | py_library( 57 | name = "box2d", 58 | srcs = ["__init__.py"], 59 | data = [":box2d_envpool.so"], 60 | deps = ["//envpool/python:api"], 61 | ) 62 | 63 | py_library( 64 | name = "box2d_registration", 65 | srcs = ["registration.py"], 66 | deps = [ 67 | "//envpool:registration", 68 | ], 69 | ) 70 | 71 | py_test( 72 | name = "box2d_deterministic_test", 73 | size = "enormous", 74 | srcs = ["box2d_deterministic_test.py"], 75 | deps = [ 76 | ":box2d", 77 | ":box2d_registration", 78 | requirement("absl-py"), 79 | requirement("numpy"), 80 | ], 81 | ) 82 | 83 | py_test( 84 | name = "box2d_correctness_test", 85 | size = "enormous", 86 | srcs = ["box2d_correctness_test.py"], 87 | deps = [ 88 | ":box2d", 89 | ":box2d_registration", 90 | requirement("absl-py"), 91 | requirement("gym"), 92 | requirement("box2d-py"), 93 | requirement("pygame"), 94 | requirement("opencv-python-headless"), 95 | requirement("numpy"), 96 | ], 97 | ) 98 | -------------------------------------------------------------------------------- /benchmark/test_dmc.py: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import argparse 16 | import time 17 | 18 | import numpy as np 19 | import tqdm 20 | from dm_control import suite 21 | 22 | import envpool 23 | 24 | 25 | def run_dmc(env, action, frame_skip, total_step): 26 | ts = env.reset() 27 | t = time.time() 28 | for i in tqdm.trange(total_step): 29 | if ts.discount == 0: 30 | ts = env.reset() 31 | else: 32 | ts = env.step(action[i]) 33 | fps = frame_skip * total_step / (time.time() - t) 34 | print(f"FPS(dmc) = {fps:.2f}") 35 | return fps 36 | 37 | 38 | def run_envpool(env, action, frame_skip, total_step): 39 | ts = env.reset() 40 | t = time.time() 41 | for i in tqdm.trange(total_step): 42 | if ts.discount[0] == 0: 43 | ts = env.reset() 44 | else: 45 | ts = env.step(action[i:i + 1]) 46 | fps = frame_skip * total_step / (time.time() - t) 47 | print(f"FPS(envpool) = {fps:.2f}") 48 | return fps 49 | 50 | 51 | if __name__ == "__main__": 52 | parser = argparse.ArgumentParser() 53 | parser.add_argument("--domain", type=str, default="cheetah") 54 | parser.add_argument("--task", type=str, default="run") 55 | parser.add_argument("--total-step", type=int, default=200000) 56 | parser.add_argument("--seed", type=int, default=0) 57 | args = parser.parse_args() 58 | print(args) 59 | 60 | # dmc 61 | env = suite.load(args.domain, args.task, {"random": args.seed}) 62 | np.random.seed(args.seed) 63 | minimum, maximum = env.action_spec().minimum, env.action_spec().maximum 64 | action = np.array( 65 | [ 66 | np.random.uniform(low=minimum, high=maximum) 67 | for _ in range(args.total_step) 68 | ] 69 | ) 70 | frame_skip = env._n_sub_steps 71 | 72 | fps_dmc = run_dmc(env, action, frame_skip, args.total_step) 73 | 74 | time.sleep(3) 75 | 76 | # envpool 77 | domain_name = "".join([i.capitalize() for i in args.domain.split("_")]) 78 | task_name = "".join([i.capitalize() for i in args.task.split("_")]) 79 | env = envpool.make_dm(f"{domain_name}{task_name}-v1", num_envs=1) 80 | assert env.spec.config.frame_skip == frame_skip 81 | 82 | fps_envpool = run_envpool(env, action, frame_skip, args.total_step) 83 | print(f"EnvPool Speedup: {fps_envpool / fps_dmc:.2f}x") 84 | -------------------------------------------------------------------------------- /envpool/mujoco/dmc/mujoco_dmc_suite_ext_deterministic_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | """Unit tests for Mujoco dm_control deterministic check.""" 15 | 16 | from typing import List, Optional 17 | 18 | import dm_env 19 | import numpy as np 20 | from absl.testing import absltest 21 | 22 | import envpool.mujoco.dmc.registration # noqa: F401 23 | from envpool.registration import make_dm 24 | 25 | 26 | class _MujocoDmcSuiteExtDeterministicTest(absltest.TestCase): 27 | 28 | def check( 29 | self, 30 | domain: str, 31 | task: str, 32 | obs_keys: List[str], 33 | blacklist: Optional[List[str]] = None, 34 | num_envs: int = 4, 35 | ) -> None: 36 | domain_name = "".join([g[:1].upper() + g[1:] for g in domain.split("_")]) 37 | task_name = "".join([g[:1].upper() + g[1:] for g in task.split("_")]) 38 | task_id = f"{domain_name}{task_name}-v1" 39 | np.random.seed(0) 40 | env0 = make_dm(task_id, num_envs=num_envs, seed=0) 41 | env1 = make_dm(task_id, num_envs=num_envs, seed=0) 42 | env2 = make_dm(task_id, num_envs=num_envs, seed=1) 43 | act_spec = env0.action_spec() 44 | for t in range(3000): 45 | action = np.array( 46 | [ 47 | np.random.uniform( 48 | low=act_spec.minimum, high=act_spec.maximum, size=act_spec.shape 49 | ) for _ in range(num_envs) 50 | ] 51 | ) 52 | ts0 = env0.step(action) 53 | obs0 = ts0.observation 54 | obs1 = env1.step(action).observation 55 | obs2 = env2.step(action).observation 56 | for k in obs_keys: 57 | o0 = getattr(obs0, k) 58 | o1 = getattr(obs1, k) 59 | o2 = getattr(obs2, k) 60 | np.testing.assert_allclose(o0, o1) 61 | if blacklist and k in blacklist: 62 | continue 63 | if np.abs(o0).sum() > 0 and ts0.step_type[0] != dm_env.StepType.FIRST: 64 | self.assertFalse(np.allclose(o0, o2), (t, k, o0, o2)) 65 | 66 | def test_humanoid_CMU(self) -> None: 67 | obs_keys = [ 68 | "joint_angles", "head_height", "extremities", "torso_vertical", 69 | "com_velocity", "velocity" 70 | ] 71 | for task in ["stand", "run"]: 72 | self.check("humanoid_CMU", task, obs_keys) 73 | 74 | 75 | if __name__ == "__main__": 76 | absltest.main() 77 | -------------------------------------------------------------------------------- /.clang-tidy: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | --- 15 | Checks: ' 16 | bugprone-*, 17 | clang-analyzer-*, 18 | google-*, 19 | modernize-*, 20 | performance-*, 21 | portability-*, 22 | readability-*, 23 | -bugprone-easily-swappable-parameters, 24 | -bugprone-implicit-widening-of-multiplication-result, 25 | -bugprone-narrowing-conversions, 26 | -modernize-use-trailing-return-type, 27 | -readability-function-cognitive-complexity, 28 | -readability-identifier-length, 29 | -readability-magic-numbers, 30 | -readability-static-accessed-through-instance, 31 | -readability-uppercase-literal-suffix, 32 | ' 33 | CheckOptions: 34 | - { key: readability-identifier-naming.ClassCase, value: CamelCase } 35 | - { key: readability-identifier-naming.EnumCase, value: CamelCase } 36 | - { key: readability-identifier-naming.FunctionCase, value: CamelCase } 37 | - { key: readability-identifier-naming.GlobalConstantCase, value: UPPER_CASE } 38 | - { key: readability-identifier-naming.MemberCase, value: lower_case } 39 | - { key: readability-identifier-naming.MemberSuffix, value: _ } 40 | - { key: readability-identifier-naming.PublicMemberCase, value: lower_case } 41 | - { key: readability-identifier-naming.TypeAliasCase, value: CamelCase } 42 | - { key: readability-identifier-naming.ConstantMemberCase, value: CamelCase } 43 | - { key: readability-identifier-naming.ConstantMemberPrefix, value: k } 44 | - { key: readability-identifier-naming.GlobalConstantCase, value: CamelCase } 45 | - { key: readability-identifier-naming.GlobalConstantPrefix, value: k } 46 | - { key: readability-identifier-naming.NamespaceCase, value: lower_case } 47 | - { key: readability-identifier-naming.StructCase, value: CamelCase } 48 | - { key: readability-identifier-naming.UnionCase, value: CamelCase } 49 | - { key: readability-identifier-naming.VariableCase, value: lower_case } 50 | WarningsAsErrors: '*' 51 | HeaderFilterRegex: '/envpool/' 52 | AnalyzeTemporaryDtors: true 53 | -------------------------------------------------------------------------------- /envpool/classic_control/registration.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | """Classic control env registration.""" 15 | 16 | from envpool.registration import register 17 | 18 | register( 19 | task_id="CartPole-v0", 20 | import_path="envpool.classic_control", 21 | spec_cls="CartPoleEnvSpec", 22 | dm_cls="CartPoleDMEnvPool", 23 | gym_cls="CartPoleGymEnvPool", 24 | gymnasium_cls="CartPoleGymnasiumEnvPool", 25 | max_episode_steps=200, 26 | reward_threshold=195.0, 27 | ) 28 | 29 | register( 30 | task_id="CartPole-v1", 31 | import_path="envpool.classic_control", 32 | spec_cls="CartPoleEnvSpec", 33 | dm_cls="CartPoleDMEnvPool", 34 | gym_cls="CartPoleGymEnvPool", 35 | gymnasium_cls="CartPoleGymnasiumEnvPool", 36 | max_episode_steps=500, 37 | reward_threshold=475.0, 38 | ) 39 | 40 | register( 41 | task_id="Pendulum-v0", 42 | import_path="envpool.classic_control", 43 | spec_cls="PendulumEnvSpec", 44 | dm_cls="PendulumDMEnvPool", 45 | gym_cls="PendulumGymEnvPool", 46 | gymnasium_cls="PendulumGymnasiumEnvPool", 47 | version=0, 48 | max_episode_steps=200, 49 | ) 50 | 51 | register( 52 | task_id="Pendulum-v1", 53 | import_path="envpool.classic_control", 54 | spec_cls="PendulumEnvSpec", 55 | dm_cls="PendulumDMEnvPool", 56 | gym_cls="PendulumGymEnvPool", 57 | gymnasium_cls="PendulumGymnasiumEnvPool", 58 | version=1, 59 | max_episode_steps=200, 60 | ) 61 | 62 | register( 63 | task_id="MountainCar-v0", 64 | import_path="envpool.classic_control", 65 | spec_cls="MountainCarEnvSpec", 66 | dm_cls="MountainCarDMEnvPool", 67 | gym_cls="MountainCarGymEnvPool", 68 | gymnasium_cls="MountainCarGymnasiumEnvPool", 69 | max_episode_steps=200, 70 | ) 71 | 72 | register( 73 | task_id="MountainCarContinuous-v0", 74 | import_path="envpool.classic_control", 75 | spec_cls="MountainCarContinuousEnvSpec", 76 | dm_cls="MountainCarContinuousDMEnvPool", 77 | gym_cls="MountainCarContinuousGymEnvPool", 78 | gymnasium_cls="MountainCarContinuousGymnasiumEnvPool", 79 | max_episode_steps=999, 80 | ) 81 | 82 | register( 83 | task_id="Acrobot-v1", 84 | import_path="envpool.classic_control", 85 | spec_cls="AcrobotEnvSpec", 86 | dm_cls="AcrobotDMEnvPool", 87 | gym_cls="AcrobotGymEnvPool", 88 | gymnasium_cls="AcrobotGymnasiumEnvPool", 89 | max_episode_steps=500, 90 | ) 91 | -------------------------------------------------------------------------------- /envpool/toy_text/nchain.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Garena Online Private Limited 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | // https://github.com/openai/gym/blob/v0.20.0/gym/envs/toy_text/nchain.py 17 | 18 | #ifndef ENVPOOL_TOY_TEXT_NCHAIN_H_ 19 | #define ENVPOOL_TOY_TEXT_NCHAIN_H_ 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | #include "envpool/core/async_envpool.h" 28 | #include "envpool/core/env.h" 29 | 30 | namespace toy_text { 31 | 32 | class NChainEnvFns { 33 | public: 34 | static decltype(auto) DefaultConfig() { return MakeDict(); } 35 | template 36 | static decltype(auto) StateSpec(const Config& conf) { 37 | return MakeDict("obs"_.Bind(Spec({-1}, {0, 4}))); 38 | } 39 | template 40 | static decltype(auto) ActionSpec(const Config& conf) { 41 | return MakeDict("action"_.Bind(Spec({-1}, {0, 1}))); 42 | } 43 | }; 44 | 45 | using NChainEnvSpec = EnvSpec; 46 | 47 | class NChainEnv : public Env { 48 | protected: 49 | int s_, max_episode_steps_, elapsed_step_; 50 | std::uniform_real_distribution<> dist_; 51 | bool done_{true}; 52 | 53 | public: 54 | NChainEnv(const Spec& spec, int env_id) 55 | : Env(spec, env_id), 56 | max_episode_steps_(spec.config["max_episode_steps"_]), 57 | dist_(0, 1) {} 58 | 59 | bool IsDone() override { return done_; } 60 | 61 | void Reset() override { 62 | s_ = 0; 63 | done_ = false; 64 | elapsed_step_ = 0; 65 | WriteState(0.0); 66 | } 67 | 68 | void Step(const Action& action) override { 69 | done_ = (++elapsed_step_ >= max_episode_steps_); 70 | int act = action["action"_]; 71 | if (dist_(gen_) < 0.2) { 72 | act = 1 - act; 73 | } 74 | float reward = 0.0; 75 | if (act != 0) { 76 | reward = 2.0; 77 | s_ = 0; 78 | } else if (s_ < 4) { 79 | ++s_; 80 | } else { 81 | reward = 10.0; 82 | } 83 | WriteState(reward); 84 | } 85 | 86 | private: 87 | void WriteState(float reward) { 88 | State state = Allocate(); 89 | state["obs"_] = s_; 90 | state["reward"_] = reward; 91 | } 92 | }; 93 | 94 | using NChainEnvPool = AsyncEnvPool; 95 | 96 | } // namespace toy_text 97 | 98 | #endif // ENVPOOL_TOY_TEXT_NCHAIN_H_ 99 | -------------------------------------------------------------------------------- /envpool/mujoco/gym/mujoco_gym_deterministic_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Garena Online Private Limited 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | """Unit tests for Mujoco gym deterministic check.""" 15 | 16 | import numpy as np 17 | from absl.testing import absltest 18 | 19 | import envpool.mujoco.gym.registration # noqa: F401 20 | from envpool.registration import make_gym 21 | 22 | 23 | class _MujocoGymDeterministicTest(absltest.TestCase): 24 | 25 | def check(self, task_id: str, num_envs: int = 4) -> None: 26 | env0 = make_gym(task_id, num_envs=num_envs, seed=0) 27 | env1 = make_gym(task_id, num_envs=num_envs, seed=0) 28 | env2 = make_gym(task_id, num_envs=num_envs, seed=1) 29 | act_space = env0.action_space 30 | eps = np.finfo(np.float32).eps 31 | obs_space = env0.observation_space 32 | obs_min, obs_max = obs_space.low - eps, obs_space.high + eps 33 | for _ in range(3000): 34 | action = np.array([act_space.sample() for _ in range(num_envs)]) 35 | obs0 = env0.step(action)[0] 36 | obs1 = env1.step(action)[0] 37 | obs2 = env2.step(action)[0] 38 | np.testing.assert_allclose(obs0, obs1) 39 | self.assertFalse(np.allclose(obs0, obs2)) 40 | self.assertTrue(np.all(obs_min <= obs0), obs0) 41 | self.assertTrue(np.all(obs_min <= obs2), obs2) 42 | self.assertTrue(np.all(obs0 <= obs_max), obs0) 43 | self.assertTrue(np.all(obs2 <= obs_max), obs2) 44 | 45 | def test_ant(self) -> None: 46 | self.check("Ant-v4") 47 | 48 | def test_half_cheetah(self) -> None: 49 | self.check("HalfCheetah-v4") 50 | 51 | def test_hopper(self) -> None: 52 | self.check("Hopper-v4") 53 | 54 | def test_humanoid(self) -> None: 55 | self.check("Humanoid-v4") 56 | 57 | def test_humanoid_standup(self) -> None: 58 | self.check("HumanoidStandup-v4") 59 | 60 | def test_inverted_double_pendulum(self) -> None: 61 | self.check("InvertedPendulum-v4") 62 | 63 | def test_inverted_pendulum(self) -> None: 64 | self.check("InvertedPendulum-v4") 65 | 66 | def test_pusher(self) -> None: 67 | self.check("Pusher-v4") 68 | 69 | def test_reacher(self) -> None: 70 | self.check("Reacher-v4") 71 | 72 | def test_swimmer(self) -> None: 73 | self.check("Swimmer-v4") 74 | 75 | def test_walker2d(self) -> None: 76 | self.check("Walker2d-v4") 77 | 78 | 79 | if __name__ == "__main__": 80 | absltest.main() 81 | -------------------------------------------------------------------------------- /third_party/box2d/box2d.BUILD: -------------------------------------------------------------------------------- 1 | cc_library( 2 | name = "box2d", 3 | srcs = glob(["include/box2d/*.h"]) + [ 4 | "src/collision/b2_broad_phase.cpp", 5 | "src/collision/b2_chain_shape.cpp", 6 | "src/collision/b2_circle_shape.cpp", 7 | "src/collision/b2_collide_circle.cpp", 8 | "src/collision/b2_collide_edge.cpp", 9 | "src/collision/b2_collide_polygon.cpp", 10 | "src/collision/b2_collision.cpp", 11 | "src/collision/b2_distance.cpp", 12 | "src/collision/b2_dynamic_tree.cpp", 13 | "src/collision/b2_edge_shape.cpp", 14 | "src/collision/b2_polygon_shape.cpp", 15 | "src/collision/b2_time_of_impact.cpp", 16 | "src/common/b2_block_allocator.cpp", 17 | "src/common/b2_draw.cpp", 18 | "src/common/b2_math.cpp", 19 | "src/common/b2_settings.cpp", 20 | "src/common/b2_stack_allocator.cpp", 21 | "src/common/b2_timer.cpp", 22 | "src/dynamics/b2_body.cpp", 23 | "src/dynamics/b2_chain_circle_contact.cpp", 24 | "src/dynamics/b2_chain_circle_contact.h", 25 | "src/dynamics/b2_chain_polygon_contact.cpp", 26 | "src/dynamics/b2_chain_polygon_contact.h", 27 | "src/dynamics/b2_circle_contact.cpp", 28 | "src/dynamics/b2_circle_contact.h", 29 | "src/dynamics/b2_contact.cpp", 30 | "src/dynamics/b2_contact_manager.cpp", 31 | "src/dynamics/b2_contact_solver.cpp", 32 | "src/dynamics/b2_contact_solver.h", 33 | "src/dynamics/b2_distance_joint.cpp", 34 | "src/dynamics/b2_edge_circle_contact.cpp", 35 | "src/dynamics/b2_edge_circle_contact.h", 36 | "src/dynamics/b2_edge_polygon_contact.cpp", 37 | "src/dynamics/b2_edge_polygon_contact.h", 38 | "src/dynamics/b2_fixture.cpp", 39 | "src/dynamics/b2_friction_joint.cpp", 40 | "src/dynamics/b2_gear_joint.cpp", 41 | "src/dynamics/b2_island.cpp", 42 | "src/dynamics/b2_island.h", 43 | "src/dynamics/b2_joint.cpp", 44 | "src/dynamics/b2_motor_joint.cpp", 45 | "src/dynamics/b2_mouse_joint.cpp", 46 | "src/dynamics/b2_polygon_circle_contact.cpp", 47 | "src/dynamics/b2_polygon_circle_contact.h", 48 | "src/dynamics/b2_polygon_contact.cpp", 49 | "src/dynamics/b2_polygon_contact.h", 50 | "src/dynamics/b2_prismatic_joint.cpp", 51 | "src/dynamics/b2_pulley_joint.cpp", 52 | "src/dynamics/b2_revolute_joint.cpp", 53 | "src/dynamics/b2_weld_joint.cpp", 54 | "src/dynamics/b2_wheel_joint.cpp", 55 | "src/dynamics/b2_world.cpp", 56 | "src/dynamics/b2_world_callbacks.cpp", 57 | "src/rope/b2_rope.cpp", 58 | ], 59 | hdrs = glob(["include/box2d/*.h"]), 60 | includes = [ 61 | "include", 62 | "src", 63 | ], 64 | linkopts = [ 65 | "-ldl", 66 | ], 67 | linkstatic = 1, 68 | visibility = ["//visibility:public"], 69 | ) 70 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | wheelhouse/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | cover/ 54 | 55 | # Translations 56 | *.mo 57 | *.pot 58 | 59 | # Django stuff: 60 | *.log 61 | local_settings.py 62 | db.sqlite3 63 | db.sqlite3-journal 64 | 65 | # Flask stuff: 66 | instance/ 67 | .webassets-cache 68 | 69 | # Scrapy stuff: 70 | .scrapy 71 | 72 | # Sphinx documentation 73 | docs/_build/ 74 | 75 | # PyBuilder 76 | .pybuilder/ 77 | target/ 78 | 79 | # Jupyter Notebook 80 | .ipynb_checkpoints 81 | 82 | # IPython 83 | profile_default/ 84 | ipython_config.py 85 | 86 | # pyenv 87 | # For a library or package, you might want to ignore these files since the code is 88 | # intended to run in multiple environments; otherwise, check them in: 89 | # .python-version 90 | 91 | # pipenv 92 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 93 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 94 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 95 | # install all needed dependencies. 96 | #Pipfile.lock 97 | 98 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 99 | __pypackages__/ 100 | 101 | # Celery stuff 102 | celerybeat-schedule 103 | celerybeat.pid 104 | 105 | # SageMath parsed files 106 | *.sage.py 107 | 108 | # Environments 109 | .env 110 | .venv 111 | venv/ 112 | ENV/ 113 | env.bak/ 114 | venv.bak/ 115 | 116 | # Spyder project settings 117 | .spyderproject 118 | .spyproject 119 | 120 | # Rope project settings 121 | .ropeproject 122 | 123 | # mkdocs documentation 124 | /site 125 | 126 | # mypy 127 | .mypy_cache/ 128 | .dmypy.json 129 | dmypy.json 130 | 131 | # Pyre type checker 132 | .pyre/ 133 | 134 | # pytype static type analyzer 135 | .pytype/ 136 | 137 | # Cython debug symbols 138 | cython_debug/ 139 | 140 | # Customized 141 | bazel-* 142 | pyflakes* 143 | .DS_Store 144 | .idea/ 145 | *.swp 146 | train_dir 147 | log 148 | _vizdoom* 149 | MUJOCO_LOG.TXT 150 | .vscode/ 151 | -------------------------------------------------------------------------------- /envpool/mujoco/gym/mujoco_env.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Garena Online Private Limited 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef ENVPOOL_MUJOCO_GYM_MUJOCO_ENV_H_ 18 | #define ENVPOOL_MUJOCO_GYM_MUJOCO_ENV_H_ 19 | 20 | #include 21 | #include 22 | 23 | #include 24 | 25 | namespace mujoco_gym { 26 | 27 | class MujocoEnv { 28 | private: 29 | std::array error_; 30 | 31 | protected: 32 | mjModel* model_; 33 | mjData* data_; 34 | mjtNum *init_qpos_, *init_qvel_; 35 | #ifdef ENVPOOL_TEST 36 | mjtNum *qpos0_, *qvel0_; // for align check 37 | #endif 38 | int frame_skip_; 39 | bool post_constraint_; 40 | int max_episode_steps_, elapsed_step_; 41 | bool done_{true}; 42 | 43 | public: 44 | MujocoEnv(const std::string& xml, int frame_skip, bool post_constraint, 45 | int max_episode_steps) 46 | : model_(mj_loadXML(xml.c_str(), nullptr, error_.begin(), 1000)), 47 | data_(mj_makeData(model_)), 48 | init_qpos_(new mjtNum[model_->nq]), 49 | init_qvel_(new mjtNum[model_->nv]), 50 | #ifdef ENVPOOL_TEST 51 | qpos0_(new mjtNum[model_->nq]), 52 | qvel0_(new mjtNum[model_->nv]), 53 | #endif 54 | frame_skip_(frame_skip), 55 | post_constraint_(post_constraint), 56 | max_episode_steps_(max_episode_steps), 57 | elapsed_step_(max_episode_steps + 1) { 58 | std::memcpy(init_qpos_, data_->qpos, sizeof(mjtNum) * model_->nq); 59 | std::memcpy(init_qvel_, data_->qvel, sizeof(mjtNum) * model_->nv); 60 | } 61 | 62 | ~MujocoEnv() { 63 | mj_deleteData(data_); 64 | mj_deleteModel(model_); 65 | delete[] init_qpos_; 66 | delete[] init_qvel_; 67 | #ifdef ENVPOOL_TEST 68 | delete[] qpos0_; 69 | delete[] qvel0_; 70 | #endif 71 | } 72 | 73 | void MujocoReset() { 74 | mj_resetData(model_, data_); 75 | MujocoResetModel(); 76 | mj_forward(model_, data_); 77 | } 78 | 79 | virtual void MujocoResetModel() { 80 | throw std::runtime_error("reset_model not implemented"); 81 | } 82 | 83 | void MujocoStep(const mjtNum* action) { 84 | for (int i = 0; i < model_->nu; ++i) { 85 | data_->ctrl[i] = action[i]; 86 | } 87 | for (int i = 0; i < frame_skip_; ++i) { 88 | mj_step(model_, data_); 89 | } 90 | if (post_constraint_) { 91 | mj_rnePostConstraint(model_, data_); 92 | } 93 | } 94 | }; 95 | 96 | } // namespace mujoco_gym 97 | 98 | #endif // ENVPOOL_MUJOCO_GYM_MUJOCO_ENV_H_ 99 | --------------------------------------------------------------------------------