Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fuzzing for sep-CMA-ES #105

Merged
merged 1 commit into from
Apr 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions cmaes/_sepcma.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@


_EPS = 1e-8
_MEAN_MAX = 1e32
_SIGMA_MAX = 1e32


Expand Down Expand Up @@ -73,6 +74,10 @@ def __init__(
):
assert sigma > 0, "sigma must be non-zero positive value"

assert np.all(
np.abs(mean) < _MEAN_MAX
), f"Abs of all elements of mean vector must be less than {_MEAN_MAX}"

n_dim = len(mean)
assert n_dim > 1, "The dimension of mean must be larger than 1"

Expand Down Expand Up @@ -233,8 +238,12 @@ def _repair_infeasible_params(self, param: np.ndarray) -> np.ndarray:

def tell(self, solutions: List[Tuple[np.ndarray, float]]) -> None:
"""Tell evaluation values"""
if len(solutions) != self._popsize:
raise ValueError("Must tell popsize-length solutions.")

assert len(solutions) == self._popsize, "Must tell popsize-length solutions."
for s in solutions:
assert np.all(
np.abs(s[0]) < _MEAN_MAX
), f"Abs of all param values must be less than {_MEAN_MAX} to avoid overflow errors"

self._g += 1
solutions.sort(key=lambda s: s[1])
Expand Down
30 changes: 29 additions & 1 deletion tests/test_fuzzing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import unittest
from hypothesis import given, strategies as st

from cmaes import CMA
from cmaes import CMA, SepCMA


class TestFuzzing(unittest.TestCase):
Expand Down Expand Up @@ -33,3 +33,31 @@ def test_cma_tell(self, data):
except AssertionError:
return
optimizer.ask()

@given(
data=st.data(),
)
def test_sepcma_tell(self, data):
dim = data.draw(st.integers(min_value=2, max_value=100))
mean = data.draw(npst.arrays(dtype=float, shape=dim))
sigma = data.draw(st.floats(min_value=1e-16))
n_iterations = data.draw(st.integers(min_value=1))
try:
optimizer = SepCMA(mean, sigma)
except AssertionError:
return
popsize = optimizer.population_size
for _ in range(n_iterations):
tell_solutions = data.draw(
st.lists(
st.tuples(npst.arrays(dtype=float, shape=dim), st.floats()),
min_size=popsize,
max_size=popsize,
)
)
optimizer.ask()
try:
optimizer.tell(tell_solutions)
except AssertionError:
return
optimizer.ask()