Ejemplo n.º 1
0
def spaceForScenario(scenario, ignoredProperties):
    """Construct a FeatureSpace for the given Scenic Scenario."""
    # create domains for objects
    assert scenario.egoObject is scenario.objects[0]
    doms = (domainForObject(obj, ignoredProperties)
            for obj in scenario.objects)
    objects = Struct({f'object{i}': dom for i, dom in enumerate(doms)})

    # create domains for global parameters
    paramDoms = {}
    quotedParams = {}
    for param, value in scenario.params.items():
        if param in ignoredParameters:
            continue
        dom = domainForValue(value)
        if dom is None:
            ty = underlyingType(value)
            print(f'WARNING: skipping param "{param}" of unknown type {ty}')
        else:
            if not param.isidentifier():  # munge quoted parameter names
                newparam = 'quoted_param' + str(len(quotedParams))
                quotedParams[newparam] = param
                param = newparam
            paramDoms[param] = dom
    params = Struct(paramDoms)

    space = FeatureSpace({
        'objects': Feature(objects),
        'params': Feature(params)
    })
    return space, quotedParams
Ejemplo n.º 2
0
def test_random_restore(tmpdir):
    space = FeatureSpace({
        'a':
        Feature(DiscreteBox([0, 12])),
        'b':
        Feature(Box((0, 1)), lengthDomain=DiscreteBox((0, 2)))
    })
    sampler = FeatureSampler.randomSamplerFor(space)

    path = os.path.join(tmpdir, 'blah.dat')
    sampler.saveToFile(path)
    sample1 = sampler.nextSample()
    sampler = FeatureSampler.restoreFromFile(path)
    sample2 = sampler.nextSample()
    assert sample1 == sample2
Ejemplo n.º 3
0
def spaceForScenario(scenario, ignoredProperties):
    """Construct a FeatureSpace for the given Scenic Scenario"""
    # create domains for objects
    assert scenario.egoObject is scenario.objects[0]
    doms = (domainForObject(obj, ignoredProperties)
            for obj in scenario.objects)
    objects = Struct({f'object{i}': dom for i, dom in enumerate(doms)})

    # create domains for global parameters
    paramDoms = {}
    for param, value in scenario.params.items():
        dom = domainForValue(value)
        if dom is None:
            ty = underlyingType(value)
            print(f'WARNING: skipping param "{param}" of unknown type {ty}')
        else:
            paramDoms[param] = dom
    params = Struct(paramDoms)

    return FeatureSpace({
        'objects': Feature(objects),
        'params': Feature(params)
    })
Ejemplo n.º 4
0
def test_space_random():
    space = FeatureSpace({
        'a':
        Feature(DiscreteBox([0, 12])),
        'b':
        Feature(Box((0, 1)), lengthDomain=DiscreteBox((0, 2)))
    })
    sampler = FeatureSampler.randomSamplerFor(space)

    def check(samples):
        for sample in samples:
            assert type(sample) is space.makePoint
            a = sample.a
            assert type(a) is tuple
            assert len(a) == 1
            assert type(a[0]) is int
            assert 0 <= a[0] <= 12
            b = sample.b
            assert type(b) is tuple
            l = len(b)
            assert 0 <= l <= 2
            if l > 0:
                assert type(b[0]) is tuple
                assert len(b[0]) == 1
                assert 0 <= b[0][0] <= 1
            if l > 1:
                assert type(b[1]) is tuple
                assert len(b[1]) == 1
                assert 0 <= b[1][0] <= 1
        assert any(sample.a[0] > 6 for sample in samples)
        assert any(sample.a[0] < 6 for sample in samples)
        assert any(len(sample.b) == 0 for sample in samples)
        assert any(len(sample.b) == 1 for sample in samples)
        assert any(len(sample.b) == 2 for sample in samples)

    check([sampler.nextSample() for i in range(100)])
    check(list(itertools.islice(sampler, 100)))