From e4f19f3a426b03a2a36f2c4ed1a069f6fc3a0083 Mon Sep 17 00:00:00 2001 From: Rick Staa Date: Wed, 31 May 2023 10:54:22 +0200 Subject: [PATCH] feat: replace gym with gymnasium (#100) This commit replaces the https://www.gymlibrary.dev/ library with the new maintained https://gymnasium.farama.org/ library. --- README.md | 2 +- TODOS.md | 19 ++ examples/requirements.txt | 2 +- examples/use_simzoo.py | 8 +- package.json | 6 +- setup.cfg | 14 +- simzoo/__init__.py | 6 +- simzoo/common/disturber.py | 14 +- simzoo/common/helpers.py | 6 +- simzoo/envs/biological/oscillator/README.md | 6 +- simzoo/envs/biological/oscillator/__init__.py | 3 +- .../envs/biological/oscillator/oscillator.py | 18 +- .../oscillator/oscillator_disturber.py | 2 +- .../biological/oscillator/requirements.txt | 2 +- .../classic_control/cart_pole_cost/README.md | 6 +- .../cart_pole_cost/__init__.py | 3 +- .../cart_pole_cost/cart_pole_cost.py | 28 +-- .../cart_pole_cost/cart_pole_gym_old.py | 234 ------------------ .../cart_pole_cost/requirements.txt | 4 +- simzoo/envs/classic_control/ex3_ekf/README.md | 6 +- .../envs/classic_control/ex3_ekf/__init__.py | 4 +- .../envs/classic_control/ex3_ekf/ex3_ekf.py | 12 +- .../classic_control/ex3_ekf/requirements.txt | 4 +- tests/test_envs_register.py | 4 +- 24 files changed, 104 insertions(+), 309 deletions(-) create mode 100644 TODOS.md delete mode 100644 simzoo/envs/classic_control/cart_pole_cost/cart_pole_gym_old.py diff --git a/README.md b/README.md index fad8e19b..07e737c5 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ [![Python 3](https://img.shields.io/badge/Python-3.8%20%7C%203.7%20%7C%203.6-brightgreen)](https://www.python.org/) [![Contributions](https://img.shields.io/badge/contributions-welcome-brightgreen.svg)](contributing.md) -A python package containing the non ROS-based [bayesian\_learning\_control](https://github.com/rickstaa/bayesian-learning-control) Openai gym environments. +A python package containing the non ROS-based [bayesian\_learning\_control](https://github.com/rickstaa/bayesian-learning-control) [Farama Foundation](https://farama.org/) gymnasium environments. ## Clone the repository diff --git a/TODOS.md b/TODOS.md new file mode 100644 index 00000000..b6b06ad8 --- /dev/null +++ b/TODOS.md @@ -0,0 +1,19 @@ +# Cartpole + +* Check if beyond\_Done is still used. +* Check why reward\_range is set. +* Check step function, check reset function, check render function. +* Get up to date with new code. +* Test truncated. + +# Oscillator + +* Review code. + +# Ex3-EKF + +* Review code. + +# Disturber + +* Cleanup code. diff --git a/examples/requirements.txt b/examples/requirements.txt index 888f389f..b7969e28 100644 --- a/examples/requirements.txt +++ b/examples/requirements.txt @@ -1 +1 @@ -gym==0.26.0 +gymnasium==0.28.1 diff --git a/examples/use_simzoo.py b/examples/use_simzoo.py index 1232c333..b67c2d89 100644 --- a/examples/use_simzoo.py +++ b/examples/use_simzoo.py @@ -1,17 +1,17 @@ """A simple example on how to use the Simzoo environments. """ -import gym +import gymnasium as gym import simzoo # noqa: F401 -ENV_NAME = "Oscillator-v1" +# ENV_NAME = "Oscillator-v1" # ENV_NAME = "Ex3EKF-v1" # ENV_NAME = "CartPoleCost-v0" -# ENV_NAME = "CartPole-v1" +ENV_NAME = "CartPole-v1" if __name__ == "__main__": - env = gym.make(ENV_NAME, render_mode="human") + env = gym.make(ENV_NAME) # Define a policy function. # NOTE: Can be any function that takes an observation and returns an action. diff --git a/package.json b/package.json index d2cdb1bd..663fdf97 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,14 @@ { "name": "simzoo", "version": "0.5.6", - "description": "Package that contains the gym environments of the bayesian-learning-control package.", + "description": "Package that contains the gymnasium environments of the bayesian-learning-control package.", "keywords": [ "reinforcement-learning", "simulation", "openai-gym", - "gym-environments" + "gym-environments", + "gymnasium", + "gymnasium-environments" ], "homepage": "https://github.com/rickstaa/simzoo#readme", "bugs": { diff --git a/setup.cfg b/setup.cfg index 6d72d34f..440d4360 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,13 +1,19 @@ [metadata] name = simzoo version = 0.5.6 -description = A python package containing the bayesian_learning_control openai gym environments. +description = A python package containing the bayesian_learning_control gymnasium environments. long_description = file: README.md, CHANGELOG.md, LICENSE long_description_content_type=text/markdown author = Rick Staa author_email = rick.staa@outlook.com url = https://github.com/rickstaa/simzoo -keywords = rl, openai gym +keywords = + "reinforcement-learning", + "simulation", + "openai-gym", + "gym-environments", + "gymnasium", + "gymnasium-environments" license = Rick Staa copyright license_file = LICENSE classifiers = @@ -21,7 +27,7 @@ classifiers = packages = find: include_package_data = True install_requires = - gym + gymnasium matplotlib iteration_utilities python_requires = >=3.5 @@ -37,7 +43,7 @@ dev = flake8 black build = - gym=>0.25.0 + gymnasium matplotlib==3.7.0 [flake8] diff --git a/simzoo/__init__.py b/simzoo/__init__.py index e3beefaf..67838158 100644 --- a/simzoo/__init__.py +++ b/simzoo/__init__.py @@ -1,10 +1,10 @@ -"""Module that register the Simzoo gym environments. +"""Module that register the Simzoo gymnasium environments. """ import importlib -import gym -from gym.envs.registration import register +import gymnasium as gym +from gymnasium.envs.registration import register # Create import prefix as stand-alone package or name_space package (mlc) if importlib.util.find_spec("simzoo") is not None: diff --git a/simzoo/common/disturber.py b/simzoo/common/disturber.py index c9bd1e44..108330f4 100644 --- a/simzoo/common/disturber.py +++ b/simzoo/common/disturber.py @@ -4,11 +4,11 @@ `Robustness Evaluation `_ documentation. """ # noqa: E501 -# IMPROVE: Replace with gym wrappers https://alexandervandekleut.github.io/gym-wrappers/ +# IMPROVE: Replace with gymnasium wrappers https://alexandervandekleut.github.io/gym-wrappers/ # noqa: E501 import re -import gym +import gymnasium as gym import numpy as np from iteration_utilities import deepflatten @@ -261,8 +261,8 @@ def __init__(self, disturber_cfg=None): present in the :class:`Disturber` class file. """ # noqa: E501 assert any([issubclass(item, gym.Env) for item in self.__class__.__bases__]), ( - "Only classes that also inherit from the 'gym.Env' class can inherit from " - "the 'Disturber' class." + "Only classes that also inherit from the 'gymnasium.Env' class can inherit " + "from the 'Disturber' class." ) self.disturber_done = False @@ -1135,7 +1135,7 @@ def init_disturber( # noqa E901 disturbance_variant=None, disturber_cfg=None, include_baseline=True, - ): # IMPROVE: Can be removed when using gym wrapper + ): # IMPROVE: Can be removed when using gymnasium wrapper """Initializes the environment/step disturber. Args: @@ -1218,7 +1218,7 @@ def init_disturber( # noqa E901 ) def disturbed_step(self, action, *args, **kwargs): # noqa: C901 - """Takes a action inside the gym environment while applying the requested + """Takes a action inside the gymnasium environment while applying the requested disturbance. Args: @@ -1231,7 +1231,7 @@ def disturbed_step(self, action, *args, **kwargs): # noqa: C901 Returns: numpy.ndarray: The disturbed step. - """ # IMPROVE: Change to action when using gym wrapper + """ # IMPROVE: Change to action when using gymnasium wrapper if self._disturbance_type is None: raise RuntimeError( "You are trying to retrieve a disturbed step while the disturber has " diff --git a/simzoo/common/helpers.py b/simzoo/common/helpers.py index fa18c41e..08a0519a 100644 --- a/simzoo/common/helpers.py +++ b/simzoo/common/helpers.py @@ -4,15 +4,15 @@ import re import numpy as np -from gym.utils import colorize as gym_colorize +from gymnasium.utils import colorize as gym_colorize def colorize(string, color, bold=False, highlight=False): """Colorize a string. .. seealso:: - This function wraps the :meth:`gym.utils.colorize` function to make sure that it - also works with empty empty color strings. + This function wraps the :meth:`gymnasium.utils.colorize` function to make sure + that it also works with empty empty color strings. Args: string (str): The string you want to colorize. diff --git a/simzoo/envs/biological/oscillator/README.md b/simzoo/envs/biological/oscillator/README.md index 48effc02..ac5d416d 100644 --- a/simzoo/envs/biological/oscillator/README.md +++ b/simzoo/envs/biological/oscillator/README.md @@ -1,6 +1,6 @@ -# Oscillator gym environment +# Oscillator gymnasium environment -A gym environment for a synthetic oscillatory network of transcriptional regulators +A gymnasium environment for a synthetic oscillatory network of transcriptional regulators called a repressilator. A repressilator is a three-gene regulatory network where the dynamics of mRNA and proteins follow an oscillatory behaviour ([see Elowitch et al. 2000](https://www-nature-com.tudelft.idm.oclc.org/articles/35002125) @@ -46,4 +46,4 @@ the error when a step is taken. This results in returning the following array: ## How to use -This environment is part of the [simzoo package](https://github.com/rickstaa/simzoo). It is therefore registered as a gym environment when you import the Simzoo package. If you want to use the environment in the stand-alone mode, you can register it yourself. +This environment is part of the [simzoo package](https://github.com/rickstaa/simzoo). It is therefore registered as a gymnasium environment when you import the Simzoo package. If you want to use the environment in the stand-alone mode, you can register it yourself. diff --git a/simzoo/envs/biological/oscillator/__init__.py b/simzoo/envs/biological/oscillator/__init__.py index f52e2c20..f11297f6 100644 --- a/simzoo/envs/biological/oscillator/__init__.py +++ b/simzoo/envs/biological/oscillator/__init__.py @@ -1,4 +1,5 @@ -"""A synthetic oscillatory network of transcriptional regulators gym environment.""" +"""A synthetic oscillatory network of transcriptional regulators gymnasium environment. +""" import importlib import sys diff --git a/simzoo/envs/biological/oscillator/oscillator.py b/simzoo/envs/biological/oscillator/oscillator.py index e5f47445..cffe3120 100644 --- a/simzoo/envs/biological/oscillator/oscillator.py +++ b/simzoo/envs/biological/oscillator/oscillator.py @@ -1,14 +1,14 @@ -"""A gym environment for a synthetic oscillatory network of transcriptional regulators -called a repressilator. A repressilator is a three-gene regulatory network where the -dynamics of mRNA and proteins follow an oscillatory behavior +"""A gymnasium environment for a synthetic oscillatory network of transcriptional +regulators called a repressilator. A repressilator is a three-gene regulatory network +where the dynamics of mRNA and proteins follow an oscillatory behavior (see https://www-nature-com.tudelft.idm.oclc.org/articles/35002125). """ -import gym +import gymnasium as gym import matplotlib.pyplot as plt import numpy as np -from gym import spaces -from gym.utils import colorize, seeding +from gymnasium import spaces +from gymnasium.utils import colorize, seeding if __name__ == "__main__": from oscillator_disturber import OscillatorDisturber @@ -22,7 +22,7 @@ class Oscillator(gym.Env, OscillatorDisturber): """Synthetic oscillatory network .. note:: - This gym environment inherits from the + This gymnasium environment inherits from the :class:`~bayesian_learning_control.simzoo.simzoo.common.disturber.Disturber` in order to be able to use it with the Robustness Evaluation tool of the Bayesian Learning Control package (BLC). For more information see @@ -180,7 +180,7 @@ def __init__( dtype=np.float32, ) - # Create random seed and set gym environment parameters + # Create random seed and set gymnasium environment parameters self.seed(seed) self.viewer = None self.state = None @@ -326,7 +326,7 @@ def reset( seed=None, random=True, ): - """Reset gym environment. + """Reset gymnasium environment. Args: seed (int, optional): A random seed for the environment. By default diff --git a/simzoo/envs/biological/oscillator/oscillator_disturber.py b/simzoo/envs/biological/oscillator/oscillator_disturber.py index fcf69b1e..1c48021b 100644 --- a/simzoo/envs/biological/oscillator/oscillator_disturber.py +++ b/simzoo/envs/biological/oscillator/oscillator_disturber.py @@ -4,7 +4,7 @@ `Robustness Evaluation `_ documentation. """ # noqa: E501 -# IMPROVE: File can be removed when a gym wrapper is used. +# IMPROVE: File can be removed when a gymnasium wrapper is used. import importlib import sys diff --git a/simzoo/envs/biological/oscillator/requirements.txt b/simzoo/envs/biological/oscillator/requirements.txt index a3ebf4ab..3581b83d 100644 --- a/simzoo/envs/biological/oscillator/requirements.txt +++ b/simzoo/envs/biological/oscillator/requirements.txt @@ -1,2 +1,2 @@ -gym==0.26.0 +gymnasium==0.28.1 matplotlib==3.7.0 diff --git a/simzoo/envs/classic_control/cart_pole_cost/README.md b/simzoo/envs/classic_control/cart_pole_cost/README.md index e520a1ad..1364ac90 100644 --- a/simzoo/envs/classic_control/cart_pole_cost/README.md +++ b/simzoo/envs/classic_control/cart_pole_cost/README.md @@ -1,8 +1,8 @@ -# CartPoleCost gym environment +# CartPoleCost gymnasium environment An un-actuated joint attaches a pole to a cart, which moves along a frictionless track. This environment corresponds to the [CartPole-v1](https://gym.openai.com/envs/CartPole-v1/) environment that is included in the -openAi gym package. It is different in the fact that: +openAi gymnasium package. It is different in the fact that: * In this version, the action space is continuous, wherein the OpenAi version it is discrete. @@ -54,4 +54,4 @@ This info dictionary contains the following keys: ## How to use -This environment is part of the [simzoo package](https://github.com/rickstaa/simzoo). It is therefore registered as a gym environment when you import the Simzoo package. If you want to use the environment in stand-alone mode, you can register it yourself. +This environment is part of the [simzoo package](https://github.com/rickstaa/simzoo). It is therefore registered as a gymnasium environment when you import the Simzoo package. If you want to use the environment in stand-alone mode, you can register it yourself. diff --git a/simzoo/envs/classic_control/cart_pole_cost/__init__.py b/simzoo/envs/classic_control/cart_pole_cost/__init__.py index 45506c88..c1305f72 100644 --- a/simzoo/envs/classic_control/cart_pole_cost/__init__.py +++ b/simzoo/envs/classic_control/cart_pole_cost/__init__.py @@ -1,4 +1,5 @@ -"""A synthetic oscillatory network of transcriptional regulators gym environment.""" +"""A synthetic oscillatory network of transcriptional regulators gymnasium environment. +""" import importlib import sys diff --git a/simzoo/envs/classic_control/cart_pole_cost/cart_pole_cost.py b/simzoo/envs/classic_control/cart_pole_cost/cart_pole_cost.py index c81b50ab..29029ecd 100644 --- a/simzoo/envs/classic_control/cart_pole_cost/cart_pole_cost.py +++ b/simzoo/envs/classic_control/cart_pole_cost/cart_pole_cost.py @@ -15,12 +15,12 @@ import math -import gym +import gymnasium as gym import matplotlib.pyplot as plt import numpy as np -from gym import logger, spaces -from gym.error import DependencyNotInstalled -from gym.utils import colorize, seeding +from gymnasium import logger, spaces +from gymnasium.error import DependencyNotInstalled +from gymnasium.utils import colorize, seeding if __name__ == "__main__": from cart_pole_cost_disturber import CartPoleDisturber @@ -31,7 +31,7 @@ class CartPoleCost(gym.Env, CartPoleDisturber): - """Continuous action space CartPole gym environment + """Continuous action space CartPole gymnasium environment Description: A pole is attached by an un-actuated joint to a cart, which moves along @@ -40,8 +40,8 @@ class CartPoleCost(gym.Env, CartPoleDisturber): velocity. Source: - This environment corresponds to the version that is included in the OpenAi gym - package. It is different in the fact that: + This environment corresponds to the version that is included in the Farama + Foundation gymnasium package. It is different in the fact that: - In this version, the action space is continuous, wherein the OpenAi version it is discrete. @@ -103,7 +103,7 @@ class CartPoleCost(gym.Env, CartPoleDisturber): metadata = { "render.modes": ["human", "rgb_array"], "render_fps": 50, - } # Not used during training but in other gym utilities + } # Not used during training but in other gymnasium utilities instances = [] def __init__( @@ -221,7 +221,7 @@ def __init__( dtype=np.float32, ) - # Create random seed and set gym environment parameters + # Create random seed and set gymnasium environment parameters self.seed(seed) # TODO: remove self.screen_width = 600 @@ -447,7 +447,7 @@ def step(self, action): ) def reset(self, seed=None, random=True): - """Reset gym environment. + """Reset gymnasium environment. Args: seed (int, optional): A random seed for the environment. By default @@ -492,7 +492,7 @@ def render(self): """Render one frame of the environment.""" if self.render_mode is None: env_command = ( - f"gym.make('{self.spec.id}', render_mode='rgb_array')" + f"gymnasium.make('{self.spec.id}', render_mode='rgb_array')" if self.spec else f'{self.__class__.__name__}(render_mode="rgb_array")' ) @@ -508,7 +508,7 @@ def render(self): from pygame import gfxdraw except ImportError: raise DependencyNotInstalled( - "pygame is not installed, run `pip install gym[classic_control]`" + "pygame is not installed, run `pip install gymnasium[classic_control]`" ) if self.screen is None: @@ -619,14 +619,14 @@ def _pole_mass_length(self): def tau(self): """Property that also makes the timestep available under the :attr:`tau` attribute. This was done to keep this environment consistent with the - original gym environment. + original gymnasium environment. """ return self.dt if __name__ == "__main__": print("Settting up CartpoleCost environment.") - env = gym.make("CartPoleCost", render_mode="human") + env = gym.make("CartPoleCost") # Take T steps in the environment T = 1000 diff --git a/simzoo/envs/classic_control/cart_pole_cost/cart_pole_gym_old.py b/simzoo/envs/classic_control/cart_pole_cost/cart_pole_gym_old.py deleted file mode 100644 index d079e8da..00000000 --- a/simzoo/envs/classic_control/cart_pole_cost/cart_pole_gym_old.py +++ /dev/null @@ -1,234 +0,0 @@ -""" -Classic cart-pole system implemented by Rich Sutton et al. -Copied from http://incompleteideas.net/sutton/book/code/pole.c -permalink: https://perma.cc/C9ZM-652R -""" - -import math -import gym -from gym import spaces, logger -from gym.utils import seeding -import numpy as np - - -class CartPoleEnv(gym.Env): - """ - Description: - A pole is attached by an un-actuated joint to a cart, which moves along - a frictionless track. The pendulum starts upright, and the goal is to - prevent it from falling over by increasing and reducing the cart's - velocity. - - Source: - This environment corresponds to the version of the cart-pole problem - described by Barto, Sutton, and Anderson - - Observation: - Type: Box(4) - Num Observation Min Max - 0 Cart Position -4.8 4.8 - 1 Cart Velocity -Inf Inf - 2 Pole Angle -0.418 rad (-24 deg) 0.418 rad (24 deg) - 3 Pole Angular Velocity -Inf Inf - - Actions: - Type: Discrete(2) - Num Action - 0 Push cart to the left - 1 Push cart to the right - - Note: The amount the velocity that is reduced or increased is not - fixed; it depends on the angle the pole is pointing. This is because - the center of gravity of the pole increases the amount of energy needed - to move the cart underneath it - - Reward: - Reward is 1 for every step taken, including the termination step - - Starting State: - All observations are assigned a uniform random value in [-0.05..0.05] - - Episode Termination: - Pole Angle is more than 12 degrees. - Cart Position is more than 2.4 (center of the cart reaches the edge of - the display). - Episode length is greater than 200. - Solved Requirements: - Considered solved when the average return is greater than or equal to - 195.0 over 100 consecutive trials. - """ - - metadata = {"render.modes": ["human", "rgb_array"], "video.frames_per_second": 50} - - def __init__(self): - self.gravity = 9.8 - self.masscart = 1.0 - self.masspole = 0.1 - self.total_mass = self.masspole + self.masscart - self.length = 0.5 # actually half the pole's length - self.polemass_length = self.masspole * self.length - self.force_mag = 10.0 - self.tau = 0.02 # seconds between state updates - self.kinematics_integrator = "euler" - - # Angle at which to fail the episode - self.theta_threshold_radians = 12 * 2 * math.pi / 360 - self.x_threshold = 2.4 - - # Angle limit set to 2 * theta_threshold_radians so failing observation - # is still within bounds. - high = np.array( - [ - self.x_threshold * 2, - np.finfo(np.float32).max, - self.theta_threshold_radians * 2, - np.finfo(np.float32).max, - ], - dtype=np.float32, - ) - - self.action_space = spaces.Discrete(2) - self.observation_space = spaces.Box(-high, high, dtype=np.float32) - - self.seed() - self.viewer = None - self.state = None - - self.steps_beyond_done = None - - def seed(self, seed=None): - self.np_random, seed = seeding.np_random(seed) - return [seed] - - def step(self, action): - err_msg = "%r (%s) invalid" % (action, type(action)) - assert self.action_space.contains(action), err_msg - - x, x_dot, theta, theta_dot = self.state - force = self.force_mag if action == 1 else -self.force_mag - costheta = math.cos(theta) - sintheta = math.sin(theta) - - # For the interested reader: - # https://coneural.org/florian/papers/05_cart_pole.pdf - temp = ( - force + self.polemass_length * theta_dot**2 * sintheta - ) / self.total_mass - thetaacc = (self.gravity * sintheta - costheta * temp) / ( - self.length * (4.0 / 3.0 - self.masspole * costheta**2 / self.total_mass) - ) - xacc = temp - self.polemass_length * thetaacc * costheta / self.total_mass - - if self.kinematics_integrator == "euler": - x = x + self.tau * x_dot - x_dot = x_dot + self.tau * xacc - theta = theta + self.tau * theta_dot - theta_dot = theta_dot + self.tau * thetaacc - else: # semi-implicit euler - x_dot = x_dot + self.tau * xacc - x = x + self.tau * x_dot - theta_dot = theta_dot + self.tau * thetaacc - theta = theta + self.tau * theta_dot - - self.state = (x, x_dot, theta, theta_dot) - - done = bool( - x < -self.x_threshold - or x > self.x_threshold - or theta < -self.theta_threshold_radians - or theta > self.theta_threshold_radians - ) - - if not done: - reward = 1.0 - elif self.steps_beyond_done is None: - # Pole just fell! - self.steps_beyond_done = 0 - reward = 1.0 - else: - if self.steps_beyond_done == 0: - logger.warn( - "You are calling 'step()' even though this " - "environment has already returned done = True. You " - "should always call 'reset()' once you receive 'done = " - "True' -- any further steps are undefined behavior." - ) - self.steps_beyond_done += 1 - reward = 0.0 - - return np.array(self.state), reward, done, {} - - def reset(self): - self.state = self.np_random.uniform(low=-0.05, high=0.05, size=(4,)) - self.steps_beyond_done = None - return np.array(self.state) - - def render(self, mode="human"): - screen_width = 600 - screen_height = 400 - - world_width = self.x_threshold * 2 - scale = screen_width / world_width - carty = 100 # TOP OF CART - polewidth = 10.0 - polelen = scale * (2 * self.length) - cartwidth = 50.0 - cartheight = 30.0 - - if self.viewer is None: - from gym.envs.classic_control import rendering - - self.viewer = rendering.Viewer(screen_width, screen_height) - l, r, t, b = -cartwidth / 2, cartwidth / 2, cartheight / 2, -cartheight / 2 - axleoffset = cartheight / 4.0 - cart = rendering.FilledPolygon([(l, b), (l, t), (r, t), (r, b)]) - self.carttrans = rendering.Transform() - cart.add_attr(self.carttrans) - self.viewer.add_geom(cart) - l, r, t, b = ( - -polewidth / 2, - polewidth / 2, - polelen - polewidth / 2, - -polewidth / 2, - ) - pole = rendering.FilledPolygon([(l, b), (l, t), (r, t), (r, b)]) - pole.set_color(0.8, 0.6, 0.4) - self.poletrans = rendering.Transform(translation=(0, axleoffset)) - pole.add_attr(self.poletrans) - pole.add_attr(self.carttrans) - self.viewer.add_geom(pole) - self.axle = rendering.make_circle(polewidth / 2) - self.axle.add_attr(self.poletrans) - self.axle.add_attr(self.carttrans) - self.axle.set_color(0.5, 0.5, 0.8) - self.viewer.add_geom(self.axle) - self.track = rendering.Line((0, carty), (screen_width, carty)) - self.track.set_color(0, 0, 0) - self.viewer.add_geom(self.track) - - self._pole_geom = pole - - if self.state is None: - return None - - # Edit the pole polygon vertex - pole = self._pole_geom - l, r, t, b = ( - -polewidth / 2, - polewidth / 2, - polelen - polewidth / 2, - -polewidth / 2, - ) - pole.v = [(l, b), (l, t), (r, t), (r, b)] - - x = self.state - cartx = x[0] * scale + screen_width / 2.0 # MIDDLE OF CART - self.carttrans.set_translation(cartx, carty) - self.poletrans.set_rotation(-x[2]) - - return self.viewer.render(return_rgb_array=mode == "rgb_array") - - def close(self): - if self.viewer: - self.viewer.close() - self.viewer = None diff --git a/simzoo/envs/classic_control/cart_pole_cost/requirements.txt b/simzoo/envs/classic_control/cart_pole_cost/requirements.txt index 978b2d66..3581b83d 100644 --- a/simzoo/envs/classic_control/cart_pole_cost/requirements.txt +++ b/simzoo/envs/classic_control/cart_pole_cost/requirements.txt @@ -1,2 +1,2 @@ -gym==0.26.0 -matplotlib==3.7.0 \ No newline at end of file +gymnasium==0.28.1 +matplotlib==3.7.0 diff --git a/simzoo/envs/classic_control/ex3_ekf/README.md b/simzoo/envs/classic_control/ex3_ekf/README.md index ee32b6c6..18bcba31 100644 --- a/simzoo/envs/classic_control/ex3_ekf/README.md +++ b/simzoo/envs/classic_control/ex3_ekf/README.md @@ -1,6 +1,6 @@ -# Ex3EKF gym environment +# Ex3EKF gymnasium environment -A gym environment for a noisy master-slave system. This environment can be used to train a +A gymnasium environment for a noisy master-slave system. This environment can be used to train a RL based stationary Kalman filter. ## Observation space @@ -40,4 +40,4 @@ cost = np.square(hat_x_1 - x_1) + np.square(hat_x_2 - x_2) ## How to use -This environment is part of the [simzoo package](https://github.com/rickstaa/simzoo). It is therefore registered as a gym environment when you import the Simzoo package. If you want to use the environment in stand-alone mode, you can register it yourself. +This environment is part of the [simzoo package](https://github.com/rickstaa/simzoo). It is therefore registered as a gymnasium environment when you import the Simzoo package. If you want to use the environment in stand-alone mode, you can register it yourself. diff --git a/simzoo/envs/classic_control/ex3_ekf/__init__.py b/simzoo/envs/classic_control/ex3_ekf/__init__.py index 87da64c8..65572f50 100644 --- a/simzoo/envs/classic_control/ex3_ekf/__init__.py +++ b/simzoo/envs/classic_control/ex3_ekf/__init__.py @@ -1,6 +1,6 @@ -"""Noisy master slave system (Ex3EKF) gym environment.""" -import sys +"""Noisy master slave system (Ex3EKF) gymnasium environment.""" import importlib +import sys # Import simzoo stand-alone package or name_space package (blc) if "simzoo" in sys.modules: diff --git a/simzoo/envs/classic_control/ex3_ekf/ex3_ekf.py b/simzoo/envs/classic_control/ex3_ekf/ex3_ekf.py index 0e7b50cd..4d6299ea 100644 --- a/simzoo/envs/classic_control/ex3_ekf/ex3_ekf.py +++ b/simzoo/envs/classic_control/ex3_ekf/ex3_ekf.py @@ -1,4 +1,4 @@ -"""Noisy master slave system (Ex3EKF) gym environment. +"""Noisy master slave system (Ex3EKF) gymnasium environment. The dynamic system whose state is to be estimated: @@ -22,11 +22,11 @@ where u=[u1,u2,u3]', u=l(\\hat(x)(k),y(k)) come from the policy network l(.,.) """ -import gym +import gymnasium as gym import matplotlib.pyplot as plt import numpy as np -from gym import spaces -from gym.utils import colorize, seeding +from gymnasium import spaces +from gymnasium.utils import colorize, seeding if __name__ == "__main__": from ex3_ekf_disturber import Ex3EKFDisturber @@ -40,7 +40,7 @@ class Ex3EKF(gym.Env, Ex3EKFDisturber): """Noisy master slave system .. note:: - This gym environment inherits from the + This gymnasium environment inherits from the :class:`~bayesian_learning_control.simzoo.simzoo.common.disturber.Disturber` in order to be able to use it with the Robustness Evaluation tool of the Bayesian Learning Control package (BLC). For more information see @@ -283,7 +283,7 @@ def step(self, action): ) def reset(self, seed=None): - """Reset gym environment. + """Reset gymnasium environment. Args: seed (int, optional): A random seed for the environment. By default diff --git a/simzoo/envs/classic_control/ex3_ekf/requirements.txt b/simzoo/envs/classic_control/ex3_ekf/requirements.txt index 978b2d66..3581b83d 100644 --- a/simzoo/envs/classic_control/ex3_ekf/requirements.txt +++ b/simzoo/envs/classic_control/ex3_ekf/requirements.txt @@ -1,2 +1,2 @@ -gym==0.26.0 -matplotlib==3.7.0 \ No newline at end of file +gymnasium==0.28.1 +matplotlib==3.7.0 diff --git a/tests/test_envs_register.py b/tests/test_envs_register.py index 1443ef22..335be378 100644 --- a/tests/test_envs_register.py +++ b/tests/test_envs_register.py @@ -1,4 +1,4 @@ -"""This script is used to test whether the gym environments were successfully +"""This script is used to test whether the gymnasium environments were successfully registered. """ @@ -6,7 +6,7 @@ import sys import pytest -from gym import envs +from gymnasium import envs # Import simzoo stand-alone package or name_space package (mlc) if "simzoo" in sys.modules: