def test_BO(dim, obj_fun, ftarget, max_FEs, lb, ub, logfile): sys.path.insert(0, '../') from bayes_optim import AnnealingBO, BO, ContinuousSpace from bayes_optim.Surrogate import GaussianProcess, trend space = ContinuousSpace(list(zip(lb, ub))) mean = trend.constant_trend(dim, beta=None) # equivalent to Ordinary Kriging 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='matern', theta0=theta0, thetaL=thetaL, thetaU=thetaU, noise_estim=False, nugget=1e-6, optimizer='BFGS', wait_iter=5, random_start=5 * dim, likelihood='concentrated', eval_budget=100 * dim) return BO(search_space=space, obj_fun=obj_fun, model=model, DoE_size=dim * 5, max_FEs=max_FEs, verbose=False, n_point=1, minimize=True, ftarget=ftarget, logger=logfile)
def test_mix_space(): dim_r = 2 # dimension of the real values def obj_fun(x): x_r = np.array([x['continuous_%d' % i] for i in range(dim_r)]) x_i = x['ordinal'] x_d = x['nominal'] _ = 0 if x_d == 'OK' else 1 return np.sum(x_r**2) + abs(x_i - 10) / 123. + _ * 2 search_space = ContinuousSpace([-5, 5], var_name='continuous') * dim_r + \ OrdinalSpace([5, 15], var_name='ordinal') + \ NominalSpace(['OK', 'A', 'B', 'C', 'D', 'E', 'F', 'G'], var_name='nominal') 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='dict', acquisition_fun='MGFI', acquisition_par={'t': 2}, n_job=3, # 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_sampling(): C = ContinuousSpace([-5, 5]) * 3 I = OrdinalSpace([[-100, 100], [-5, 5]], 'heihei') N = NominalSpace([['OK', 'A', 'B', 'C', 'D', 'E', 'A']] * 2, ['x', 'y']) S = N + I + C S.sampling(5, method='LHS') S.sampling(5, method='uniform')
def test_scale(): C = ContinuousSpace([1, 5], scale='log') assert C.bounds[0][0] == 0 C = ContinuousSpace([0.5, 0.8], scale='logit') assert C.bounds[0][0] == 0 C = ContinuousSpace([-1, 1], scale='bilog') assert C.bounds[0][0] == -np.log(2) assert C.bounds[0][1] == np.log(2) C = ContinuousSpace([-1, 1], scale='bilog') * 2 X = np.array([-np.log(2), np.log(2)]) a = C.to_linear_scale(X) assert all(a == np.array([-1, 1]))
def test_warm_data_with_RF(): space = ContinuousSpace([-10, 10]) * 2 + \ OrdinalSpace([5, 15]) + \ NominalSpace(['OK', 'A', 'B', 'C', 'D', 'E', 'F', 'G']) X = space.sampling(10) y = [obj_fun(x) for x in X] model = RandomForest(levels=space.levels) opt = BO(search_space=space, obj_fun=obj_fun, model=model, minimize=True, eval_type='list', max_FEs=10, verbose=True, acquisition_fun='EI', warm_data=(X, y)) opt.run() assert opt.data.shape[0] == 20
def test_ProductSpace(): C = ContinuousSpace([-5, 5], precision=1) * 3 # product of the same space I = OrdinalSpace([[-100, 100], [-5, 5]], 'heihei') N = NominalSpace([['OK', 'A', 'B', 'C', 'D', 'E', 'A']] * 2, ['x', 'y']) space = C + C + C print(space.sampling(2)) # cartesian product of heterogeneous spaces space = C + I + N print(space.sampling(10)) print(space.bounds) print(space.var_name) print(space.var_type) print((C * 2).var_name) print((N * 3).sampling(2)) C = ContinuousSpace([[0, 1]] * 2, var_name='weight') print(C.var_name)
def test_precision(): C = ContinuousSpace([-5, 5], precision=2) * 3 X = C.round(C.sampling(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 = C.round(C.sampling(1, method='uniform')) 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) * 10 - 5 assert isinstance(C.round(X), np.ndarray) X_ = C.round(X.tolist()) assert isinstance(X_, list) assert isinstance(X_[0], list) assert np.all(np.array(X_) == C.round(X))
def test_BO_constraints(): search_space = OrdinalSpace([1, 10], var_name='mu') + \ OrdinalSpace([1, 10], var_name='lambda') + \ ContinuousSpace([0, 1], var_name='pc') + \ ContinuousSpace([0.005, 0.5], var_name='p') model = RandomForest(levels=search_space.levels) xopt, _, __ = BO(search_space=search_space, obj_fun=obj_func, ineq_fun=g, model=model, max_FEs=30, DoE_size=3, eval_type='dict', acquisition_fun='MGFI', acquisition_par={ 't': 2 }, n_job=1, n_point=1, verbose=True).run() assert isinstance(xopt, dict) assert all(np.array(g(xopt)) <= 0)
def test_pickling2(): dim = 5 lb, ub = -1, 5 def fitness(x): x = np.asarray(x) return np.sum(x**2) space = ContinuousSpace([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, logger='log') opt.save('test') opt = BO.load('test') print(opt.run()) os.remove('test') os.remove('log')
def test_warm_data_with_GPR(): dim = 2 lb, ub = -5, 5 def fitness(x): x = np.asarray(x) return np.sum(x**2) X = np.random.rand(5, dim) * (ub - lb) + lb y = [fitness(x) for x in X] space = ContinuousSpace([lb, ub]) * dim 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(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, warm_data=(X, y), max_FEs=10, verbose=True, n_point=1) assert np.all(np.asarray(opt.data) == np.asarray(opt.warm_data)) assert opt.model.is_fitted opt.run()
sys.path.insert(0, '../') from bayes_optim import BO, ContinuousSpace from bayes_optim.Surrogate import GaussianProcess, trend np.random.seed(123) dim = 5 lb, ub = -1, 5 def fitness(x): x = np.asarray(x) return np.sum(x**2) space = ContinuousSpace([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(theta0=theta0, thetaL=thetaL, thetaU=thetaU, nugget=0, noise_estim=False, optimizer='BFGS', wait_iter=3, random_start=dim, likelihood='concentrated',
def test_MIES(): def obj_fun(X): v = [] for x in X: x_r = np.array([x['real_%d' % i] for i in range(2)]) x_i = np.array([x['int_%d' % i] for i in range(2)]) x_d = np.array([x['cat_%d' % i] for i in range(2)]) v.append( np.sum(x_r ** 2) + \ np.sum(np.abs(x_i - 10) / 123.) + \ np.sum(x_d == 'OK') ) return v search_space = ContinuousSpace([-5, 5], var_name='real') * 2 + \ OrdinalSpace([5, 15], var_name='int') * 2 + \ NominalSpace( ['OK', 'A', 'B', 'C', 'D', 'E', 'F', 'G'], var_name='cat' ) * 2 opt = MIES( search_space=search_space, obj_func=obj_fun, max_eval=100, eval_type='dict', verbose=True # turn this off, if you prefer no output ) xopt, fopt, stop_dict = opt.optimize() print('xopt: {}'.format(xopt)) print('fopt: {}'.format(fopt)) print('stop criteria: {}'.format(stop_dict)) # def test_multi_acquisition(): # dim_r = 2 # dimension of the real values # def obj_fun(x): # x_r = np.array([x['continuous_%d'%i] for i in range(dim_r)]) # x_i = x['ordinal'] # x_d = x['nominal'] # _ = 0 if x_d == 'OK' else 1 # return np.sum(x_r ** 2) + abs(x_i - 10) / 123. + _ * 2 # search_space = ContinuousSpace([-5, 5], var_name='continuous') * dim_r + \ # OrdinalSpace([5, 15], var_name='ordinal') + \ # NominalSpace(['OK', 'A', 'B', 'C', 'D', 'E', 'F', 'G'], var_name='nominal') # model = RandomForest(levels=search_space.levels) # opt = MultiAcquisitionBO( # search_space=search_space, # obj_fun=obj_fun, # model=model, # max_FEs=8, # DoE_size=4, # the initial DoE size # eval_type='dict', # n_job=4, # number of processes # n_point=4, # 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))
_ = 0 if x_d == 'OK' else 1 return np.sum(x_r**2) + abs(x_i - 10) / 123. + _ * 2 def obj_fun(x): x_r = np.array([x['continuous_%d' % i] for i in range(dim_r)]) x_i = x['ordinal'] x_d = x['nominal'] _ = 0 if x_d == 'OK' else 1 return np.sum(x_r**2) + abs(x_i - 10) / 123. + _ * 2 # Continuous variables can be specified as follows: # a 2-D variable in [-5, 5]^2 # for 2 variables, the naming scheme is continuous0, continuous1 C = ContinuousSpace([-5, 5], var_name='continuous') * dim_r # Equivalently, you can also use # C = ContinuousSpace([[-5, 5]]] * dim) # The general usage is: # ContinuousSpace([[lb_1, ub_1], [lb_2, ub_2], ..., [lb_n, ub_n]]) # Integer (ordinal) variables can be specified as follows: # The domain of integer variables can be given as with continuous ones # var_name is optional I = OrdinalSpace([5, 15], var_name='ordinal') # Discrete (nominal) variables can be specified as follows: # No lb, ub... a list of categories instead N = NominalSpace(['OK', 'A', 'B', 'C', 'D', 'E', 'F', 'G'], var_name='nominal')
from bayes_optim import ContinuousSpace, OrdinalSpace, NominalSpace from bayes_optim.Surrogate import RandomForest from bayes_optim.Extension import MultiAcquisitionBO dim_r = 2 # dimension of the real values def obj_fun(x): x_r = np.array([x['continuous_%d' % i] for i in range(dim_r)]) x_i = x['ordinal'] x_d = x['nominal'] _ = 0 if x_d == 'OK' else 1 return np.sum(x_r**2) + abs(x_i - 10) / 123. + _ * 2 search_space = ContinuousSpace([-5, 5], var_name='continuous') * dim_r + \ OrdinalSpace([5, 15], var_name='ordinal') + \ NominalSpace(['OK', 'A', 'B', 'C', 'D', 'E', 'F', 'G'], var_name='nominal') model = RandomForest(levels=search_space.levels) opt = MultiAcquisitionBO( search_space=search_space, obj_fun=obj_fun, model=model, max_FEs=40, DoE_size=4, # the initial DoE size eval_type='dict', n_job=4, # number of processes n_point=4, # number of the candidate solution proposed in each iteration verbose=True # turn this off, if you prefer no output )