class documentation
Handle boundaries by a smooth, piecewise linear and quadratic transformation into the feasible domain.
>>> import numpy as np >>> import cma >>> from cma.constraints_handler import BoundTransform >>> from cma import fitness_transformations as ft >>> veq = cma.utilities.math.Mh.vequals_approximately >>> b = BoundTransform([0, None]) >>> assert b.bounds == [[0], [None]] >>> assert veq(b.repair([-0.1, 0, 1, 1.2]), np.array([0.0125, 0.0125, 1, 1.2])), b.repair([-0.1, 0, 1, 1.2]) >>> assert b.is_in_bounds([0, 0.5, 1]) >>> assert veq(b.transform([-1, 0, 1, 2]), [0.9, 0.0125, 1, 2 ]), b.transform([-1, 0, 1, 2]) >>> bounded_sphere = ft.ComposedFunction([ ... cma.ff.sphere, ... BoundTransform([[], 5 * [-1] + [np.inf]]).transform ... ]) >>> o1 = cma.fmin(bounded_sphere, 6 * [-2], 0.5) # doctest: +ELLIPSIS (4_w,9)-aCMA-ES (mu_w=2.8,w_1=49%) in dimension 6 (seed=... >>> o2 = cma.fmin(cma.ff.sphere, 6 * [-2], 0.5, options={ ... 'BoundaryHandler': cma.s.ch.BoundTransform, ... 'bounds': [[], 5 * [-1] + [np.inf]] }) # doctest: +ELLIPSIS (4_w,9)-aCMA-ES (mu_w=2.8,w_1=49%) in dimension 6 (seed=... >>> assert o1[1] < 5 + 1e-8 and o2[1] < 5 + 1e-8 >>> b = BoundTransform([-np.random.rand(120), np.random.rand(120)]) >>> for i in range(0, 100, 9): ... x = (-i-1) * np.random.rand(120) + i * np.random.randn(120) ... x_to_b = b.repair(x) ... x2 = b.inverse(x_to_b) ... x2_to_b = b.repair(x2) ... x3 = b.inverse(x2_to_b) ... x3_to_b = b.repair(x3) ... assert veq(x_to_b, x2_to_b) ... assert veq(x2, x3) ... assert veq(x2_to_b, x3_to_b) >>> for _ in range(5): ... lb = np.random.randn(4) ... ub = lb + 1e-7 + np.random.rand(4) ... b = BoundTransform([lb, ub]) ... for x in [np.random.randn(4) / np.sqrt(np.random.rand(4)) for _ in range(22)]: ... assert all(lb <= b.transform(x)), (lb, ub, b.__dict__) ... assert all(b.transform(x) <= ub), (lb, ub, b.__dict__)
Details: this class uses class BoxConstraintsLinQuadTransformation
Method | __init__ |
Argument bounds can be None or bounds[0] and bounds[1] are lower and upper domain boundaries, each is either None or a scalar or a list or array of appropriate size. |
Method | inverse |
inverse transform of x from the bounded domain. |
Method | repair |
transforms x into the bounded domain. |
Method | transform |
Undocumented |
Instance Variable | bounds |
Undocumented |
Inherited from BoundaryHandlerBase
:
Method | __call__ |
return penalty or list of penalties, by default zero(s). |
Method | amend |
set bounds away from at=0.5 such that |
Method | get |
return lower and upper bound of variable with index index |
Method | get |
get_bounds('lower', 8) returns the lower bounds in 8-D |
Method | has |
return True if any variable is bounded |
Method | idx |
return index list of out-of-bound values in x . |
Method | is |
not yet tested |
Method | to |
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 |
Method | _get |
ib == 0/1 means lower/upper bound, return a vector of length dimension |