Exemplo n.º 1
0
def test_SearchSpace_sub():
    for _ in range(3):
        cs = RealSpace([0, 5], "x") + IntegerSpace(
            [-10, 10], "y") + DiscreteSpace(["A", "B", "C"], "z")
        _cs = DiscreteSpace(["A", "B", "C"], "z") + IntegerSpace([10, 20], "p")

        cs2 = cs - cs[1:]
        assert cs2.var_name[0] == "x"
        assert cs.dim == 3

        assert set((cs - _cs).var_name) == set(["x", "y"])
        cs -= _cs
        assert set(cs.var_name) == set(["x", "y"])

        cs -= cs[1:]
        assert cs.var_name[0] == "x"
Exemplo n.º 2
0
def test_update():
    cs = RealSpace([0, 5], "x") * 3
    cs2 = RealSpace([-100, 100], "x1") + IntegerSpace([0, 10], "y")
    cs.update(cs2)
    assert "y" in cs.var_name
    assert cs[[1]].data[0].bounds[0] == -100
    assert cs[[1]].data[0].bounds[1] == 100
Exemplo n.º 3
0
def test_fixed_var():
    search_space = (BoolSpace(var_name="bool") +
                    IntegerSpace([5, 15], var_name="ordinal") +
                    RealSpace([-5, 5], var_name="continuous", precision=2) +
                    DiscreteSpace(["OK", "A", "B", "C", "D", "E", "F", "G"],
                                  var_name="nominal"))
    opt = MOBO(
        search_space=search_space,
        obj_fun=(f1, f2),
        model=RandomForest(levels=search_space.levels),
        max_FEs=100,
        DoE_size=1,  # the initial DoE size
        eval_type="dict",
        n_job=1,  # number of processes
        verbose=True,  # turn this off, if you prefer no output
    )
    X = opt.ask(3, fixed={"ordinal": 5, "continuous": 3.2})
    assert all([x["ordinal"] == 5 and x["continuous"] == 3.2 for x in X])
    opt.tell(X, [(f1(x), f2(x)) for x in X])
    X = opt.ask(1, fixed={"nominal": "OK", "bool": False})
    assert all([x["nominal"] == "OK" and not x["bool"] for x in X])
    opt.recommend()

    with pytest.raises(NotImplementedError):
        X = opt.ask(3)
Exemplo n.º 4
0
def test_filter():
    cs = (RealSpace([1e-10, 1e-1], "x", 0.01, scale="log") +
          IntegerSpace([-10, 10], "y") +
          DiscreteSpace(["A", "B", "C", "D", "E"], "z"))
    cs *= 2
    assert cs.filter(["x1"]).var_name == ["x1"]
    assert "x1" not in cs.filter(["x1"], invert=True).var_name
Exemplo n.º 5
0
def test_fixed_var():
    def obj_fun(x):
        return (x["continuous"]**2 + abs(x["ordinal"] - 10) / 123.0 +
                (x["nominal"] != "OK") * 2 + int(x["bool"]) * 3 +
                (x["subset"] == ["A"]) * 5)

    search_space = (BoolSpace(var_name="bool") +
                    IntegerSpace([5, 15], var_name="ordinal") +
                    RealSpace([-5, 5], var_name="continuous") +
                    DiscreteSpace(["OK", "A", "B", "C", "D", "E", "F", "G"],
                                  var_name="nominal") +
                    SubsetSpace(["A", "B", "C"], var_name="subset"))
    opt = ParallelBO(
        search_space=search_space,
        obj_fun=obj_fun,
        model=RandomForest(levels=search_space.levels),
        max_FEs=100,
        DoE_size=3,  # the initial DoE size
        eval_type="dict",
        acquisition_fun="MGFI",
        acquisition_par={"t": 2},
        n_job=1,  # number of processes
        n_point=3,  # number of the candidate solution proposed in each iteration
        verbose=True,  # turn this off, if you prefer no output
    )
    X = opt.ask(3, fixed={"ordinal": 5, "continuous": 3.2})
    assert all([x["ordinal"] == 5 and x["continuous"] == 3.2 for x in X])
    opt.tell(X, [obj_fun(x) for x in X])
    X = opt.ask(3, fixed={"nominal": "OK", "bool": False})
    assert all([x["nominal"] == "OK" and not x["bool"] for x in X])
Exemplo n.º 6
0
def test_iter():
    cs = (RealSpace([1e-10, 1e-1], "x", 1e-3, scale="log") +
          IntegerSpace([-10, 10], "y") +
          DiscreteSpace(["A", "B", "C", "D", "E"], "z"))
    cs *= 2

    for var in iter(cs):
        assert isinstance(var, Variable)
Exemplo n.º 7
0
def test_Variable():
    x = Real([1e-20, 5], "x", 2, scale="log10")
    bounds_transformed = getattr(x, "_bounds_transformed")
    assert bounds_transformed[0] == -20
    assert np.isclose(bounds_transformed[1], 0.6989700043360189)

    x = IntegerSpace([[5, 10]] * 3, ["x", "y", "z"])
    assert all(np.asarray(x.var_name) == np.asarray(["x", "y", "z"]))
    assert all(np.asarray(x.var_type) == np.asarray(["Integer"] * 3))
Exemplo n.º 8
0
def test_SearchSpace_mul():
    cs = RealSpace([0, 5], "x") + IntegerSpace([-10, 10], "y") + DiscreteSpace(
        ["A", "B", "C"], "z")
    __ = ["x0", "y0", "z0", "x1", "y1", "z1"]
    assert (cs * 2).dim == 6
    assert all(np.array((2 * cs).var_name) == np.asarray(__))

    cs *= 2
    assert cs.dim == 6
Exemplo n.º 9
0
def test_sample():
    cs = (RealSpace([1e-10, 1e-1], "x", 1e-3, scale="log") +
          IntegerSpace([-10, 10], "y") +
          DiscreteSpace(["A", "B", "C", "D", "E"], "z"))
    X = cs.sample(10)
    assert np.asarray(X).shape == (10, 3)
    cs.sample(5, method="LHS")
    X = cs.sample(5, method="uniform")
    cs.to_linear_scale(X)
Exemplo n.º 10
0
def test_SearchSpace_slice():
    cs = (RealSpace([1, 5], "x", 2, scale="log") +
          IntegerSpace([-10, 10], "y") + DiscreteSpace(["A", "B", "C"], "z"))
    assert isinstance(cs[[0]], RealSpace)
    assert isinstance(cs[[1]], IntegerSpace)
    assert isinstance(cs[[2]], DiscreteSpace)

    assert isinstance(cs[0], Real)
    assert isinstance(cs[1], Integer)
    assert isinstance(cs[2], Discrete)
    assert isinstance(cs["z"], Discrete)

    cs = (RealSpace([1, 5], "x", 2, scale="log") * 2 +
          IntegerSpace([-10, 10], "y") + DiscreteSpace(["A", "B", "C"], "z"))
    assert isinstance(cs[:2], RealSpace)
    assert isinstance(cs[["x0", "x1"]], RealSpace)
    assert isinstance(cs[[False, False, True, False]], IntegerSpace)

    assert isinstance(cs.filter(["x0", "x1"]), RealSpace)
Exemplo n.º 11
0
def test_in():
    cs = (IntegerSpace([-10, 10], "y") +
          DiscreteSpace(["A", "B", "C", "D", "E"], "z") +
          RealSpace([1e-10, 1e-1], "x", 0.01, scale="log"))
    x = Solution(cs.sample(1)[0], var_name=cs.var_name)
    assert RealSpace([1e-10, 1e-1], "x", 0.01, scale="log") in cs
    assert "x" in cs
    assert "xx" not in cs
    assert x.tolist() in cs
    assert x.to_dict() in cs
Exemplo n.º 12
0
def test_SearchSpace_var_name():
    cs = (RealSpace([1e-10, 1e-1], "x", 1e-3, scale="log") +
          IntegerSpace([-10, 10], "y") +
          DiscreteSpace(["A", "B", "C", "D", "E"], "z"))
    cs.var_name = "x"
    assert all(np.asarray(cs.var_name) == np.asarray(["x0", "x1", "x2"]))

    cs.var_name = ["y0", "y1", "y2"]
    assert all(np.asarray(cs.var_name) == np.asarray(["y0", "y1", "y2"]))

    with pytest.raises(AssertionError):
        cs.var_name = ["y0", "y1"]
Exemplo n.º 13
0
def test_SearchSpace_remove():
    cs = (RealSpace([1e-10, 1e-1], "x", 1e-3, scale="log") +
          IntegerSpace([-10, 10], "y") +
          DiscreteSpace(["A", "B", "C", "D", "E"], "z"))

    cs.remove(0)
    assert isinstance(cs[[0]], IntegerSpace)

    cs.remove("z")
    assert isinstance(cs, IntegerSpace)

    with pytest.raises(KeyError):
        cs.remove("aaa")
Exemplo n.º 14
0
def test_SearchSpace_concat():
    cs_list = [RealSpace([0, 5], "x") for _ in range(3)]
    cs = SearchSpace.concat(*cs_list)
    assert all(np.array(cs.var_name) == np.array(["x0", "x1", "x2"]))
    assert isinstance(cs, RealSpace)

    cs_list = [
        RealSpace([0, 5], "x"),
        DiscreteSpace([1, 2, 3]),
        IntegerSpace([-5, 5])
    ]
    cs = SearchSpace.concat(*cs_list)
    assert isinstance(cs, SearchSpace)
Exemplo n.º 15
0
def test_homogenous(var_type):
    dim = 5

    def fitness(_):
        return np.random.rand()

    if var_type == "r":
        lb, ub = -1, 5
        space = RealSpace([lb, ub]) * dim
        mean = trend.constant_trend(dim, beta=None)
        thetaL = 1e-10 * (ub - lb) * np.ones(dim)
        thetaU = 10 * (ub - lb) * np.ones(dim)
        theta0 = np.random.rand(dim) * (thetaU - thetaL) + thetaL

        model = GaussianProcess(
            mean=mean,
            corr="squared_exponential",
            theta0=theta0,
            thetaL=thetaL,
            thetaU=thetaU,
            nugget=0,
            noise_estim=False,
            optimizer="BFGS",
            wait_iter=3,
            random_start=dim,
            likelihood="concentrated",
            eval_budget=100 * dim,
        )
    else:
        if var_type == "b":
            space = BoolSpace() * dim
        elif var_type == "i":
            space = IntegerSpace([0, 10], step=1) * dim
        elif var_type == "c":
            space = DiscreteSpace(list(range(10))) * dim
        elif var_type == "o":
            space = OrdinalSpace(list(string.ascii_lowercase))
        elif var_type == "s":
            space = SubsetSpace(list(string.ascii_lowercase)[:5])
        model = RandomForest(levels=space.levels)

    opt = BO(
        search_space=space,
        obj_fun=fitness,
        model=model,
        DoE_size=5,
        max_FEs=10,
        verbose=True,
        n_point=1,
    )
    print(opt.run())
Exemplo n.º 16
0
def test_mix_space(eval_type):
    dim_r = 2  # dimension of the real values
    if eval_type == "dict":

        def obj_fun(x):
            # Do explicit type-casting since dataframe rows might be strings otherwise
            x_r = np.array(
                [float(x["continuous%d" % i]) for i in range(dim_r)])
            x_i = int(x["ordinal"])
            x_d = x["nominal"]
            _ = 0 if x_d == "OK" else 1
            return np.sum(x_r**2) + abs(x_i - 10) / 123.0 + _ * 2

    elif eval_type == "list":

        def obj_fun(x):
            x_r = np.array([x[i] for i in range(dim_r)])
            x_i = x[dim_r]
            x_d = x[dim_r + 1]
            _ = 0 if x_d == "OK" else 1
            return np.sum(x_r**2) + abs(x_i - 10) / 123.0 + _ * 2

    else:
        raise NotImplementedError

    search_space = (RealSpace([-5, 5], var_name="continuous") * dim_r +
                    IntegerSpace([5, 15], var_name="ordinal") +
                    DiscreteSpace(["OK", "A", "B", "C", "D", "E", "F", "G"],
                                  var_name="nominal") +
                    SubsetSpace(["A", "B", "C"], var_name="subset"))
    model = RandomForest(levels=search_space.levels)
    opt = ParallelBO(
        search_space=search_space,
        obj_fun=obj_fun,
        model=model,
        max_FEs=6,
        DoE_size=3,  # the initial DoE size
        eval_type=eval_type,
        acquisition_fun="MGFI",
        acquisition_par={"t": 2},
        n_job=1,  # number of processes
        n_point=3,  # number of the candidate solution proposed in each iteration
        verbose=True,  # turn this off, if you prefer no output
    )
    xopt, fopt, stop_dict = opt.run()

    print("xopt: {}".format(xopt))
    print("fopt: {}".format(fopt))
    print("stop criteria: {}".format(stop_dict))
Exemplo n.º 17
0
def test_recommend():
    search_space = (RealSpace([10, 30], var_name="p1", precision=2) +
                    IntegerSpace([20, 40], var_name="p2") +
                    DiscreteSpace([128, 256, 512], var_name="p3") +
                    BoolSpace(var_name="p4"))
    opt = MOBO(
        search_space=search_space,
        obj_fun=(f1, f2),
        model=RandomForest(levels=search_space.levels),
        max_FEs=100,
        DoE_size=3,  # the initial DoE size
        eval_type="dict",
        n_job=1,  # number of processes
        verbose=True,  # turn this off, if you prefer no output
    )
    with pytest.raises(RecommendationUnavailableError):
        opt.recommend()
Exemplo n.º 18
0
def test_constraint():
    search_space = (BoolSpace(var_name="bool") +
                    IntegerSpace([5, 15], var_name="ordinal") +
                    RealSpace([-5, 5], var_name="continuous") +
                    DiscreteSpace(["OK", "A", "B", "C", "D", "E", "F", "G"],
                                  var_name="nominal"))
    opt = MOBO(
        search_space=search_space,
        obj_fun=(f1, f2),
        model=RandomForest(levels=search_space.levels),
        ineq_fun=lambda x: x["continuous"],
        max_FEs=10,
        DoE_size=3,  # the initial DoE size
        eval_type="dict",
        n_job=1,  # number of processes
        verbose=True,  # turn this off, if you prefer no output
    )
    opt.run()
Exemplo n.º 19
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.º 20
0
def test_contains():
    cs = (IntegerSpace([-10, 10], "y") +
          DiscreteSpace(["A", "B", "C", "D", "E"], "z") +
          RealSpace([1e-10, 1e-1], "x", 0.01, scale="log"))
    assert RealSpace([1e-10, 1e-1], "x", 0.01, scale="log") in cs
Exemplo n.º 21
0
def test_SearchSpace_iadd():
    cs = RealSpace([0, 5], "x")
    cs += RealSpace([5, 10])
    assert isinstance(cs, RealSpace)
    cs += IntegerSpace([-5, 5])
    assert isinstance(cs, SearchSpace)