diff --git a/whynot/gym/__init__.py b/whynot/gym/__init__.py index 30c32f8..9c9f0aa 100644 --- a/whynot/gym/__init__.py +++ b/whynot/gym/__init__.py @@ -5,10 +5,8 @@ import sys import warnings -from gym import error -from gym.core import Env -from gym import logger +from gymnasium import error +from gymnasium.core import Env +from gymnasium import logger -from whynot.gym.envs import make, spec, register - -__all__ = ["Env", "make", "spec", "register"] +__all__ = ["Env"] diff --git a/whynot/gym/envs/__init__.py b/whynot/gym/envs/__init__.py index 176ce29..da71253 100644 --- a/whynot/gym/envs/__init__.py +++ b/whynot/gym/envs/__init__.py @@ -1,4 +1,3 @@ """Environments based on WhyNot simulators.""" -from whynot.gym.envs.registration import registry, register, make, spec from whynot.gym.envs.ode_env import ODEEnvBuilder diff --git a/whynot/gym/envs/registration.py b/whynot/gym/envs/registration.py deleted file mode 100644 index 60613a0..0000000 --- a/whynot/gym/envs/registration.py +++ /dev/null @@ -1,28 +0,0 @@ -"""Global registry of environments, for consistency with openai gym.""" -import importlib - -from gym.envs.registration import EnvRegistry - -# Keep for consistency with original API -# pylint:disable-msg=invalid-name -# Have a global registry -registry = EnvRegistry() - - -# pylint:disable-msg=redefined-builtin -def register(id, **kwargs): - """Register the environment.""" - return registry.register(id, **kwargs) - - -def make(id, **kwargs): - """Build the environment.""" - return registry.make(id, **kwargs) - - -def spec(id): - """View the spec for the environment.""" - return registry.spec(id) - - -warn_once = True diff --git a/whynot/gym/spaces/__init__.py b/whynot/gym/spaces/__init__.py index 31dbfd3..3bf2a06 100644 --- a/whynot/gym/spaces/__init__.py +++ b/whynot/gym/spaces/__init__.py @@ -1,15 +1,15 @@ -"""Ensure gym spaces are accessible if you import whynot.gym as gym.""" -from gym.spaces.space import Space -from gym.spaces.box import Box -from gym.spaces.discrete import Discrete -from gym.spaces.multi_discrete import MultiDiscrete -from gym.spaces.multi_binary import MultiBinary -from gym.spaces.tuple import Tuple -from gym.spaces.dict import Dict +"""Ensure gymnasium spaces are accessible if you import whynot.gym as gym.""" +from gymnasium.spaces.space import Space +from gymnasium.spaces.box import Box +from gymnasium.spaces.discrete import Discrete +from gymnasium.spaces.multi_discrete import MultiDiscrete +from gymnasium.spaces.multi_binary import MultiBinary +from gymnasium.spaces.tuple import Tuple +from gymnasium.spaces.dict import Dict -from gym.spaces.utils import flatdim -from gym.spaces.utils import flatten -from gym.spaces.utils import unflatten +from gymnasium.spaces.utils import flatdim +from gymnasium.spaces.utils import flatten +from gymnasium.spaces.utils import unflatten __all__ = [ "Space", diff --git a/whynot/gym/utils/__init__.py b/whynot/gym/utils/__init__.py index 66ab481..be867da 100644 --- a/whynot/gym/utils/__init__.py +++ b/whynot/gym/utils/__init__.py @@ -1,2 +1,2 @@ """Ensure gym utils are accessible if you import whynot.gym as gym.""" -from gym.utils import seeding +from gymnasium.utils import seeding diff --git a/whynot/simulators/credit/environments.py b/whynot/simulators/credit/environments.py index 02dab5b..3a448d9 100644 --- a/whynot/simulators/credit/environments.py +++ b/whynot/simulators/credit/environments.py @@ -1,10 +1,11 @@ """Interactive environment for the credit simulator.""" import copy +import gymnasium import numpy as np from whynot.gym import spaces -from whynot.gym.envs import ODEEnvBuilder, register +from whynot.gym.envs import ODEEnvBuilder from whynot.simulators.credit import ( Config, Intervention, @@ -93,9 +94,10 @@ def build_credit_env(config=None, initial_state=None): ) -register( +gymnasium.register( id="Credit-v0", entry_point=build_credit_env, + apply_api_compatibility=True, max_episode_steps=100, reward_threshold=0, ) diff --git a/whynot/simulators/hiv/environments.py b/whynot/simulators/hiv/environments.py index 2309cdd..89f7918 100644 --- a/whynot/simulators/hiv/environments.py +++ b/whynot/simulators/hiv/environments.py @@ -6,10 +6,11 @@ https://pdfs.semanticscholar.org/c030/127238b1dbad2263fba6b64b5dec7c3ffa20.pdf """ +import gymnasium import numpy as np from whynot.gym import spaces -from whynot.gym.envs import ODEEnvBuilder, register +from whynot.gym.envs import ODEEnvBuilder from whynot.simulators.hiv import Config, Intervention, simulate, State @@ -59,6 +60,8 @@ def observation_space(): reward_fn=get_reward, ) -register( - id="HIV-v0", entry_point=HivEnv, max_episode_steps=400, reward_threshold=1e10, +gymnasium.register( + id="HIV-v0", entry_point=HivEnv, apply_api_compatibility=True, max_episode_steps=400, reward_threshold=1e10, ) + +env = gymnasium.make("HIV-v0", apply_api_compatibility=True) diff --git a/whynot/simulators/opioid/environments.py b/whynot/simulators/opioid/environments.py index 1427e00..932234b 100644 --- a/whynot/simulators/opioid/environments.py +++ b/whynot/simulators/opioid/environments.py @@ -1,8 +1,10 @@ """Reinforcement learning environments for the opioid epidemic simulator.""" +import gymnasium + import numpy as np from whynot.gym import spaces -from whynot.gym.envs import ODEEnvBuilder, register +from whynot.gym.envs import ODEEnvBuilder from whynot.simulators.opioid import Config, Intervention, simulate, State @@ -58,9 +60,10 @@ def observation_space(): reward_fn=get_reward, ) -register( +gymnasium.register( id="opioid-v0", entry_point=OpioidEnv, + apply_api_compatibility=True, # The simulator starts in 2002 and ends in 2030. max_episode_steps=28, reward_threshold=0, diff --git a/whynot/simulators/world3/environments.py b/whynot/simulators/world3/environments.py index 32adf7f..423fdd5 100644 --- a/whynot/simulators/world3/environments.py +++ b/whynot/simulators/world3/environments.py @@ -1,10 +1,12 @@ """Reinforcment learning for world3.""" +import gymnasium + from itertools import product import numpy as np from whynot.gym import spaces -from whynot.gym.envs import ODEEnvBuilder, register +from whynot.gym.envs import ODEEnvBuilder from whynot.simulators.world3 import Config, Intervention, simulate, State @@ -63,6 +65,6 @@ def observation_space(): reward_fn=get_reward, ) -register( - id="world3-v0", entry_point=World3Env, max_episode_steps=400, reward_threshold=1e5, +gymnasium.register( + id="world3-v0", entry_point=World3Env, apply_api_compatibility=True, max_episode_steps=400, reward_threshold=1e5, ) diff --git a/whynot/simulators/zika/environments.py b/whynot/simulators/zika/environments.py index ae68b62..8212da6 100644 --- a/whynot/simulators/zika/environments.py +++ b/whynot/simulators/zika/environments.py @@ -6,10 +6,12 @@ https://www.sciencedirect.com/science/article/pii/S2211692316301084#! """ +import gymnasium + import numpy as np from whynot.gym import spaces -from whynot.gym.envs import ODEEnvBuilder, register +from whynot.gym.envs import ODEEnvBuilder from whynot.simulators.zika import Config, Intervention, simulate, State @@ -78,6 +80,6 @@ def action_space(): reward_fn=get_reward, ) -register( - id="Zika-v0", entry_point=ZikaEnv, max_episode_steps=200, reward_threshold=1e10, +gymnasium.register( + id="Zika-v0", entry_point=ZikaEnv, apply_api_compatibility=True, max_episode_steps=200, reward_threshold=1e10, )