Exemple #1
0
def get_bbob(name, n_var=10, **kwargs):
    try:
        import cocoex as ex
    except:
        raise Exception(
            "COCO test suite not found. \nInstallation Guide: https://github.com/numbbo/coco"
        )

    args = name.split("-")

    n_instance = int(args[-1])
    n_function = int(args[-2].replace("f", ""))

    assert 1 <= n_function <= 24, f"BBOB has 24 different functions to be chosen. {n_function} is out of range."

    suite_filter_options = f"function_indices: {n_function} " \
                           f"instance_indices: {n_instance} " \
                           f"dimensions: {n_var}"

    problems = ex.Suite("bbob", "", suite_filter_options)
    assert len(problems) == 1, "COCO problem not found."

    coco = problems.next_problem()

    return coco
Exemple #2
0
def main(args):
    ## arguments ##
    parser = argparse.ArgumentParser()
    #parser.add_argument('--nfe'  , dest='nfe'  , type=float, help="Integer   : Number of Function Evaluations")
    parser.add_argument('--n',
                        dest='n',
                        type=float,
                        help="Integer   : Population size")
    parser.add_argument('--w',
                        dest='w',
                        type=float,
                        help="Real value: velocity modifier")
    parser.add_argument('--c1',
                        dest='c1',
                        type=float,
                        help="Real value: pbest modifier")
    parser.add_argument('--c2',
                        dest='c2',
                        type=float,
                        help="Real value: gbest modifier")
    parser.add_argument('--m',
                        dest='m',
                        type=int,
                        help="Real value: 1-8, specifying which PSO to run")
    #parser.add_argument('--bbob', dest='bbob'  , type=str  , help="String    : BBOB suite e.g.:function_indices:1 dimensions:2 instance_indices:1")
    args = parser.parse_args()

    ## repair and initialization types ##
    m = [
        0,
        (op.repair_truncate, op.repairv_zero, op.initv_half_dif),  #1
        (op.repair_random, op.repairv_diff, op.initv_half_dif),  #2
        (op.repair_truncate, op.repairv_zero, op.initv_random),  #3
        (op.repair_random, op.repairv_diff, op.initv_random),  #4
        (op.repair_truncate, op.repairv_zero, op.initv_zero),  #5
        (op.repair_random, op.repairv_diff, op.initv_zero),  #6
        (op.repair_truncate, op.repairv_zero, op.initv_small_random),  #7
        (op.repair_random, op.repairv_diff, op.initv_small_random)  #8
    ]

    ## bbob validation suite ##
    suite = cocoex.Suite(
        "bbob", "",
        "function_indices:2-7,9-12,14,16-19,21-24 dimensions:10 instance_indices:1-15"
    )
    observer = cocoex.Observer("bbob",
                               "result_folder: " + "PSO_" + str(args.m))
    #minimal_print = cocoex.utilities.MiniPrint()

    ## nfe ##
    nfe = 1e+7

    ## loop over problems ##
    for problem in suite:
        problem.observe_with(observer)
        sol = pso(args.m, args.n, problem,
                  (problem.lower_bounds[0], problem.upper_bounds[0]),
                  problem.dimension, nfe, args.w, args.c1, args.c2, *m[args.m])
        #minimal_print(problem, final=problem.index == len(suite) - 1)
    return
Exemple #3
0
def main():
    suite_name = "bbob"
    output_folder = "benchmark-output"

    suite = cocoex.Suite(suite_name, "", "")
    observer = cocoex.Observer(suite_name, "result_folder: " + output_folder)

    if len(sys.argv) < 2:
        sys.stderr.write("invalid number of arguments")
        sys.exit(1)

    evolution_func = differential_evolution
    if sys.argv[1] == "dg":
        evolution_func = differential_evolution_dg
    elif sys.argv[1] != "classic":
        sys.stderr.write("expected either classic or dg as an argument")
        sys.exit(1)
    seed = get_seed()
    print("seed: " + str(seed))

    for problem in suite:
        if problem.number_of_objectives > 1:
            continue

        observer.observe(problem)
        bounds = np.asarray([problem.lower_bounds, problem.upper_bounds]).T
        for _ in evolution_func(problem, bounds, seed, iteration_count=100):
            pass
def run_optimizer(optimizer, dim, fID, instance, logfile, lb, ub, max_FEs,
                  data_path, bbob_opt):
    """Parallel BBOB/COCO experiment wrapper
    """
    # Set different seed for different processes
    start = time()
    seed = np.mod(int(start) + os.getpid(), 1000)
    np.random.seed(seed)

    data_path = os.path.join(data_path, str(instance))
    max_FEs = eval(max_FEs)

    suite_filter_options = "dimensions: %d instance_indices: %d year:2019" % (
        dim, instance)
    suite = cocoex.Suite('bbob', "", suite_filter_options)

    observer = cocoex.Observer('bbob', "result_folder: " + data_path)
    f = suite.get_problem(fID)
    f.observe_with(observer)
    assert dim == f.dimension

    opt = optimizer(dim, f, -np.inf, max_FEs, f.lower_bounds, f.upper_bounds,
                    logfile)
    opt.run()

    f.finalizerun()
    with open('out', 'a') as fout:
        fout.write(
            "{} on f{} in {}D, instance {}: FEs={}, fbest-ftarget={:.4e}, "
            "elapsed time [m]: {:.3f}\n".format(optimizer, fID, dim, instance,
                                                f.evaluations,
                                                f.fbest - f.ftarget,
                                                (time() - start) / 60.))
Exemple #5
0
def bbob(solver, output_folder):
    ### input
    suite_name = "bbob"
    output_folder = output_folder
    budget_multiplier = 100  # increase to 10, 100, ...
    solver = solver

    ### prepare
    suite = cocoex.Suite(suite_name, "", "")
    observer = cocoex.Observer(suite_name, "result_folder: " + output_folder)
    minimal_print = cocoex.utilities.MiniPrint()

    ### go
    for problem in suite:  # this loop will take several minutes or longer
        problem.observe_with(
            observer)  # generates the data for cocopp post-processing
        # apply restarts while neither the problem is solved nor the budget is exhausted
        while (problem.evaluations < problem.dimension * budget_multiplier
               and not problem.final_target_hit):

            sw.pso(10, problem, problem.lower_bounds, problem.upper_bounds,
                   problem.dimension, 3)

        minimal_print(problem, final=problem.index == len(suite) - 1)

    ### post-process data
    cocopp.main(
        observer.result_folder)  # re-run folders look like "...-001" etc
    webbrowser.open("file://" + os.getcwd() + "/ppdata/index.html")
Exemple #6
0
 def __init__(self):
     # Initialise base fitness function class.
     super().__init__()
     
     #parameters from ge.txt
     self.max_nfe = params['MAX_NFE']
     self.runs    = params['RUNS']
     self.suite   = cocoex.Suite(
                         "bbob", "", 
                         "function_indices:"  +str(params['FUNCTION'])+
                         " dimensions:"       +str(params['DIMENSIONS'])+ 
                         " instance_indices:" +str(params['INSTANCE_INDICES'])
                         )
     
     print("Using BBOB suite: ",self.suite)
     
     #learning method
     file = open("bbob_final_target_fvalue1.pkl",'rb')
     self.ftarget_values = pickle.load(file)
     file.close()
     self.multiplier = params['MULTIPLIER']
     self._ind = -1
     self._gen = 0
     
     #log
     self.logh = open(params['FILE_PATH']+"/history.csv", 'w') #190312: log
     output_list = []
     output_list.append("gen")
     output_list.append("indv")
     output_list.append("hh_fit")
     for p in self.suite:
         output_list.append(p.id)
     self.logh.write(",".join(map(str,output_list))+"\n")
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)
Exemple #8
0
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)
Exemple #9
0
def generate_test_data_for_suite(suite_name,
                                 filename,
                                 solution_array=solution_array):
    """write regression test data into file.

    Argument `solution_array(dimension)` is a function which returns a
    number of solutions of dimension `dimension`.
    """
    if os.path.exists(filename):
        raise ValueError("file '" + filename + "' exists already")
    suite = cocoex.Suite(suite_name, "", "")
    xfc_dict = {}
    for i, f in enumerate(suite):
        for x in solution_array(f.dimension):
            res = (
                f(x) if f.number_of_objectives == 1 else list(f(x)),
                list(f.constraint(x) if f.number_of_constraints > 0 else []))
            if is_finite(res):
                xfc_dict[
                    i, tuple(x)] = res  # tuple, because keys must be immutable
            else:
                print("rejected: ", f.name, i, x, res)

    with open(filename, 'w') as f:
        f.write(repr(xfc_dict))  # caveat: entries are by definition not sorted
def main(args):
    ## arguments ##
    parser = argparse.ArgumentParser()
    #parser.add_argument('--nfe'  , dest='nfe'  , type=float, help="Integer   : Number of Function Evaluations")
    parser.add_argument('--n',
                        dest='n',
                        type=float,
                        help="Integer   : Population size")
    parser.add_argument('--w',
                        dest='w',
                        type=float,
                        help="Real value: velocity modifier")
    parser.add_argument('--c1',
                        dest='c1',
                        type=float,
                        help="Real value: pbest modifier")
    parser.add_argument('--c2',
                        dest='c2',
                        type=float,
                        help="Real value: gbest modifier")
    parser.add_argument('--m',
                        dest='m',
                        type=int,
                        help="Real value: 1-8, specifying which PSO to run")
    #parser.add_argument('--bbob', dest='bbob'  , type=str  , help="String    : BBOB suite e.g.:function_indices:1 dimensions:2 instance_indices:1")
    args = parser.parse_args()

    ## repair and initialization types ##
    m = [
        0,
        (op.repair_truncate, op.repairv_zero, op.initv_half_dif),  #1
        (op.repair_random, op.repairv_diff, op.initv_half_dif),  #2
        (op.repair_truncate, op.repairv_zero, op.initv_random),  #3
        (op.repair_random, op.repairv_diff, op.initv_random),  #4
        (op.repair_truncate, op.repairv_zero, op.initv_zero),  #5
        (op.repair_random, op.repairv_diff, op.initv_zero),  #6
        (op.repair_truncate, op.repairv_zero, op.initv_small_random),  #7
        (op.repair_random, op.repairv_diff, op.initv_small_random)  #8
    ]

    ## bbob training suite ##
    suite = cocoex.Suite(
        "bbob", "",
        "function_indices:1,8,13,15,20 dimensions:10 instance_indices:1-6")

    ## nfe ##
    nfe = 1e+4

    ## loop over problems ##
    fitness = 0
    for problem in suite:
        sol = pso(args.m, args.n, problem,
                  (problem.lower_bounds[0], problem.upper_bounds[0]),
                  problem.dimension, nfe, args.w, args.c1, args.c2, *m[args.m])

        fitness += sol.best.getFitness()
    ## irace get information from standard output ##
    print(fitness)
    return
Exemple #11
0
 def __init__(self):
     self.action_space = args.action_space
     self.problem = None
     if self.action_space != 784:
         suite_name = "bbob"
         suite_filter_options = ("dimensions: " +
                                 str(max(self.action_space, 2)))
         self.suite = cocoex.Suite(suite_name, "", suite_filter_options)
Exemple #12
0
 def __init__(self):
     # Initialise base fitness function class.
     super().__init__()
     self.max_nfe = params['MAX_NFE']
     self.runs = params['RUNS']
     self.suite = cocoex.Suite(
         "bbob", "", "function_indices:" + str(params['FUNCTION']) +
         "dimensions:" + str(params['DIMENSIONS']) + "instance_indices:" +
         str(params['INSTANCE_INDICES']))
Exemple #13
0
    def __init__(self, problem_index):
        self.latent = args.latent
        self.vae = VaeModel(args.vae)
        self.vae.load_model()
        self.vae.model.eval()
        self.problem = None

        suite_name = "bbob"
        suite_filter_options = ("dimensions: " + str(self.latent))
        self.suite = cocoex.Suite(suite_name, "", suite_filter_options)
        self.reset(problem_index)
Exemple #14
0
def bbob_rastrigin(x):
    if not hasattr(bbob_rastrigin, 'fun'):
        import cocoex
        dim = len(x)
        suite = cocoex.Suite('bbob',
                             'year:2017',
                             'dimensions: {0} function_indices: 15 instance_indices: 1'.format(dim))
        for fun in suite:
            bbob_rastrigin.fun = fun
            break

    return bbob_rastrigin.fun(x)
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)
Exemple #17
0
def regression_test_a_suite(suite_name, filename):
    """filename contains previously generated test data to compare against.
    
    Details: on a Windows machine we see differences like
    f12 instance 58 in 2D (177, (1447.3149385050367, -830.3270488085931))
        1.7499057709942032e+141 vs 6.09043250958e+67 (original): log-err = 0.351...
    f17: 3.648247252180286e+57 vs 3.46559033612e+57: log-err = 0.0002
    f17 f17: [2.885437508322743e+22, 1322751113639934.8] vs [2.05085412e+22, 1.32275111e+15] or
    f14 f17: [31585031.800419718, 6.480639092419489e+28] vs [3.15850318e+07, 1.69518822e+28]: log-err = 0.01
    f16: -0.13227493309325666 vs -0.132274933067: rel-err = 9.9e-11
    problem f12 instance 60 in 10D: 1.2219569577863538e+76 vs 1.2963795943e+27: log-err = 0.47...
    bbob_f001_i02_d40__bbob_f017_i04_d40: [287089787.64410305, 3.1488536291123374e+39] vs [  2.87089788e+008   9.76803002e+118]: log-err = 0.50156...
    large-scale suite problem f17 instance 4 in 40D (5.449929602038278e+136, []) 6.96081091792e+29: log-err = 0.64...
    """
    verbose = 1
    xfc_dict = literal_eval(open(filename).read())
    if verbose:
        print("using file %s with %d test cases " % (filename, len(xfc_dict)),
              end="")
        sys.stdout.flush()
        t0 = time.process_time()
    suite = cocoex.Suite(suite_name, "year: 0000",
                         "")  # choose "default" year for test
    failed_test_counter = 0
    for key in sorted(xfc_dict):
        f, x = suite[key[0]], key[1]
        try:
            assert is_equal(f(x), xfc_dict[key][0])
        except AssertionError:
            print(f.name, "id,x =", key, "stored f(x),con(x) =", xfc_dict[key],
                  "computed f(x) =", f(x))
            failed_test_counter += 1
        if f.number_of_constraints > 0:
            try:
                assert is_equal(f.constraint(x), xfc_dict[key][1])
            except AssertionError:
                print(f.name, "index,x =", key, "stored f,con =",
                      xfc_dict[key], "computed con =", f.constraint(x))
                failed_test_counter += 1
    if failed_test_counter > 0:
        raise AssertionError(
            "{} assertions failed".format(failed_test_counter))
    if verbose:
        print("done in %.1fs" % (time.process_time() - t0))
Exemple #18
0
    def __init__(self, func_choice):
        # Content common to all episodes
        self.n_ops = len(mutations)
        self.action_space = spaces.Discrete(self.n_ops)
        self.observation_space = spaces.Box(-np.inf, np.inf, shape=(199,), dtype = np.float32)
        self.func_choice = func_choice
        self.FF = 0.5
        self.CR = 1.0
        self.max_gen = 10
        self.window_size = 50
        self.number_metric = 5

        # BBOB
        suite_name = "bbob"
        #suite_options = "dimensions: 2, 3, 5, 10, 20, 40"
        suite_options = "dimensions: 20"
        self.suite = cocoex.Suite(suite_name, "", suite_options)
        # First "" takes following arguments: year, instances; Second "" takes following arguments: dimensions, dimension_indices, function_indices, instance_indices
        self.observer = cocoex.Observer(suite_name, "result_folder: data")
        self.fun_index = 0
Exemple #19
0
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)
n = 100
iteration = 20

#pso: w, c1, c2
params_pso = (1, 1, 1)

#de: beta, crossover prob
params_de = (.7, .8)

#sa: T
T = sa.temperature_exp(1000, .2, 100)
# T = sa.temperature_lin(1000, -5, 100)

### prepare
suite = cocoex.Suite(
    suite_name, "",
    suite_dim)  #Suite("bbob", "year:2009", "dimensions:20 instance_indices:1"
observer = cocoex.Observer(suite_name, "result_folder: " + output_folder)
minimal_print = cocoex.utilities.MiniPrint()

### go
for problem in suite:  # this loop will take several minutes or longer
    problem.observe_with(
        observer)  # generates the data for cocopp post-processing
    # apply restarts while neither the problem is solved nor the budget is exhausted
    while (problem.evaluations < problem.dimension * budget_multiplier
           and not problem.final_target_hit):

        pso(n, iteration, problem.dimension, problem, problem.lower_bounds,
            problem.upper_bounds, *params_pso)
#         sa(n, problem.dimension, problem, problem.lower_bounds, problem.upper_bounds, T)
def main_lia():
    # -------------------------------------------------------
    # set on COCO
    # -------------------------------------------------------
    suite_name = "bbob"
    solver = lia
    algorithm_name = "LiA_algorithm"  # no spaces allowed
    output_folder = algorithm_name  # no spaces allowed
    suite = cocoex.Suite(suite_name, "", "")
    observer = cocoex.Observer(
        suite_name, "result_folder: {0} algorithm_name: {1}".format(
            output_folder, algorithm_name))

    # ---------------------------------------------------------------------------
    # initial variables
    # ---------------------------------------------------------------------------
    minimal_print = cocoex.utilities.MiniPrint()
    stoppings = defaultdict(list)  # dict of lists, key is the problem index
    timings = defaultdict(list)  # key is the dimension

    print(
        '------------------------<< Start LiA analysis >>-------------------------'
    )
    # ------------------------------------------------------------------------------
    # start benchmark functions COCO
    # ------------------------------------------------------------------------------
    instances, instances_tot = 1, 15
    func_dim = 24 * instances_tot
    # --------------------------------------------------------------
    # ide => 0 -> 2D, 1 -> 3D, 2 -> 5D, 3 -> 10D, 4 -> 20D, 5 -> 40D
    # -------------------------------------------------------------
    ide, func = np.array([1, 2, 3, 4]).astype('int'), 1
    ini_dim = ide * func_dim
    ini_fun = ini_dim + (instances_tot * (func - 1))
    fin_dim = ini_dim + func_dim
    # -------------------------------------------------
    # number of instances
    # -------------------------------------------------
    n_instance = 15
    ini_instance = ini_dim
    instances = n_instance + ini_instance
    fin_instance = ini_instance + 15
    # ------------------------------------------------
    steps_min, steps_max = [1e-1, 1e-1, 1e-1, 1e-1], [1e-8, 1e-8, 1e-9, 1e-9]
    n_groups = [7, 7, 8, 8]
    itr_max = [5e2, 2e3, 4e3, 5e3]
    act = 1

    time0 = time.time()
    for index, problem in enumerate(suite):
        if act > len(fin_instance) - 1:
            break
        if index == fin_instance[act]:
            ini_instance[act] = index
            instances[act] = n_instance + ini_instance[act]
            fin_instance[act] = ini_instance[act] + 15

        if ini_dim[act] <= index < fin_dim[act] and ini_fun[
                act] <= index < instances[act]:
            print(index)
            print(problem)
            # --------------------------------------------
            # generate the data for cocopp post-processing
            # ---------------------------------------------
            problem.observe_with(observer)
            problem(np.zeros(problem.dimension))
            if not len(timings[problem.dimension]) and len(timings) > 1:
                print("\n   %s %d-D done in %.1e seconds/evaluations" %
                      (minimal_print.stime, sorted(timings)[-2],
                       np.median(timings[sorted(timings)[-2]])),
                      end='')
            # ---------------------------------------------------------------
            # star LiA algorithm
            # ---------------------------------------------------------------
            time1 = time.time()
            max_runs = int(itr_max[act])

            s_min, s_max = steps_min[act], steps_max[act]

            output = solver(problem,
                            stp_min=s_min,
                            stp_max=s_max,
                            itr=max_runs,
                            ide_dim=act,
                            n_gr=n_groups[act])
            stoppings[problem.index].append(output[1:])
            timings[problem.dimension].append(
                (time.time() - time1) /
                problem.evaluations if problem.evaluations else 0)

            with open(output_folder + '_stopping_conditions.pydict',
                      'wt') as file_:
                file_.write(
                    "# code to read in these data:\n"
                    "# import ast\n"
                    "# with open('%s_stopping_conditions.pydict', 'rt') as file_:\n"
                    "# stoppings = ast.literal_eval(file_.read())\n" %
                    output_folder)
                file_.write(repr(dict(stoppings)))

            # ----------------------------------------------------------
            # timings
            # ----------------------------------------------------------
            timings[problem.dimension].append(
                (time.time() - time1) /
                problem.evaluations if problem.evaluations else 0)
            minimal_print(problem, final=problem.index == len(suite) - 1)

        if index > fin_dim[act]:
            act += 1

    # ----------------------------------------------------------
    # print timings and final message
    # ----------------------------------------------------------
    print("\n   %s %d-D done in %.1e seconds/evaluations" %
          (minimal_print.stime, sorted(timings)[-1],
           np.median(timings[sorted(timings)[-1]])))
    print("*** Full experiment done in %s ***" %
          cocoex.utilities.ascetime(time.time() - time0))

    print("Timing summary:\n"
          "  dimension  median seconds/evaluations\n"
          "  -------------------------------------")
    for dimension in sorted(timings):
        print("    %3d       %.1e" %
              (dimension, np.median(timings[dimension])))
    print("  -------------------------------------")

    # -----------------------------------------------------------------------
    # post-process data
    # -----------------------------------------------------------------------
    cocopp.main(observer.result_folder)  # re-run folders look like "...-001"
    webbrowser.open("file://" + os.getcwd() + "/ppdata/index.html")
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
    avg = np.mean(f)
    low = np.min(f)
    high = np.max(f)
    offset = (1 - exploration_param) * avg + exploration_param * low
    return (f - offset) / (high - low + 1e-6)


# %% Setup problem
RUN_OPTIMISATION = True
MIN_ITERATION = 1
MAX_ITERATION = 21

# Define input domain in GPyOpt format and fitness evaluation function
f_id = 71 + 20

suite = cocoex.Suite("bbob", "instances: 1", "")
#for p in suite:
#    print(f"{p.name}")
fitnessfunc = suite.get_problem(f_id)
FUNCTION_NAME = fitnessfunc.name
print(f"Optimising {FUNCTION_NAME}")
NUM_INPUT_DIMS = fitnessfunc.dimension
NUM_TOTAL_EVALUATIONS = 30 * NUM_INPUT_DIMS

tests = []
for xi in [-1.0, -0.5, 0.0, 0.5, 1.0]:
    for zeta in [-0.5, -0.1, -0.01, 0.0, 0.01, 0.1]:
        for it in range(MIN_ITERATION, MAX_ITERATION + 1):
            tests.append((it, xi, zeta))

domain = []
### input
suite_name = "bbob"
output_folder = "OUT1_40D_1000it"
fmin = scipy.optimize.fmin
budget_multiplier = 1  # increase to 10, 100, ...


# fmin is re-defined to call our solver
def fmin(fun, lbounds, ubounds, dim, budget):
    result = solvers.franken10(50, fun, lbounds, ubounds, dim, 1000)
    return result


### prepare
suite = cocoex.Suite(
    suite_name, "",
    "function_indices:20,21,22,24 dimensions:40 instance_indices:1-15")
observer = cocoex.Observer(suite_name, "result_folder: " + output_folder)
minimal_print = cocoex.utilities.MiniPrint()

### go
for problem in suite:  # this loop will take several minutes or longer
    problem.observe_with(
        observer)  # generates the data for cocopp post-processing
    x0 = problem.initial_solution
    # apply restarts while neither the problem is solved nor the budget is exhausted
    while (problem.evaluations < problem.dimension * budget_multiplier
           and not problem.final_target_hit):
        fmin(
            problem, problem.lower_bounds, problem.upper_bounds,
            problem.dimension, problem.dimension * budget_multiplier
Exemple #25
0
        raise ValueError("printed help and aborted")
    input_params = cocoex.utilities.args_to_dict(
        sys.argv[1:],
        globals(), {'batch': 'current_batch/batches'},
        print=print)
    globals().update(input_params)  # (re-)assign variables

# extend output folder input parameter, comment out if desired otherwise
# output_folder += '_%s_%dD_on_%s' % (
#         fmin.__module__, int(budget_multiplier), suite_name)

if batches > 1:
    output_folder += "_batch%03dof%d" % (current_batch, batches)

### prepare
suite = cocoex.Suite(suite_name, "", suite_filter_options)
observer = cocoex.Observer(suite_name, "result_folder: " + output_folder)
minimal_print = cocoex.utilities.MiniPrint()
stoppings = defaultdict(list)  # dict of lists, key is the problem index
timings = defaultdict(list)  # key is the dimension

### go
print('*** benchmarking %s from %s on suite %s ***' %
      (fmin.__name__, fmin.__module__, suite_name))
time0 = time.time()
for batch_counter, problem in enumerate(
        suite):  # this loop may take hours or days...
    if batch_counter % batches != current_batch % batches:
        continue
    if not len(timings[problem.dimension]) and len(timings) > 1:
        print("\n   %s %d-D done in %.1e seconds/evaluations" %
Exemple #26
0
        d = {
            "max_nfe": max_nfe,
            "dimension": problem.dimension,
            "my_func": problem,
            "bounds": (problem.lower_bounds[0], problem.upper_bounds[0])
        }

        exec(code, d)

        d_fitness[problem.id] = d['XXX_output_XXX']
        d_target_hit[problem.id] = 0
        if np.abs(d_fitness[problem.id] -
                  ftarget_values[problem.id]) <= precision:
            d_target_hit[problem.id] = 1

    result = sum(d_target_hit.values()) / len(suite)

    #write results to a file
    write_log(d_fitness, result)


if __name__ == "__main__":
    precision = float(sys.argv[1])
    max_nfe = int(sys.argv[2])
    experiments = sys.argv[3:]
    suite = cocoex.Suite(
        "bbob", "",
        "function_indices:1,15 dimensions:20,40 instance_indices:1-10")

    for experiment_name in experiments:
        experiment_test(experiment_name, max_nfe, precision, suite)
Exemple #27
0
import cocoex as ex
import numpy as np
print(ex.known_suite_names)

suite_name = "bbob"  # cocoex.known_suite_names
suite_instance = ""  # "year:2016"
suite_options = "dimensions:40"  # "dimensions: 2,3,5,10,20 "  # if 40 is not desired

suite = ex.Suite(suite_name, suite_instance, suite_options)
fun = suite[0]
range_ = fun.upper_bounds - fun.lower_bounds
center = fun.lower_bounds + range_ / 2
dim = fun.dimension
evals = fun.evaluations
evalc = fun.evaluations_constraints
fin_target = fun.final_target_hit
x0 = fun.initial_solution
xf = fun.best_observed_fvalue1
Id = fun.id
name = fun.name
fun(np.random.rand(40))

for problem_index, problem in enumerate(suite):
    print(problem.name)
    print(problem.dimension)
    print(probelm(np.random.rand(40)))
    print()

#In Julia
'''
using PyCall
Exemple #28
0
batches = 1  # number of batches, batch=3/32 works to set both, current_batch and batches
current_batch = 1  # only current_batch modulo batches is relevant

### possibly modify/overwrite above input parameters from input args
if __name__ == "__main__":
    input_params = cocoex.utilities.args_to_dict(
        sys.argv[1:], globals(), {'batch': 'current_batch/batches'}, print=print)
    globals().update(input_params)  # (re-)assign variables

# overwrites folder input parameter, comment out if desired otherwise
output_folder = '%s_of_%s_%dD_on_%s' % (fmin.__name__, fmin.__module__,
                                        int(budget_multiplier), suite_name)

### prepare
suite = cocoex.Suite(suite_name, "", "")
observer = cocoex.Observer(suite_name, "result_folder: " + output_folder)
minimal_print = cocoex.utilities.MiniPrint()
stoppings = defaultdict(list)  # dict of lists, key is the problem index
timings = defaultdict(list)  # key is the dimension

### go
print('*** benchmarking %s from %s on suite %s ***'
      % (fmin.__name__, fmin.__module__, suite_name))
time0 = time.time()
for problem in suite:  # this loop may take several minutes or hours or days...
    if omit_last_dimension and problem.dimension == max(suite.dimensions):
        break
    if problem.index % batches != current_batch % batches:
        continue
    if not len(timings[problem.dimension]) and len(timings) > 1:
Exemple #29
0
### input
suite_name = "bbob"
output_folder = "franken30"
fmin = scipy.optimize.fmin
budget_multiplier = 1000  # increase to 10, 100, ...


# fmin is re-defined to call our solver
def fmin(fun, lbounds, ubounds, dim, budget):
    result = solvers.new_franken30(50, fun, lbounds, ubounds, dim, 30)
    return result


### prepare
suite = cocoex.Suite(suite_name, "",
                     "function_indices:20,21,22,24 dimensions:2")
observer = cocoex.Observer(suite_name, "result_folder: " + output_folder)
minimal_print = cocoex.utilities.MiniPrint()

### go
for problem in suite:  # this loop will take several minutes or longer
    problem.observe_with(
        observer)  # generates the data for cocopp post-processing
    x0 = problem.initial_solution
    # apply restarts while neither the problem is solved nor the budget is exhausted
    while (problem.evaluations < problem.dimension * budget_multiplier
           and not problem.final_target_hit):
        fmin(
            problem, problem.lower_bounds, problem.upper_bounds,
            problem.dimension, problem.dimension * budget_multiplier
        )  # here we assume that `fmin` evaluates the final/returned solution
                break

        print("final population")
        print("time: ", time.time() - begin)
        print("Optimization ending")

        return self.population_list


###################################################################
#                       Test on COCO                              #
###################################################################

if __name__ == '__main__':

    import cocoex as ex

    suite = ex.Suite("bbob-biobj", "", "")
    observer = ex.Observer("bbob-biobj", "result_folder:doctest")

    for fun in suite:
        print("Number of objectives: ", fun.number_of_objectives)
        fun.observe_with(observer)
        R2 = R2_EMOAs(fun)
        # Configuration by default from R2 Indicator-Based Multiobjective Search research paper (Dimo Brockhoff)
        # Only the iteration number is changed due to the computation time
        # Tested for 100 - 150 - 500 - 1000 - 5000 - 10000
        # Official results for 150 and 1000 only (tecnology restriction)
        R2.optimize(fun, np.array([-1e-1, -1e-1]), 200000, 10, 15, 0.9, 0.5,
                    (1 / fun.dimension), 20, 100, -100)