Package: scenario-gym
Scenario Gym - a scenario-centric lightweight simulatorAccepted toInternational Conference on Intelligent Transport Systems 2023Scenario Gym will be presented at the special session Simulation Verification of Autonomous Driving Technologies at ITSC 2023.OverviewScenario 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.OverviewScenario 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.InstallationInstall withpip:pip install scenario_gymTo install extras for specific integrations or development requirements:pip install scenario_gym[extra]Getting startedTo run a scenario in OpenSCENARIO format:fromscenario_gymimportScenarioGymgym=ScenarioGym()gym.load_scenario("path_to_xosc")gym.rollout()Or load a scenario directly into memory:fromscenario_gym.xosc_interfaceimportimport_scenarioscenario=import_scenario("path_to_xosc")scenario.plot()Several example scenarios are given in thetests/input_files/Scenariosdirectory.Intelligent AgentsAgents are defined by a subclass ofAgentas well as aSensorand aController. They use implement the_stepmethod to produce actions from the observations which will be passed to the controller.fromscenario_gymimportAgentfromscenario_gym.sensorimportRasterizedMapSensorfromscenario_gym.controllerimportVehicleControllerclassExampleAgent(Agent):def__init__(self,entity):controller=VehicleController(entity)sensor=RasterizedMapSesnor(entity)super().__init__(entity,controller,sensor,)def_step(self,observation):action=...returnactionTo run scenarios with intelligent agents we just define acreate_agentmethod 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 argumentsscenarioandentityand 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 theExampleAgentimplemented above for the ego only:defcreate_agent(scenario,entity):ifentity.ref=="ego":returnExampleAgent(entity)gym.load_scenario("path_to_xosc",create_agent=create_agent)gym.rollout()MetricsTo track performance statistics or record events theMetricclass can be used. These implement the_resetand_stepmethod to maintin an internal state across the scenario and theget_statemethod to return their recorded data. A selection metrics are already implemented and can be run by passing them to theScenarioGym:fromscenario_gym.metricsimportCollisionMetric,EgoAvgSpeedgym=ScenarioGym(metrics=[CollisionMetric(),EgoAvgSpeed()])gym.load_scenario("path_to_xosc")gym.rollout()gym.get_metrics()Deep reinforcement learningFor 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 ourcreate_agentfunction. The observation from the ego agent's sensor will be returned by the environment and the action passed tostepwill be passed to the agent's controller.fromscenario_gym.integrations.openaigymimportScenarioGymenv=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 theexamplesdirectory.