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

Refactor constants definition #85

Merged
merged 1 commit into from
Nov 26, 2020
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
14 changes: 7 additions & 7 deletions cmaes/_cma.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import math
import sys
import numpy as np

from typing import Any
Expand All @@ -9,6 +8,10 @@
from typing import Tuple


_EPS = 1e-8
_FLT_MAX = 1e32


class CMA:
"""CMA-ES stochastic optimizer class with ask-and-tell interface.

Expand Down Expand Up @@ -179,9 +182,6 @@ def __init__(
self._funhist_term = 10 + math.ceil(30 * n_dim / population_size)
self._funhist_values = np.empty(self._funhist_term * 2)

# for avoid numerical errors
self._epsilon = 1e-8

def __getstate__(self) -> Dict[str, Any]:
attrs = {}
for name in self.__dict__:
Expand Down Expand Up @@ -241,7 +241,7 @@ def _eigen_decomposition(self) -> Tuple[np.ndarray, np.ndarray]:

self._C = (self._C + self._C.T) / 2
D2, B = np.linalg.eigh(self._C)
D = np.sqrt(np.where(D2 < 0, self._epsilon, D2))
D = np.sqrt(np.where(D2 < 0, _EPS, D2))
self._C = np.dot(np.dot(B, np.diag(D ** 2)), B.T)

self._B, self._D = B, D
Expand Down Expand Up @@ -305,7 +305,7 @@ def tell(self, solutions: List[Tuple[np.ndarray, float]]) -> None:
self._sigma *= np.exp(
(self._c_sigma / self._d_sigma) * (norm_p_sigma / self._chi_n - 1)
)
self._sigma = min(self._sigma, sys.float_info.max / 5)
self._sigma = min(self._sigma, _FLT_MAX)

# Covariance matrix adaption
h_sigma_cond_left = norm_p_sigma / math.sqrt(
Expand All @@ -323,7 +323,7 @@ def tell(self, solutions: List[Tuple[np.ndarray, float]]) -> None:
w_io = self._weights * np.where(
self._weights >= 0,
1,
self._n_dim / (np.linalg.norm(C_2.dot(y_k.T), axis=0) ** 2 + self._epsilon),
self._n_dim / (np.linalg.norm(C_2.dot(y_k.T), axis=0) ** 2 + _EPS),
)

delta_h_sigma = (1 - h_sigma) * self._cc * (2 - self._cc) # (p.28)
Expand Down
12 changes: 6 additions & 6 deletions cmaes/_sepcma.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import math
import sys
import numpy as np

from typing import Any
Expand All @@ -9,6 +8,10 @@
from typing import Tuple


_EPS = 1e-8
_FLT_MAX = 1e32


class SepCMA:
"""Separable CMA-ES stochastic optimizer class with ask-and-tell interface.

Expand Down Expand Up @@ -155,9 +158,6 @@ def __init__(
self._funhist_term = 10 + math.ceil(30 * n_dim / population_size)
self._funhist_values = np.empty(self._funhist_term * 2)

# for avoid numerical errors
self._epsilon = 1e-8

@property
def dim(self) -> int:
"""A number of dimensions"""
Expand Down Expand Up @@ -208,7 +208,7 @@ def ask(self) -> np.ndarray:
def _eigen_decomposition(self) -> np.ndarray:
if self._D is not None:
return self._D
self._D = np.sqrt(np.where(self._C < 0, self._epsilon, self._C))
self._D = np.sqrt(np.where(self._C < 0, _EPS, self._C))
return self._D

def _sample_solution(self) -> np.ndarray:
Expand Down Expand Up @@ -268,7 +268,7 @@ def tell(self, solutions: List[Tuple[np.ndarray, float]]) -> None:
self._sigma *= np.exp(
(self._c_sigma / self._d_sigma) * (norm_p_sigma / self._chi_n - 1)
)
self._sigma = min(self._sigma, sys.float_info.max / 5)
self._sigma = min(self._sigma, _FLT_MAX)

# Covariance matrix adaption
h_sigma_cond_left = norm_p_sigma / math.sqrt(
Expand Down