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")
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 experiment_test(experiment_name, max_nfe, precision, suite): # bbob observer output_folder = experiment_name observer = cocoex.Observer("bbob", "result_folder: " + output_folder) #get best code file = open( "./results/" + experiment_name + "/" + str(get_best_indv()) + ".txt", 'r') best = file.readlines() file.close() code = "import numpy as np\nfrom src.src.solution import Solution\nimport src.src.operators as op\n" for line in best[6:-8]: code += line #run code on each problem in suite for problem in suite: problem.observe_with( observer) # generates the data for cocopp post-processing d = { "max_nfe": max_nfe, "dimension": problem.dimension, "my_func": problem, "bounds": (problem.lower_bounds[0], problem.upper_bounds[0]) } exec(code, d) cocopp.main(observer.result_folder)
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.))
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
def create_observer(rep1, rep2=None, split=None): """ Creates a cocoex-observer and specifies the correct datapaths and algorithm name / info :return: a cocoex-observer """ if rep2 is not None and split is not None: opts = f"result_folder:{datapath}/SingleSplit/Rep{rep1}To{rep2}At{split} " \ f"algorithm_name:Single_split_{rep1}_To_{rep2} " \ f"algorithm_info:Splitpoint is {split}" else: opts = f"result_folder:{datapath}/Static/Rep{rep1} " \ f"algorithm_name: Static_{rep1}" obs = cocoex.Observer("bbob", opts.__str__()) return obs
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
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" % (minimal_print.stime, sorted(timings)[-2],
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")
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)