Skip to content

Commit

Permalink
Merge pull request #18 from IOHprofiler/cleaning
Browse files Browse the repository at this point in the history
Cleaning
  • Loading branch information
jacobdenobel authored Jan 13, 2021
2 parents 75a61ce + 83576f6 commit 6b5685d
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ sphinx:
python:
version: 3.8
install:
- requirements: requirements.txt
- requirements: docs/requirements.txt
14 changes: 9 additions & 5 deletions modcma/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,19 +386,23 @@ def init_adaptation_parameters(self) -> None:
Examples are recombination weights and learning rates for the covariance
matrix adapation.
TODO: clean the initlialization of these weights, this can be wrong in some cases
when mu != .5
"""

if self.weights_option == "equal":
ws = np.ones(self.lambda_) / self.lambda_
self.weights = np.append(ws, ws[::-1] * -1)
self.weights = np.append(ws[:self.mu], ws[self.mu::-1] * -1)

if self.lambda_ % 2 != 0:
self.weights = np.append([1 / self.lambda_], self.weights)
self.weights = np.append([1 / self.lambda_], self.weights[:-1])
elif self.weights_option == "1/2^lambda":
ws = 1 / 2 ** np.arange(1, self.lambda_ + 1) + (
(1 / (2 ** self.lambda_)) / self.lambda_
)
self.weights = np.append(ws, ws[::-1] * -1)
self.weights = np.append(ws[:self.mu], ws[self.mu::-1] * -1)
if self.lambda_ % 2 != 0:
self.weights = np.append([1 / self.lambda_ ** 2], self.weights)
self.weights = np.append([1 / self.lambda_ ** 2], self.weights[:-1])
else:
self.weights = np.log((self.lambda_ + 1) / 2) - np.log(
np.arange(1, self.lambda_ + 1)
Expand Down Expand Up @@ -831,7 +835,7 @@ def adapt(self, used_budget: int) -> None:
self.lambda_large *= 2
else:
self.budget_small -= used_previous_iteration

self.lambda_small = np.floor(
self.lambda_init
* (0.5 * self.lambda_large / self.lambda_init) ** (np.random.uniform() ** 2)
Expand Down
8 changes: 7 additions & 1 deletion tests/test_modularcmaes.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ def test_local_restart(self):
class TestModularCMAESSingle(unittest.TestCase):
"""Test case for ModularCMAES Object, holds custom tests."""

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_str_repr(self):
"""Test the output of repr and str."""
c = modularcmaes.ModularCMAES(sum, 5)
Expand Down Expand Up @@ -188,7 +194,7 @@ def test_evaluate_bbob(self, mock_std):
self.assertTrue(os.path.isdir(data_folder))
modularcmaes.evaluate_bbob(1, 1, 1, logging=True, data_folder=data_folder)
shutil.rmtree(data_folder)
modularcmaes.evaluate_bbob(1, 1, 1)
modularcmaes.evaluate_bbob(1, 1, 2)



Expand Down
19 changes: 18 additions & 1 deletion tests/test_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import numpy as np

from modcma.parameters import Parameters
from modcma.parameters import Parameters, BIPOPParameters
from modcma.utils import AnyOf
from modcma.population import Population

Expand Down Expand Up @@ -177,6 +177,23 @@ def test_save_load(self):
self.p.load(tmpfile)
os.remove(tmpfile)

def test_fix_lambda_even(self):
self.p.lambda_ = 11
self.p.mirrored = 'mirrored pairwise'
self.assertEqual(self.p.lambda_, 11)
self.p.init_selection_parameters()
self.assertEqual(self.p.lambda_, 12)

for weights_option in ("equal", "1/2^lambda"):
self.p.weights_option = weights_option
self.p.lambda_ = 11
self.p.mu = 5
self.p.init_adaptation_parameters()
self.assertEqual(len(self.p.weights), 11)

b = BIPOPParameters(7, 20, .5)
b.adapt(11)
self.assertEqual(b.lambda_small, 8)

if __name__ == "__main__":
unittest.main()
1 change: 1 addition & 0 deletions tests/test_population.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def test_d(self):
def test_repr(self):
"""Test representation."""
self.assertEqual(type(repr(self.pop)), str)
self.assertEqual(type(str(self.pop)), str)


if __name__ == "__main__":
Expand Down
1 change: 1 addition & 0 deletions tests/test_sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def test_halton(self):
"""Test halton sampling."""
sampler = sampling.halton_sampling(self._dim)
self.is_sampler(sampler)
self.assertEqual(sampling.Halton.vectorized_next(10, 2), .3125)

def test_orthogonal(self):
"""Test orthogonal sampling."""
Expand Down

0 comments on commit 6b5685d

Please sign in to comment.