VariaSimRun a scenario

Developer guide

Write and run scenario tests

Run an OpenSCENARIO® DSL scenario against a stand-in driver or connect your own simulator. Vary the starting conditions, search for a failure, and replay the run that triggered it.

The CLI examples use Python 3.10 or later and the early-access VariaSim package. If you have a platform wheel, follow the installation instructions.

Write a scenario

Describe what each actor does and which conditions can vary. In this double cut-in, two slower cars merge ahead of the vehicle under test at the same time. The right-hand car enters closer, so the driver must track both cars and brake for the nearer one.

Declare the actors and inputs

Import the reusable cut-in maneuver and declare one vehicle for each adjacent lane. Both cars travel slower than the vehicle under test (sut.car) by dv. Give each car its own time to collision (TTC) at the start of the merge.

OpenSCENARIO DSL
import osc.standard.all
import variasim.library.blocks.cut_in_ttc

scenario sut.edge_double_cut_in:
    near_cutter: vehicle
    far_cutter: vehicle
    v_sut: speed = 100kph
    dv: speed = 30kph
    ttc_near: time = 1.5s
    ttc_far: time = 3s
    lat_speed: speed = 1.5mps
    lat_neg: speed
    v_cut: speed
    gap_near: length
    gap_far: length

Constrain the inputs and measure coverage

Use keep to constrain the generated cases. Here the far car’s TTC must exceed the near car’s by at least one second. The starting gaps account for two seconds of closing speed before the cars begin to merge.

OpenSCENARIO DSL
    keep(v_sut in [80kph..130kph])
    keep(dv in [15kph..40kph])
    keep(ttc_near in [1s..2.5s])
    keep(ttc_far in [2.5s..4.5s])
    keep(ttc_far >= ttc_near + 1s)
    keep(lat_speed in [1mps..2.5mps])
    cover(ttc_near, every: 0.25s, unit: s)
    cover(ttc_far - ttc_near, every: 0.5s, unit: s)
    keep(near_cutter.vehicle_category == car)
    keep(far_cutter.vehicle_category == car)
    keep(v_cut == v_sut - dv)
    keep(lat_neg == -lat_speed)
    # both reach their TTC 2 s into the run
    keep(gap_near == (ttc_near + 2s) * dv + 4.6m)
    keep(gap_far == (ttc_far + 2s) * dv + 4.6m)

The first cover expression groups the near car’s TTC into quarter-second bins. The second groups the difference between the two TTCs into half-second bins. Inspect both distributions to see which timings your runs exercised.

Schedule the maneuvers together

Place the ego drive and both cut-ins under do parallel. Each car uses the same maneuver with its own side, starting gap and target TTC. All three actions share a six-second window.

OpenSCENARIO DSL
    do parallel(duration: 6s):
        sut.car.drive() with:
            speed(v_sut, at: start)
        near_cutter.cut_in_ttc(side: right, start_gap: gap_near, speed: v_cut, ttc: ttc_near, lat_speed: lat_speed)
        far_cutter.cut_in_ttc(side: left, start_gap: gap_far, speed: v_cut, ttc: ttc_far, lat_speed: lat_speed)

Save the complete double-cut-in scenario in scenarios/. Run it on the built-in highway, which has lanes on both sides of the ego vehicle.

Shell
variasim run scenarios/double_cut_in.osc \
  --map builtin:highway --seed 1 -o double-cut-in.json

Run and replay a scenario

Choose a map and a seed to reproduce the same starting conditions. For this example, save the braking scenario in a directory named scenarios and run the command from its parent. The CLI writes the run record to run.json.

Shell
variasim run scenarios/lead_vehicle_decelerating.osc \
  --map builtin:highway --seed 1 -o run.json

Generate an HTML report with the same inputs. Open report.html and move the timeline to 2 seconds, when the lead vehicle starts braking. Compare its speed with the following vehicle’s response.

Shell
variasim view scenarios/lead_vehicle_decelerating.osc \
  --map builtin:highway --seed 1 -o report.html

Find and shrink a failure

Use falsify to search a scenario’s parameter ranges, then simplify a failing case. Save the lane-change scenario in scenarios. Here the search budget is 40 runs.

Shell
variasim falsify scenarios/lane_change_same_direction.osc \
  --map builtin:highway --budget 40 --seed 1

In this example, the stand-in driver collided during a right lane change with a requested rear gap of 2.63 m. Inspect the coverage matrix to see which other gaps and directions the search exercised.

Run a scenario from Python

Compile a scenario, resolve its parameters on a map, and pass the instance to ScenarioEngine. Save the double cut-in scenario in scenarios to try this with two cars merging ahead of the driver. The example uses the built-in stand-in driver and a 10 ms time step.

Python
from variasim.concretize import concretize
from variasim.engine import ScenarioEngine, attach_events
from variasim.map import load_map
from variasim.semantic import compile_file
from variasim.sim import KinematicSim, StubDriver
program = compile_file("scenarios/double_cut_in.osc")["program"]
graph = load_map("builtin:highway")
instance = attach_events(concretize(program, graph, seed=1), program)
engine = ScenarioEngine(instance, graph, KinematicSim(StubDriver()), dt=0.01)
record = engine.run()

For an external driver, connect your simulator over TCP, a Unix socket or shared memory. Each tick, the engine sends NPC states and a step request. The simulator returns the states of its actors.

Embed a replay in React

Use bundle to package the map, actor tracks and checks for a replay. This command builds a bundle for the braking example.

Shell
variasim bundle scenarios/lead_vehicle_decelerating.osc \
  --map builtin:highway --seed 1 -o braking.bundle.json

Load braking.bundle.json in your app and pass the parsed object to Replay. The component uses the early-access React viewer. Its container needs an explicit height.

React · JSX
import { ScenarioViewer } from 'variasim-react';
import 'variasim-react/styles.css';
export function Replay({ bundle }) {
  return <div style={{ height: '100vh' }}>
    <ScenarioViewer bundle={bundle} theme="auto" />
  </div>;
}

Fit a scenario to a U.S. road

To test on a road of your choice, import a lane map and apply an operational design domain (ODD) profile. This interstate example uses a bounding box. Set SOUTH, WEST, NORTH and EAST to the bounds of your test area in decimal degrees. Fetch the motorway network from OpenStreetMap, then convert it to a lane map with SUMO’s netconvert installed.

Shell
variasim map fetch-osm --bbox "$SOUTH" "$WEST" "$NORTH" "$EAST" --roads motorway -o section.osm
variasim map import-osm section.osm --profile motorway -o road-section.map.json --report
variasim fit 'odd.highway.*' --map road-section.map.json --odd variasim.odd.us_interstate

Review the import report for inferred lane geometry and missing tags. The profile specifies a minimum lane width of 3.6 m and a maximum posted speed of 85 mph.

Test a stacked interchange in 2.5D

The importer uses OpenStreetMap layer, bridge and tunnel tags to assign road levels and infer deck heights. When a vehicle finishes a free path on an overpass, the engine uses its height to rejoin a lane on that deck. Collision checks account for vertical separation and lane levels, so crossing traffic on separate decks can pass above or below the vehicle.

Estimate the cost of a scenario sweep

Set the starting conditions and the number of seeds to run for each combination. Use the case preview to inspect the sweep before you run it.

Budget separately for compilation, map loading, model inference and external simulators.

Measure time to solve a case

Time the call to concretize() with your scenario compiled and your map loaded. Divide process CPU time by the number of cases and enter the result in milliseconds. The default of 1.99 ms comes from the two-car braking scenario on the built-in highway, using the Python solver on an Apple M5 Pro. It is the median of three sweep averages across five speeds, four gaps and 50 seeds per combination.

CPU time for one 100 Hz tick

Total actorsCPU time per tick
11.47 µs
21.87 µs
5044.3 µs
10097.4 µs
200200.1 µs

One actor is the ego car. Two actors adds one NPC. Each value is the median of five trial averages on a three-lane test road, using the release C++ engine and built-in simulator on an Apple M5 Pro. Each trial warms up for 100 ticks, then measures 10,000 ticks for one or two actors and 1,000 ticks for larger counts. A 100 Hz tick advances the scenario by 10 ms.

Highway benchmark

On a U.S. highway map at 25 vehicles per kilometre per lane, a 15-second scenario used 163 ms of engine CPU. This times engine.run() through the Python API, including conversion to the run record, after loading and setup. The figure is the best of seven measurements in a Linux ARM64 container.

Run with the native engine

Use Python 3.10 or newer and choose the early-access wheel for your operating system and processor.

  • Linux on x86-64 or ARM64. Compatible releases include Ubuntu 18.10+, Debian 10+, RHEL 8+ and Fedora 29+ (glibc 2.28 or newer).
  • macOS 11 or later on Apple silicon: macosx_11_0_arm64

Install the wheel in your Python environment, then pass --engine cpp to run the scenario with the C++ engine.

Shell
python -m pip install /path/to/wheel.whl
variasim run scenarios/lead_vehicle_decelerating.osc --map builtin:highway --seed 1 --engine cpp -o run.json

Use --engine python to run the same scenario with the Python engine.

Choose a CLI command

For a quick check after editing a scenario, compile the file before running it.

Shell
variasim check scenarios/double_cut_in.osc
check · ast · ir · scan
Compile source, inspect its structure or scan a scenario corpus.
map · fit · concretize
Inspect maps, find suitable lanes and solve the scenario constraints.
run · bundle · view
Simulate, package a recording or generate an offline report.
falsify
Search for a failure and shrink the counterexample.