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)
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])
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
def test_OnePlusOne_Cholesky_CMA(): _, __, stop_dict = OnePlusOne_Cholesky_CMA( search_space=RealSpace([-5, 5]) * 2, obj_fun=obj_fun_min, sigma0=2, ftol=1e-2, verbose=False, random_seed=42, ).run() assert "ftol" in stop_dict
def test_with_constraints(): search_space = (RealSpace([0, 100], var_name="left", precision=2) + RealSpace([0, 100], var_name="up", precision=2) + RealSpace([0, 100], var_name="right", precision=2)) f1 = lambda x: x["left"]**2 + x["up"] + x["right"]**2 f2 = lambda x: 10 * x["left"] - x["up"]**2 + x["right"] g = lambda x: x["left"] + x["up"] + x["right"] - 100 dim = search_space.dim thetaL = 1e-10 * 100 * np.ones(dim) thetaU = 10 * 100 * np.ones(dim) theta0 = np.random.rand(dim) * (thetaU - thetaL) + thetaL model = GaussianProcess( theta0=theta0, thetaL=thetaL, thetaU=thetaU, nugget=0, noise_estim=False, likelihood="concentrated", ) opt = MOBO( search_space=search_space, obj_fun=(f1, f2), ineq_fun=g, model=model, 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 acquisition_optimization={"optimizer": "OnePlusOne_Cholesky_CMA"}, ) for _ in range(50): X = opt.ask(1) opt.tell(X, [(f1(x), f2(x)) for x in X]) assert g(X[0]) <= 0 opt.recommend() with pytest.raises(NotImplementedError): X = opt.ask(3)
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())
def test_OnePlusOne_Cholesky_CMA_constraint(): xopt, _, __, = OnePlusOne_Cholesky_CMA( search_space=RealSpace([-5, 5]) * 2, obj_fun=obj_fun_max, h=h, sigma0=2, max_FEs=500, minimize=False, verbose=False, random_seed=42, ).run() assert np.isclose(h(xopt), 0, atol=1e-1) OnePlusOne_Cholesky_CMA( search_space=RealSpace([-5, 5]) * 2, obj_fun=obj_fun_min, g=g, sigma0=2, max_FEs=1000, verbose=False, random_seed=42, ).run() assert np.isclose(g(xopt), 0, atol=1e-1)
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))
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()
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()
def test_infeasible_constraints(): dim = 5 lb, ub = -5, 5 def fitness(_): return 1 space = RealSpace([lb, ub]) * dim model = RandomForest(levels=space.levels) opt = BO( search_space=space, obj_fun=fitness, model=model, DoE_size=5, ineq_fun=lambda x: x[0] + 5.1, max_FEs=10, verbose=True, n_point=1, ) with pytest.raises(AskEmptyError): opt.run()
def test_flat_continuous(): dim = 5 lb, ub = -1, 5 def fitness(_): return 1 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, ) opt = BO( search_space=space, obj_fun=fitness, model=model, DoE_size=5, max_FEs=10, verbose=True, n_point=1, ) with pytest.raises(FlatFitnessError): opt.run()
import sys import numpy as np sys.path.insert(0, "./") from bayes_optim.acquisition import OnePlusOne_Cholesky_CMA from bayes_optim.search_space import RealSpace def obj_fun(x): return np.sum(x**2) opt = OnePlusOne_Cholesky_CMA( search_space=RealSpace([-5, 5]) * 30, obj_fun=obj_fun, lb=-100, ub=100, sigma0=40, ftarget=1e-8, verbose=True, ) opt.run() print(opt.stop_dict)