Train reinforcement learning policies on city-scale traffic using GPU simulation as the environment
Live Demo Output (from actual GPU run)
Python 3.10Gymnasium 1.3NumPy
>>> from lpsim_env import LPSimEnv
>>> env = LPSimEnv(
... network_path="data/networks/sf_bay_area",
... reward_type="travel_time",
... num_trips=1000
... )
Action space: Box(low=0, high=1, shape=(100,))
Observation space: Box(shape=(547697, 3)) — [speed, density, flow] per edge
Network: 547,697 edges, 100 controllable
>>> obs, info = env.reset(seed=42)
obs.shape = (547697, 3)
obs.dtype = float32
>>> action = env.action_space.sample()
action.shape = (100,)
action[:5] = [0.050, 0.327, 0.650, 0.845, 0.252]
>>> obs, reward, terminated, truncated, info = env.step(action)
reward = -26.02
terminated = True
info = {
'avg_travel_time': 26.02,
'total_co': 470.81,
'num_completed': 702
}
>>>
>>> from stable_baselines3 import PPO
>>> model = PPO('MlpPolicy', env, verbose=1)
>>> model.learn(total_timesteps=10000)
Reward Functions
env = LPSimEnv(reward_type="travel_time")
env = LPSimEnv(reward_type="emissions")
env = LPSimEnv(reward_type="throughput")
class MyEnv(LPSimEnv):
def _compute_reward(self, results):
return -results['avg_travel_time'] * 0.7 - results['total_co'] * 0.3
Action Space
env = LPSimEnv(
action_edges=[12345, 67890, 11111],
num_trips=5000
)
env = LPSimEnv(action_edges=list(range(env.num_edges)))