Skip to main content

Scenario Gym - a scenario-centric lightweight simulator

Tests Python 3.7+

Accepted to International Conference on Intelligent Transport Systems 2023

Scenario Gym will be presented at the special session Simulation Verification of Autonomous Driving Technologies at ITSC 2023.

Overview

Scenario Gym is a universal autonomous driving simulation tool that allows fast execution of unconfined, complex scenarios containing a range of road users. It allows rich insight via customised metrics and includes a framework for designing intelligent agents for reactive simulation. It can be used for a variety of tasks relevant for AV development, such agent modelling, controller parameter tuning and deep reinforcement learning.

       

Overview

Scenario Gym defines a flexible in-memory scenario representation that is compatible with the OpenSCENARIO description language and OpenDRIVE road network representation. Entities can adopt predefined trajectories, or control themselves intelligently with a high-level goal (e.g. reach a target position) or via a complex trained policy. Scenarios are simulated synchronously in discrete time steps within which each agent selects an action and the pose of each entity is updated before moving to the next step.

Intelligent agents interact with the environment through a simple sensor-agent-controller architecture. This streamlines the agent design by splitting it into three components that emulate the design of autonomous agent systems. The sensor component produces a logical observation for the agent from the current global state of the environment. The agent then selects an action and passes it to the controller. The controller manages the physical model of the agent e.g. converting steering and acceleration commands into a new pose. This modular architecture provides reusability and quick iteration of agent designs, not only for vehicular agents but also pedestrians, bicycles and other entity types.

Custom metrics can be implemented to allow quick and specific yet comprehensive insights. Through the scenario representation these can be constructed to efficiently track statistics such as speeds and distances, to record events such as collisions and near misses or to capture more compound measures such as safe distances and risk measures.

Installation

Install with pip:

pip install scenario_gym

To install extras for specific integrations or development requirements:

pip install scenario_gym[extra]

Getting started

To run a scenario in OpenSCENARIO format:

from scenario_gym import ScenarioGym

gym = ScenarioGym()
gym.load_scenario("path_to_xosc")
gym.rollout()

Or load a scenario directly into memory:

from scenario_gym.xosc_interface import import_scenario

scenario = import_scenario("path_to_xosc")
scenario.plot()

Several example scenarios are given in the tests/input_files/Scenarios directory.

Intelligent Agents

Agents are defined by a subclass of Agent as well as a Sensor and a Controller. They use implement the _step method to produce actions from the observations which will be passed to the controller.

from scenario_gym import Agent
from scenario_gym.sensor import RasterizedMapSensor
from scenario_gym.controller import VehicleController

class ExampleAgent(Agent):

    def __init__(self, entity):

        controller = VehicleController(entity)
        sensor = RasterizedMapSesnor(entity)
        super().__init__(
            entity,
            controller,
            sensor,
        )

    def _step(self, observation):
        action = ...
        return action

To run scenarios with intelligent agents we just define a create_agent method which will assign agents to each entity in the scenario. This is passed to the gym instance when loading a scenario. The function must take arguments scenario and entity and optionally return agents. If an agent is not returned for an entity then the entity will simply follow its predefined trajectory. For example, here we use the ExampleAgent implemented above for the ego only:

def create_agent(scenario, entity):
    if entity.ref == "ego":
        return ExampleAgent(entity)

gym.load_scenario("path_to_xosc", create_agent=create_agent)
gym.rollout()

Metrics

To track performance statistics or record events the Metric class can be used. These implement the _reset and _step method to maintin an internal state across the scenario and the get_state method to return their recorded data. A selection metrics are already implemented and can be run by passing them to the ScenarioGym:

from scenario_gym.metrics import CollisionMetric, EgoAvgSpeed

gym = ScenarioGym(metrics=[CollisionMetric(), EgoAvgSpeed()])
gym.load_scenario("path_to_xosc")
gym.rollout()

gym.get_metrics()

Deep reinforcement learning

For reinforcement learning applications Scenario Gym supports an OpenAI Gym compatible implementation. When creating the environment we need to specify the observation and action spaces used by the ego agent as well as our create_agent function. The observation from the ego agent's sensor will be returned by the environment and the action passed to step will be passed to the agent's controller.

from scenario_gym.integrations.openaigym import ScenarioGym

env = ScenarioGym(
    observation_space=observation_space,
    action_space=action_space,
    create_agent=create_agent,
)
obs = env.reset()
action = model(obs)
obs, reward, done, info = env.step(action)

For more code examples please see the examples directory.

Metadata

Release files for scenario-gym 0.4.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for scenario-gym 0.4.5
File Size Uploaded Provenance Actions
scenario_gym-0.4.5.tar.gz 97.6 kB Without provenance Details for scenario_gym-0.4.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for scenario-gym 0.4.5
File Interpreter ABI Platform Actions
scenario_gym-0.4.5-py3-none-any.whl Python 3 none any Details for scenario_gym-0.4.5-py3-none-any.whl

Total release size: 203.3 kB

Release files / scenario_gym-0.4.5.tar.gz

Download URL scenario_gym-0.4.5.tar.gz
Size 97.6 kB
Tags Source
SHA-256 checksum
How to use checksums
82ef3d15c2b60a6f78efbac56f5472b05d6ea39157e0eb13160260871377ea9f
BLAKE2b-256 checksum
How to use checksums
914c356577740c41bf33d8e8cd95e21ad0519952060b60073c91d9e6f7f44062
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release files / scenario_gym-0.4.5-py3-none-any.whl

Download URL scenario_gym-0.4.5-py3-none-any.whl
Size 105.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c95715bd34feb13dd06918270b2368902fc35cc6fe9d306948b92197db1cacf7
BLAKE2b-256 checksum
How to use checksums
aab0929ea64bb8f2087fe5470968f54644896f9337fbd8c4eeea974adbb1a555
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Security for scenario-gym 0.4.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page