### Install Bazelisk using Go Source: https://github.com/sail-sg/envpool/blob/main/docs/content/build.rst Installs Bazelisk. This command requires `sudo` privileges. ```bash make bazel-install ``` -------------------------------- ### Install Benchmark Dependencies Source: https://github.com/sail-sg/envpool/blob/main/benchmark/README.md Install the required packages for running benchmark scripts. ```bash $ pip install -r requirements.txt ``` -------------------------------- ### Install EnvPool and Dependencies Source: https://github.com/sail-sg/envpool/blob/main/demo/envpool_demo_halfcheetah.ipynb System-level and Python package installation commands for setting up the environment on Ubuntu. ```bash !apt-get install -y \ libgl1-mesa-dev \ libgl1-mesa-glx \ libglew-dev \ libosmesa6-dev \ software-properties-common !apt-get install -y patchelf !pip install git+https://github.com/Denys88/rl_games !pip install envpool !pip install gym !pip install mujoco !apt-get install -y xvfb python-opengl ffmpeg > /dev/null 2>&1 !pip install imageio==2.4.1 !pip install -U colabgymrender ``` -------------------------------- ### Install Requirements Source: https://github.com/sail-sg/envpool/blob/main/examples/acme_examples/README.md Install the necessary dependencies for the project. ```bash pip install -r requirements.txt ``` -------------------------------- ### Install Rendering and Video Tools Source: https://github.com/sail-sg/envpool/blob/main/demo/envpool_demo_ant.ipynb Installs xvfb, python-opengl, and ffmpeg, which are necessary for rendering environments and capturing video output, especially in environments where a display server is not available. The output is redirected to /dev/null to keep the installation clean. ```bash !sudo apt-get install -y xvfb python-opengl ffmpeg > /dev/null 2>&1 ``` -------------------------------- ### Install EnvPool using pip Source: https://github.com/sail-sg/envpool/blob/main/README.md Install EnvPool using pip. This is the recommended method for most users. ```bash pip install envpool ``` -------------------------------- ### Install Go and Bazel from alternative sources (China) Source: https://github.com/sail-sg/envpool/blob/main/docs/content/build.rst Installs Go and Bazel from alternative sources for users in mainland China, including setting a Go proxy and downloading pre-compiled binaries. ```bash wget https://studygolang.com/dl/golang/go1.18.1.linux-amd64.tar.gz # then follow the instructions on golang official website go env -w GOPROXY=https://goproxy.cn wget https://mirrors.huaweicloud.com/bazel/8.6.0/bazel-8.6.0-linux-x86_64 chmod +x bazel-8.6.0-linux-x86_64 mkdir -p $HOME/go/bin mv bazel-8.6.0-linux-x86_64 $HOME/go/bin/bazel export PATH=$PATH:$HOME/go/bin # or write to .bashrc / .zshrc # check if successfully installed bazel ``` -------------------------------- ### Install Bazelisk via Go Source: https://github.com/sail-sg/envpool/blob/main/docs/content/build.rst Installs Bazelisk using Go, setting up the PATH and creating a symlink for bazel. Requires Go version 1.16 or higher. ```bash sudo apt install -y golang-go export PATH=$HOME/go/bin:$PATH go install github.com/bazelbuild/bazelisk@latest ln -sf $HOME/go/bin/bazelisk $HOME/go/bin/bazel ``` -------------------------------- ### Install Built Wheel and Run Release Smoke Tests Source: https://github.com/sail-sg/envpool/blob/main/docs/content/build.rst Installs the previously built wheel and executes smoke tests, including a render call after reset, to verify the release version. ```bash make release-test ``` -------------------------------- ### Configure EnvPool with batch size Source: https://github.com/sail-sg/envpool/blob/main/README.md Example of initializing an environment pool with specific total environments and batch size. ```python envpool.make("Pong-v5", env_type="gym", num_envs=64, batch_size=16) ``` -------------------------------- ### Install Python Packages for RL and EnvPool Source: https://github.com/sail-sg/envpool/blob/main/demo/envpool_demo_ant.ipynb Installs the rl_games library from a GitHub repository, the envpool library, and the gym toolkit. Also installs mujoco, imageio, and colabgymrender for specific environment functionalities and rendering. ```bash !pip install git+https://github.com/Denys88/rl_games ``` ```bash !pip install envpool ``` ```bash !pip install gym ``` ```bash !pip install mujoco ``` ```bash !pip install imageio==2.4.1 ``` ```bash !pip install -U colabgymrender ``` -------------------------------- ### Install System Dependencies for EnvPool Source: https://github.com/sail-sg/envpool/blob/main/demo/envpool_demo_ant.ipynb Installs essential system libraries required for graphics rendering and environment simulation, such as OpenGL, GLEW, and OSMesa. Also installs patchelf for binary patching, which might be needed by some environments. ```bash !sudo apt-get install -y \ libgl1-mesa-dev \ libgl1-mesa-glx \ libglew-dev \ libosmesa6-dev \ software-properties-common ``` ```bash !sudo apt-get install -y patchelf ``` -------------------------------- ### Install EnvPool via pip Source: https://github.com/sail-sg/envpool/blob/main/docs/index.rst Use this command to install the EnvPool package from PyPI. ```bash $ pip install envpool ``` -------------------------------- ### Execute Training Runner Source: https://github.com/sail-sg/envpool/blob/main/demo/envpool_demo_breakout.ipynb Initializes the Runner instance with the configuration and starts the training process. ```python runner = Runner() runner.load(breakout_config) runner.run({ 'train': True, }) ``` -------------------------------- ### Install Bazelisk via npm Source: https://github.com/sail-sg/envpool/blob/main/docs/content/build.rst Installs Bazelisk, a version manager for Bazel, using npm. Ensure Node.js and npm are installed first. ```bash sudo apt install -y npm npm install -g @bazel/bazelisk ``` -------------------------------- ### Generate and Install EnvPool Wheel Source: https://github.com/sail-sg/envpool/blob/main/docs/content/new_env.rst Commands to generate the EnvPool wheel file and then install it using pip. ```bash # generate .whl file make bazel-build # install .whl pip install dist/envpool--*.whl ``` -------------------------------- ### Install macOS Dependencies Source: https://github.com/sail-sg/envpool/blob/main/docs/content/build.rst Installs user-space dependencies on macOS using Homebrew after Xcode Command Line Tools are installed. Includes Go, OpenJDK, CMake, Ninja, SWIG, and Qt. ```bash xcode-select --install brew install go openjdk@17 cmake ninja swig qt@5 sudo ln -sf "$(brew --prefix cmake)/bin/cmake" /usr/local/bin/cmake sudo ln -sf "$(brew --prefix ninja)/bin/ninja" /usr/local/bin/ninja export PATH="$(brew --prefix openjdk@17)/bin:$PATH" export BAZEL_RULES_QT_DIR="$(brew --prefix qt@5)" ``` -------------------------------- ### Verify EnvPool Installation Source: https://github.com/sail-sg/envpool/blob/main/README.md Verify that EnvPool has been successfully installed by importing it and printing its version. ```python import envpool print(envpool.__version__) ``` -------------------------------- ### Install Linux Dependencies Source: https://github.com/sail-sg/envpool/blob/main/docs/content/build.rst Installs essential development packages on Ubuntu 24.04 for building EnvPool, including build tools, JDK, Python, Go, CMake, Ninja, SWIG, and Qt. ```bash sudo apt install -y build-essential openjdk-17-jdk python3-dev \ python3-pip python-is-python3 golang-go cmake ninja-build swig qtbase5-dev \ qtdeclarative5-dev # Some Bazel Qt rules still look for this legacy include path. sudo ln -sf "$(qmake -query QT_INSTALL_HEADERS)" /usr/include/qt ``` -------------------------------- ### ViZDoom Environment Creation Source: https://github.com/sail-sg/envpool/blob/main/docs/env/vizdoom.rst Example of creating a ViZDoom environment using envpool.make, showcasing basic and custom configurations. ```APIDOC ## Creating ViZDoom Environments ### Basic Usage To create a standard ViZDoom environment: ```python env = envpool.make("Vizdoom-v0", ...) ``` ### Custom Configuration For customized ViZDoom environments, use `VizdoomCustom-v1` and specify configuration and WAD file paths: ```python env = envpool.make( "VizdoomCustom-v1", cfg_path="xxx.cfg", wad_path="xxx.wad", ... ) ``` ### Available Options The following options can be passed to `envpool.make` for ViZDoom environments: #### Path Parameters - **task_id** (str) - Required - The specific ViZDoom task to use. - **num_envs** (int) - Required - The number of parallel environments to create. #### Query Parameters - **batch_size** (int) - Optional - The expected batch size for return results. Defaults to `num_envs`. - **num_threads** (int) - Optional - The maximum number of threads for `env.step`. Defaults to `batch_size`. - **seed** (int | Sequence[int]) - Optional - The environment seed. Defaults to `42`. - **max_episode_steps** (int) - Optional - Maximum steps per episode. Defaults to `525`. - **img_height** (int) - Optional - Desired observation image height. Defaults to `84`. - **img_width** (int) - Optional - Desired observation image width. Defaults to `84`. - **stack_num** (int) - Optional - Number of frames to stack. Defaults to `4`. - **frame_skip** (int) - Optional - Number of frames to skip per action. Defaults to `4`. - **use_inter_area_resize** (bool) - Optional - Use `cv::INTER_AREA` for image resizing. Defaults to `True`. - **episodic_life** (bool) - Optional - Treat end-of-life as end-of-episode. Defaults to `False`. - **use_combined_action** (bool) - Optional - Use discrete action space. Defaults to `False`. - **force_speed** (bool) - Optional - Press SPEED button if available. Defaults to `False`. - **lmp_save_dir** (str) - Optional - Directory to save `.lmp` files. Defaults to `""`. - **vzd_path** (str) - Optional - Path to the ViZDoom binary. Defaults to `vizdoom/bin/vizdoom`. - **cfg_path** (str) - Optional - Path to the `.cfg` file for custom setups. Defaults to `""`. - **wad_path** (str) - Optional - Path to the `.wad` file for custom setups. Defaults to `""`. - **iwad_path** (str) - Optional - Path to the rendering resource package. Defaults to `vizdoom/bin/freedoom2`. - **map_id** (str) - Optional - ViZDoom map ID. Defaults to `"map01"`. - **game_args** (str) - Optional - Arguments string for the ViZDoom game. Defaults to `""`. - **reward_config** (Dict[str, Tuple[float, float]]) - Optional - Custom reward configuration. Defaults to a predefined dictionary. - **weapon_duration** (int) - Optional - Duration for weapon-based rewards. Defaults to `5`. - **selected_weapon_reward_config** (Dict[int, float]) - Optional - Reward configuration for specific weapons held for `weapon_duration`. - **delta_button_config** (Dict[str, Tuple[int, float, float]]) - Optional - Configuration for delta button actions. ``` -------------------------------- ### Add Tianshou as a Build Dependency Source: https://github.com/sail-sg/envpool/blob/main/docs/content/new_env.rst This diff shows how to add 'tianshou' as an install requirement in setup.cfg. ```diff typing-extensions + tianshou ``` -------------------------------- ### Create Docker Development Environment Source: https://github.com/sail-sg/envpool/blob/main/docs/content/build.rst Builds and starts a Docker container for development. The code will be available under `/app` and host file system access via `/host`. ```bash make docker-dev ``` -------------------------------- ### Basic Gymnasium Environment Interaction Source: https://github.com/sail-sg/envpool/blob/main/docs/env/gymnasium_robotics.rst Demonstrates how to create, reset, step, and render a Gymnasium environment using EnvPool. Ensure EnvPool is installed with Gymnasium support. ```python import envpool env = envpool.make_gymnasium("FetchReach-v4", num_envs=4, seed=0) obs, info = env.reset() obs, rew, term, trunc, info = env.step(env.action_space.sample()[None, :].repeat(4, axis=0)) frame = env.render() env.close() ``` -------------------------------- ### Define Bazel BUILD Rules Source: https://github.com/sail-sg/envpool/blob/main/docs/content/new_env.rst Example of defining cc_library and pybind_extension rules within a BUILD file. ```python load("@pip_requirements//:requirements.bzl", "requirement") load("@pybind11_bazel//:build_defs.bzl", "pybind_extension") package(default_visibility = ["//visibility:public"]) cc_library( name = "cartpole", hdrs = ["cartpole.h"], deps = [ "//envpool/core:async_envpool", ], ) pybind_extension( name = "classic_control_envpool", srcs = [ "classic_control.cc", ], deps = [ ":cartpole", "//envpool/core:py_envpool", ], ) py_library( name = "classic_control", srcs = ["__init__.py"], ``` -------------------------------- ### Send Actions to Environment Source: https://github.com/sail-sg/envpool/blob/main/docs/content/python_interface.rst Examples of sending single-array and dictionary-based action inputs to the environment. ```python envpool.send(np.ones(batch_size)) envpool.send(np.ones(batch_size), env_id=np.arange(batch_size)) envpool.send({ # note: please be careful with dtype here "action": np.ones(batch_size, dtype=np.int32), "env_id": np.arange(batch_size, dtype=np.int32), }) ``` -------------------------------- ### Install System Dependencies for EnvPool Source: https://github.com/sail-sg/envpool/blob/main/demo/envpool_demo_breakout.ipynb Installs essential system libraries required for EnvPool and related environments on Ubuntu 18.04. Ensure you have sufficient permissions to run apt-get commands. ```bash # OS: Ubuntu 18.04.4 LTS x86_64 # Kernel: 4.18.0-15-generic # CPU: Intel(R) Core(TM) i9-10920X CPU (24) @ 3.50GHz # GPU: NVIDIA GeForce RTX 2080 Ti !apt-get install -y \ libgl1-mesa-dev \ libgl1-mesa-glx \ libglew-dev \ libosmesa6-dev \ software-properties-common !apt-get install -y patchelf !pip install git+https://github.com/Denys88/rl_games !pip install envpool !pip install gym !pip install mujoco !apt-get install -y xvfb python-opengl ffmpeg > /dev/null 2>&1 !pip install imageio==2.4.1 !pip install -U colabgymrender ``` -------------------------------- ### List Available Environments Source: https://context7.com/sail-sg/envpool/llms.txt Demonstrates how to discover all registered environment task IDs using envpool.list_all_envs(). Includes filtering examples for Atari, Mujoco, and classic control environments. ```python import envpool # Get list of all available environments all_envs = envpool.list_all_envs() print(f"Total environments: {len(all_envs)}") # Filter by category atari_envs = [e for e in all_envs if e.endswith("-v5") and not any( x in e.lower() for x in ["ant", "cheetah", "hopper", "walker", "humanoid"] )] mujoco_envs = [e for e in all_envs if any( x in e for x in ["Ant-", "HalfCheetah-", "Hopper-", "Walker2d-", "Humanoid-"] )] classic_envs = [e for e in all_envs if any( x in e for x in ["CartPole", "Pendulum", "MountainCar", "Acrobot"] )] print(f"Sample Atari envs: {atari_envs[:5]}") print(f"Mujoco envs: {mujoco_envs[:5]}") print(f"Classic control envs: {classic_envs}") ``` -------------------------------- ### Load and Run Training Script Source: https://github.com/sail-sg/envpool/blob/main/demo/envpool_demo_halfcheetah.ipynb This Python script initializes a Runner object, loads the specified configuration, and starts the training process. Ensure the Runner class is defined and accessible. ```python runner = Runner() runner.load(halfcheetah_config) runner.run({ 'train': True, }) ``` -------------------------------- ### Load and Run Pong Training Source: https://github.com/sail-sg/envpool/blob/main/demo/envpool_demo_pong.ipynb Initializes a Runner object, loads the Pong configuration, and starts the training process. Ensure the Runner class is defined and accessible. ```python runner = Runner() runner.load(pong_config) runner.run({ 'train': True, }) ``` -------------------------------- ### Reset Environment and Get Observations in Python Source: https://github.com/sail-sg/envpool/blob/main/docs/content/new_env.rst Reset the environment and retrieve the initial observations and info. ```python obs, info = env.reset() ``` -------------------------------- ### Create DeepMind Control Suite Environment Source: https://context7.com/sail-sg/envpool/llms.txt Initializes a DeepMind Control Suite environment using EnvPool's naming convention. This example demonstrates accessing observations and stepping with continuous actions. ```python import envpool import numpy as np # dm_control: suite.load("hopper", "hop") -> EnvPool: "HopperHop-v1" # dm_control: suite.load("ball_in_cup", "catch") -> EnvPool: "BallInCupCatch-v1" env = envpool.make_dm("CheetahRun-v1", num_envs=8) # dm_env interface with observation as NamedTuple timestep = env.reset() obs = timestep.observation # Access individual observation components print(f"Position: {obs.position.shape}") # (8, 8) print(f"Velocity: {obs.velocity.shape}") # (8, 9) # Step with continuous actions action_spec = env.action_spec() print(f"Action shape: {action_spec.shape}") # (6,) for _ in range(100): action = np.random.uniform(-1, 1, size=(8, 6)).astype(np.float32) timestep = env.step(action) # dm_env discount is 0.0 on terminal states if (timestep.discount == 0.0).any(): print("Episode(s) terminated") ``` -------------------------------- ### Load dm_control Environment and EnvPool Equivalent Source: https://github.com/sail-sg/envpool/blob/main/docs/env/dm_control.rst Demonstrates the conversion of dm_control's suite.load function calls to EnvPool's make_dm format for creating environments. This is useful for migrating existing dm_control setups to EnvPool. ```python dm_raw_env = suite.load("hopper", "hop") # equal to envpool_env = envpool.make_dm("HopperHop-v1", num_envs=1) ``` ```python suite.load("ball_in_cup", "catch") # equal to envpool.make_dm("BallInCupCatch-v1", num_envs=1) ``` -------------------------------- ### Environment Creation and Configuration Source: https://github.com/sail-sg/envpool/blob/main/docs/env/minigrid.rst Details on how to initialize a MiniGrid environment using envpool.make_gymnasium and the available configuration options. ```APIDOC ## Environment Configuration ### Description Initializes a vectorized MiniGrid environment. The configuration parameters control the parallel execution and environment behavior. ### Parameters - **task_id** (str) - Required - The identifier for the MiniGrid or BabyAI environment. - **num_envs** (int) - Required - Number of environments to create. - **batch_size** (int) - Optional - Expected batch size for results, defaults to num_envs. - **num_threads** (int) - Optional - Maximum threads for env.step, defaults to batch_size. - **seed** (int | Sequence[int]) - Optional - Environment seed, defaults to 42. - **max_episode_steps** (int) - Optional - Maximum steps per episode, defaults to upstream registration. ### Request Example ```python import envpool env = envpool.make_gymnasium("MiniGrid-DoorKey-8x8-v0", num_envs=1) ``` ``` -------------------------------- ### Initialize and Step Synchronous EnvPool Source: https://github.com/sail-sg/envpool/blob/main/README.md Demonstrates creating a synchronous environment pool and performing a batch step operation. ```python import envpool import numpy as np # make gym env env = envpool.make("Pong-v5", env_type="gym", num_envs=100) # or use envpool.make_gym(...) obs = env.reset() # should be (100, 4, 84, 84) act = np.zeros(100, dtype=int) obs, rew, term, trunc, info = env.step(act) ``` -------------------------------- ### Configure Environment Rendering Source: https://github.com/sail-sg/envpool/blob/main/docs/content/python_interface.rst Demonstrates creating environments with rgb_array and human render modes. ```python env = envpool.make( "Ant-v5", env_type="gymnasium", num_envs=4, render_mode="rgb_array", render_width=480, render_height=480, ) env.reset() frames = env.render(env_ids=[0, 2]) assert frames.shape == (2, 480, 480, 3) viewer = envpool.make( "WalkerWalk-v1", env_type="gymnasium", num_envs=1, render_mode="human", render_env_id=0, ) viewer.reset() viewer.render() ``` -------------------------------- ### Run Sample Factory Benchmark Source: https://github.com/sail-sg/envpool/blob/main/benchmark/README.md Execute the Sample Factory benchmark for Atari and Mujoco environments. ```bash # atari python3 -m sample_factory.run_algorithm --algo=DUMMY_SAMPLER --env=atari_pong --env_frameskip=4 --num_workers=12 --num_envs_per_worker=1 --sample_env_frames=1600000 # mujoco python3 -m sample_factory.run_algorithm --algo=DUMMY_SAMPLER --env=mujoco_ant --env_frameskip=1 --num_workers=12 --num_envs_per_worker=1 --sample_env_frames=1000000 ``` -------------------------------- ### Create Customized VizDoom Environment Source: https://github.com/sail-sg/envpool/blob/main/docs/env/vizdoom.rst Use VizdoomCustom-v1 with custom configuration and WAD files. Ensure paths to cfg and wad files are correctly specified. ```python env = envpool.make( "VizdoomCustom-v1", cfg_path="xxx.cfg", wad_path="xxx.wad", ... ) ``` -------------------------------- ### Run Mujoco Benchmark with Sample Factory Source: https://github.com/sail-sg/envpool/blob/main/docs/content/benchmark.rst Executes the Mujoco benchmark using the Sample Factory library. Note that Sample Factory has specific dependency requirements and may not be included in the main `requirements.txt`. ```bash # mujoco python3 -m sample_factory.run_algorithm --algo=DUMMY_SAMPLER --env=mujoco_ant --env_frameskip=1 --num_workers=12 --num_envs_per_worker=1 --sample_env_frames=1000000 ``` -------------------------------- ### Create Classic Control Environments (CartPole, Pendulum) Source: https://context7.com/sail-sg/envpool/llms.txt Demonstrates creating parallel CartPole and Pendulum environments for quick testing. CartPole uses discrete actions, while Pendulum requires continuous actions. ```python import envpool import numpy as np # CartPole with multiple parallel environments env = envpool.make_gym("CartPole-v1", num_envs=20, seed=0) print(f"Obs space: {env.observation_space}") # Box(4,) - [x, dx, theta, dtheta] print(f"Action space: {env.action_space}") # Discrete(2) - left/right obs, info = env.reset() # Simple policy: push right if pole tilting right episode_rewards = np.zeros(20) for step in range(500): pole_angle = obs[:, 2] # theta is index 2 action = (pole_angle > 0).astype(np.int32) # 1 if tilting right, 0 if left obs, reward, terminated, truncated, info = env.step(action) episode_rewards += reward print(f"Mean episode reward: {episode_rewards.mean():.1f}") # ~500 for good policy # Pendulum (continuous control) pend_env = envpool.make_gymnasium("Pendulum-v1", num_envs=10) obs, _ = pend_env.reset() # Continuous action in [-2, 2] (torque) action = np.random.uniform(-2, 2, size=(10, 1)).astype(np.float32) obs, reward, terminated, truncated, info = pend_env.step(action) print(f"Pendulum reward range: [{reward.min():.2f}, {reward.max():.2f}]") ``` -------------------------------- ### Initialize Asynchronous EnvPool Source: https://github.com/sail-sg/envpool/blob/main/README.md Basic imports for setting up an asynchronous EnvPool environment. ```python import envpool import numpy as np ``` -------------------------------- ### Install Windows Dependencies via Chocolatey Source: https://github.com/sail-sg/envpool/blob/main/docs/content/build.rst Installs command-line dependencies on Windows using Chocolatey, including CMake, Make, Ninja, Strawberry Perl, and SWIG. Also sets environment variables for Qt. ```powershell choco install -y cmake make ninja strawberryperl swig $env:BAZEL_SH = "C:/Program Files/Git/usr/bin/bash.exe" $env:QT_ROOT_DIR = "C:/Qt/5.15.2/msvc2019_64" $env:BAZEL_RULES_QT_DIR = $env:QT_ROOT_DIR $env:PATH = "$env:QT_ROOT_DIR\bin;$env:PATH" ``` -------------------------------- ### Using step() with Different Action Formats Source: https://context7.com/sail-sg/envpool/llms.txt Demonstrates how to use the step() function, which combines observation, action, and info for a single step. It supports sending actions using either a direct action array or an action array with an explicit env_id. ```python # Using step() with different formats obs, info = env.reset() obs, reward, terminated, truncated, info = env.step(action, info["env_id"]) ``` -------------------------------- ### Initialize and run asynchronous EnvPool Source: https://github.com/sail-sg/envpool/blob/main/README.md Sets up an asynchronous environment pool and processes actions using send and recv. ```python num_envs = 64 batch_size = 16 env = envpool.make("Pong-v5", env_type="gym", num_envs=num_envs, batch_size=batch_size) action_num = env.action_space.n env.async_reset() # send the initial reset signal to all envs while True: obs, rew, term, trunc, info = env.recv() env_id = info["env_id"] action = np.random.randint(action_num, size=batch_size) env.send(action, env_id) ``` -------------------------------- ### envpool.make_gym, envpool.make_dm, envpool.make_gymnasium Source: https://github.com/sail-sg/envpool/blob/main/docs/content/python_interface.rst Shortcuts for creating environments with specific env_type. ```APIDOC ## envpool.make_gym, envpool.make_dm, envpool.make_gymnasium ### Description These are shortcuts for `envpool.make(..., env_type="gym" | "dm" | "gymnasium")` respectively. ### Method `envpool.make_gym(...)` `envpool.make_dm(...)` `envpool.make_gymnasium(...)` ### Endpoint N/A (Function calls) ### Parameters Same as `envpool.make`. ### Request Example ```python import envpool env_gym = envpool.make_gym("CartPole-v1", num_envs=2) env_dm = envpool.make_dm("dm_control_suite/cartpole-swingup", num_envs=2) env_gymnasium = envpool.make_gymnasium("Pong-v5", num_envs=4) ``` ### Response Returns a batched environment object with the specified `env_type`. ``` -------------------------------- ### Run Atari Benchmark with Sample Factory Source: https://github.com/sail-sg/envpool/blob/main/docs/content/benchmark.rst Executes the Atari benchmark using the Sample Factory library. Note that Sample Factory has specific dependency requirements and may not be included in the main `requirements.txt`. ```bash # atari python3 -m sample_factory.run_algorithm --algo=DUMMY_SAMPLER --env=atari_pong --env_frameskip=4 --num_workers=12 --num_envs_per_worker=1 --sample_env_frames=1600000 ``` -------------------------------- ### Define Batch Action Array Source: https://github.com/sail-sg/envpool/blob/main/README.md Example of creating an action array matching the number of parallel environments. ```python act = np.zeros(100, dtype=int) ``` -------------------------------- ### Create ThreadPool Directory and BUILD Files Source: https://github.com/sail-sg/envpool/blob/main/docs/content/new_env.rst Commands to create the necessary directory structure and empty BUILD files for the ThreadPool third-party dependency. ```bash mkdir -p third_party/threadpool touch third_party/threadpool/BUILD touch third_party/threadpool/threadpool.BUILD ``` -------------------------------- ### Create CartPoleEnvPool in C++ Source: https://github.com/sail-sg/envpool/blob/main/docs/content/new_env.rst Creates an instance of `CartPoleEnvPool` by specializing `AsyncEnvPool` with `CartPoleEnv`. This is a straightforward way to get a pool of CartPole environments. ```c++ using CartPoleEnvPool = AsyncEnvPool; ``` -------------------------------- ### Check Documentation Style and Spelling Source: https://github.com/sail-sg/envpool/blob/main/docs/content/contributing.rst Verify documentation adheres to style guides (doc8) and check for spelling errors. This ensures consistency and readability of the documentation. ```bash make docstyle make spelling ``` -------------------------------- ### Rendering API Source: https://github.com/sail-sg/envpool/blob/main/docs/content/python_interface.rst EnvPool exposes rendering capabilities through its Python wrapper. You can render environments in 'rgb_array' mode to get frames or in 'human' mode to display them visually. ```APIDOC ## Render API ### Description Provides methods to render environment states, either as RGB arrays or for human visualization. ### Method `render(env_ids: int | Sequence[int] | None = None, camera_id: int | None = None)` ### Parameters #### Path Parameters None #### Query Parameters - **env_ids** (int | Sequence[int] | None) - Optional - The environment IDs to render. If omitted, `render_env_id` is used. - **camera_id** (int | None) - Optional - The camera to use for rendering. ### Request Example ```python env = envpool.make( "Ant-v5", env_type="gymnasium", num_envs=4, render_mode="rgb_array", render_width=480, render_height=480, ) env.reset() frames = env.render(env_ids=[0, 2]) # frames.shape == (2, 480, 480, 3) viewer = envpool.make( "WalkerWalk-v1", env_type="gymnasium", num_envs=1, render_mode="human", render_env_id=0, ) viewer.reset() viewer.render() ``` ### Response #### Success Response (200) - **frames** (np.ndarray) - When `render_mode="rgb_array"`, returns a batch of RGB frames with shape `(B, H, W, 3)` and `uint8` data type. The batch dimension is always present, even for a single environment. - **None** - When `render_mode="human"`, returns `None` after displaying the frame via OpenCV. #### Response Example ```json { "example": "(batch_size, height, width, 3) uint8 numpy array for rgb_array mode" } ``` ``` -------------------------------- ### Run Python Lint and Formatting Checks Source: https://github.com/sail-sg/envpool/blob/main/docs/content/contributing.rst Execute Python linting with ruff and formatting checks. These commands ensure code quality and adherence to style guides. ```bash make ruff make py-format ``` -------------------------------- ### Apply Python-side Wrappers Source: https://github.com/sail-sg/envpool/blob/main/docs/content/new_env.rst Instantiate environment specifications and pools using the py_env utility in the package initialization. ```python from envpool.python.api import py_env from .classic_control_envpool import _CartPoleEnvPool, _CartPoleEnvSpec ( CartPoleEnvSpec, CartPoleDMEnvPool, CartPoleGymEnvPool, CartPoleGymnasiumEnvPool, ) = py_env(_CartPoleEnvSpec, _CartPoleEnvPool) __all__ = [ "CartPoleEnvSpec", "CartPoleDMEnvPool", "CartPoleGymEnvPool", "CartPoleGymnasiumEnvPool", ] ``` -------------------------------- ### Inspect Environment Specifications Source: https://context7.com/sail-sg/envpool/llms.txt Shows how to use envpool.make_spec to get environment specifications without instantiating the full environment. Accesses observation and action spaces, and configuration details. ```python import envpool # Get spec without creating full environment spec = envpool.make_spec("Ant-v5", num_envs=16) # Gym-style spaces print(f"Observation space: {spec.observation_space}") print(f"Action space: {spec.action_space}") # dm_env-style specs obs_spec = spec.observation_spec() action_spec = spec.action_spec() print(f"Observation spec: {obs_spec}") print(f"Action spec: {action_spec}") # Access configuration print(f"Config: {spec.config}") print(f"Reward threshold: {spec.reward_threshold}") ``` -------------------------------- ### Build Wheel for Release Source: https://github.com/sail-sg/envpool/blob/main/docs/content/build.rst Builds a wheel file specifically for release, likely including optimizations and production configurations. ```bash make bazel-release ``` -------------------------------- ### Get XLA Functions from EnvPool Instance Source: https://github.com/sail-sg/envpool/blob/main/docs/content/xla_interface.rst Obtain the XLA-compatible step, recv, and send functions from an envpool instance. These functions are required for integrating with JAX/Tensorflow for JIT compilation. ```python env = envpool.make(..., env_type="gym" | "dm" | "gymnasium") handle, recv, send, step = env.xla() ``` -------------------------------- ### Training Initialization Output Source: https://github.com/sail-sg/envpool/blob/main/demo/envpool_demo_pong.ipynb This output indicates the successful initialization of the training process, including the seed value, device selection, and initial network build details. ```text self.seed = 322 Started to train current training device: cuda:0 conv_name: conv2d build mlp: 3136 RunningMeanStd: (1,) RunningMeanStd: (4, 84, 84) ``` -------------------------------- ### Configure Thread Affinity and NUMA Source: https://context7.com/sail-sg/envpool/llms.txt Demonstrates high-performance configuration for multi-core systems using thread pool size and thread affinity settings. Includes examples for both general high-performance and NUMA-aware execution. ```python import envpool import numpy as np # High-performance configuration for multi-core systems env = envpool.make_gym( "Pong-v5", num_envs=256, batch_size=64, num_threads=64, # Thread pool size thread_affinity_offset=-1, # -1 disables affinity (default) seed=42, ) # For NUMA-aware execution on multi-socket machines: # Set thread_affinity_offset to bind threads to specific cores # Example: offset=0 binds to cores 0-63, offset=64 binds to 64-127 env_numa = envpool.make_gym( "Ant-v5", num_envs=128, batch_size=32, num_threads=32, thread_affinity_offset=0, # Start binding from core 0 ) # Run benchmark import time obs, info = env.reset() start = time.time() steps = 10000 for _ in range(steps): action = np.random.randint(env.action_space.n, size=64) obs, reward, terminated, truncated, info = env.step(action) elapsed = time.time() - start fps = (steps * 64) / elapsed print(f"Throughput: {fps:.0f} FPS") ``` -------------------------------- ### Add Classic Control Registration to py_library Source: https://github.com/sail-sg/envpool/blob/main/docs/content/new_env.rst This diff shows how to add the classic control registration to the main entry point library. ```diff py_library( name = "entry", srcs = ["entry.py"], deps = [ "//envpool/atari:atari_registration", + "//envpool/classic_control:classic_control_registration", ], ) ``` -------------------------------- ### Run Training with Ant-v4 EnvPool Configuration Source: https://github.com/sail-sg/envpool/blob/main/demo/envpool_demo_ant.ipynb This Python script initializes a Runner object, loads the Ant-v4 EnvPool configuration, and starts the training process. Ensure the Runner class is defined and accessible. ```python runner = Runner() runner.load(ant_config) runner.run({ 'train': True, }) ``` -------------------------------- ### Include EnvPool Headers for Python Integration in C++ Source: https://github.com/sail-sg/envpool/blob/main/docs/content/new_env.rst Includes necessary headers from the EnvPool library to enable integration with Python interfaces using pybind11. This setup is required for exposing C++ environments to Python. ```c++ #include "envpool/classic_control/cartpole.h" #include "envpool/core/py_envpool.h" ``` -------------------------------- ### Create Gymnasium-Compatible Environment Source: https://context7.com/sail-sg/envpool/llms.txt Use envpool.make_gymnasium as a shortcut for creating environments with the modern Gymnasium interface. This is recommended for new projects. It supports continuous action spaces. ```python import envpool import numpy as np # Create Gymnasium-compatible environment pool env = envpool.make_gymnasium("Ant-v5", num_envs=8) # Access Gymnasium-style spaces print(f"Observation space: {env.observation_space}") # Box(27,) print(f"Action space: {env.action_space}") # Box(8,) continuous # Reset returns (obs, info) tuple following Gymnasium convention obs, info = env.reset() print(f"Initial obs shape: {obs.shape}") # (8, 27) # Step with continuous actions action = np.random.uniform(-1, 1, size=(8, 8)).astype(np.float32) obs, reward, terminated, truncated, info = env.step(action) # Check which environments finished done = np.logical_or(terminated, truncated) print(f"Episodes done: {done.sum()}") ``` -------------------------------- ### Asynchronous Execution Mode Setup Source: https://context7.com/sail-sg/envpool/llms.txt Configure EnvPool for asynchronous execution by setting batch_size less than num_envs. This decouples environment execution from action sending, allowing faster environments to return results while others continue processing. ```python import envpool import numpy as np # Create async environment with batch_size < num_envs num_envs = 64 batch_size = 16 # Only 16 results returned per recv() env = envpool.make_gym( "Pong-v5", num_envs=num_envs, batch_size=batch_size, ) action_num = env.action_space.n # Async reset sends reset signal to all envs without waiting env.async_reset() ``` -------------------------------- ### Define State Specification for Multi-Player Environments in C++ Source: https://github.com/sail-sg/envpool/blob/main/docs/content/new_env.rst Defines the state specification for multi-player environments using `MakeDict` and `Bind`. This example shows how to specify shapes and types for various state components, including player-specific observations and information. ```c++ template static decltype(auto) StateSpec(const Config& conf) { return MakeDict( "obs:players.obs"_.Bind(Spec({-1, 4, 84, 84})), "obs:players.location"_.Bind(Spec({-1, 2})), "info:players.health"_.Bind(Spec({-1})), "info:player_num"_.Bind(Spec({})), "info:bla"_.Bind(Spec({2, 3, 3})), "info:list"_.Bind(Spec>({-1})) ); } ``` -------------------------------- ### Environment Rendering to RGB Arrays Source: https://context7.com/sail-sg/envpool/llms.txt Shows how to enable rendering in EnvPool environments using 'rgb_array' mode and retrieve frames as NumPy arrays. Demonstrates rendering specific environments and single environments. ```python import envpool import numpy as np # Create environment with rendering enabled env = envpool.make_gymnasium( "Ant-v5", num_envs=4, render_mode="rgb_array", render_width=480, render_height=480, ) # Reset and take some steps obs, info = env.reset() action = np.random.uniform(-1, 1, size=(4, 8)).astype(np.float32) obs, reward, terminated, truncated, info = env.step(action) # Render specific environments - returns batched RGB frames frames = env.render(env_ids=[0, 2]) print(f"Frames shape: {frames.shape}") # (2, 480, 480, 3) print(f"Frame dtype: {frames.dtype}") # uint8 # Render single environment (still returns batch dimension) frame = env.render(env_ids=[0]) print(f"Single frame shape: {frame.shape}") # (1, 480, 480, 3) # Human mode displays via OpenCV (requires opencv-python) viewer = envpool.make_gymnasium( "WalkerWalk-v1", num_envs=1, render_mode="human", render_env_id=0, ) viewer.reset() viewer.render() # Opens window, returns None ``` -------------------------------- ### Run Single Bazel Test and Execute Source: https://github.com/sail-sg/envpool/blob/main/docs/content/contributing.rst Execute a specific Bazel test, such as the Mujoco integration test, without building other dependencies like OpenCV. Alternatively, navigate to the runfiles directory and execute the test binary directly. ```bash bazel test --test_output=all //envpool/mujoco:mujoco_gym_align_test --config=test # or alternatively cd bazel-bin/envpool/mujoco/mujoco_gym_align_test.runfiles/envpool/ ./envpool/mujoco/mujoco_gym_align_test ``` -------------------------------- ### Actor Loop with Synchronous XLA Step Source: https://github.com/sail-sg/envpool/blob/main/docs/content/xla_interface.rst Example of an actor loop using the synchronous XLA step function. This is suitable for environments compatible with gym versions prior to 0.26, or for DataMirror environments. Ensure correct handling of return values based on the gym version. ```python def actor_step(iter, loop_var): handle0, states = loop_var action = policy(states) # for gym < 0.26 handle1, (new_states, rew, done, info) = step(handle0, action) # for gym >= 0.26 # handle1, (new_states, rew, term, trunc, info) = step(handle0, action) # for dm # handle1, new_states = step(handle0, action) return (handle1, new_states) @jit def run_actor_loop(num_steps, init_var): return lax.fori_loop(0, num_steps, actor_step, init_var) states = env.reset() run_actor_loop(100, (handle, states)) ``` -------------------------------- ### POST /envpool/make_dm Source: https://github.com/sail-sg/envpool/blob/main/docs/env/dm_control.rst Initializes a DeepMind Control Suite environment within EnvPool. ```APIDOC ## POST /envpool/make_dm ### Description Creates a vectorized environment instance for a specific DeepMind Control Suite task. ### Method POST ### Endpoint envpool.make_dm(task_name, num_envs) ### Parameters #### Request Body - **task_name** (string) - Required - The environment name in 'DomainNameTaskName-v1' format (e.g., 'HopperHop-v1'). - **num_envs** (integer) - Required - The number of parallel environments to instantiate. ### Request Example { "task_name": "HopperHop-v1", "num_envs": 1 } ### Response #### Success Response (200) - **env** (object) - The initialized EnvPool environment instance. ```