Skip to content

Commit

Permalink
Fixed subtle testing bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Vin committed Jan 25, 2025
1 parent 95e05be commit 54f4045
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
3 changes: 2 additions & 1 deletion examples/contracts/dev.contract
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import random
from typing import Union
import numpy

import scenic
from scenic.core.geometry import normalizeAngle
from scenic.domains.driving.controllers import PIDLongitudinalController, PIDLateralController
from scenic.domains.driving.actions import RegulatedControlAction
Expand Down Expand Up @@ -396,7 +397,7 @@ cs_safety = compose over car.cs:
max_brake=MAX_BRAKE,
max_accel=MAX_ACCEL) using LeanContractProof(localPath("ScenicLean/"), "SafeThrottleFilter", localPath("repl"))

max_braking_force = assume car.cac satisfies MaxBrakingForce(MAX_BRAKE), with correctness 0.99, with confidence 0.99
max_braking_force = assume car.cac satisfies MaxBrakingForce(MAX_BRAKE), with correctness 0.9, with confidence 0.95

keeps_distance_raw = compose over car:
use accurate_distance
Expand Down
3 changes: 2 additions & 1 deletion examples/contracts/highway.scenic
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ class EgoCar(Car):
targetDir[dynamic, final]: float(roadDirection[self.position].yaw)

ego = new EgoCar at roadDirection.followFrom(toVector(leadCar), -STARTING_DISTANCE, stepSize=0.1),
with leadDist STARTING_DISTANCE, with behavior FollowLaneBehavior(), with name "EgoCar", with timestep 0.1
with leadDist STARTING_DISTANCE,
with behavior FollowLaneBehavior(), with name "EgoCar", with timestep 0.1

# Create/activate monitor to store lead distance
monitor UpdateDistance(tailCar, leadCar):
Expand Down
8 changes: 5 additions & 3 deletions src/scenic/contracts/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,17 @@ def testScene(self, scene):
# Instantiate simulator
simulator = self.scenario.getSimulator()

def vw_update_hook():
for vw_name, vw in base_value_windows.items():
vw.update()

# Step contract till termination
with simulator.simulateStepped(scene, maxSteps=self.maxSteps) as simulation:
while not simulation.result or eval_step < sim_step:
# If simulation not terminated, advance simulation one time step, catching any rejections
if not simulation.result:
try:
simulation.advance()
simulation.advance(vw_update_hook)
except (
RejectSimulationException,
RejectionException,
Expand All @@ -237,8 +241,6 @@ def testScene(self, scene):
continue

# If the simulation didn't finish, update all base value windows
for vw_name, vw in base_value_windows.items():
vw.update()

# Increment simulation step
sim_step += 1
Expand Down
4 changes: 3 additions & 1 deletion src/scenic/contracts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ def leadDistance(source, target, network, maxDistance=250):
# print(source.position, target.position, hash(network))

# Find all lanes this point could be a part of and recurse on them.
viable_lanes = [lane for lane in network.lanes if lane.containsPoint(source.position)]
viable_lanes = [
lane for lane in network.lanes if lane.containsPoint(toVector(source))
]

return min(
min(
Expand Down
6 changes: 5 additions & 1 deletion src/scenic/core/simulators.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ def _run(self):
if self.terminationType:
return

def advance(self):
def advance(self, preActionHook=None):
if self.terminationType or self._cleaned:
raise TerminatedSimulationException()

Expand Down Expand Up @@ -530,6 +530,10 @@ def advance(self):
# Log lastActions
agent.lastActions = actions

# Run the preActionHook if provided
if preActionHook is not None:
preActionHook()

# Execute the actions
if self.verbosity >= 3:
for agent, actions in allActions.items():
Expand Down

0 comments on commit 54f4045

Please sign in to comment.