class documentation

quick hack versatile base class.

To guaranty that modifications of the attribute bounds after this instance was already used are effective, the class attribute _bounds_dict must be reset like _bounds_dict = {}.

Method __call__ return penalty or list of penalties, by default zero(s).
Method __init__ bounds can be None or [lb, ub]
Method amend_bounds_for_integer_variables set bounds away from at=0.5 such that
Method get_bound return lower and upper bound of variable with index index
Method get_bounds get_bounds('lower', 8) returns the lower bounds in 8-D
Method has_bounds return True if any variable is bounded
Method idx_out_of_bounds return index list of out-of-bound values in x.
Method into_bounds set out-of-bound values on bounds and return x.
Method inverse inverse of repair if it exists, at least it should hold repair == repair o inverse o repair
Method is_in_bounds return True if x is in bounds.
Method repair projects infeasible values on the domain bound, might be overwritten by derived class
Method to_dim_times_two return boundaries in format [[lb0, ub0], [lb1, ub1], ...], as used by BoxConstraints... class.
Method update end-iteration callback of boundary handler (abstract/empty)
Instance Variable bounds Undocumented
Instance Variable use_cached_values default behavior as to whether to use cached values
Method _get_bounds ib == 0/1 means lower/upper bound, return a vector of length dimension
Instance Variable _bounds_dict saved return values of get_bounds(i, dim). Changing self.bounds may only be effective when this is reset.
def __call__(self, solutions, *args, **kwargs):

return penalty or list of penalties, by default zero(s).

This interface seems too specifically tailored to the derived BoundPenalty class, it should maybe change.

def __init__(self, bounds):

bounds can be None or [lb, ub]

where lb and ub are either None or a vector (which can have None entries).

On return, the bounds attribute of self are the bounds in a normalized form.

To compute bounds for any dimension, the last entry of bounds is then recycled for variables with indices >= len(bounds[i]) for i in (0,1).

def amend_bounds_for_integer_variables(self, integer_indices, at=0.5, offset=1e-09):

set bounds away from at=0.5 such that

a repaired solution is always rounded into the feasible domain.

def get_bound(self, index):

return lower and upper bound of variable with index index

def get_bounds(self, which, dimension):

get_bounds('lower', 8) returns the lower bounds in 8-D

def has_bounds(self, which='both'):

return True if any variable is bounded

def idx_out_of_bounds(self, x):

return index list of out-of-bound values in x.

if bounds.idx_out_of_bounds evaluates to True if and only if x is out of bounds.

def into_bounds(self, x, copy=True):

set out-of-bound values on bounds and return x.

Make a copy when x is changed and copy is True, otherwise change in place.

>>> import numpy as np
>>> import cma
>>> b = cma.boundary_handler.BoundaryHandlerBase(bounds=[[0.1], 0.2])
>>> assert all(0.1 <= b.into_bounds(np.random.randn(22)))
>>> assert all(0.2 >= b.into_bounds(np.random.randn(11)))
>>> b = cma.boundary_handler.BoundaryHandlerBase(bounds=[[-0.1], np.inf])
>>> assert all(-0.1 <= b.into_bounds(np.random.randn(22)))
>>> b = cma.boundary_handler.BoundaryHandlerBase(bounds=[[-0.1], [None]])
>>> assert all(-0.1 <= b.into_bounds(np.random.randn(22)))
>>> b = cma.boundary_handler.BoundaryHandlerBase(bounds=[-np.inf, 0.1])
>>> assert all(0.1 >= b.into_bounds(np.random.randn(22)))
def inverse(self, y, copy_if_changed=True):

inverse of repair if it exists, at least it should hold repair == repair o inverse o repair

def is_in_bounds(self, x):

return True if x is in bounds.

>>> import numpy as np
>>> import cma
>>> b = cma.boundary_handler.BoundaryHandlerBase(bounds=[[-0.5], 0.2])
>>> for n in [2, 4, 9]:
...     x = np.random.randn(n)
...     assert (all(-0.5 <= x) and all(0.2 >= x)) is b.is_in_bounds(x)
def repair(self, x, copy_if_changed=True):

projects infeasible values on the domain bound, might be overwritten by derived class

def to_dim_times_two(self, bounds=None):

return boundaries in format [[lb0, ub0], [lb1, ub1], ...], as used by BoxConstraints... class.

Use by default bounds = self.bounds.

def update(self, *args, **kwargs):

end-iteration callback of boundary handler (abstract/empty)

bounds =

Undocumented

use_cached_values =

default behavior as to whether to use cached values

def _get_bounds(self, ib, dimension):

ib == 0/1 means lower/upper bound, return a vector of length dimension

_bounds_dict: dict =

saved return values of get_bounds(i, dim). Changing self.bounds may only be effective when this is reset.