In the quest for efficiency and effectiveness in urban transportation, finding the optimal routes to take passengers from their initial locations to their desired destinations is paramount. This challenge is not just about reducing travel time; it's about enhancing the overall experience for both drivers and passengers, ensuring safety, and minimizing environmental impact.
You have been asked to revolutionize the way taxis navigate the urban landscape, ensuring passengers reach their destinations swiftly, safely, and satisfactorily. As an initial step, your goal is to build a reinforcement learning agent that solves this problem within a simulated environment.
The Taxi-v3 environment
The Taxi-v3 environment is a strategic simulation, offering a grid-based arena where a taxi navigates to address daily challenges akin to those faced by a taxi driver. This environment is defined by a 5x5 grid where the taxi's mission involves picking up a passenger from one of four specific locations (marked as Red, Green, Yellow, and Blue) and dropping them off at another designated spot. The goal is to accomplish this with minimal time on the road to maximize rewards, emphasizing the need for route optimization and efficient decision-making for passenger pickup and dropoff.
Key Components:
- Action Space: Comprises six actions where 0 moves the taxi south, 1 north, 2 east, 3 west, 4 picks up a passenger, and 5 drops off a passenger.
- Observation Space: Comprises 500 discrete states, accounting for 25 taxi positions, 5 potential passenger locations, and 4 destinations.
- Rewards System: Includes a penalty of -1 for each step taken without other rewards, +20 for successful passenger delivery, and -10 for illegal pickup or dropoff actions. Actions resulting in no operation, like hitting a wall, also incur a time step penalty.
# Re-run this cell to install and import the necessary libraries and load the required variables
import numpy as np
import gymnasium as gym
import imageio
from IPython.display import Image
from gymnasium.utils import seeding
# Initialize the Taxi-v3 environment
env = gym.make("Taxi-v3", render_mode='rgb_array')
# Seed the environment for reproducibility
env.np_random, _ = seeding.np_random(42)
env.action_space.seed(42)
np.random.seed(42)
# Maximum number of actions per training episode
max_actions = 100
# Start coding here
# Feel free to add as many cells as you want
num_episodes = 2000
q_table = np.zeros((env.observation_space.n, env.action_space.n))
episode_returns = []
alpha = 0.1
gamma = 0.99
for episode in range(num_episodes):
state, info = env.reset()
terminated = False
total_reward = 0
while not terminated:
if np.random.rand() < 0.9:
action = env.action_space.sample()
else:
action = np.argmax(q_table[state])
next_state, reward, terminated, _, _ = env.step(action)
total_reward += reward
q_table[state, action] = (1 - alpha) * q_table[state, action] + alpha * (reward + gamma * max(q_table[next_state]))
state = next_state
episode_returns.append(total_reward)
policy = {s: np.argmax(q_table[s]) for s in range(env.observation_space.n)}
state, info = env.reset()
terminated = False
frames = []
episode_total_reward = 0
while not terminated:
action = policy[state]
next_state, reward, terminated, _, _ = env.step(action)
episode_total_reward += reward
frames.append(env.render())
state = next_state
# Once you are done, run this cell to visualize the agent's behavior through the episode
# Save frames as a GIF
imageio.mimsave('taxi_agent_behavior.gif', frames, fps=5, loop=0)
# Display GIF
gif_path = "taxi_agent_behavior.gif"
Image(gif_path)