Skip to content

Commit

Permalink
Refactor gym to gymnasium
Browse files Browse the repository at this point in the history
Fixes Update `gym` to `gymnasium`. #8
  • Loading branch information
ll7 committed Mar 6, 2024
1 parent dda5f8a commit ed271f1
Show file tree
Hide file tree
Showing 15 changed files with 32 additions and 31 deletions.
3 changes: 2 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"github.copilot",
"genieai.chatgpt-vscode",
"ms-python.python",
"ms-python.pylint"
"ms-python.pylint",
"github.vscode-pull-request-github"
]
}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
This project provides a training environment for the simulation of a robot moving
in a pedestrian-filled space.

The project interfaces with OpenAI Gym to facilitate trainings with various
The project interfaces with Faram Foundations "Gymnasium" (former OpenAI Gym)
to facilitate trainings with various
SOTA reinforcement learning algorithms like e.g. StableBaselines3.
For simulating the pedestrians, the SocialForce model is used via a dependency
on a fork of PySocialForce.
Expand Down
5 changes: 2 additions & 3 deletions demo_defensive.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import numpy as np
from gym import spaces
from gymnasium import spaces
from stable_baselines3 import PPO

from robot_sf.robot_env import RobotEnv, OBS_DRIVE_STATE, OBS_RAYS
from robot_sf.sim_config import EnvSettings
from robot_sf.sim.sim_config import SimulationSettings
from robot_sf.robot.differential_drive import DifferentialDriveSettings


def training():

env_config = EnvSettings(
sim_config=SimulationSettings(stack_steps=1, difficulty=0, ped_density_by_difficulty=[0.06]),
robot_config=DifferentialDriveSettings(radius=1.0))
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fonttools==4.49.0
fsspec==2024.2.0
greenlet==3.0.3
grpcio==1.60.1
gym==0.26.2
# gym==0.26.2
gym-notices==0.0.8
gymnasium==0.29.1
iniconfig==2.0.0
Expand Down
2 changes: 1 addition & 1 deletion robot_sf/feature_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import List

import numpy as np
from gym import spaces
from gymnasium import spaces
import torch as th
from torch import nn
from stable_baselines3.common.torch_layers import BaseFeaturesExtractor
Expand Down
2 changes: 1 addition & 1 deletion robot_sf/robot/bicycle_drive.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Tuple

import numpy as np
from gym import spaces
from gymnasium import spaces


Vec2D = Tuple[float, float]
Expand Down
6 changes: 3 additions & 3 deletions robot_sf/robot/differential_drive.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Tuple

import numpy as np
from gym import spaces
from gymnasium import spaces


Vec2D = Tuple[float, float]
Expand Down Expand Up @@ -207,7 +207,7 @@ def observation_space(self) -> spaces.Box:
Defines the observation space for the robot based on its configuration.
Returns:
Box: An instance of gym.spaces.Box representing the continuous
Box: An instance of gymnasium.spaces.Box representing the continuous
observation space where each observation is a vector containing
linear and angular speeds.
"""
Expand All @@ -223,7 +223,7 @@ def action_space(self) -> spaces.Box:
Defines the action space for the robot based on its configuration.
Returns:
Box: An instance of gym.spaces.Box representing the continuous
Box: An instance of gymnasium.spaces.Box representing the continuous
action space where each action is a vector containing
linear and angular speeds.
"""
Expand Down
12 changes: 6 additions & 6 deletions robot_sf/robot_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
timeout condition, simulation time elapsed, and timestep count.
3. `RobotEnv`: A class that represents the robot's environment. It inherits from `VectorEnv`
from the `gym` library, which is a base class for environments that operate over
from the `gymnasium` library, which is a base class for environments that operate over
vectorized actions and observations. It includes methods for stepping through the environment,
resetting it, rendering it, and closing it.
It also defines the action and observation spaces for the robot.
Expand All @@ -31,9 +31,9 @@
from multiprocessing.pool import ThreadPool

import numpy as np
from gym.vector import VectorEnv
from gym import Env, spaces
from gym.utils import seeding
from gymnasium.vector import VectorEnv
from gymnasium import Env, spaces
from gymnasium.utils import seeding
from robot_sf.nav.map_config import MapDefinition
from robot_sf.nav.navigation import RouteNavigator

Expand Down Expand Up @@ -373,7 +373,7 @@ def init_spaces(env_config: EnvSettings, map_def: MapDefinition):

class RobotEnv(Env):
"""
Representing an OpenAI Gym environment for training a self-driving robot
Representing a Gymnasium environment for training a self-driving robot
with reinforcement learning.
"""

Expand Down Expand Up @@ -562,7 +562,7 @@ def exit(self):


class MultiRobotEnv(VectorEnv):
"""Representing an OpenAI Gym environment for training
"""Representing a Gymnasium environment for training
multiple self-driving robots with reinforcement learning"""

def __init__(
Expand Down
2 changes: 1 addition & 1 deletion robot_sf/sensor/goal_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Tuple, Union

import numpy as np
from gym import spaces
from gymnasium import spaces

Vec2D = Tuple[float, float]
PolarVec2D = Tuple[float, float]
Expand Down
2 changes: 1 addition & 1 deletion robot_sf/sensor/range_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import numpy as np
import numba
from gym import spaces
from gymnasium import spaces

from robot_sf.nav.occupancy import ContinuousOccupancy

Expand Down
2 changes: 1 addition & 1 deletion robot_sf/sensor/sensor_fusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from dataclasses import dataclass, field

import numpy as np
from gym import spaces
from gymnasium import spaces

PolarVec2D = Tuple[float, float]

Expand Down
12 changes: 6 additions & 6 deletions scripts/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import json

import numpy as np
import gym
from gym import spaces
import gymnasium
from gymnasium import spaces
from tqdm import tqdm
from stable_baselines3 import PPO, A2C

Expand Down Expand Up @@ -56,7 +56,7 @@ class EvalSettings:


@dataclass
class AdaptedEnv(gym.Env):
class AdaptedEnv(gymnasium.Env):
orig_env: RobotEnv
config: GymAdapterSettings

Expand All @@ -78,7 +78,7 @@ def reset(self):
return self.config.obs_adapter(obs)


def evaluate(env: gym.Env, model: DriveModel, num_episodes: int) -> EnvMetrics:
def evaluate(env: gymnasium.Env, model: DriveModel, num_episodes: int) -> EnvMetrics:
eval_metrics = EnvMetrics(cache_size=num_episodes)

for _ in tqdm(range(num_episodes)):
Expand All @@ -97,7 +97,7 @@ def evaluate(env: gym.Env, model: DriveModel, num_episodes: int) -> EnvMetrics:
return eval_metrics


def prepare_env(settings: EvalSettings, difficulty: int) -> gym.Env:
def prepare_env(settings: EvalSettings, difficulty: int) -> gymnasium.Env:
env_settings = EnvSettings()
env_settings.sim_config.prf_config = settings.prf_config
env_settings.sim_config.ped_density_by_difficulty = settings.ped_densities
Expand All @@ -108,7 +108,7 @@ def prepare_env(settings: EvalSettings, difficulty: int) -> gym.Env:
return AdaptedEnv(orig_env, settings.gym_config)


def prepare_model(model_path: str, env: gym.Env) -> DriveModel:
def prepare_model(model_path: str, env: gymnasium.Env) -> DriveModel:
return A2C.load(model_path, env=env)


Expand Down
2 changes: 1 addition & 1 deletion scripts/training_ppo.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def make_env():
)

model.learn(
total_timesteps=1_000_000,
total_timesteps=10_000_000,
progress_bar=True,
callback=combined_callback
)
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
PACKAGE_NAME = "robot_sf"
PACKAGE_VERSION = "2.0.0"
PACKAGE_AUTHORS = "Marco Tröster"
PACKAGE_DESCRIPTION = """This package allows implementing a "gym-style" environment
PACKAGE_DESCRIPTION = """This package allows implementing a "gymnasium-style" environment
for navigating a crowd with autonomous micromobility vehicles
"""
HOME_REPO = "https://github.com/Bonifatius94/robot_env"
EXCLUDE_FILES = []
PACKAGE_DATA = {"robot_sf": ["maps/*.json"]}
INSTALL_REQUIREMENTS = ["numpy", "gym", "pylint", "pytest", "scalene", "numba", "pygame",
INSTALL_REQUIREMENTS = ["numpy", "gymnasium", "pylint", "pytest", "scalene", "numba", "pygame",
"stable-baselines3", "tqdm", "rich", "tensorboard"]

# TODO Update this package information

def get_ext_paths(root_dir, exclude_files):
paths = []
Expand Down
2 changes: 1 addition & 1 deletion tests/env_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from gym import spaces
from gymnasium import spaces
from robot_sf.robot_env import RobotEnv, OBS_DRIVE_STATE, OBS_RAYS


Expand Down

0 comments on commit ed271f1

Please sign in to comment.