def test_n_generations(self): """Test n iterations of the mechanism.""" c = modularcmaes.ModularCMAES(sum, 5, n_generations=5) self.assertEqual(1, len(c.break_conditions)) for _ in range(5): c.step() self.assertTrue(any(c.break_conditions)) c = modularcmaes.ModularCMAES(sum, 5) self.assertEqual(2, len(c.break_conditions))
def test_select_raises(self): """Test whether errors are produced correctly.""" c = modularcmaes.ModularCMAES(sum, 5, mirrored="mirrored pairwise") c.mutate() c.parameters.population = c.parameters.population[:3] with self.assertRaises(ValueError): c.select()
def test_tpa_threshold_cov_sequential(self): c = modularcmaes.ModularCMAES(sum, 2, threshold_convergence=True, sequential=True, step_size_adaptation='tpa', budget=10).run() self.assertLess(c.parameters.fopt, 0.)
def test_local_restart(self): """Test a single iteration of the mechanism with a given local restart active.""" for lr in filter(None, parameters.Parameters.local_restart.options): c = modularcmaes.ModularCMAES(sum, self._dim, local_restart=lr) for _ in range(10): c.step() c.parameters.max_iter = 5 c.step()
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) self.p = parameters.Parameters(self._dim, budget=self._budget, **{module: value}) 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], )
def run_module(self, module, value): """Test a single run of the mechanism with a given module active.""" self.p = parameters.Parameters(self._dim, budget=self._budget, **{module: value}) self.c = modularcmaes.ModularCMAES(sum, parameters=self.p).run()
def test_str_repr(self): """Test the output of repr and str.""" c = modularcmaes.ModularCMAES(sum, 5) self.assertIsInstance(str(c), str) self.assertIsInstance(repr(c), str)
def test_best_so_far_storage(self): """Test storage of best so far individual.""" c = modularcmaes.ModularCMAES(sum, 5) c.step() self.assertEqual(len(c.parameters.xopt), 5) self.assertEqual(sum(c.parameters.xopt), c.parameters.fopt)