pythonadvanced
Gymnasium RL Custom Environment
Create a custom reinforcement learning environment with Gymnasium's Env interface.
pythonPress ⌘/Ctrl + Shift + C to copy
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!');
breakUse Cases
- custom RL environments
- game AI
- robotics simulation
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
typescriptintermediate
OpenAI Chat Completion with Streaming
Stream GPT responses token-by-token using the OpenAI SDK with async iteration.
Best for: chatbot UI
#openai#streaming
typescriptbeginner
Generate Text Embeddings with OpenAI
Create vector embeddings for semantic search and similarity matching using text-embedding-3-small.
Best for: semantic search
#openai#embeddings
typescriptadvanced
RAG Pipeline (Retrieve + Augment + Generate)
Minimal RAG implementation: embed a query, retrieve top-k chunks, inject into prompt.
Best for: document Q&A
#rag#embeddings
typescriptbeginner
Claude Messages API (Anthropic SDK)
Send messages to Claude using the official Anthropic SDK with system prompt and user turn.
Best for: AI assistant
#anthropic#claude