|
| 1 | +from time import perf_counter |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import modcma.c_maes as modcma |
| 5 | +import ioh |
| 6 | +import pandas as pd |
| 7 | + |
| 8 | +from multiprocessing import Pool |
| 9 | + |
| 10 | + |
| 11 | +from pprint import pprint |
| 12 | + |
| 13 | +np.random.seed(12) |
| 14 | + |
| 15 | +def run_modma(problem: ioh.ProblemType, |
| 16 | + x0: np.ndarray, |
| 17 | + logger_obj, |
| 18 | + matrix_adaptation = modcma.options.COVARIANCE |
| 19 | + ): |
| 20 | + modules = modcma.parameters.Modules() |
| 21 | + modules.matrix_adaptation = matrix_adaptation |
| 22 | + |
| 23 | + settings = modcma.Settings( |
| 24 | + problem.meta_data.n_variables, |
| 25 | + x0=x0, |
| 26 | + modules=modules, |
| 27 | + lb=problem.bounds.lb, |
| 28 | + ub=problem.bounds.ub, |
| 29 | + verbose=True, |
| 30 | + sigma0=2.0, |
| 31 | + target=problem.optimum.y + 1e-8, |
| 32 | + budget=problem.meta_data.n_variables * 100_000 |
| 33 | + ) |
| 34 | + |
| 35 | + cma = modcma.ModularCMAES(settings) |
| 36 | + |
| 37 | + start = perf_counter() |
| 38 | + while not cma.break_conditions(): |
| 39 | + if cma.p.criteria.any(): |
| 40 | + logger_obj.update(cma.p.criteria.items) |
| 41 | + cma.step(problem) |
| 42 | + |
| 43 | + cma.run(problem) |
| 44 | + stop = perf_counter() |
| 45 | + elapsed = stop - start |
| 46 | + return elapsed, cma.p.stats.t, problem.state.evaluations, cma.p.stats.n_updates |
| 47 | + |
| 48 | + |
| 49 | +class RestartCollector: |
| 50 | + def __init__(self, strategy = modcma.options.RestartStrategy.NONE): |
| 51 | + modules = modcma.parameters.Modules() |
| 52 | + modules.restart_strategy = strategy |
| 53 | + settings = modcma.Settings( |
| 54 | + 2, |
| 55 | + modules=modules, |
| 56 | + ) |
| 57 | + cma = modcma.ModularCMAES(settings) |
| 58 | + self.names = [x.name for x in cma.p.criteria.items] |
| 59 | + self.reset() |
| 60 | + |
| 61 | + def update(self, items): |
| 62 | + for item in items: |
| 63 | + if item.met: |
| 64 | + setattr(self, item.name, getattr(self, item.name) + 1) |
| 65 | + |
| 66 | + |
| 67 | + def reset(self): |
| 68 | + for item in self.names: |
| 69 | + setattr(self, item, 0) |
| 70 | + |
| 71 | + |
| 72 | +if __name__ == "__main__": |
| 73 | + dims = 2, 3, 5, 10, 20, 40, 100 |
| 74 | + functions = [1, 2, 6, 7, 8, 9, 10, 11, 12, 13, 14] |
| 75 | + |
| 76 | + n_repeats = 100 |
| 77 | + options = modcma.options.MatrixAdaptationType.__members__ |
| 78 | + del options['COVARIANCE_NO_EIGV'] |
| 79 | + |
| 80 | + |
| 81 | + def collect(name, option): |
| 82 | + logger = ioh.logger.Analyzer( |
| 83 | + folder_name=name, |
| 84 | + algorithm_name=name, |
| 85 | + root="data" |
| 86 | + ) |
| 87 | + collector = RestartCollector() |
| 88 | + logger.add_run_attributes(collector, collector.names) |
| 89 | + for fid in functions: |
| 90 | + for d in dims: |
| 91 | + problem = ioh.get_problem(fid, 1, d) |
| 92 | + problem.attach_logger(logger) |
| 93 | + for i in range(n_repeats): |
| 94 | + modcma.utils.set_seed(21 + fid * d * i) |
| 95 | + collector.reset() |
| 96 | + run_modma(problem, np.zeros(d), collector, option) |
| 97 | + print(name, fid, d, problem.state.current_best_internal.y, problem.state.evaluations) |
| 98 | + problem.reset() |
| 99 | + |
| 100 | + with Pool(len(options)) as p: |
| 101 | + p.starmap(collect, options.items()) |
| 102 | + |
| 103 | + # problem = ioh.get_problem(fid, 1, d) |
| 104 | + # run_modma(problem, np.zeros(d), modcma.options.CMSA) |
| 105 | + # print(problem.state.evaluations, problem.state.current_best_internal.y) |
0 commit comments