Skip to main content

Async agents for Stable Baselines 3

Project description

Async Gym Agents

Wrapper environments and agent injectors to allow for drop-in async training.

import gymnasium as gym
from stable_baselines3 import TD3

from async_gym_agents.agents.async_agent import get_injected_agent
from async_gym_agents.envs.multi_env import IndexableMultiEnv

# Create env with 8 parallel envs
env = IndexableMultiEnv([lambda: gym.make("Pendulum-v1") for i in range(8)])

# Create the model, injected with async capabilities
model = get_injected_agent(TD3)("MlpPolicy", env)

# Train the model
model.learn(total_timesteps=10)

# Shut down workers
model.shutdown()

Multiprocessing

AsyncGymAgents is primarily designed for IO/Networking heavy situations and uses threads. For CPU-constrained applications, multiprocessing can be enabled using use_mp=True:

import gymnasium as gym
from async_gym_agents.agents.async_agent import get_injected_agent
from stable_baselines3 import PPO


def env_func():
    # Can be one or a list of either gym.Env or VecEnv
    # return [gym.make("Pendulum-v1") for _ in range(4)]
    # return [DummyVecEnv([partial(gym.make, "Pendulum-v1") for _ in range(4)]) for _ in range(4)]
    # return DummyVecEnv([partial(gym.make, "Pendulum-v1")
    return gym.make("Pendulum-v1")


# Create env to define spaces, it will not be used otherwise in MP mode.
env = gym.make("Pendulum-v1")

# Create the model, injected with async capabilities
model = get_injected_agent(PPO, use_mp=True)("MlpPolicy", env, envs=[env_func for _ in range(8)])

Since not all envs can be transferred to processes, a constructor envs is required. This constructor allows returning a list of processes, run in threads within a single process. This allows, e.g., balancing the tradeoff between GIL and memory usage.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

async_gym_agents-0.2.0.tar.gz (12.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

async_gym_agents-0.2.0-py3-none-any.whl (18.2 kB view details)

Uploaded Python 3

File details

Details for the file async_gym_agents-0.2.0.tar.gz.

File metadata

  • Download URL: async_gym_agents-0.2.0.tar.gz
  • Upload date:
  • Size: 12.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.10.8 Windows/10

File hashes

Hashes for async_gym_agents-0.2.0.tar.gz
Algorithm Hash digest
SHA256 364048d17678547f3eae087d3b0ca521447a1e20bd6d727da1ac7356d1445836
MD5 453bcaa183e73cbc00a55f32e7f4c2fe
BLAKE2b-256 16551fc56a4c51bd6c73995141e47d8964818b00cf7f91e185a22b36d2469120

See more details on using hashes here.

File details

Details for the file async_gym_agents-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: async_gym_agents-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 18.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.10.8 Windows/10

File hashes

Hashes for async_gym_agents-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 33ec943094226581f45b97c3ba96b23d35a55fbbedd23e4e173e4a97bc11eb8b
MD5 6da72f7234243df681c9e15ce68bd450
BLAKE2b-256 d95fe850744b99e86059d03b2184c5b0c61b79efe3bbd82bac283bc7f0508c5e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page