Skip to content

Getting started

Install

gym_classics2 requires Python 3.10 or newer. A stable release version is available on PyPI, install the release with:

python -m pip install gym-classics2

To install the current development version from GitHub:

python -m pip install "gym-classics2 @ git+https://github.com/mhahsler/gym-classics2.git"

Pygame rendering is optional. Install the render extra to use human or rgb_array rendering modes:

python -m pip install "gym-classics2[render]"

or

python -m pip install "gym-classics2[render] @ git+https://github.com/mhahsler/gym-classics2.git"

Run an environment

Importing gym_classics2 automatically registers its environment IDs with Gymnasium.

import gymnasium as gym
import numpy as np
import gym_classics2

seed = 42
rng = np.random.default_rng(seed)

env = gym.make("ClassicGridworld-v1", tabular=True)

observation, info = env.reset(seed=seed)
terminated = truncated = False

while not (terminated or truncated):
    action = rng.integers(env.action_space.n)
    observation, reward, terminated, truncated, info = env.step(action)

env.close()

Gymnasium returns both terminated (the task reached a terminal condition) and truncated (an external time or step limit in the environment was reached defined). gym-classics2 environments do not use truncated, but you should treat the episode as finished when either is true.

Choose a state representation

Gridworld constructors in this package accept tabular=True by default:

  • tabular=True represents observations as consecutive integer IDs and is required by the tabular algorithms included in this package.
  • tabular=False represents grid observations as (x, y) coordinates (a MultiDiscrete observation space in Gymnasium). This is useful for visualization and function approximation.

The unwrapped environment converts between the two forms:

base_env = env.unwrapped
state_id = base_env.state2id((0, 0))
coordinates = base_env.id2state(state_id)

Continue with Environments or Model access.