Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Heuristic Sampling Improvements #287

Merged
merged 9 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/scenic/domains/driving/roads.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def _toVector(thing: Vectorlike) -> Vector:


def _rejectSample(message):
if veneer.isActive():
if not veneer.allowSampleRejection():
Eric-Vin marked this conversation as resolved.
Show resolved Hide resolved
raise InvalidScenarioError(message)
else:
raise RejectionException(message)
Expand Down
23 changes: 22 additions & 1 deletion src/scenic/syntax/veneer.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@
### Internals

activity = 0
heuristicSampling = 0
currentScenario = None
scenarioStack = []
scenarios = []
Expand Down Expand Up @@ -331,6 +332,25 @@ def isActive():
return activity > 0


def allowSampleRejection():
"""Should a RejectionException() be allowed

Returning True means a RejectionException is valid. Returning False indicates
a RejectionException() means the scenario is invalid.
"""
return isActive() or heuristicSampling
Eric-Vin marked this conversation as resolved.
Show resolved Hide resolved


@contextmanager
def enableHeuristicSampling():
global heuristicSampling
heuristicSampling += 1
try:
yield
finally:
heuristicSampling -= 1


def activate(options, namespace=None):
"""Activate the veneer when beginning to compile a Scenic module."""
global activity, _globalParameters, lockedParameters, lockedModel, currentScenario
Expand Down Expand Up @@ -1517,7 +1537,8 @@ def alwaysProvidesOrientation(region):
return True
else: # TODO improve somehow!
try:
sample = region.sample()
with enableHeuristicSampling():
sample = region.sample()
return sample.orientation is not None or sample is nowhere
except RejectionException:
return False
Expand Down
Loading