-
Notifications
You must be signed in to change notification settings - Fork 58
VerifAI Dynamic Sampling #58
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
base: main
Are you sure you want to change the base?
Changes from all commits
168568b
d1f4b46
f12ca28
c07dc0f
0787c93
0b0477f
2e266d9
9c5539b
62c97f2
8e03161
26e63d4
0d74dcb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -16,7 +16,7 @@ | |||||||||||||
|
|
||||||||||||||
| from verifai.features import (Constant, Categorical, Real, Box, Array, Struct, | ||||||||||||||
| Feature, FeatureSpace) | ||||||||||||||
| from verifai.samplers.feature_sampler import FeatureSampler | ||||||||||||||
| from verifai.samplers.feature_sampler import FeatureSampler, Sample | ||||||||||||||
| from verifai.utils.frozendict import frozendict | ||||||||||||||
|
|
||||||||||||||
| scenicMajorVersion = int(importlib.metadata.version('scenic').split('.')[0]) | ||||||||||||||
|
|
@@ -223,6 +223,22 @@ def spaceForScenario(scenario, ignoredProperties): | |||||||||||||
| }) | ||||||||||||||
| return space, quotedParams | ||||||||||||||
|
|
||||||||||||||
| class ScenicSample(Sample): | ||||||||||||||
| def __init__(self, space, staticSample, updateCallback, dynamicSampleLengths): | ||||||||||||||
| super().__init__(space, dynamicSampleLengths) | ||||||||||||||
| self._staticSample = staticSample | ||||||||||||||
| self._updateCallback = updateCallback | ||||||||||||||
|
|
||||||||||||||
| @property | ||||||||||||||
| def staticSample(self): | ||||||||||||||
| return self._staticSample | ||||||||||||||
|
|
||||||||||||||
| def _getDynamicSample(self, info): | ||||||||||||||
| raise RuntimeError("ScenicSampler does not support dynamic sampling.") | ||||||||||||||
|
|
||||||||||||||
| def update(self, rho): | ||||||||||||||
| self._updateCallback(rho) | ||||||||||||||
|
|
||||||||||||||
| class ScenicSampler(FeatureSampler): | ||||||||||||||
| """Samples from the induced distribution of a Scenic scenario. | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -236,14 +252,16 @@ class ScenicSampler(FeatureSampler): | |||||||||||||
| def __init__(self, scenario, maxIterations=None, ignoredProperties=None): | ||||||||||||||
| self.scenario = scenario | ||||||||||||||
| self.maxIterations = 2000 if maxIterations is None else maxIterations | ||||||||||||||
| self._nextScene = None | ||||||||||||||
| self.lastScene = None | ||||||||||||||
| self.lastFeedback = None | ||||||||||||||
| if ignoredProperties is None: | ||||||||||||||
| ignoredProperties = defaultIgnoredProperties | ||||||||||||||
| space, self.quotedParams = spaceForScenario(scenario, ignoredProperties) | ||||||||||||||
| super().__init__(space) | ||||||||||||||
|
|
||||||||||||||
| @classmethod | ||||||||||||||
| def fromScenario(cls, path, maxIterations=None, | ||||||||||||||
| def fromScenario(cls, path, maxIterations=None, maxSteps=None, | ||||||||||||||
| ignoredProperties=None, **kwargs): | ||||||||||||||
| """Create a sampler corresponding to a Scenic program. | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -262,25 +280,44 @@ def fromScenario(cls, path, maxIterations=None, | |||||||||||||
| e.g. ``params`` to override global parameters or ``model`` to set the | ||||||||||||||
| :term:`world model`. | ||||||||||||||
| """ | ||||||||||||||
| if "params" not in kwargs: | ||||||||||||||
| kwargs["params"] = {} | ||||||||||||||
|
|
||||||||||||||
| kwargs["params"]["timeBound"] = maxSteps if maxSteps else 0 | ||||||||||||||
|
Comment on lines
+283
to
+286
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Also, arguably if
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good suggestion. I added your fix and I'll leave the note open as a reminder. |
||||||||||||||
|
|
||||||||||||||
| scenario = scenic.scenarioFromFile(path, **kwargs) | ||||||||||||||
| return cls(scenario, maxIterations=maxIterations, | ||||||||||||||
| ignoredProperties=ignoredProperties) | ||||||||||||||
|
|
||||||||||||||
| @classmethod | ||||||||||||||
| def fromScenicCode(cls, code, maxIterations=None, | ||||||||||||||
| def fromScenicCode(cls, code, maxIterations=None, maxSteps=None, | ||||||||||||||
| ignoredProperties=None, **kwargs): | ||||||||||||||
| """As above, but given a Scenic program as a string.""" | ||||||||||||||
| if "params" not in kwargs: | ||||||||||||||
|
Eric-Vin marked this conversation as resolved.
|
||||||||||||||
| kwargs["params"] = {} | ||||||||||||||
|
|
||||||||||||||
| kwargs["params"]["timeBound"] = maxSteps if maxSteps else 0 | ||||||||||||||
|
|
||||||||||||||
| scenario = scenic.scenarioFromString(code, **kwargs) | ||||||||||||||
| return cls(scenario, maxIterations=maxIterations, | ||||||||||||||
| ignoredProperties=ignoredProperties) | ||||||||||||||
|
|
||||||||||||||
| def nextSample(self, feedback=None): | ||||||||||||||
| def getSample(self): | ||||||||||||||
| ret = self.scenario.generate( | ||||||||||||||
| maxIterations=self.maxIterations, feedback=feedback, verbosity=0 | ||||||||||||||
| maxIterations=self.maxIterations, feedback=self.lastFeedback, verbosity=0 | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| self.lastFeedback = None | ||||||||||||||
| self.lastScene, _ = ret | ||||||||||||||
|
|
||||||||||||||
| return self.pointForScene(self.lastScene) | ||||||||||||||
|
|
||||||||||||||
| def update(self, sample_id, rho): | ||||||||||||||
| assert sample_id == 0 | ||||||||||||||
| if self.lastFeedback is not None: | ||||||||||||||
| raise RuntimeError("Called `update` twice in a row (ScenicSampler does not support non-sequential sampling)") | ||||||||||||||
| self.lastFeedback = rho | ||||||||||||||
|
|
||||||||||||||
| def pointForScene(self, scene): | ||||||||||||||
| """Convert a sampled Scenic :obj:`~scenic.core.scenarios.Scene` to a point in our feature space. | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -314,7 +351,12 @@ def pointForScene(self, scene): | |||||||||||||
| params[param] = pointForValue(subdom, scene.params[originalName]) | ||||||||||||||
| paramPoint = paramDomain.makePoint(**params) | ||||||||||||||
|
|
||||||||||||||
| return self.space.makePoint(objects=objPoint, params=paramPoint) | ||||||||||||||
| staticSample = self.space.makeStaticPoint(objects=objPoint, params=paramPoint) | ||||||||||||||
|
|
||||||||||||||
| updateCallback = lambda rho: self.update(0, rho) | ||||||||||||||
| dynamicSampleLengths = [] | ||||||||||||||
|
|
||||||||||||||
| return ScenicSample(self.space, staticSample, updateCallback, dynamicSampleLengths) | ||||||||||||||
|
|
||||||||||||||
| @staticmethod | ||||||||||||||
| def nameForObject(i): | ||||||||||||||
|
|
||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| param map = localPath('Town01.xodr') | ||
| param carla_map = 'Town01' | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not needed, since this is running in Newtonian. |
||
|
|
||
| model scenic.domains.driving.model | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just specify Newtonian here and then you don't need to when compiling the scenario? |
||
|
|
||
| foo = TimeSeries(VerifaiRange(0,0.01)) | ||
|
|
||
| behavior TestBehavior(): | ||
| lastVal = None | ||
| while True: | ||
| newVal = foo.getSample() | ||
| assert newVal != lastVal, (newVal, lastVal) | ||
| lastVal = newVal | ||
| take SetThrottleAction(newVal) | ||
|
|
||
| ego = new Car on road, with behavior TestBehavior() | ||
| new Car behind ego by VerifaiRange(1,4) | ||
|
|
||
| terminate after 5 seconds | ||
Uh oh!
There was an error while loading. Please reload this page.