def test_generate(self): d1 = ContinuousDomain(uniform, path='a', loc=-100.0, scale=100.0) d2 = DiscreteDomain([i for i in range(-100, 0)] + [i for i in range(1, 101)], path='b') # Test with one continuous domain m = self.__model_class__(domains=[d1]) for _ in range(1000): p = m()[-1] m.add_result(Result(m, loss=p['a']**2, values=Value(p['a'], d1))) assert 'a' in p assert p['a'] >= -100.0 and p['a'] < 100 # Test with one continuous domain m = self.__model_class__(domains=[d2]) for _ in range(1000): p = m()[-1] m.add_result(Result(m, loss=p['b']**2, values=Value(p['b'], d2))) assert 'b' in p assert p['b'] >= -100 and p['b'] <= 100 # Test with one continuous and one discrete domain m = self.__model_class__(domains=[d1, d2]) for _ in range(1000): p = m()[-1] m.add_result(Result(m, loss=p['a'] * p['b'], values=[Value(p['a'], d1), Value(p['b'], d2)])) assert 'a' in p # assert p['a'] >= -100 and p['a'] < 100 assert 'b' in p assert p['b'] >= -100 and p['b'] <= 100
def test_init(self): m1 = Model() m2 = Model() r1 = Result(m1) r2 = Result(m2) d1 = Domain() d2 = Domain() # Test initializing with various values, results, and domains values = [None, True, False, 1, 1.0, '1', (1, ), [1], {'1': 1}] for val in values: v = Value(val, d1, r1) assert v.value == val assert v.result() is r1 assert v.domain() is d1 v = Value(val, d2, r1) assert v.value == val assert v.result() is r1 assert v.domain() is d2 v = Value(val, d1, r2) assert v.value == val assert v.result() is r2 assert v.domain() is d1 v = Value(val, d2, r2) assert v.value == val assert v.result() is r2 assert v.domain() is d2 values = [True, False, 1, 1.0, (1, ), [1], {'1': 1}] # Test initializing with invalid domains for val in values: with pytest.raises(InvalidDomainError): Value(val, val, r1) with pytest.raises(InvalidDomainError): Value(val, val, r2) # Test initializing with invalid results for val in values[1:]: with pytest.raises(InvalidResultError): Value(val, d1, val) with pytest.raises(InvalidResultError): Value(val, d2, val)
def test_to_json(self): m1 = Model() m2 = Model() r1 = Result(m1) r2 = Result(m2) d1 = Domain() d2 = Domain() # Test initializing with various values, results, and domains values = [None, True, False, 1, 1.0, '1', (1, ), [1], {'1': 1}] for val in values: v = Value(val, d1, r1) assert v.to_json() == { 'value': val, 'domain': d1.id, 'result': r1.id } v = Value(val, d2, r1) assert v.to_json() == { 'value': val, 'domain': d2.id, 'result': r1.id } v = Value(val, d1, r2) assert v.to_json() == { 'value': val, 'domain': d1.id, 'result': r2.id } v = Value(val, d2, r2) assert v.to_json() == { 'value': val, 'domain': d2.id, 'result': r2.id }