Skip to content

Commit

Permalink
Replace Gym with Gymnasium (#1580)
Browse files Browse the repository at this point in the history
* Replace Gym with Gymnasium

The maintainers of OpenAI's Gym decided to stop maintaining the library
and moved over to a fork called Gymnasium.

They advise to switch to the new library, which can be used as a drop-in
replacement.

See: https://github.com/openai/gym/blob/master/README.md

* Upgrade Gymnasium

* Add a float conversion to please MyPy
  • Loading branch information
e10e3 authored Jul 22, 2024
1 parent 038ad5d commit 9d05b77
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 46 deletions.
61 changes: 32 additions & 29 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pandas = "^2.1"

[tool.poetry.group.dev.dependencies]
graphviz = "^0.20.1"
gym = "^0.26.2"
gymnasium = "^0.29.0"
matplotlib = "^3.0.2"
mypy = "^1.6.1"
pre-commit = "^3.5.0"
Expand Down
2 changes: 1 addition & 1 deletion river/bandit/bayes_ucb.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class BayesUCB(bandit.base.Policy):
Examples
--------
>>> import gym
>>> import gymnasium as gym
>>> from river import bandit
>>> from river import proba
>>> from river import stats
Expand Down
2 changes: 1 addition & 1 deletion river/bandit/envs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

try:
import gym
import gymnasium as gym

GYM_INSTALLED = True
except ImportError:
Expand Down
4 changes: 2 additions & 2 deletions river/bandit/envs/candy_cane.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import dataclasses

import gym
import gymnasium as gym


@dataclasses.dataclass
Expand All @@ -25,7 +25,7 @@ class CandyCaneContest(gym.Env):
Examples
--------
>>> import gym
>>> import gymnasium as gym
>>> from river import stats
>>> env = gym.make('river_bandits/CandyCaneContest-v0')
Expand Down
2 changes: 1 addition & 1 deletion river/bandit/envs/testbed.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import math

import gym
import gymnasium as gym


class KArmedTestbed(gym.Env):
Expand Down
2 changes: 1 addition & 1 deletion river/bandit/epsilon_greedy.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class EpsilonGreedy(bandit.base.Policy):
Examples
--------
>>> import gym
>>> import gymnasium as gym
>>> from river import bandit
>>> from river import stats
Expand Down
4 changes: 2 additions & 2 deletions river/bandit/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import typing

try:
import gym
import gymnasium as gym
except ImportError:
...

Expand Down Expand Up @@ -52,7 +52,7 @@ def evaluate(
Examples
--------
>>> import gym
>>> import gymnasium as gym
>>> from river import bandit
>>> trace = bandit.evaluate(
Expand Down
2 changes: 1 addition & 1 deletion river/bandit/exp3.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Exp3(bandit.base.Policy):
Examples
--------
>>> import gym
>>> import gymnasium as gym
>>> from river import bandit
>>> from river import proba
>>> from river import stats
Expand Down
2 changes: 1 addition & 1 deletion river/bandit/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class RandomPolicy(bandit.base.Policy):
Examples
--------
>>> import gym
>>> import gymnasium as gym
>>> from river import bandit
>>> from river import proba
>>> from river import stats
Expand Down
3 changes: 2 additions & 1 deletion river/bandit/test_envs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import gym.utils.env_checker
import gymnasium as gym
import gymnasium.utils.env_checker
import pytest

from river import bandit
Expand Down
6 changes: 3 additions & 3 deletions river/bandit/test_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import inspect
import random

import gym
import gymnasium as gym
import pytest

from river import bandit, metrics
Expand Down Expand Up @@ -111,7 +111,7 @@ def test_better_than_random_policy(policy: bandit.base.Policy, env: gym.Env):
arm_id = policy.pull(arm_ids) # type: ignore
observation, reward, terminated, truncated, info = env.step(arm_id)
policy.update(arm_id, reward)
policy_reward += reward
policy_reward += float(reward)

random_arm_id = random_policy.pull(arm_ids) # type: ignore
(
Expand All @@ -122,7 +122,7 @@ def test_better_than_random_policy(policy: bandit.base.Policy, env: gym.Env):
info,
) = random_env.step(random_arm_id)
random_policy.update(random_arm_id, reward)
random_reward += reward
random_reward += float(reward)

n_successes += policy_reward > random_reward

Expand Down
2 changes: 1 addition & 1 deletion river/bandit/thompson.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ThompsonSampling(bandit.base.Policy):
Examples
--------
>>> import gym
>>> import gymnasium as gym
>>> from river import bandit
>>> from river import proba
>>> from river import stats
Expand Down
2 changes: 1 addition & 1 deletion river/bandit/ucb.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class UCB(bandit.base.Policy):
Examples
--------
>>> import gym
>>> import gymnasium as gym
>>> from river import bandit
>>> from river import preprocessing
>>> from river import stats
Expand Down

0 comments on commit 9d05b77

Please sign in to comment.