Launch the interactive walkthrough to discover each component, including the 3D Q-Sphere Lab & Python in Colab.
Understand the world the agent inhabits: deterministic vs stochastic worlds, full vs partial observability, and the Gymnasium API.
reset(): Restart the game.
- step(action): Take a move and see what happens!| Symbol / Term | Meaning | Beginner Interpretation |
|---|---|---|
| Transition probability distribution | How the world changes | |
| Reward function | Points awarded for transition |
A robot tries to move forward with slippery wheels: 80% chance it succeeds, 20% chance it slips and stays in place.
Result: Stochastic environment transition
obs, info = env.reset() initializes the world and returns initial sensory observation.
obs, reward, terminated, truncated, info = env.step(action) updates physics.
If terminated or truncated is True, episode ends and environment resets.
Problem: Training physical bipedal humanoid robots without breaking expensive hardware.
Solution: High-fidelity physics environments simulate gravity and joint friction at 10,000x real-time.
Interactive activity component
Run the standard Gymnasium interaction loop with random actions.
For Google Colab or Jupyter notebooks, run this shell command to install the required dependencies:
!# Standard Gymnasium pattern simulation1 import numpy as np
import numpy as np β Array processing
1 # A minimal Gymnasium-compliant Environment class: 2 class SimpleCartPole: 3 def __init__(self): 4 self.pole_angle = 0.0 5 def reset(self): 6 self.pole_angle = np.random.uniform(-0.05, 0.05) 7 return np.array([self.pole_angle]) 8 def step(self, action): 9 # Action: 0 = Push Left, 1 = Push Right 10 force = 0.1 if action == 1 else -0.1 11 self.pole_angle += force + np.random.normal(0, 0.02) 12 reward = 1.0 # +1 for every step balanced 13 terminated = abs(self.pole_angle) > 0.5 # Fell over! 14 return np.array([self.pole_angle]), reward, terminated 15 16 env = SimpleCartPole() 17 obs = env.reset() 18 total_reward = 0 19 20 for t in range(50): 21 action = 1 if obs[0] < 0 else 0 # Simple heuristic: push opposite to tilt 22 obs, reward, terminated = env.step(action) 23 total_reward += reward 24 if terminated: 25 print(f"Pole fell over at step {t+1}!") 26 break 27 print(f"Total Episode Reward: {total_reward}")
# A minimal Gymnasium-compliant Environment class:
Class structure
class SimpleCartPole:
Simulated environment
def __init__(self):
Constructor
self.pole_angle = 0.0
State: angle in degrees
def reset(self):
Reset method
self.pole_angle = np.random.uniform(-0.05, 0.05)
Slight random start
return np.array([self.pole_angle])
Return observation
def step(self, action):
Step method
# Action: 0 = Push Left, 1 = Push Right
Action space
force = 0.1 if action == 1 else -0.1
Apply force
self.pole_angle += force + np.random.normal(0, 0.02)
Physics update
reward = 1.0 # +1 for every step balanced
Reward
terminated = abs(self.pole_angle) > 0.5 # Fell over!
Terminal condition
return np.array([self.pole_angle]), reward, terminated
Return tuple
env = SimpleCartPole()
Instantiate env
obs = env.reset()
Reset world
total_reward = 0
Accumulator
for t in range(50):
Run up to 50 steps
action = 1 if obs[0] < 0 else 0 # Simple heuristic: push opposite to tilt
Reflex policy
obs, reward, terminated = env.step(action)
Step environment
total_reward += reward
Add reward
if terminated:
Check game over
print(f"Pole fell over at step {t+1}!")
Terminated message
break
Exit loop
print(f"Total Episode Reward: {total_reward}")
Display total points
Click "Copy for Colab", open Google Colab, paste into a single cell, and run.
1 # ========================================================= 2 # Interacting with a Gymnasium Environment 3 # Prepared for Google Colab (Run with Shift + Enter) 4 # ========================================================= 5 6 # Step 1: Install required packages 7 !# Standard Gymnasium pattern simulation 8 9 # Step 2: Import libraries 10 import numpy as np 11 12 # Step 3: Run Demonstration 13 # A minimal Gymnasium-compliant Environment class: 14 class SimpleCartPole: 15 def __init__(self): 16 self.pole_angle = 0.0 17 def reset(self): 18 self.pole_angle = np.random.uniform(-0.05, 0.05) 19 return np.array([self.pole_angle]) 20 def step(self, action): 21 # Action: 0 = Push Left, 1 = Push Right 22 force = 0.1 if action == 1 else -0.1 23 self.pole_angle += force + np.random.normal(0, 0.02) 24 reward = 1.0 # +1 for every step balanced 25 terminated = abs(self.pole_angle) > 0.5 # Fell over! 26 return np.array([self.pole_angle]), reward, terminated 27 28 env = SimpleCartPole() 29 obs = env.reset() 30 total_reward = 0 31 32 for t in range(50): 33 action = 1 if obs[0] < 0 else 0 # Simple heuristic: push opposite to tilt 34 obs, reward, terminated = env.step(action) 35 total_reward += reward 36 if terminated: 37 print(f"Pole fell over at step {t+1}!") 38 break 39 print(f"Total Episode Reward: {total_reward}")
Initializes pole with slight tilt angle.
Agent observes angle, pushes cart, and earns +1 reward per balanced timestep.
Simulated CartPole physics environment
Total Episode Reward: 50.0 (Survived full episode!)
The standard env.reset() and env.step() contract enables testing arbitrary algorithms across standardized environments.
Q-learning, Q-table updates, DQN architectures, and exploration-exploitation trade-offs.
Test your understanding with these questions. Remember, this is for learning, not examination!