def test_list_object_lazy(): scenario = lazyTestScenario('Uniform([0, x], [1, x])[1]', offset='Uniform(0, 1)') hs = [sampleEgo(scenario).heading for i in range(60)] assert all(h == pytest.approx(1) or h == pytest.approx(2) for h in hs) assert any(h == pytest.approx(1) for h in hs) assert any(h == pytest.approx(2) for h in hs)
def test_vector_method_lazy(): scenario = lazyTestScenario( 'vf.followFrom(Uniform(0, 90 deg) @ 0, x, steps=1).y') hs = [sampleEgo(scenario).heading for i in range(60)] assert all(h == pytest.approx(1) or h == pytest.approx(-1) for h in hs) assert any(h == pytest.approx(1) for h in hs) assert any(h == pytest.approx(-1) for h in hs)
def test_shared_dependency(): scenario = compileScenic('x = Range(-1, 1)\n' 'ego = Object at (x * x) @ 0') xs = [sampleEgo(scenario).position.x for i in range(60)] assert all(0 <= x <= 1 for x in xs) assert any(x < 0.25 for x in xs) assert any(0.25 < x for x in xs)
def test_requirement(): scenario = compileScenic(""" ego = Object at Range(-10, 10) @ 0 require ego.position.x >= 0 """) xs = [sampleEgo(scenario, maxIterations=60).position.x for i in range(60)] assert all(0 <= x <= 10 for x in xs)
def test_uniform_discrete(): scenario = compileScenic('ego = Object at Uniform(1, 2, 3.4) @ 0') xs = [sampleEgo(scenario).position.x for i in range(100)] assert all(x == 1 or x == 2 or x == 3.4 for x in xs) assert any(x == 1 for x in xs) assert any(x == 2 for x in xs) assert any(x == 3.4 for x in xs)
def test_intersection_requirement_disabled_2(): scenario = compileScenic(""" ego = Object at Range(0, 2) @ 0 other = Object with allowCollisions True """) xs = [sampleEgo(scenario, maxIterations=60).position.x for i in range(60)] assert any(x < 1 for x in xs)
def test_containment_requirement(): scenario = compileScenic(""" foo = RectangularRegion(0@0, 0, 10, 10) ego = Object at Range(0, 10) @ 0, with regionContainedIn foo """) xs = [sampleEgo(scenario, maxIterations=60).position.x for i in range(60)] assert all(0 <= x <= 5 for x in xs)
def test_intersection_requirement(): scenario = compileScenic(""" ego = Object at Range(0, 2) @ 0 other = Object """) xs = [sampleEgo(scenario, maxIterations=60).position.x for i in range(60)] assert all(x >= 1 for x in xs)
def test_halton(): scenario = compileScenic('from scenic.core.external_params import *\n' 'param verifaiSamplerType = "halton"\n' 'ego = Object at VerifaiRange(5, 15) @ 0') xs = [sampleEgo(scenario).position.x for i in range(60)] assert all(5 <= x <= 15 for x in xs) assert 29 <= sum(x < 10 for x in xs) <= 31
def test_vector_operator(): scenario = compileScenic( 'ego = Object at Range(-3, 3) @ 0 + Range(100, 110) @ 0') xs = [sampleEgo(scenario).position.x for i in range(100)] assert all(97 <= x <= 113 for x in xs) assert any(x < 105 for x in xs) assert any(105 < x for x in xs)
def test_list_nested_argument(): scenario = compileScenic(""" mylist = Uniform(list(range(1000)), [1, 1, 1, 1, 2000]) ego = Object with foo max(*mylist) """) vs = [sampleEgo(scenario).foo for i in range(60)] assert 5 <= sum((v == 2000) for v in vs) <= 55
def test_soft_requirement(): scenario = compileScenic(""" ego = Object at Range(-10, 10) @ 0 require[0.9] ego.position.x >= 0 """) xs = [sampleEgo(scenario, maxIterations=60).position.x for i in range(350)] count = sum(x >= 0 for x in xs) assert 255 <= count < 350
def test_in_distribution(): scenario = compileScenic('ra = RectangularRegion(0@0, 0, 2, 2)\n' 'rb = RectangularRegion(10@0, 0, 2, 2)\n' 'ego = Object in Uniform(ra, rb)') xs = [sampleEgo(scenario).position.x for i in range(60)] assert all(-1 <= x <= 1 or 9 <= x <= 11 for x in xs) assert any(x < 5 for x in xs) assert any(x > 5 for x in xs)
def test_tuple(): scenario = compileScenic('ego = Object with foo tuple([3, Uniform(1, 2)])') ts = [sampleEgo(scenario).foo for i in range(60)] assert all(type(t) is tuple for t in ts) assert all(t[0] == 3 for t in ts) assert all(t[1] == 1 or t[1] == 2 for t in ts) assert any(t[1] == 1 for t in ts) assert any(t[1] == 2 for t in ts)
def test_attribute(): scenario = compileScenic('place = Uniform(1 @ 1, 2 @ 4, 3 @ 9)\n' 'ego = Object at place.x @ place.y') xs = [sampleEgo(scenario).position.x for i in range(100)] assert all(x == 1 or x == 2 or x == 3 for x in xs) assert any(x == 1 for x in xs) assert any(x == 2 for x in xs) assert any(x == 3 for x in xs)
def test_method(): scenario = compileScenic('field = VectorField("Foo", lambda pos: pos[1])\n' 'ang = field[0 @ (100, 200)]\n' 'ego = Object facing ang') angles = [sampleEgo(scenario).heading for i in range(60)] assert all(100 <= x <= 200 for x in angles) assert any(x < 150 for x in angles) assert any(150 < x for x in angles)
def test_function(): scenario = compileScenic( 'ego = Object at sin(Uniform(45 deg, 90 deg)) @ 0') xs = [sampleEgo(scenario).position.x for i in range(60)] valA, valB = (pytest.approx(math.sin(math.radians(a))) for a in (45, 90)) assert all(x == valA or x == valB for x in xs) assert any(x == valA for x in xs) assert any(x == valB for x in xs)
def test_inside_delayed_argument(): scenario = lazyTestScenario('Uniform(1.2, x)', offset='Uniform(-1, 1)') hs = [sampleEgo(scenario).heading for i in range(140)] assert all(h == 1.2 or h == pytest.approx(0) or h == pytest.approx(2) for h in hs) assert any(h == 1.2 for h in hs) assert any(h == pytest.approx(0) for h in hs) assert any(h == pytest.approx(2) for h in hs)
def test_requirement(): scenario = compileScenic(""" scenario Main(): setup: ego = Object with width Range(1, 3) require ego.width > 2 """) ws = [sampleEgo(scenario, maxIterations=60).width for i in range(60)] assert all(2 < w <= 3 for w in ws)
def test_containment(): """Test pruning based on object containment.""" scenario = compileScenic( 'workspace = Workspace(PolygonalRegion([0@0, 2@0, 2@2, 0@2]))\n' 'ego = Object in workspace') # Sampling should only require 1 iteration after pruning xs = [sampleEgo(scenario).position.x for i in range(60)] assert all(0.5 <= x <= 1.5 for x in xs) assert any(0.5 <= x <= 0.7 or 1.3 <= x <= 1.5 for x in xs)
def test_list_filtered(): scenario = compileScenic(""" mylist = [Range(-10, -5), Range(3, 7), Range(-1, 1)] filtered = filter(lambda x: x > 0, mylist) ego = Object with foo Uniform(*filtered) """) vs = [sampleEgo(scenario).foo for i in range(60)] assert all(v > 0 for v in vs) assert any(v < 1 for v in vs)
def test_mutate(): scenario = compileScenic(""" ego = Object at 3@1, facing 0 mutate """) ego1 = sampleEgo(scenario) assert ego1.position.x != pytest.approx(3) assert ego1.position.y != pytest.approx(1) assert ego1.heading != pytest.approx(0)
def test_namedtuple(): scenario = compileScenic( 'from collections import namedtuple\n' 'Data = namedtuple("Data", ["bar", "baz"])\n' 'ego = Object with foo Data(bar=3, baz=Uniform(1, 2))') ts = [sampleEgo(scenario).foo for i in range(60)] assert all(t.bar == 3 for t in ts) assert all(t.baz == 1 or t.baz == 2 for t in ts) assert any(t.baz == 1 for t in ts) assert any(t.baz == 2 for t in ts)
def test_in_heading_distribution(): scenario = compileScenic( 'ra = RectangularRegion(0@0, 0, 2, 2)\n' 'ra.orientation = VectorField("foo", lambda pt: 1)\n' 'rb = PolylineRegion([0 @ 0, 1 @ 1])\n' 'ego = Object in Uniform(ra, rb)') hs = [sampleEgo(scenario).heading for i in range(60)] h2 = pytest.approx(-math.pi / 4) assert all(h == 1 or h == h2 for h in hs) assert any(h == 1 for h in hs) assert any(h == h2 for h in hs)
def test_shared_dependency_lazy_1(): scenario = compileScenic(""" vf = VectorField("Foo", lambda pos: 2 * pos.x) x = 1 relative to vf y = Uniform(0, x) ego = Object with foo y, with bar y """) for i in range(60): ego = sampleEgo(scenario) assert ego.foo == 0 or ego.foo == 1 assert ego.foo == ego.bar
def test_method_lazy_2(): # See previous comment scenario = compileScenic( 'from scenic.core.distributions import distributionMethod\n' 'class Foo(object):\n' ' @distributionMethod\n' ' def bar(self, arg):\n' ' return -arg * Range(100, 200)\n' 'vf = VectorField("Baz", lambda pos: 1 + pos.x)\n' 'ego = Object facing Foo().bar(0 relative to vf)') angles = [sampleEgo(scenario).heading for i in range(60)] assert all(-200 <= x <= -100 for x in angles) assert any(x < -150 for x in angles) assert any(-150 < x for x in angles)
def test_relative_heading(): scenario = compileScenic( 'r1 = PolygonalRegion([0@0, 10@0, 10@10, 0@10])\n' # First cell: heading 0 deg 'r2 = PolygonalRegion([20@0, 30@0, 30@10, 20@10])\n' # Second cell: heading 90 deg 'vf = PolygonalVectorField("Foo", [[r1.polygons, 0], [r2.polygons, 90 deg]])\n' 'union = r1.union(r2)\n' 'ego = Object in union, facing vf\n' # Objects can be in either cell 'other = Object in union, facing vf\n' 'require (relative heading of other) >= 60 deg' # Forces ego in cell 1, other in cell 2 ) # Sampling should only require 1 iteration after pruning xs = [sampleEgo(scenario).position.x for i in range(60)] assert all(0 <= x <= 10 for x in xs) assert any(x > 5 for x in xs)
def test_intersection(cached_maps): scenario = compileDrivingScenario( cached_maps, """ intersection = Uniform(*network.intersections) lane = Uniform(*intersection.incomingLanes) maneuver = Uniform(*lane.maneuvers) ego = Car on maneuver.connectingLane.centerline """) for i in range(50): ego = sampleEgo(scenario, maxIterations=1000) intersection = ego.intersection assert intersection is not None directions = intersection.nominalDirectionsAt(ego) assert any(ego.heading == pytest.approx(direction) for direction in directions)
def test_method_lazy(): # There are no distributionMethods built into the core language at this time, # so we need to define our own scenario = compileScenic( 'from scenic.core.distributions import distributionMethod\n' 'class Foo:\n' ' @distributionMethod\n' ' def bar(self, arg):\n' ' return -arg\n' 'vf = VectorField("Baz", lambda pos: 1 + pos.x)\n' 'ego = Object facing Foo().bar((100, 200) * (0 relative to vf))') angles = [sampleEgo(scenario).heading for i in range(60)] assert all(-200 <= x <= 100 for x in angles) assert any(x < -150 for x in angles) assert any(-150 < x for x in angles)
def test_relative_heading(): """Test pruning based on requirements bounding relative headings.""" scenario = compileScenic(""" r1 = PolygonalRegion([0@0, 10@0, 10@10, 0@10]) # First cell: heading 0 deg r2 = PolygonalRegion([20@0, 30@0, 30@10, 20@10]) # Second cell: heading 90 deg vf = PolygonalVectorField("Foo", [[r1.polygons, 0], [r2.polygons, 90 deg]]) union = r1.union(r2) ego = Object in union, facing vf # Objects can be in either cell other = Object in union, facing vf require (relative heading of other) >= 60 deg # Forces ego in cell 1, other in cell 2 """) # Sampling should only require 1 iteration after pruning xs = [sampleEgo(scenario).position.x for i in range(60)] assert all(0 <= x <= 10 for x in xs) assert any(x > 5 for x in xs)