From 965c2c5bbc49f81dbea34ec1b73d26a7ab546047 Mon Sep 17 00:00:00 2001 From: DVermetten Date: Thu, 30 Jun 2022 10:14:06 +0200 Subject: [PATCH 01/11] Fix missing closing bracket --- modcma/parameters.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modcma/parameters.py b/modcma/parameters.py index 6323994..fb840d8 100644 --- a/modcma/parameters.py +++ b/modcma/parameters.py @@ -465,7 +465,7 @@ def init_dynamic_parameters(self) -> None: """ self.sigma = np.float64(self.sigma0) if hasattr(self, "m") or self.x0 is None: - self.m = np.float64(np.random.uniform(self.lb, self.ub, (self.d, 1)) + self.m = np.float64(np.random.uniform(self.lb, self.ub, (self.d, 1))) else: self.m = np.float64(self.x0.copy()) self.m_old = np.empty((self.d, 1), dtype=np.float64) From 78225ea52ae1ea46343f8ed97948e29eb6786aec Mon Sep 17 00:00:00 2001 From: DVermetten Date: Thu, 30 Jun 2022 10:38:39 +0200 Subject: [PATCH 02/11] Add function for manual population size updates --- modcma/parameters.py | 9 +++++++++ tests/test_modularcmaes.py | 11 +++++++++++ 2 files changed, 20 insertions(+) diff --git a/modcma/parameters.py b/modcma/parameters.py index fb840d8..c0b3880 100644 --- a/modcma/parameters.py +++ b/modcma/parameters.py @@ -841,6 +841,15 @@ def update(self, parameters: dict, reset_default_modules=False): self.init_selection_parameters() self.init_adaptation_parameters() self.init_local_restart_parameters() + + def update_popsize(self, lambda_new): + """Manually control the population size""" + if self.local_restart is not None: + warnings.warn("Modification of population size is disabled when local restart startegies are used") + return + self.lambda_ = lambda_new + self.mu = lambda_new//2 + self.init_adaptation_parameters() class BIPOPParameters(AnnotatedStruct): diff --git a/tests/test_modularcmaes.py b/tests/test_modularcmaes.py index 7faaaf4..92b6077 100644 --- a/tests/test_modularcmaes.py +++ b/tests/test_modularcmaes.py @@ -187,6 +187,17 @@ def testcorrect_bounds(self): with self.assertRaises(ValueError): modularcmaes.correct_bounds(x.copy(), ub, lb, "something_undefined") + + def test_popsize_changes(self): + """Test manual changes to population size.""" + c = modularcmaes.ModularCMAES(sum, 2) + c.step() + c.parameters.update_popsize(40) + c.step() + self.assertEqual(c.parameters.population.n, 40) + c.parameters.update_popsize(6) + c.step() + self.assertEqual(c.parameters.population.n, 6) @unittest.mock.patch("sys.stdout", new_callable=io.StringIO) def test_evaluate_bbob(self, mock_std): From 540c690b4e6cadda0cfd84867d772342053f0d42 Mon Sep 17 00:00:00 2001 From: DVermetten Date: Fri, 1 Jul 2022 11:00:36 +0200 Subject: [PATCH 03/11] Change IOHexperimenter to new package name --- modcma/parameters.py | 4 ++-- requirements.txt | 2 +- tests/test_modularcmaes.py | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/modcma/parameters.py b/modcma/parameters.py index c0b3880..22b3746 100644 --- a/modcma/parameters.py +++ b/modcma/parameters.py @@ -244,7 +244,7 @@ class Parameters(AnnotatedStruct): n_generations: int = None lambda_: int = None mu: int = None - sigma0: float = 0.5 + sigma0: float = 0.2 a_tpa: float = 0.5 b_tpa: float = 0.0 cs: float = None @@ -463,7 +463,7 @@ def init_dynamic_parameters(self) -> None: Examples of such parameters are the Covariance matrix C and its eigenvectors and the learning rate sigma. """ - self.sigma = np.float64(self.sigma0) + self.sigma = np.float64(self.sigma0) * (self.ub[0,0] - self.lb[0,0]) if hasattr(self, "m") or self.x0 is None: self.m = np.float64(np.random.uniform(self.lb, self.ub, (self.d, 1))) else: diff --git a/requirements.txt b/requirements.txt index c4dfe5a..0ab854f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -IOHexperimenter>=0.2.8 +ioh>=0.3.2.8.3 numba>=0.52.0 numpy>=1.18.5 scipy>=1.4.1 \ No newline at end of file diff --git a/tests/test_modularcmaes.py b/tests/test_modularcmaes.py index 92b6077..77c5883 100644 --- a/tests/test_modularcmaes.py +++ b/tests/test_modularcmaes.py @@ -9,7 +9,7 @@ import numpy as np from modcma import parameters, utils, modularcmaes -from IOHexperimenter import IOH_function +import ioh from .expected import BBOB_2D_PER_MODULE_20_ITER @@ -67,7 +67,7 @@ def run_module(self, module, value): def run_bbob_function(self, module, value, fid): """Expects the output to be consistent with BBOB_2D_PER_MODULE_20_ITER.""" np.random.seed(42) - f = IOH_function(fid, self._dim, 1) + f = ioh.get_problem(fid, dimension=self._dim, instance=1) self.p = parameters.Parameters( self._dim, budget=self._budget, **{module: value} ) From 0cbfc57e99e653b2246a520e5a0c738ed3e8f947 Mon Sep 17 00:00:00 2001 From: DVermetten Date: Fri, 1 Jul 2022 11:26:47 +0200 Subject: [PATCH 04/11] Change expected values to json file and add generation function --- requirements.txt | 3 +- tests/create_expected.py | 38 ++ tests/expected.json | 1 + tests/expected.py | 706 ------------------------------------- tests/test_modularcmaes.py | 12 +- 5 files changed, 50 insertions(+), 710 deletions(-) create mode 100644 tests/create_expected.py create mode 100644 tests/expected.json delete mode 100644 tests/expected.py diff --git a/requirements.txt b/requirements.txt index 0ab854f..bd8ae0d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ ioh>=0.3.2.8.3 numba>=0.52.0 numpy>=1.18.5 -scipy>=1.4.1 \ No newline at end of file +scipy>=1.4.1 +json \ No newline at end of file diff --git a/tests/create_expected.py b/tests/create_expected.py new file mode 100644 index 0000000..6417582 --- /dev/null +++ b/tests/create_expected.py @@ -0,0 +1,38 @@ +from modcma import ModularCMAES, parameters, utils +import ioh +import numpy as np +import json + +def run_bbob_function(module, value, fid): + """Creates the output BBOB_2D_PER_MODULE_20_ITER.""" + np.random.seed(42) + dim = 2 + budget = 20 + iid = 1 + f = ioh.get_problem(fid, dimension=dim, instance=iid) + p = parameters.Parameters( + dim, budget=budget, **{module: value} + ) + c = ModularCMAES(f, parameters=p).run() + return f.state.current_best_internal.y + +def create_expected_dict(): + BBOB_2D_PER_MODULE_20_ITER = dict() + for module in parameters.Parameters.__modules__: + m = getattr(parameters.Parameters, module) + if type(m) == utils.AnyOf: + for o in filter(None, m.options): + BBOB_2D_PER_MODULE_20_ITER[f"{module}_{o}"] = np.zeros(24) + for fid in range(1, 25): + BBOB_2D_PER_MODULE_20_ITER[f"{module}_{o}"][fid - 1] = run_bbob_function(module, o, fid) + + elif type(m) == utils.InstanceOf: + BBOB_2D_PER_MODULE_20_ITER[f"{module}_{True}"] = np.zeros(24) + for fid in range(1, 25): + BBOB_2D_PER_MODULE_20_ITER[f"{module}_{True}"][fid - 1] = run_bbob_function(module, True, fid) + + +if __name__ == "__main__": + BBOB_2D_PER_MODULE_20_ITER = create_expected_dict() + with open("expected.json", "w") as f: + json.dump(BBOB_2D_PER_MODULE_20_ITER, f) \ No newline at end of file diff --git a/tests/expected.json b/tests/expected.json new file mode 100644 index 0000000..40eb765 --- /dev/null +++ b/tests/expected.json @@ -0,0 +1 @@ +{"active_True": [0.5079023784181027, 3994.4755180670527, 12.043295738621183, 4.886212069485996, 0.0, 304.44598143380546, 94.62349805255676, 36.18253759553309, 19.85070276244657, 36645.87647130221, 13315.51512952369, 2683.703847500958, 44.81379842261522, 0.4609332855389666, 9.711117955511472, 1.759139939785059, 1.3933170719608428, 20.10914118263286, 0.4559623215635096, 0.9345626367105797, 1.990063610751629, 8.05905284872558, 21.25735111797754, 10.91219903774687], "elitist_True": [0.6699833162594511, 1227.9721054463228, 16.869910137817453, 19.572392428411835, 0.0, 212.3024228554834, 95.34366236043742, 7.623121487433538, 3.161557968895952, 14355.480702885847, 29507.974482338563, 2683.703847500958, 28.411660356917345, 0.6299432344321189, 7.265400966461772, 14.434537776429282, 1.4118850095554634, 23.630235905875463, 0.4559623215635096, 2.1620882643582515, 1.990963814929167, 7.845446138574841, 7.628611542889807, 14.194368416235939], "orthogonal_True": [0.25534271814624465, 118072.30539981391, 2.0096992931251494, 10.626106984940332, 2.8786810407197265, 270686.4186762697, 95.44019339797313, 12.550406947379894, 0.33525044424635597, 25908.565659852564, 1237306.2027346096, 1702.7335324051267, 70.43902585372217, 0.6976581726574429, 12.52074418264813, 12.81291626426489, 58.44704396496336, 66.6243546212341, 2.667737341562461, 2.158758789228943, 2.7378732590422024, 2.0770051329721895, 7.955480122219026, 14.581514536704073], "sequential_True": [19.5719406285756, 7210188.745246256, 13.681793553365424, 23.313513761722504, 0.0, 52255.4145901049, 93.54091559010935, 184.20999101158142, 384.33208310135626, 23103333.812051296, 32971525.026216447, 2683.703847500958, 214.39433012072365, 57.622398804713555, 25.18091794870781, 20.16251348556511, 1.9775544663974183, 52.20467342507745, 0.4559623215635096, 259.10398673490283, 2.0601778166852416, 11.496305678387635, 8.260780485902643, 43.60153110381705], "threshold_convergence_True": [0.7347847130472607, 3621.5783803371305, 9.440988022081923, 17.588360915885023, 0.0, 387.4010412103078, 95.44019339797313, 24.543790706144733, 11.6043233309905, 419398.0095371446, 3430.404715228205, 926.4455952079577, 28.411660356917345, 0.6893117820736764, 32.31733995435986, 15.751386747152557, 1.4118850095554634, 12.429946026578554, 0.4559623215635096, 3.055334914776176, 0.8924655543998508, 1.9930860112728006, 6.085273695307758, 7.74642446736389], "step_size_adaptation_csa": [0.6699833162594511, 4858.983093703771, 13.575313361038532, 20.615929060719242, 0.0, 314.32588235229133, 95.00863388735908, 7.623121487433538, 2.40985693994124, 8829.9538591471, 75722.58537541116, 2683.703847500958, 28.411660356917345, 0.46048487751008504, 23.76532915356332, 20.16251348556511, 1.4118850095554634, 22.517169808626214, 0.4559623215635096, 1.3074796822154253, 2.011681868741659, 10.719130303012593, 17.153972345540915, 15.81948309019006], "step_size_adaptation_tpa": [0.03767014607534176, 2175.9911995416614, 13.567807397656335, 11.874870433498288, 0.0, 1071.7434978918488, 94.32587149819872, 7.124026768321718, 9.334989121212201, 266.0481801806988, 252465.61185230396, 691.4718777227441, 38.68678171583922, 0.2866364961210628, 11.326830236653606, 15.751386747152557, 3.855740332598697, 32.942464249892616, 0.4559623215635096, 1.9737590640108762, 2.5904060477232917, 0.00110904003110431, 9.77176743348317, 3.9402019732311335], "step_size_adaptation_msr": [0.21287712512225715, 295.54884244593984, 30.872441630043006, 2.438539936320332, 0.0, 291.62475583684414, 95.00863388735908, 7.124026768321718, 9.334989121212201, 1927.7199333449996, 252293.37460247002, 2683.703847500958, 38.68678171583922, 0.1609115280864648, 23.76532915356332, 15.751386747152557, 3.453551937059726, 31.36699937737331, 0.4559623215635096, 2.101822038572848, 0.13402589068051024, 1.7724620177602137, 5.498985829997645, 10.501507178287895], "step_size_adaptation_xnes": [3.685803014042311, 53903.67207319866, 5.424387513620404, 8.476988158726103, 0.0, 70.60878462167778, 93.54091559010935, 3.8843531249961387, 255.5973743918305, 216076.36001404427, 13572.632799955189, 6.099589325005635, 364.92895618522044, 4.2640726765416295, 3.5220968308719414, 15.751386747152557, 4.013689588890096, 20.10958871116135, 0.4559623215635096, 1.9622096635746784, 4.6098062526230805, 0.07660928447563826, 7.97304712403716, 6.572006292778018], "step_size_adaptation_m-xnes": [0.21287712512225715, 2175.9911995416614, 20.43694180566843, 11.874870433498288, 0.0, 34.18405821417568, 95.00863388735908, 7.124026768321718, 9.334989121212201, 1927.7199333449996, 256392.77602054115, 1447.1339175020744, 38.68678171583922, 0.5070656236522031, 23.76532915356332, 15.751386747152557, 2.8882839166644008, 31.36699937737331, 0.4559623215635096, 2.3635090154834093, 0.4682737151651404, 0.43657925301521344, 7.819215103667412, 13.559901346380887], "step_size_adaptation_lp-xnes": [1.7332064445719153, 4419.683662806729, 17.098151479760155, 14.562651567781499, 4.766267911300538, 113.70160820754681, 93.54091559010935, 4.863385150913383, 4.659487885926415, 4387.270026268735, 105506.49942948109, 2512.7600761088174, 26.127030760680967, 0.7712613615349453, 5.842531481068716, 8.687032329156176, 7.279161607717865, 61.20592146441295, 3.744194191668644, 2.4622889158859524, 2.8965324473324405, 12.262312434816277, 17.73216844338432, 3.4960227654881573], "step_size_adaptation_psr": [0.21287712512225715, 1088.5057791931495, 16.688455670695674, 5.5288647474243735, 0.0, 266.943391560345, 95.00863388735908, 7.124026768321718, 9.334989121212201, 1927.7199333449996, 207576.52536598608, 2683.703847500958, 38.68678171583922, 0.36530618515212254, 23.76532915356332, 15.751386747152557, 3.855740332598697, 31.36699937737331, 0.4559623215635096, 3.8792443115816027, 0.3441163839687159, 8.044092332563334, 3.6171907727443897, 3.655695515119866], "mirrored_mirrored": [0.7554264630844963, 30.995928352373653, 10.727147924627236, 28.35505849865668, 0.0, 73.03264789844737, 98.3482403109842, 19.205395254336498, 10.73932209031653, 306.7532919121426, 47432.05526640019, 2683.703847500958, 199.99567611651995, 1.976899305627097, 8.448239046275782, 10.619873784979125, 2.0468365502110966, 3.1760484231995507, 0.4559623215635096, 3.141781349728834, 2.5738103658736153, 2.7415047738254796, 11.184397856398496, 13.905359243715296], "mirrored_mirrored pairwise": [0.03373835261991253, 981.7390996048255, 10.727147924627236, 10.296884748016868, 0.0, 73.03264789844737, 93.54091559010935, 19.205395254336498, 10.73932209031653, 191683.40442372032, 157018.28884094275, 2683.703847500958, 199.99567611651995, 1.976899305627097, 29.38549651637714, 17.063577075453246, 6.758992843888698, 32.39714226445396, 0.4559623215635096, 4.628179175342729, 1.2275961378528137, 2.7415047738254796, 1.3838182113641495, 15.305137607192382], "base_sampler_gaussian": [0.6699833162594511, 4858.983093703771, 13.575313361038532, 20.615929060719242, 0.0, 314.32588235229133, 95.00863388735908, 7.623121487433538, 2.40985693994124, 8829.9538591471, 75722.58537541116, 2683.703847500958, 28.411660356917345, 0.46048487751008504, 23.76532915356332, 20.16251348556511, 1.4118850095554634, 22.517169808626214, 0.4559623215635096, 1.3074796822154253, 2.011681868741659, 10.719130303012593, 17.153972345540915, 15.81948309019006], "base_sampler_sobol": [1.9382794743647251, 109862.7926336296, 13.944304419608123, 11.193683455264821, 0.0, 118.86959643555977, 95.44019339797313, 16.763635893103455, 2.740797812510343, 10861.51714525837, 330815.36309132184, 2.247461255023546, 91.12343223127826, 0.17212699679736815, 15.0833956786349, 14.972757376351383, 4.563680366683454, 13.656010721207792, 0.5370518570202893, 3.7387173359156574, 4.083582516166707, 0.5337698024248725, 12.081102335875753, 16.254121099911366], "base_sampler_halton": [0.05313470065815114, 39526.522004619226, 0.8917398679522033, 14.397195194898835, 0.0, 28.54887919616731, 95.00863388735908, 2.720260852291106, 1.568529005755524, 3817.305420396278, 3037.041404951838, 2438.638535172438, 6.0599988960457, 0.7756151204183547, 10.384034775256028, 12.984958685857494, 2.65445820393611, 21.224991223612605, 0.06917463900662568, 2.6908223459686598, 6.306229527285339, 1.8896710371272878, 3.848846366265107, 23.027563725186226], "weights_option_default": [0.6699833162594511, 4858.983093703771, 13.575313361038532, 20.615929060719242, 0.0, 314.32588235229133, 95.00863388735908, 7.623121487433538, 2.40985693994124, 8829.9538591471, 75722.58537541116, 2683.703847500958, 28.411660356917345, 0.46048487751008504, 23.76532915356332, 20.16251348556511, 1.4118850095554634, 22.517169808626214, 0.4559623215635096, 1.3074796822154253, 2.011681868741659, 10.719130303012593, 17.153972345540915, 15.81948309019006], "weights_option_equal": [0.6983733129352282, 53903.67207319866, 16.490693161011574, 19.983425781178084, 0.0, 1446.9386344210945, 98.3482403109842, 1.3914507176564086, 9.829561857450923, 175618.54116703727, 297974.9703969646, 2683.703847500958, 84.70468494483656, 0.26559027142197433, 11.095075704114704, 12.546429428472235, 4.124331898352289, 8.982949573227335, 0.3552769868031991, 5.125690097532708, 2.0431895662294055, 1.9396308505454316, 6.357120407299142, 16.044865034726843], "weights_option_1/2^lambda": [0.29537552250716453, 14961.853448878097, 24.071007496709008, 15.36361770066693, 0.0, 219.60735320690065, 95.00863388735908, 12.504251100709112, 10.055671647360219, 658.2473391082603, 158260.00841298988, 2683.703847500958, 41.970757571346695, 0.3473139627356672, 18.68616700712326, 12.985714196316431, 4.074463999656349, 14.549555170427439, 0.4559623215635096, 2.5857528075054406, 3.0145388205852823, 1.8648570790235144, 16.61377903494341, 4.6556683869958135], "local_restart_IPOP": [0.6699833162594511, 4858.983093703771, 13.575313361038532, 20.615929060719242, 0.0, 314.32588235229133, 95.00863388735908, 7.623121487433538, 2.40985693994124, 8829.9538591471, 75722.58537541116, 2683.703847500958, 28.411660356917345, 0.46048487751008504, 23.76532915356332, 20.16251348556511, 1.4118850095554634, 22.517169808626214, 0.4559623215635096, 1.3074796822154253, 2.011681868741659, 10.719130303012593, 17.153972345540915, 15.81948309019006], "local_restart_BIPOP": [0.6699833162594511, 4858.983093703771, 13.575313361038532, 20.615929060719242, 0.0, 314.32588235229133, 95.00863388735908, 7.623121487433538, 2.40985693994124, 8829.9538591471, 75722.58537541116, 2683.703847500958, 28.411660356917345, 0.46048487751008504, 23.76532915356332, 20.16251348556511, 1.4118850095554634, 22.517169808626214, 0.4559623215635096, 1.3074796822154253, 2.011681868741659, 10.719130303012593, 17.153972345540915, 15.81948309019006], "bound_correction_saturate": [0.6699833162594511, 4858.983093703771, 13.575313361038532, 20.615929060719242, 0.0, 33.09501100232642, 95.00863388735908, 0.025008654234555786, 48.30190325684145, 7345.169565828286, 39778.9260263472, 99.69985510848147, 81.24353869555779, 0.21972787749128886, 23.76532915356332, 12.874535394768513, 0.9011977159517143, 11.737632986991574, 0.6914037267515631, 1.3074796822154253, 0.8684924540993583, 0.49728904266780494, 7.630280738995454, 13.415576504682278], "bound_correction_unif_resample": [0.07835166888299767, 833.1172241710973, 19.627044667914628, 9.628926397266468, 5.160304518362757, 9.813141025564207, 98.3482403109842, 1.7357869403053625, 1.0497014040510548, 41678.76392402864, 168961.6744824513, 240.67153518558044, 3.835466543661913, 0.6842126813587452, 13.958153917707243, 3.678177569860251, 2.4855552649703743, 31.962561311838957, 0.10223176862010597, 3.162540017099194, 2.0966511126841403, 0.2943473719176201, 5.120439721241175, 12.236723816743432], "bound_correction_COTN": [0.16760938470242162, 4351.211417663171, 6.047980325222314, 21.912573748869775, 3.1244128771674013, 16.390734449771912, 98.3482403109842, 5.83832165250612, 0.14735284098644652, 190.06470116101258, 401.02023186453124, 2683.703847500958, 123.96634855859521, 0.7422687242316163, 4.997713381178798, 9.253215577323422, 3.996596680370051, 0.6749752706464678, 1.593872207016755, 2.732266723801698, 2.2807913444204497, 0.0009542510237096509, 5.825774943800214, 11.291346927128165], "bound_correction_toroidal": [1.4444217300032234, 668.4603295428242, 13.575313361038532, 20.615929060719242, 2.0223088241078173, 36.79824179639371, 95.00863388735908, 13.504258712369403, 55.92343205593169, 52508.86982339246, 7782.459127523065, 1.1032079131590031, 142.56590632579102, 1.8515935221861943, 23.76532915356332, 13.68135734438805, 3.160513609285335, 12.298476154812828, 0.15035979308807157, 1.3074796822154253, 4.6098062526230805, 2.216804022881296, 4.91679849370395, 7.427104342941419], "bound_correction_mirror": [1.1660440655024709, 1651.4439597423843, 8.374450285848416, 16.89173845810526, 5.8032753382871505, 33.55789312438171, 97.45871992360821, 7.7492720281054694, 1.0486600710191158, 25873.673262665754, 900.4477757873151, 1133.3973327215838, 70.13442419670197, 1.1138637242611467, 17.042022539322453, 3.9534263555903633, 1.2140101666427907, 4.101698783919703, 0.21716792040892763, 3.0087533050106425, 1.9277425466961398, 1.8126859185350808, 10.029522452683748, 14.76909640109017]} \ No newline at end of file diff --git a/tests/expected.py b/tests/expected.py deleted file mode 100644 index 48cd36b..0000000 --- a/tests/expected.py +++ /dev/null @@ -1,706 +0,0 @@ -"""Fixtures/Test data is placed in this file.""" - -BBOB_2D_PER_MODULE_20_ITER = { - "active_True": [ - 79.60870540043959, - -6.834740339081748, - -446.942231900092, - -453.1166279102467, - 12.91474629678331, - 2057.8823697805324, - 93.54091559010935, - 149.29454116123992, - 123.88066647543144, - 6731.6154970887155, - 102443.81430369585, - 643625.010286943, - 45.175034363167605, - -52.01531942754774, - 1017.3030673363197, - 71.43871188992895, - 3.146564668875037 , - -8.279403543275532, - -102.10176326857986, - -543.6999174336394, - 42.700744146686965, - -999.9997057338787, - 13.04988097465894, - 117.29640024093563, - ], - "base_sampler_gaussian": [ - 79.60197400323416, - 9075.81294241299, - -443.0425148340509, - -449.63233155751726, - 15.221583968074889, - 1938.1349061808248, - 93.54091559010935, - 149.27592202799931, - 123.88066647543144, - 7116.932319384926, - 335648.9894252748, - 1407321.3639251885, - 43.368771569040824, - -52.116212719435566, - 1017.7040292314994, - 72.00354123874057, - 7.140121147903702, - 34.24774851152607, - -102.24646124346252, - -543.698445859984, - 42.700744146686965, - -999.8961680110624, - 13.582863728801364, - 116.76955516252966, - ], - "base_sampler_halton": [ - 79.48077842021073, - 136.53129100639717, - -446.7506458965296, - -446.87082153002194, - 11.950884905405736, - 5329.481213692499, - 93.0361464944175, - 149.71900030510446, - 124.02881103802382, - 70984.29907315908, - 1122036.1339262475, - 323500.4088727226, - 40.771980667227886, - -51.48698849157431, - 1020.9436930982043, - 78.05502682158907, - -4.961694831220257, - 157.23783058587483, - -102.30221897973205, - -543.7781557151272, - 42.67339847137175, - -999.9963011818812, - 14.73597350603703, - 124.02200389471975, - ], - "base_sampler_sobol": [ - 79.58776048088133, - 3422.226350474581, - -445.62619936885244, - -452.131564688653, - 7.17122029401677, - 42.96515961182474, - 93.45715847183976, - 149.54498657058198, - 126.88596518033647, - 298.42477393412827, - 1365728.4147451057, - 48635.303416026385, - 48.505871726336075, - -51.74059898989086, - 1016.9939691575535, - 72.24266434815569, - -2.605200628010161, - 169.34805329384733, - -102.5486907676662, - -543.3053885969988, - 42.781850042137705, - -999.9962323826467, - 12.046411510472002, - 107.7143392718549, - ], - "bound_correction_COTN": [ - 79.60197400323416, - 9075.81294241299, - -443.0425148340509, - -449.63233155751726, - 15.221583968074889, - 1938.1349061808248, - 93.54091559010935, - 149.27592202799931, - 123.88066647543144, - 7116.932319384926, - 335648.9894252748, - 1407321.3639251885, - 43.368771569040824, - -52.116212719435566, - 1017.7040292314994, - 72.00354123874057, - 7.140121147903702, - 34.24774851152607, - -102.24646124346252, - -543.698445859984, - 42.700744146686965, - -999.8961680110624, - 13.582863728801364, - 116.76955516252966, - ], - "bound_correction_mirror": [ - 79.60197400323416, - 9075.81294241299, - -443.0425148340509, - -449.63233155751726, - 15.221583968074889, - 1938.1349061808248, - 93.54091559010935, - 149.27592202799931, - 123.88066647543144, - 7116.932319384926, - 335648.9894252748, - 1407321.3639251885, - 43.368771569040824, - -52.116212719435566, - 1017.7040292314994, - 72.00354123874057, - 7.140121147903702, - 34.24774851152607, - -102.24646124346252, - -543.698445859984, - 42.700744146686965, - -999.8961680110624, - 13.582863728801364, - 116.76955516252966, - ], - "bound_correction_saturate": [ - 79.60197400323416, - 9075.81294241299, - -443.0425148340509, - -449.63233155751726, - 15.221583968074889, - 1938.1349061808248, - 93.54091559010935, - 149.27592202799931, - 123.88066647543144, - 7116.932319384926, - 335648.9894252748, - 1407321.3639251885, - 43.368771569040824, - -52.116212719435566, - 1017.7040292314994, - 72.00354123874057, - 7.140121147903702, - 34.24774851152607, - -102.24646124346252, - -543.698445859984, - 42.700744146686965, - -999.8961680110624, - 13.582863728801364, - 116.76955516252966, - ], - "bound_correction_toroidal": [ - 79.60197400323416, - 9075.81294241299, - -443.0425148340509, - -449.63233155751726, - 15.221583968074889, - 1938.1349061808248, - 93.54091559010935, - 149.27592202799931, - 123.88066647543144, - 7116.932319384926, - 335648.9894252748, - 1407321.3639251885, - 43.368771569040824, - -52.116212719435566, - 1017.7040292314994, - 72.00354123874057, - 7.140121147903702, - 34.24774851152607, - -102.24646124346252, - -543.698445859984, - 42.700744146686965, - -999.8961680110624, - 13.582863728801364, - 116.76955516252966, - ], - "bound_correction_unif_resample": [ - 79.60197400323416, - 9075.81294241299, - -443.0425148340509, - -449.63233155751726, - 15.221583968074889, - 1938.1349061808248, - 93.54091559010935, - 149.27592202799931, - 123.88066647543144, - 7116.932319384926, - 335648.9894252748, - 1407321.3639251885, - 43.368771569040824, - -52.116212719435566, - 1017.7040292314994, - 72.00354123874057, - 7.140121147903702, - 34.24774851152607, - -102.24646124346252, - -543.698445859984, - 42.700744146686965, - -999.8961680110624, - 13.582863728801364, - 116.76955516252966, - ], - "elitist_True": [ - 79.58359771151284, - 3722.3761689261983, - -444.6996743229732, - -449.63233155751726, - 11.403773075168843, - 1938.1349061808248, - 93.54091559010935, - 149.72424316023083, - 123.88066647543144, - 7116.932319384926, - 335648.9894252748, - 338749.1203340543, - 43.368771569040824, - -51.8908644736279, - 1017.567313102047, - 82.16884108988424, - 5.9547855849801365, - 1.4811701439965574, - -102.1160727430898, - -543.698445859984, - 42.60165481703265, - -999.9991133679798, - 9.843395190328877, - 111.69367145373013, - ], - "local_restart_BIPOP": [ - 79.60197400323416, - 9075.81294241299, - -443.0425148340509, - -449.63233155751726, - 15.221583968074889, - 1938.1349061808248, - 93.54091559010935, - 149.27592202799931, - 123.88066647543144, - 7116.932319384926, - 335648.9894252748, - 1407321.3639251885, - 43.368771569040824, - -52.116212719435566, - 1017.7040292314994, - 72.00354123874057, - 7.140121147903702, - 34.24774851152607, - -102.24646124346252, - -543.698445859984, - 42.700744146686965, - -999.8961680110624, - 13.582863728801364, - 116.76955516252966, - ], - "local_restart_IPOP": [ - 79.60197400323416, - 9075.81294241299, - -443.0425148340509, - -449.63233155751726, - 15.221583968074889, - 1938.1349061808248, - 93.54091559010935, - 149.27592202799931, - 123.88066647543144, - 7116.932319384926, - 335648.9894252748, - 1407321.3639251885, - 43.368771569040824, - -52.116212719435566, - 1017.7040292314994, - 72.00354123874057, - 7.140121147903702, - 34.24774851152607, - -102.24646124346252, - -543.698445859984, - 42.700744146686965, - -999.8961680110624, - 13.582863728801364, - 116.76955516252966, - ], - "mirrored_mirrored pairwise": [ - 79.49271732413378, - -3.9902436014381237, - -449.9527094090616, - -453.43140996395596, - -5.716753396809776, - 38.656680881133425, - 93.54091559010935, - 149.66006205975523, - 124.3073642920228, - 496.9430991621704, - 5774.525239039822, - 10810.490466468276, - 65.01376333683325, - -52.14160901603763, - 1012.9356025505306, - 79.37035747661811, - -0.08086837288995241, - -7.360624274953013, - -102.5420591790735, - -545.1782424507006, - 42.700744146686965, - -999.9999959402685, - 11.36066981667674, - 120.60256772763952, - ], - "mirrored_mirrored": [ - 79.49271732413378, - -3.9902436014381237, - -449.43912237059925, - -454.76923415255794, - -5.716753396809776, - 38.656680881133425, - 92.96068633887359, - 149.4276007295378, - 124.29995348854516, - 496.9430991621704, - 5774.525239039822, - 10810.490466468276, - 65.01376333683325, - -52.14160901603763, - 1016.7839906734483, - 77.37715351910123, - -0.08317538023811721, - 71.41166512690214, - -101.99373657488567, - -545.1782424507006, - 42.700744146686965, - -999.5409569330642, - 16.018941166090844, - 118.76164430789493, - ], - "orthogonal_True": [ - 80.07173193213463, - -25.0810474775418, - -448.44029337005736, - -450.2217227339582, - 17.131150531337912, - 104768.75359048153, - 92.94000013395934, - 149.41567464975682, - 125.54323736579038, - 1949380.8182024239, - 7892515.3458270915, - 1608304.8817702506, - 75.99337230697218, - -50.85961901646829, - 1013.0164228422623, - 76.63491974976154, - 15.945556157304662, - 218.11413710292456, - -102.47909541110062, - -543.8195887825041, - 42.93989939186737, - -970.8073891259909, - 13.673346703024826, - 119.86000340058254, - ], - "sequential_True": [ - 83.26223403453476, - 117512.71823407112, - -443.0425148340509, - -449.63233155751726, - 23.133202262711286, - 10389.813539761111, - 97.60995333434538, - 149.3343488478487, - 127.59393177390666, - 7694330.796085853, - 17017375.25140306, - 4597655.93725784, - 76.81564795511831, - -47.55613893161405, - 1025.8209383489839, - 77.72987263145716, - 7.988026029647063, - 73.98356807373503, - -100.19493286681244, - -544.9528467594567, - 42.700744146686965, - -999.39715508158, - 18.64162794684728, - 121.36057695184782, - ], - "step_size_adaptation_csa": [ - 79.60197400323416, - 9075.81294241299, - -443.0425148340509, - -449.63233155751726, - 15.221583968074889, - 1938.1349061808248, - 93.54091559010935, - 149.27592202799931, - 123.88066647543144, - 7116.932319384926, - 335648.9894252748, - 1407321.3639251885, - 43.368771569040824, - -52.116212719435566, - 1017.7040292314994, - 72.00354123874057, - 7.140121147903702, - 34.24774851152607, - -102.24646124346252, - -543.698445859984, - 42.700744146686965, - -999.8961680110624, - 13.582863728801364, - 116.76955516252966, - ], - "step_size_adaptation_lp-xnes": [ - 79.60059107122254, - 1156.2973035672185, - -444.90820695481506, - -448.94358552780955, - -36.4848051463744, - 8358.025517299127, - 92.9460091559011, - 149.7493269470448, - 124.04952799814593, - 2621.0381521404584, - 1495208.493849278, - 5626.592106895178, - 62.31859253339392, - -52.32649247097711, - 1010.1744131780363, - 72.09734755535143, - -15.912959021956848, - 32.30960255054117, - -102.34645248406176, - -544.3080167220543, - 42.749007493000086, - -999.7029654400319, - 22.669115494695657, - 113.67506020056967, - ], - "step_size_adaptation_m-xnes": [ - 79.55906633738796, - 4386.41620419817, - -440.14992398127674, - -460.98138016361116, - 16.40228675582609, - 41.770782199901596, - 93.54091559010935, - 149.3001510889701, - 123.88066647543144, - 18684.968308009655, - 128482.83441910103, - 1281857.3005289235, - 44.362127255980496, - -50.98280710710338, - 1017.7040292314994, - 71.81002578700317, - -14.500528708427396, - -6.233462492224653, - -102.10176326857986, - -544.1923846845665, - 42.700744146686965, - -999.9963799088132, - 9.561553063179554, - 112.87464428268412, - ], - "step_size_adaptation_msr": [ - 79.54244003306202, - -118.26581009548049, - -440.14992398127674, - -455.4835960403074, - 18.736041419360657, - 3605.4061633764236, - 93.54091559010935, - 149.26601857625818, - 123.88066647543144, - -21.626275737512415, - 58043.62661894809, - 2620394.1433755127, - 44.362127255980496, - -51.667675482694605, - 1017.7040292314994, - 72.26712789062267, - -7.438752386989677, - 15.992223171740047, - -102.10176326857986, - -543.8534185721645, - 42.700744146686965, - -999.9068914798465, - 9.561553063179554, - 112.57452019629068, - ], - "step_size_adaptation_psr": [ - 79.52102181383879, - -43.91160560551333, - -441.11726502191607, - -456.56128813648616, - 17.849839938781177, - 3694.7014049597274, - 93.54091559010935, - 149.26690941710297, - 123.88066647543144, - 1913.6658308753742, - 148364.03186841556, - 2676492.5753466967, - 44.362127255980496, - -51.62579424252932, - 1017.7040292314994, - 72.30933289942361, - -14.511086097928466, - 43.86640712845018, - -102.10176326857986, - -544.024382872537, - 42.700744146686965, - -999.945912328108, - 9.561553063179554, - 112.87464428268412, - ], - "step_size_adaptation_tpa": [ - 79.55308020971381, - -207.50259603890402, - -440.14992398127674, - -458.57288088617247, - 5.4741183447131725, - 6560.528235059466, - 93.54091559010935, - 149.65559838973334, - 123.87243680449797, - 233.45934327394536, - 1016974.9297563785, - 23982.268064532716, - 79.31957554796386, - -51.0613012456259, - 1017.7040292314994, - 74.72824129498854, - 2.558947945699611, - 83.81239421766517, - -102.15624729506743, - -543.8204123260658, - 42.65562681704674, - -999.8888868157063, - 15.715875189059393, - 113.50420157724996, - ], - "step_size_adaptation_xnes": [ - 79.52868923287008, - 177.2921188004035, - -446.3366142028796, - -457.76830870934236, - 7.460165563308379, - 458.5247153830321, - 93.54091559010935, - 149.45732280899628, - 123.88066647543144, - 326.56650297189964, - 86.90556172988374, - 267979.67340446153, - 77.00389803635447, - -52.16277774524084, - 1017.5812992339265, - 74.05900952761623, - -1.0401150483872836, - -15.049763678473477, - -102.19153553838316, - -544.0421116229078, - 42.700744146686965, - -999.7036847182642, - 16.574926415489365, - 117.94855761761136, - ], - "threshold_convergence_True": [ - 79.5493946792015, - -205.8874080989055, - -440.3860520213715, - -454.59404450938604, - 11.02595182176956, - 747.5617626591987, - 93.54091559010935, - 149.20184452013788, - 125.29702895733809, - 9842.867547245534, - 25856.991912239057, - 15020.589468025295, - 43.368771569040824, - -52.317831132167846, - 1025.8209383489839, - 75.65775413626635, - -15.694063684846274, - -6.053852018150245, - -101.94712477091193, - -544.8856238640917, - 42.68657509138354, - -999.9820990897024, - 11.66582534228891, - 117.58369741868964, - ], - "weights_option_1/2^lambda": [ - 79.57458524535643, - 6277.488135347779, - -444.31128872130535, - -458.9430906155403, - 17.70768251894154, - 2266.7358410247793, - 93.54091559010935, - 149.48997026626708, - 123.88066647543144, - 3477.138762872567, - 3086.6572842078576, - 2758225.2627536957, - 59.257202521725354, - -51.89091502074251, - 1017.7040292314994, - 72.22771406842722, - 3.1834296917116482, - 12.043970710043297, - -102.10653687894265, - -543.698445859984, - 42.700744146686965, - -999.9818092494717, - 13.787357550858435, - 111.45022572520682, - ], - "weights_option_default": [ - 79.60197400323416, - 9075.81294241299, - -443.0425148340509, - -449.63233155751726, - 15.221583968074889, - 1938.1349061808248, - 93.54091559010935, - 149.27592202799931, - 123.88066647543144, - 7116.932319384926, - 335648.9894252748, - 1407321.3639251885, - 43.368771569040824, - -52.116212719435566, - 1017.7040292314994, - 72.00354123874057, - 7.140121147903702, - 34.24774851152607, - -102.24646124346252, - -543.698445859984, - 42.700744146686965, - -999.8961680110624, - 13.582863728801364, - 116.76955516252966, - ], - "weights_option_equal": [ - 79.4841795554343, - -206.93295112420896, - -446.8074233371284, - -445.1350192190321, - 21.06207513561158, - 46.678137440267065, - 93.54091559010935, - 149.56940870119797, - 123.86194759511636, - 29972.293198196898, - 361029.7991199966, - 7697201.177123937, - 89.58749452760148, - -51.4644595745646, - 1017.7040292314994, - 77.27755720351972, - -10.054973805866888, - 12.496064113477054, - -102.10176326857986, - -543.7171538434422, - 42.677106643653936, - -999.99941581072, - 14.937956237333932, - 115.99850936925196, - ], -} \ No newline at end of file diff --git a/tests/test_modularcmaes.py b/tests/test_modularcmaes.py index 77c5883..ebdc387 100644 --- a/tests/test_modularcmaes.py +++ b/tests/test_modularcmaes.py @@ -10,8 +10,9 @@ from modcma import parameters, utils, modularcmaes import ioh +import json -from .expected import BBOB_2D_PER_MODULE_20_ITER +# from .expected import BBOB_2D_PER_MODULE_20_ITER class TestModularCMAESMeta(type): @@ -56,6 +57,11 @@ class TestModularCMAES(unittest.TestCase, metaclass=TestModularCMAESMeta): _dim = 2 _budget = int(1e1 * _dim) + + def __init__(self, args, **kwargs): + with open("tests/expected.json", "r") as f: + self.BBOB_2D_PER_MODULE_20_ITER = json.load(f) + super().__init__(args, **kwargs) def run_module(self, module, value): """Test a single run of the mechanism with a given module active.""" @@ -73,8 +79,8 @@ def run_bbob_function(self, module, value, fid): ) self.c = modularcmaes.ModularCMAES(f, parameters=self.p).run() self.assertAlmostEqual( - self.c.parameters.fopt, - BBOB_2D_PER_MODULE_20_ITER[f"{module}_{value}"][fid - 1], + f.state.current_best_internal.y, + self.BBOB_2D_PER_MODULE_20_ITER[f"{module}_{value}"][fid - 1], ) def test_select_raises(self): From c8dd8a82af1b1c8666bb21a23372be918d599f23 Mon Sep 17 00:00:00 2001 From: DVermetten Date: Fri, 1 Jul 2022 11:42:52 +0200 Subject: [PATCH 05/11] fix error with requirements --- requirements.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index bd8ae0d..0ab854f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,4 @@ ioh>=0.3.2.8.3 numba>=0.52.0 numpy>=1.18.5 -scipy>=1.4.1 -json \ No newline at end of file +scipy>=1.4.1 \ No newline at end of file From ecc790c34c90162b025c9472322298528f8ca78d Mon Sep 17 00:00:00 2001 From: DVermetten Date: Fri, 1 Jul 2022 11:53:07 +0200 Subject: [PATCH 06/11] Update evaluate_bbob --- modcma/modularcmaes.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/modcma/modularcmaes.py b/modcma/modularcmaes.py index 71ad2ef..2922408 100644 --- a/modcma/modularcmaes.py +++ b/modcma/modularcmaes.py @@ -457,19 +457,19 @@ def evaluate_bbob( """ # This speeds up the import, this import is quite slow, so import it lazy here # pylint: disable=import-outside-toplevel - from IOHexperimenter import IOH_function, IOH_logger + import ioh evals, fopts = np.array([]), np.array([]) if seed: np.random.seed(seed) - fitness_func = IOH_function( - fid, dim, instance, target_precision=target_precision, suite="BBOB" + fitness_func = ioh.get_problem( + fid, dimension=dim, instance=instance, suite="BBOB" ) if logging: data_location = data_folder if os.path.isdir(data_folder) else os.getcwd() - logger = IOH_logger(data_location, f"{label}F{fid}_{dim}D") - fitness_func.add_logger(logger) + logger = ioh.logger.Analyzer(data_location, f"{label}F{fid}_{dim}D") + fitness_func.attach_logger(logger) print( f"Optimizing function {fid} in {dim}D for target " @@ -479,11 +479,11 @@ def evaluate_bbob( for idx in range(iterations): if idx > 0: fitness_func.reset() - target = fitness_func.get_target() + target = fitness_func.objective.y + target_precision optimizer = ModularCMAES(fitness_func, dim, target=target, **kwargs).run() - evals = np.append(evals, fitness_func.evaluations) - fopts = np.append(fopts, fitness_func.best_so_far_precision) + evals = np.append(evals, fitness_func.state.evaluations) + fopts = np.append(fopts, fitness_func.state.current_best_internal.y) result_string = ( "FCE:\t{:10.8f}\t{:10.4f}\n" From 81099ddf49f8721890c772503e1a494a3dc476cb Mon Sep 17 00:00:00 2001 From: DVermetten Date: Fri, 1 Jul 2022 13:46:23 +0200 Subject: [PATCH 07/11] Update ioh package referene in ask-tell test --- tests/test_asktellcmaes.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/test_asktellcmaes.py b/tests/test_asktellcmaes.py index a2138bd..3bc8a87 100644 --- a/tests/test_asktellcmaes.py +++ b/tests/test_asktellcmaes.py @@ -4,8 +4,7 @@ import numpy as np from modcma import asktellcmaes -from IOHexperimenter import IOH_function - +import ioh class AskTellCMAESTestCase(unittest.TestCase): """Test case for ask-tell interface of Modular CMA-ES.""" @@ -14,7 +13,7 @@ def setUp(self): """Test setup method.""" self.d = 5 self.fid = 1 - self.func = IOH_function(1, 5, 1, suite="BBOB") + self.func = ioh.get_problem(1, dimension=5, instance=1, suite="BBOB") self.opt = asktellcmaes.AskTellCMAES(self.d, target=79.48) def test_sequential_selection_disabled(self): From 6ba30a984ed7c1d75a4a47d1e9ed0b7e70336b13 Mon Sep 17 00:00:00 2001 From: DVermetten Date: Fri, 1 Jul 2022 13:52:10 +0200 Subject: [PATCH 08/11] Fix coding style issues --- modcma/parameters.py | 2 +- tests/create_expected.py | 9 ++++++--- tests/test_asktellcmaes.py | 2 +- tests/test_modularcmaes.py | 1 + 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/modcma/parameters.py b/modcma/parameters.py index 22b3746..0c7f82c 100644 --- a/modcma/parameters.py +++ b/modcma/parameters.py @@ -843,7 +843,7 @@ def update(self, parameters: dict, reset_default_modules=False): self.init_local_restart_parameters() def update_popsize(self, lambda_new): - """Manually control the population size""" + """Manually control the population size.""" if self.local_restart is not None: warnings.warn("Modification of population size is disabled when local restart startegies are used") return diff --git a/tests/create_expected.py b/tests/create_expected.py index 6417582..cc5d471 100644 --- a/tests/create_expected.py +++ b/tests/create_expected.py @@ -1,10 +1,12 @@ +"""Script to re-initialize the expected function values in expected.json.""" + from modcma import ModularCMAES, parameters, utils import ioh import numpy as np import json def run_bbob_function(module, value, fid): - """Creates the output BBOB_2D_PER_MODULE_20_ITER.""" + """Runs the specified version of ModularCMAES on the bbob-function and returns its output.""" np.random.seed(42) dim = 2 budget = 20 @@ -13,10 +15,11 @@ def run_bbob_function(module, value, fid): p = parameters.Parameters( dim, budget=budget, **{module: value} ) - c = ModularCMAES(f, parameters=p).run() + ModularCMAES(f, parameters=p).run() return f.state.current_best_internal.y def create_expected_dict(): + """Creates the dictionary containing the expected final function values.""" BBOB_2D_PER_MODULE_20_ITER = dict() for module in parameters.Parameters.__modules__: m = getattr(parameters.Parameters, module) @@ -30,7 +33,7 @@ def create_expected_dict(): BBOB_2D_PER_MODULE_20_ITER[f"{module}_{True}"] = np.zeros(24) for fid in range(1, 25): BBOB_2D_PER_MODULE_20_ITER[f"{module}_{True}"][fid - 1] = run_bbob_function(module, True, fid) - + return BBOB_2D_PER_MODULE_20_ITER if __name__ == "__main__": BBOB_2D_PER_MODULE_20_ITER = create_expected_dict() diff --git a/tests/test_asktellcmaes.py b/tests/test_asktellcmaes.py index 3bc8a87..53de965 100644 --- a/tests/test_asktellcmaes.py +++ b/tests/test_asktellcmaes.py @@ -13,7 +13,7 @@ def setUp(self): """Test setup method.""" self.d = 5 self.fid = 1 - self.func = ioh.get_problem(1, dimension=5, instance=1, suite="BBOB") + self.func = ioh.get_problem(1, dimension=5, instance=1) self.opt = asktellcmaes.AskTellCMAES(self.d, target=79.48) def test_sequential_selection_disabled(self): diff --git a/tests/test_modularcmaes.py b/tests/test_modularcmaes.py index ebdc387..7c1044d 100644 --- a/tests/test_modularcmaes.py +++ b/tests/test_modularcmaes.py @@ -59,6 +59,7 @@ class TestModularCMAES(unittest.TestCase, metaclass=TestModularCMAESMeta): _budget = int(1e1 * _dim) def __init__(self, args, **kwargs): + """Initializes the expected function value dictionary.""" with open("tests/expected.json", "r") as f: self.BBOB_2D_PER_MODULE_20_ITER = json.load(f) super().__init__(args, **kwargs) From 0e23007251a3e0549b5242018b302101aa590b0f Mon Sep 17 00:00:00 2001 From: DVermetten Date: Fri, 1 Jul 2022 14:04:07 +0200 Subject: [PATCH 09/11] Fix errors found by CI --- modcma/modularcmaes.py | 2 +- tests/create_expected.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/modcma/modularcmaes.py b/modcma/modularcmaes.py index 2922408..09afec6 100644 --- a/modcma/modularcmaes.py +++ b/modcma/modularcmaes.py @@ -463,7 +463,7 @@ def evaluate_bbob( if seed: np.random.seed(seed) fitness_func = ioh.get_problem( - fid, dimension=dim, instance=instance, suite="BBOB" + fid, dimension=dim, instance=instance ) if logging: diff --git a/tests/create_expected.py b/tests/create_expected.py index cc5d471..f086456 100644 --- a/tests/create_expected.py +++ b/tests/create_expected.py @@ -17,7 +17,7 @@ def run_bbob_function(module, value, fid): ) ModularCMAES(f, parameters=p).run() return f.state.current_best_internal.y - + def create_expected_dict(): """Creates the dictionary containing the expected final function values.""" BBOB_2D_PER_MODULE_20_ITER = dict() From 83e20914c13d9e3da9a2780cf686abbe25238e09 Mon Sep 17 00:00:00 2001 From: DVermetten Date: Fri, 1 Jul 2022 14:12:16 +0200 Subject: [PATCH 10/11] Fix evaluate bbob arguments --- modcma/modularcmaes.py | 2 +- tests/test_modularcmaes.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/modcma/modularcmaes.py b/modcma/modularcmaes.py index 09afec6..f5baf3b 100644 --- a/modcma/modularcmaes.py +++ b/modcma/modularcmaes.py @@ -468,7 +468,7 @@ def evaluate_bbob( if logging: data_location = data_folder if os.path.isdir(data_folder) else os.getcwd() - logger = ioh.logger.Analyzer(data_location, f"{label}F{fid}_{dim}D") + logger = ioh.logger.Analyzer(root=data_location, folder_name=f"{label}F{fid}_{dim}D") fitness_func.attach_logger(logger) print( diff --git a/tests/test_modularcmaes.py b/tests/test_modularcmaes.py index 7c1044d..2b18aba 100644 --- a/tests/test_modularcmaes.py +++ b/tests/test_modularcmaes.py @@ -213,9 +213,9 @@ def test_evaluate_bbob(self, mock_std): if not os.path.isdir(data_folder): os.mkdir(data_folder) self.assertTrue(os.path.isdir(data_folder)) - modularcmaes.evaluate_bbob(1, 1, 1, logging=True, data_folder=data_folder) + modularcmaes.evaluate_bbob(1, 2, 1, logging=True, data_folder=data_folder) shutil.rmtree(data_folder) - modularcmaes.evaluate_bbob(1, 1, 2) + modularcmaes.evaluate_bbob(1, 2, 2) From 57809854e2f1d2e20581b323fabe183c06d9b4f1 Mon Sep 17 00:00:00 2001 From: DVermetten Date: Mon, 4 Jul 2022 15:46:40 +0200 Subject: [PATCH 11/11] Make population update more generally functional with other components --- modcma/parameters.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/modcma/parameters.py b/modcma/parameters.py index 0c7f82c..d203496 100644 --- a/modcma/parameters.py +++ b/modcma/parameters.py @@ -384,8 +384,7 @@ def init_local_restart_parameters(self) -> None: TODO: check if we can move this to separate object. """ - if len(self.restarts) == 0: - self.restarts.append(self.t) + self.max_iter = 100 + 50 * (self.d + 3) ** 2 / np.sqrt(self.lambda_) self.nbin = 10 + int(np.ceil(30 * self.d / self.lambda_)) self.n_stagnation = min(int(120 + (30 * self.d / self.lambda_)), 20000) @@ -628,6 +627,10 @@ def adapt_evolution_paths(self) -> None: def perform_local_restart(self) -> None: """Method performing local restart, if a restart strategy is specified.""" if self.local_restart: + + if len(self.restarts) == 0: + self.restarts.append(self.t) + if self.local_restart == "IPOP" and self.mu > 512: self.mu *= self.ipop_factor self.lambda_ *= self.ipop_factor @@ -743,6 +746,7 @@ def save(self, filename: str = "parameters.pkl") -> None: def record_statistics(self) -> None: """Method for recording metadata.""" + # if self.local_restart or self.compute_termination_criteria: self.flat_fitnesses.append( self.population.f[0] == self.population.f[self.flat_fitness_index] ) @@ -849,7 +853,9 @@ def update_popsize(self, lambda_new): return self.lambda_ = lambda_new self.mu = lambda_new//2 + self.init_selection_parameters() self.init_adaptation_parameters() + self.init_local_restart_parameters() class BIPOPParameters(AnnotatedStruct):