Skip to content

Getting started

Install

gym_classics2 requires Python 3.9 or newer. Install the current version from GitHub:

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

Run an environment

Registration connects the package's environment IDs to Gymnasium. It only needs to happen once in a Python process.

import gymnasium as gym
import numpy as np
import gym_classics2

gym_classics2.register()

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 was reached). Treat the episode as finished when either is true.

Choose a state representation

Gridworld constructors 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 and 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.