Skip to content

Commit

Permalink
feat: replace gym with gymnasium (#100)
Browse files Browse the repository at this point in the history
This commit replaces the https://www.gymlibrary.dev/ library with the
new maintained https://gymnasium.farama.org/ library.
  • Loading branch information
rickstaa authored May 31, 2023
1 parent 4a97126 commit e4f19f3
Show file tree
Hide file tree
Showing 24 changed files with 104 additions and 309 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 19 additions & 0 deletions TODOS.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion examples/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
gym==0.26.0
gymnasium==0.28.1
8 changes: 4 additions & 4 deletions examples/use_simzoo.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
14 changes: 10 additions & 4 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -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 = [email protected]
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 =
Expand All @@ -21,7 +27,7 @@ classifiers =
packages = find:
include_package_data = True
install_requires =
gym
gymnasium
matplotlib
iteration_utilities
python_requires = >=3.5
Expand All @@ -37,7 +43,7 @@ dev =
flake8
black
build =
gym=>0.25.0
gymnasium
matplotlib==3.7.0

[flake8]
Expand Down
6 changes: 3 additions & 3 deletions simzoo/__init__.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
14 changes: 7 additions & 7 deletions simzoo/common/disturber.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
`Robustness Evaluation <https://rickstaa.github.io/bayesian-learning-control/control/robustness_eval.html>`_
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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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 "
Expand Down
6 changes: 3 additions & 3 deletions simzoo/common/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions simzoo/envs/biological/oscillator/README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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.
3 changes: 2 additions & 1 deletion simzoo/envs/biological/oscillator/__init__.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
18 changes: 9 additions & 9 deletions simzoo/envs/biological/oscillator/oscillator.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion simzoo/envs/biological/oscillator/oscillator_disturber.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
`Robustness Evaluation <https://rickstaa.github.io/bayesian-learning-control/control/robustness_eval.html>`_
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
Expand Down
2 changes: 1 addition & 1 deletion simzoo/envs/biological/oscillator/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
gym==0.26.0
gymnasium==0.28.1
matplotlib==3.7.0
6 changes: 3 additions & 3 deletions simzoo/envs/classic_control/cart_pole_cost/README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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.
3 changes: 2 additions & 1 deletion simzoo/envs/classic_control/cart_pole_cost/__init__.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Loading

0 comments on commit e4f19f3

Please sign in to comment.