pythonadvanced

Gymnasium RL Custom Environment

Create a custom reinforcement learning environment with Gymnasium's Env interface.

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

class SimpleGridEnv(gym.Env):
    metadata = {'render_modes': ['human']}

    def __init__(self, size: int = 5):
        self.size = size
        self.observation_space = spaces.Box(low=0, high=size-1, shape=(2,), dtype=np.int32)
        self.action_space      = spaces.Discrete(4)  # up, down, left, right
        self._target = np.array([size-1, size-1])

    def reset(self, seed=None, options=None):
        super().reset(seed=seed)
        self._agent = np.array([0, 0])
        return self._agent.copy(), {}

    def step(self, action):
        delta = {0: [-1,0], 1: [1,0], 2: [0,-1], 3: [0,1]}[action]
        self._agent = np.clip(self._agent + delta, 0, self.size - 1)
        terminated = np.array_equal(self._agent, self._target)
        reward = 1.0 if terminated else -0.01
        return self._agent.copy(), reward, terminated, False, {}

env = SimpleGridEnv()
obs, _ = env.reset(seed=42)
for _ in range(20):
    action = env.action_space.sample()
    obs, reward, done, _, _ = env.step(action)
    if done:
        print('Goal reached!');
        break

Use Cases

  • custom RL environments
  • game AI
  • robotics simulation

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.