def apply(self, problem, iterations): def fWrapper(x): return -problem.evaluate(x) X = problem.getDomain() x0 = np.mean(X, axis=1) #center point as initial starting point fmin_smac( func=fWrapper, # function x0=x0, # default configuration bounds=problem.getDomain(), # limits maxfun=iterations, # maximum number of evaluations rng=3) return problem
def run_smac(max_fun=30): from smac.facade.func_facade import fmin_smac x, cost, smac = fmin_smac(func=test_func, x0=[-0], # default values bounds=[(-5, 5)], # bounds of each x maxfun=max_fun, # maximal number of function evaluations rng=1234 # random seed ) runhistory = smac.get_runhistory() # extract x value and corresponding y value x_smac = [] y_smac = [] for entry in runhistory.data: # iterate over data because it is an OrderedDict config_id = entry.config_id # look up config id config = runhistory.ids_config[config_id] # look up config y_ = runhistory.get_cost(config) # get cost x_ = config["x1"] # there is only one entry in our example x_smac.append(x_) y_smac.append(y_) x_smac = np.array(x_smac) y_smac = np.array(y_smac) return smac, x_smac, y_smac
def run_smac(python_path, w_dir, n_iter=5, input_file='../rawAllx1000.json', seeds=[1], task_ids=None, max_tries=10): def test_func(cutoff): result = find_cut_off.main(python_path=python_path, w_dir=w_dir, iter=n_iter, input_file=input_file, cutoffs=[cutoff], seeds=seeds, task_ids=task_ids) cleaned = [x[1] for x in result if 0.0 < x[1] < 1.0] mean = np.mean(cleaned) if cleaned else 0.0 mean = mean if mean != 1.0 else 0.0 return 1.0 - mean x, cost, smac = fmin_smac( func=test_func, x0=[5], # default values bounds=[(1, 99)], # bounds of each x maxfun=max_tries, # maximal iterations rng=1234 # random seed ) return x, cost, smac
def _optimize_by_smac_pkg(self, func_caller, max_capital, options): """ Optimizes the function using smac package """ try: from smac.facade.func_facade import fmin_smac except ImportError: raise ImportError('smac package is not installed') domain_bounds = func_caller.domain.bounds bounds = [(float(b[0]), float(b[1])) for b in domain_bounds] x0 = [(b[0] + b[1]) / 2 for b in domain_bounds] func_to_min = _get_func_to_min_from_func_caller(func_caller) history = Namespace() if options.capital_type == 'realtime': x, _, trace = fmin_smac( func=func_to_min, # function x0=x0, bounds=bounds, maxfun=10000, maxtime=max_capital) # maximum number of evaluations else: x, _, trace = fmin_smac( func=func_to_min, # function x0=x0, bounds=bounds, maxfun=max_capital) # maximum number of evaluations runhistory = trace.get_runhistory() data = [([ int(k.config_id), str(k.instance_id) if k.instance_id is not None else None, int(k.seed) ], list(v)) for k, v in runhistory.data.items()] config_ids_to_serialize = set([entry[0][0] for entry in data]) configs = { id_: conf.get_dictionary() for id_, conf in runhistory.ids_config.items() if id_ in config_ids_to_serialize } # all queried points total_num_queries = len(configs) history.query_step_idxs = [i for i in range(total_num_queries)] history.query_points = [ list(configs[i].values()) for i in range(1, total_num_queries + 1) ] history.query_send_times = [0] * total_num_queries history.query_receive_times = list(range(1, total_num_queries + 1)) history.query_vals = [-vlist[0] for (_, vlist) in data] history = common_final_operations_for_all_external_packages( history, self.func_caller, options) return history
def test_parameter_order(self): def func(x): for i in range(len(x)): self.assertLessEqual(i - 1, x[i]) self.assertGreaterEqual(i, x[i]) return 1 default = [i - 0.5 for i in range(10)] bounds = [(i - 1, i) for i in range(10)] print(default, bounds) _, _, smac = fmin_smac(func=func, x0=default, bounds=bounds, maxfun=1) self.output_dirs.append(smac.scenario.output_dir)
def test_func_smac(self): func = rosenbrock_2d x0 = [-3, -4] bounds = [(-5, 5), (-5, 5)] x, f, smac = fmin_smac(func, x0, bounds, maxfun=10) x_s, f_s, _ = fmin_l_bfgs_b(func, x0, bounds, maxfun=10, approx_grad=True) self.assertEqual(type(x), type(x_s)) self.assertEqual(type(f), type(f_s)) self.output_dirs.append(smac.scenario.output_dir)
def main(func_name): func = eval(func_name) func_name = func_name[:-4] smac_x, cost, smac = fmin_smac(func=func, x0=default_values[func_name], # default values bounds=bounds_values[func_name], # bounds of each x maxfun=20, # maximal number of function evaluations rng=1234 # random seed ) runhistory = smac.get_runhistory() # extract x value and corresponding y value x_smac = [] y_smac = [] z_smac = [] for entry in runhistory.data: # iterate over data because it is an OrderedDict config_id = entry.config_id # look up config id config = runhistory.ids_config[config_id] # look up config x_ = config["x1"] y_ = config["x2"] z_ = runhistory.get_cost(config) # get cost x_smac.append(x_) y_smac.append(y_) z_smac.append(z_) x_smac = np.array(x_smac) y_smac = np.array(y_smac) z_smac = np.array(z_smac) # 3d plot fig = plt.figure() ax = fig.gca(projection='3d') x = np.linspace(*bounds_values[func_name][0], 100) y = np.linspace(*bounds_values[func_name][1], 100) x, y = np.meshgrid(x, y) zs = np.array([func(a, b) for a, b in zip(np.ravel(x), np.ravel(y))]) z = zs.reshape(x.shape) ax.plot_surface(x, y, z, cmap=cm.coolwarm, linewidth=0) ax.scatter(x_smac, y_smac, z_smac, color='red') plt.savefig('func-%s-surf.pdf' % func_name) ax = plt.subplot(111) ax.boxplot(z_smac) plt.savefig('func-%s-box.pdf' % func_name)
import logging from branin import branin from smac.facade.func_facade import fmin_smac """ Example for the use of fmin_smac, a basic SMAC-wrapper to optimize a function. We optimize the branin-function, which has two parameters: x1 and x2. The fmin_smac needs neither a scenario-file, nor a configuration space. All relevant information is directly passed to the function. """ if __name__ == '__main__': logging.basicConfig(level=20) # 10: debug; 20: info x, cost, _ = fmin_smac( func=branin, # function x0=[0, 0], # default configuration bounds=[(-5, 10), (0, 15)], # limits maxfun=10, # maximum number of evaluations rng=3) # random seed print("Optimum at {} with cost of {}".format(x, cost))
for alpha_min in range(-6, 7, 3): for alpha_max in range(alpha_min + 3, 7, 3): for beta_min in range(-6, 7, 3): for beta_max in range(beta_min + 3, 7, 3): costs = [] for i in range(10): # fmin_smac assumes that the function is deterministic # and uses under the hood the SMAC4HPO x, cost, _ = fmin_smac(func=rosenbrock_2d, model=ABLR, model_kwargs={ 'alpha_min': alpha_min, 'alpha_max': alpha_max, 'beta_min': beta_min, 'beta_max': beta_max, }, runhistory2epm=RunHistory2EPM4Cost, acquisition_function=EI, x0=[2.5, 7.5], bounds=[(-5, 10), (0, 15)], maxfun=50, initial_design=LHDesign, rng=i) # Passing a seed makes fmin_smac determistic print("Best x: %s; with cost: %f" % (str(x), cost)) costs.append(cost) costs = np.mean(costs) results[(alpha_min, alpha_max, beta_min, beta_max)] = costs print(alpha_min, alpha_max, beta_min, beta_max, costs) for key in results:
num_classes = 10 y_train = keras.utils.to_categorical(y_train, num_classes) y_test = keras.utils.to_categorical(y_test, num_classes) return x_train, y_train, x_test, y_test def run(params): print('Start time: ', datetime.datetime.now()) result = cnn(params, get_data()) print('Result: ', params, result) print('End time: ', datetime.datetime.now()) return result if __name__ == '__main__': select_gpu() logging.basicConfig(level=20) # 10: debug; 20: info x, cost, _ = fmin_smac( func=run, # function x0=[ 50, 50, 50, 50, 50, 50, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.01, 0.01, 0.01, 0.01 ], # default configuration bounds=[(10, 500), (10, 500), (10, 500), (10, 500), (10, 500), (10, 500), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0.0001, 0.1), (0.0001, 0.1), (0.0001, 0.1), (0.0001, 0.1)], # limits maxfun=500, # maximum number of evaluations rng=3) # random seed print("Optimum at {} with cost of {}".format(x, cost))
def rosenbrock_2d(x): """ The 2 dimensional Rosenbrock function as a toy model The Rosenbrock function is well know in the optimization community and often serves as a toy problem. It can be defined for arbitrary dimensions. The minimium is always at x_i = 1 with a function value of zero. All input parameters are continuous. The search domain for all x's is the interval [-5, 10]. """ x1 = x[0] x2 = x[1] val = 100. * (x2 - x1**2.)**2. + (1 - x1)**2. return val # debug output logging.basicConfig(level=20) logger = logging.getLogger("Optimizer") # Enable to show Debug outputs # fmin_smac assumes that the function is deterministic # and uses under the hood the SMAC4HPO x, cost, _ = fmin_smac(func=rosenbrock_2d, x0=[-3, -4], bounds=[(-5, 10), (-5, 10)], maxfun=10, rng=3) # Passing a seed makes fmin_smac determistic print("Best x: %s; with cost: %f" % (str(x), cost))
from ObjectiveFunction import DistributedFunction from Components import Dimension from Tools import RandomOperator import numpy as np dimension_size = 10 dimension = Dimension() dimension.set_dimension_size(dimension_size) dimension.set_regions([[-0.5, 0.5] for _ in range(dimension_size)], [0 for _ in range(dimension_size)]) func = DistributedFunction(dimension, bias_region=[-0.5, 0.5]) target_bias = [0.25 for _ in range(dimension_size)] func.setBias(target_bias) ro = RandomOperator() prob_fct = func.DisRosenbrock x0 = [ro.get_uniform_double(-0.5, 0.5) for _ in range(dimension_size)] ans = [] for i in range(10): x, cost, _ = fmin_smac(func=prob_fct, x0=x0, bounds=[[-0.5, 0.5] for _ in range(dimension_size)], maxfun=50, rng=3) ans.append(x) # print("Optimum at {} with cost of {}".format(x, cost)) print(np.mean(ans)) print(np.std(ans))
generations = x[0] prob = pg.problem(pg.rosenbrock(2)) # 2 - Instantiate a pagmo algorithm algo = pg.algorithm(pg.sga(gen=generations, cr = .90, eta_c = 1., m = 0.02, param_m = 1., param_s = 2, crossover = "exponential", mutation = "polynomial", selection = "tournament", seed = 3)) # 3 - Instantiate an archipelago with 16 islands having each 20 individuals archi = pg.archipelago(16, algo=algo, prob=prob, pop_size=20) # 4 - Run the evolution in parallel on the 16 separate islands 10 times. archi.evolve(10) # 5 - Wait for the evolutions to be finished archi.wait() # 6 - Print the fitness of the best solution in each island res = [isl.get_population().champion_f for isl in archi] import pdb; pdb.set_trace(); return res # debug output logging.basicConfig(level=20) logger = logging.getLogger("Optimizer") # Enable to show Debug outputs x, cost, _ = fmin_smac(func=rosenbrock_2d, x0=[51], bounds=[(50, 100)], maxfun=5, rng=3) # Passing a seed makes fmin_smac determistic print("Best x: %s; with cost: %f"% (str(x), cost))
from smac.facade.func_facade import fmin_smac def rosenbrock_2d(x): """ The 2 dimensional Rosenbrock function as a toy model The Rosenbrock function is well know in the optimization community and often serves as a toy problem. It can be defined for arbitrary dimensions. The minimium is always at x_i = 1 with a function value of zero. All input parameters are continuous. The search domain for all x's is the interval [-5, 5]. """ x1 = x[0] x2 = x[1] val = 100. * (x2 - x1**2.)**2. + (1 - x1)**2. return val # debug output logging.basicConfig(level=10) logger = logging.getLogger("Optimizer") # Enable to show Debug outputs x, cost, _ = fmin_smac(func=rosenbrock_2d, x0=[-3, -4], bounds=[(-5, 5), (-5, 5)], maxfun=325, rng=3) print(x, cost)
# We need to provide a pickable function and use __main__ # to be compliant with multiprocessing API # Below is a work around to have a packaged function called # rosenbrock_2d # -------------------------------------------------------------- import os import sys sys.path.append(os.path.join(os.path.dirname(__file__))) from rosenbrock_2d_delayed_func import rosenbrock_2d # noqa: E402 # -------------------------------------------------------------- if __name__ == '__main__': # debug output logging.basicConfig(level=20) logger = logging.getLogger("Optimizer") # Enable to show Debug outputs # fmin_smac assumes that the function is deterministic # and uses under the hood the SMAC4HPO # n_workers tells the SMBO loop to execute in parallel x, cost, smac = fmin_smac( func=rosenbrock_2d, intensifier=SimpleIntensifier, x0=[-3, -4], bounds=[(-5, 10), (-5, 10)], maxfun=25, rng=3, n_jobs=4, ) # Passing a seed makes fmin_smac determistic print("Best x: %s; with cost: %f" % (str(x), cost))
from test import tester from smac.facade.func_facade import fmin_smac x, cost, sth = fmin_smac( func=tester, x = 5, bounds = [(-10,10)], maxfun = 20, rng = 3) print('---') print(x) print(cost) print(sth) print('---')