def runSingleSplit_pycma(fid, dim, iids=[1, 2, 3, 4, 5], num_reps=5): """ 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("Pycma", None, None) suite = cocoex.Suite("bbob", "", f"dimensions:{dim}") HittingTimes = [] for i, iid in enumerate(iids): 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 = CMAEvolutionStrategy([0] * 5, 0.5) cma.optimize(f) HittingTimes.append(cma.result.evaluations) print(cma.result.xbest) fitness_function.free() return np.mean(HittingTimes)
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 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 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