Exemplo n.º 1
0
def test_constraints():
    cs = RealSpace([-5, 5], "x") * 2
    g = lambda x: x[0] + x[1] - 5
    X = cs.sample(10, g=g)
    assert all([g(x) <= 0 for x in X])

    X = cs.sample(10, g=lambda x: x[0] + 5.1)
    assert len(X) == 0
Exemplo n.º 2
0
def test_precision():
    cs = RealSpace([0, 1], precision=2) * 3
    X = cs.sample(1, method="LHS")
    X = [re.sub(r"^-?\d+\.(\d+)$", r"\1", str(_)) for _ in X[0]]
    assert all([len(x) <= 2 for x in X])

    X = cs.round(np.random.randn(3))
    X = [re.sub(r"^-?\d+\.(\d+)$", r"\1", str(_)) for _ in X[0]]
    assert all([len(x) <= 2 for x in X])

    X = np.random.rand(2, 3)
    assert isinstance(cs.round(X), np.ndarray)

    X = Solution(cs.sample(10, method="LHS"))
    cs.round(X)

    cs = (RealSpace([0, 1], "x", precision=2) + IntegerSpace([-10, 10], "y") +
          DiscreteSpace(["A", "B", "C", "D", "E"], "z"))

    X = cs.sample(1, method="LHS")[0][0]
    X = re.sub(r"^-?\d+\.(\d+)$", r"\1", str(X))
    assert len(X) <= 2
Exemplo n.º 3
0
def test_scale():
    cs = RealSpace([1e-10, 1e-1], "x", scale="log", random_seed=42)
    x = cs.sample(1)
    assert np.isclose(x, 2.3488813e-07)
    assert np.isclose(cs.to_linear_scale(-15.812834391811666), 1.35697948e-07)
    assert np.isclose(cs.to_linear_scale([-15.812834391811666]),
                      1.35697948e-07)
    assert np.isclose(cs.to_linear_scale((-15.812834391811666)),
                      1.35697948e-07)

    C = RealSpace([1, 5], scale="log")
    assert getattr(C.data[0], "_bounds_transformed")[0] == 0

    C = RealSpace([0.5, 0.8], scale="logit")
    assert getattr(C.data[0], "_bounds_transformed")[0] == 0

    C = RealSpace([-1, 1], scale="bilog")
    assert getattr(C.data[0], "_bounds_transformed")[0] == -np.log(2)
    assert getattr(C.data[0], "_bounds_transformed")[1] == np.log(2)

    C = RealSpace([-1, 1], scale="bilog") * 2
    x = C.to_linear_scale([-np.log(2), np.log(2)])
    assert np.all(x == np.array([-1, 1]))
Exemplo n.º 4
0
def test_sample_with_constraints():
    g = lambda x: x - 0.1
    cs = RealSpace([1e-10, 1e-1], "x", 0.01, scale="log")
    X = cs.sample(10, g=g)
    assert all(list(map(lambda x: g(x) <= 0, X)))