def runSingleSplitExample(): """ Example run of a single-split CMA-ES. Runs on F1, 2 and 3 instance 1; 5 repetitions each Stores results in exdata/Experiments/SingleSplit/ :return: """ dim = 5 rep1s = [2703, 399, 3567] rep2s = [2163, 2163, 2163] budget = 50000 suite = cocoex.Suite("bbob", "", "dimensions:5 instance_indices:1,2,3,4,5") for fid_idx in range(3): fid = 5 split_idx = 11 split_target = get_target(fid, 1, split_idx) rep1 = rep1s[fid_idx] rep2 = rep2s[fid_idx] obs = create_observer(rep1, rep2, split_idx) for i in range(5): fitness_function = suite.get_problem_by_function_dimension_instance( fid, dim, 1) fitness_function.observe_with(obs) f = fitnessFunc(fitness_function) cma = SingleSplitCMAES(dim, f, budget, representation=Utils.intToRepr(rep1), representation2=Utils.intToRepr(rep2), seed=i, split=split_target) cma.run_optimizer() fitness_function.free() print(cma.best_individual) print(cma.used_budget)
def single_split_with_hyperparams(fid, dim, rep1, rep2, split_idx, record_runs=False, iids=[1, 2, 3, 4, 5], num_reps=5, hyperparams=None): """ Function running single-split CMA-ES with specific hyperparameters. :param fid: The function id (from bbob) :param dim: The dimension to run the bbob-problem in :param rep1: The configuration to run before the splitpoint :param rep2: The configuration to run after the splitpoint :param split_idx: The splitpoint-index at which to switch between rep1 and rep2 :param iids: The instances of the bbob-function to run :param num_reps: The amount of repetitions to run :param record_runs: Whether or not to record a .dat-file during the runs of the CMA-ES :param hyperparams: Dictionary of the hyperparameters to use :return: The ERT over all num_reps runs on all instances in iids """ if record_runs: obs = create_observer(rep1, rep2, split_idx) suite = cocoex.Suite("bbob", "", f"dimensions:{dim}") budget = dim * Config.budget_factor hittingtimes = [] succeeded = 0 for i, iid in enumerate(iids): split_target = get_target(fid, iid) + 10**(2 - (split_idx / 5)) for j in range(num_reps): fitness_function = suite.get_problem_by_function_dimension_instance( fid, dim, iid) if record_runs: fitness_function.observe_with(obs) f = fitnessFunc(fitness_function) cma = SingleSplitCMAES(dim, f, budget, representation=Utils.intToRepr(rep1), representation2=Utils.intToRepr(rep2), seed=j, split=split_target) cma.set_hyperparameters(hyperparams) cma.run_optimizer() hittingtimes.append(cma.used_budget) print(cma.used_budget) succeeded += fitness_function.final_target_hit fitness_function.free() return sum(hittingtimes) / max(succeeded, 1)
def set_modules(self): ''' Set modules after configuration switch. ''' options = Utils.getOpts(self.representation) self.parameters.__dict__.update(**options) if self.parameters.selection != 'pairwise': selector = Selection.best else: selector = Selection.pairwise if options['sequential']: options['seq_cutoff'] = 2 self.parameters.seq_cutoff = 2 if self.lambda2_ is not None: self.parameters.lambda_ = self.lambda2_ self.parameters.lambda_, \ self.parameters.eff_lambda, \ self.parameters.mu = self.calculateDependencies( options, self.parameters.lambda_, self.parameters.mu ) self.select = lambda pop, new_pop, _, param: selector(pop, new_pop, param) self.mutate = partial( Mutation.CMAMutation, sampler=self.get_sampler(options), threshold_convergence=options["threshold"] ) self.change_parameter_weights()
def runSingleSplit(fid, dim, rep1, rep2, split_idx, iids=[1, 2, 3, 4, 5], num_reps=5, budget=None): """ Function running single-split CMA-ES. :param fid: The function id (from bbob) :param dim: The dimension to run the bbob-problem in :param rep1: The configuration to run before the splitpoint :param rep2: The configuration to run after the splitpoint :param split_idx: The splitpoint-index at which to switch between rep1 and rep2 :param iids: The instances of the bbob-function to run :param num_reps: The amount of repetitions to run :return: The ERT over all num_reps runs on all instances in iids """ obs = create_observer(rep1, rep2, split_idx) suite = cocoex.Suite("bbob", "", f"dimensions:{dim}") if budget is None: budget = dim * Config.budget_factor HittingTimes = [] succeeded = 0 for i, iid in enumerate(iids): split_target = get_target(fid, iid) + 10**(2 - (split_idx / 5)) for j in range(num_reps): fitness_function = suite.get_problem_by_function_dimension_instance( fid, dim, iid) fitness_function.observe_with(obs) f = fitnessFunc(fitness_function) cma = SingleSplitCMAES(dim, f, budget, representation=Utils.intToRepr(rep1), representation2=Utils.intToRepr(rep2), seed=j, split=split_target) cma.run_optimizer() HittingTimes.append(cma.used_budget) succeeded += fitness_function.final_target_hit fitness_function.free() return sum(HittingTimes) / max(succeeded, 1)
def run_ranking_test_lambda(fid=21, confs_nr=0): # results = [] configs = np.load(f"{datapath_npy}Statics_F{fid}_lambda.npy") params_full = ['c_1', 'c_c', 'c_mu', 'lambda_'] optimizer0 = hyperparameterOptimizer(params=params_full, n_init_sample=50, max_iter=650, part_to_optimize=-1) config = configs[confs_nr] x = Utils.intToRepr(config) x[-1] = 0 config_new = Utils.reprToInt(x) print(config_new) open(f"{datapath}Data_Full/F{fid}_rep{config_new}_lambda.csv", 'w').close() optimizer0(fid, 5, config_new, config_new, 51, target_idx=51, budget=25000, num_reps=10, data_file=f"{datapath}Data/F{fid}_st_{config_new}_lambda")
def runStaticExample(): """ Example run of a static CMA-ES. Runs on F1 and 2; instance 1; 5 repetitions each Stores results in exdata/Experiments/Static/ :return: """ dim = 5 reps = [0, 3] budget = 50000 suite = cocoex.Suite("bbob", "", "dimensions:5 instance_indices:1,2,3,4,5") for fid in range(2): rep = Utils.intToRepr(reps[fid]) obs = create_observer(reps[fid]) for i in range(5): fitness_function = suite.get_problem_by_function_dimension_instance( fid + 1, dim, 1) fitness_function.observe_with(obs) f = fitnessFunc(fitness_function) cma = StaticCMA(dim, f, budget, representation=rep, seed=i) cma.run_optimizer() fitness_function.free() print(cma.used_budget)
def single_split_hyperparam_single(iid, rep_nr=None, hyperparams=None, hyperparams2=None, rep1=None, rep2=None, fid=None, split_idx=None, dim=None, record_runs=None, budget=None, target_idx=None, lambda_=None, lambda2_=None, opt_split=False): """ Function handling single runs of single-split CMA-ES during hyperparameter optimization. :param fid: The function id (from bbob) :param dim: The dimension to run the bbob-problem in :param rep1: The configuration to run before the splitpoint :param rep2: The configuration to run after the splitpoint :param split_idx: The splitpoint-index at which to switch between rep1 and rep2 :param iid: The instance of the bbob-function to run :param rep_nr: The repetition number (used for controlled randomness) :param record_runs: Whether or not to record a .dat-file during the runs of the CMA-ES :param hyperparams: Dictionary of the hyperparameters to use for C1 :param hyperparams2: Dictionary of the hyperparameters to use for C2 :param lambda_: Population size :return: The budget used and whether or not the final target was hit """ if rep_nr is None: if len(iid) == 2: rep_nr = iid[1] iid = iid[0] else: raise Exception("Missing rep_nr parameter") if record_runs: obs = create_observer(rep1, rep2, split_idx) suite = cocoex.Suite("bbob", "", f"dimensions:{dim}") if budget is None: budget = dim * Config.budget_factor split_target = get_target(fid, iid, split_idx) fitness_function = suite.get_problem_by_function_dimension_instance( fid, dim, iid) if record_runs: fitness_function.observe_with(obs) if target_idx is not None: target = get_target(fid, iid, target_idx) else: target = None f = fitnessFunc(fitness_function, target) cma = SingleSplitCMAES(dim, f, budget, representation=Utils.intToRepr(rep1), representation2=Utils.intToRepr(rep2), seed=rep_nr, split=split_target, hyperparams=hyperparams, hyperparams2=hyperparams2, lambda_=lambda_, lambda2_=lambda2_) # cma.set_hyperparameters(hyperparams) cma.run_optimizer() target_hit = f.final_target_hit fitness_function.free() # print(cma.used_budget, target_hit) if opt_split: return cma.used_budget_at_split, cma.switched, cma.used_budget, target_hit return cma.used_budget, target_hit
def default_values(self): return Utils.getVals(self.default_parameters)
def default_options(self): if self.representation is not None: return Utils.getOpts(self.representation) else: return Utils.getOpts(self.default_representation)