class documentation

class FitnessFunctions(object):

View In Hierarchy

collection of objective functions.

Static Method binval return sum_i(0 if (optimum[0] <= x[i] <= optimum[1]) else 2**i)
Static Method leadingones return len(x) - nb of leading-ones-in-x to be minimized,
Static Method onemax return sum_i(0 if (optimum[0] <= x[i] <= optimum[1]) else 1)
Method __init__ Undocumented
Method absplussin multimodal function with the global optimum at x_i = -1.152740846
Method bohachevsky a moderately difficult multimodal function with global structure,
Method branin Undocumented
Method bukin Bukin function from Wikipedia, generalized simplistically from 2-D.
Method cigar Cigar test objective function
Method cigtab Cigtab test objective function
Method cigtab2 cigtab with 1 + 5% long and short axes.
Method cornerelli Undocumented
Method cornerellirot Undocumented
Method cornersphere Sphere (squared norm) test objective function constraint to the corner
Method diagonal_cigar Undocumented
Method diffpow Diffpow test objective function
Method dixonprice Dixon-Price function.
Method elli Ellipsoid test objective function
Method elliconstraint ellipsoid test objective function with "constraints"
Method ellihalfrot return ellirot(x[:N2]) + elli(x[N2:]) where N2 is roughly frac*len(x)
Method ellirot Undocumented
Method elliwithoneconstraint Undocumented
Method epslow Undocumented
Method epslowsphere TODO: define as wrapper
Method flat Undocumented
Method fun_as_arg fun_as_arg(x, fun, *more_args) calls fun(x, *more_args).
Method goldsteinprice Undocumented
Method grad_cigar Undocumented
Method grad_elli Undocumented
Method grad_numerical symmetric gradient
Method grad_rosen Undocumented
Method grad_sphere Undocumented
Method grad_tablet Undocumented
Method grad_to_one Undocumented
Method griewank with search range [-5, 5] instead of [-600, 600]
Method halfelli Undocumented
Method happycat a difficult sharp ridge type function on a circle with xopt == -1.
Method hyperelli Undocumented
Method levy a rather benign multimodal function.
Method lincon ridge like linear function with one linear constraint
Method linear Undocumented
Method lineard Undocumented
Method noise Undocumented
Method noiseC Undocumented
Method noisysphere noise/len(x) is the multiplicative, noise_offset is the additive factor.
Method normalSkew Undocumented
Method optprob Undocumented
Method partsphere Sphere (squared norm) test objective function
Method pnorm Undocumented
Method powel_singular Undocumented
Method rand Random test objective function
Method rastrigin Rastrigin test objective function
Method ridge Undocumented
Method ridgecircle deprecated, see ridgeoncircle
Method ridgeoncircle A sharp ridge on the hypersphere surface with xopt = -1
Method ridgeopt ridge with optimum in zero, by default a sharp ridge, namely return
Method rosen Rosenbrock test objective function, x0=0
Method rosen0 Rosenbrock test objective function with optimum in all-zeros, x0=-1
Method rosen_chained Undocumented
Method rosen_nesterov needs exponential number of steps in a non-increasing f-sequence.
Method rosenelli Undocumented
Method rot returns fun(rotation(x), *args), ie. fun applied to a rotated argument
Method schaffer Schaffer function x0 in [-100..100]
Method schwefel2_22 Schwefel 2.22 function
Method schwefelelli Undocumented
Method schwefelmult multimodal Schwefel function with domain -500..500
Method sectorsphere asymmetric Sphere (squared norm) test objective function
Method somenan returns sometimes np.nan, otherwise fun(x)
Method sphere Sphere (norm squared) test objective function.
Method sphere_pos Sphere (squared norm) test objective function
Method spherew Sphere (squared norm) with sum x_i = 1 test objective function
Method spherewithnconstraints Undocumented
Method spherewithoneconstraint Undocumented
Method styblinski_tang in [-5, 5] found also in Lazar and Jarre 2016, optimum in f(-2.903534...)=0
Method subspace_sphere random subspace sphere function, may be very difficult to solve
Method tablet Tablet test objective function
Method trid Undocumented
Method twoaxes Cigtab test objective function
Method xinsheyang2 a multimodal function which is rather unsolvable in larger dimension.
Class Variable evaluations Undocumented
Property BBOB Undocumented
Instance Variable _subspace_sphere_changed_count Undocumented
Instance Variable _subspace_sphere_count Undocumented
Instance Variable _subspace_sphere_permutation Undocumented
@staticmethod
def binval(x, foffset=None):

return sum_i(0 if (optimum[0] <= x[i] <= optimum[1]) else 2**i)

to be minimized and add binary_foffset at the optimum.

Details: the result is computed as int, because in dimension > 54 a float representation can not account for the least sensitive bit anymore. Because we minimize, this is not necessarily a big problem.

@staticmethod
def leadingones(x, foffset=None):

return len(x) - nb of leading-ones-in-x to be minimized,

where only values in [optimum[0], optimum[1]] are considered to be "equal to" 1 and add binary_foffset at the optimum.

@staticmethod
def onemax(x, foffset=None):

return sum_i(0 if (optimum[0] <= x[i] <= optimum[1]) else 1)

to be minimized and add binary_foffset at the optimum.

def __init__(self):

Undocumented

def absplussin(self, x):

multimodal function with the global optimum at x_i = -1.152740846

def bohachevsky(self, x, version=1):

a moderately difficult multimodal function with global structure,

generalized from 2-D like the Rosenbrock function. The search domain is [-100, 100].

See https://www.sfu.ca/~ssurjano/boha.html

def branin(self, x):

Undocumented

def bukin(self, x):

Bukin function from Wikipedia, generalized simplistically from 2-D.

http://en.wikipedia.org/wiki/Test_functions_for_optimization

def cigar(self, x, rot=0, cond=1000000.0, noise=0):

Cigar test objective function

def cigtab(self, y):

Cigtab test objective function

def cigtab2(self, x, condition=100000000.0, n_axes=None):

cigtab with 1 + 5% long and short axes.

n_axes: int, if > 0, sets the number of long as well as short axes to n_axes, respectively.

def cornerelli(self, x):

Undocumented

def cornerellirot(self, x):

Undocumented

def cornersphere(self, x):

Sphere (squared norm) test objective function constraint to the corner

def diagonal_cigar(self, x, cond=1000000.0):

Undocumented

def diffpow(self, x, rot=0):

Diffpow test objective function

def dixonprice(self, x):

Dixon-Price function.

The function has a local attractor at [1/3, 0, ..., 0] which starts to dominate for dimensions larger than about five. The global optimum is [1, 0.70710678, ...] with the limit of 1/2 for increasing index.

>>> import cma
>>> def xstar(n):
...     return [2**-((2**i - 2) / 2**i) for i in range(1, n + 1)]
>>> assert cma.ff.dixonprice(xstar(4)) < 1e-11, cma.ff.dixonprice(xstar(4))

see https://al-roomi.org/benchmarks/unconstrained/n-dimensions/236-dixon-price-s-function

def elli(self, x, rot=0, xoffset=0, cond=1000000.0, actuator_noise=0.0, beta_noise=0, both=False):

Ellipsoid test objective function

def elliconstraint(self, x, cfac=100000000.0, tough=True, cond=1000000.0):

ellipsoid test objective function with "constraints"

def ellihalfrot(self, x, frac=0.5, cond1=1000000.0, cond2=1000000.0):

return ellirot(x[:N2]) + elli(x[N2:]) where N2 is roughly frac*len(x)

def ellirot(self, x, cond=1000000.0):

Undocumented

def elliwithoneconstraint(self, x, idx=[(-1)]):

Undocumented

def epslow(self, fun, eps=1e-07, Neff=(lambda x: int(len(x) ** 0.5))):

Undocumented

def epslowsphere(self, x, eps=1e-07, Neff=(lambda x: int(len(x) ** 0.5))):

TODO: define as wrapper

def flat(self, x):

Undocumented

def fun_as_arg(self, x, *args):

fun_as_arg(x, fun, *more_args) calls fun(x, *more_args).

Use case:

fmin(cma.fun_as_arg, args=(fun,), gradf=grad_numerical)

calls fun_as_args(x, args) and grad_numerical(x, fun, args=args)

def goldsteinprice(self, x):

Undocumented

def grad_cigar(self, x, *args):

Undocumented

def grad_elli(self, x, *args):

Undocumented

def grad_numerical(self, x, func, epsilon=None):

symmetric gradient

def grad_rosen(self, x, *args):

Undocumented

def grad_sphere(self, x, *args):

Undocumented

def grad_tablet(self, x, *args):

Undocumented

def grad_to_one(self, x, *args):

Undocumented

def griewank(self, x):

with search range [-5, 5] instead of [-600, 600]

def halfelli(self, x):

Undocumented

def happycat(self, x, alpha=1.0/8):

a difficult sharp ridge type function on a circle with xopt == -1.

See ridgeoncircle for a more accessible implementation.

Reference: Beyer & Finck 2012, Happycat - a simple function class where well-known direct search algorithms do fail.

def hyperelli(self, x):

Undocumented

def levy(self, x):

a rather benign multimodal function.

xopt == ones, fopt == 0.0

def lincon(self, x, theta=0.01):

ridge like linear function with one linear constraint

def linear(self, x):

Undocumented

def lineard(self, x):

Undocumented

def noise(self, x, func=sphere, fac=10, expon=1):

Undocumented

def noiseC(self, x, func=sphere, fac=10, expon=0.8):

Undocumented

def noisysphere(self, x, noise=2.1e-09, cond=1.0, noise_offset=0.1):

noise/len(x) is the multiplicative, noise_offset is the additive factor.

noise=10 does not work with default popsize, cma.NoiseHandler(dimension, 1e7) helps.

See also cma.fitness_transformations.NoisyFitness.

def normalSkew(self, f):

Undocumented

def optprob(self, x):

Undocumented

def partsphere(self, x):

Sphere (squared norm) test objective function

def pnorm(self, x, p=0.5):

Undocumented

def powel_singular(self, x):

Undocumented

def rand(self, x):

Random test objective function

def rastrigin(self, x):

Rastrigin test objective function

def ridge(self, x, expo=2, factor=100):

Undocumented

def ridgecircle(self, x, expo=0.5):

deprecated, see ridgeoncircle

def ridgeoncircle(self, x, expo=1, factor=100, inner_expo=1):

A sharp ridge on the hypersphere surface with xopt = -1

and f(xopt) = 0. The ridge surface is at the radius r = sqrt(n).

This is an implementation of happycat with a richer and more intuitive parametrization and different defaults. The function is composed of three different terms. Conceptually, while omitting an appropriate weighting of terms, we have with decreasing relevance:

f(x) = ||x| - r| + linear(x) + |x|^2

where |.| means Eucledian norm or absolute value. The purpose of the last term is to prevent domination of the linear term when |x| > r.

expo = 1 is the exponent for ||x| - r|. The happycat default is 1/4 (alpha == 1/8).

factor = 100 weighs the towards-ridge component in comparison to the position-along-the-ridge component. By construction, df/dx of the latter converges to zero when approaching the optimum, whereas the former remains constant when expo == 1 and diverges when expo < 1. This discrepancy between vanishing and nonvanishing (potentially infinite) gradients makes this function difficult to solve. For the happycat, this factor is not parametrized but equals in effect 2 * sqrt(n), see below.

inner_expo = 1 is the exponent for both, |x| and r in the first term. It creates an implicit factor multiplier which is, close to the ridge, about inner_expo * sqrt(n)**(inner_expo - 1), hence 1 by default. The happycat default is 2 which makes the factor multiplier 2 * sqrt(n) and different dimensions slightly incomparable.

Details

The most interesting parameter range is probably like 1 <= factor <= 1000 and 0.5 <= expo <= 1.5. SLSQP succeeds to get close to the optimum with expo >= 1.5 and, with expo <= 1, fails when factor >= 10 or expo <= 0.8.

This function can be considered as a nonstraight version of ridgeopt. The vanishing gradient along the ridge due to the quadratic nature of the hypersphere surface is analogous to exponent0 = 2 for ridgeopt.

The inner_expo does not cancel expo, because the argument becomes zero at the optimum only for the latter. The idea that these exponents cancel might have lead to the default parameter choice for alpha of the original Happycat function.

As by default, with expo=1 and factor=100, this looks quite similar to "the curve fitting problem".

functools.partial(cma.ff.ridgeoncircle, expo=1/2, factor=1, inner_expo=2) instantiates ridgecircle up to a missing |x|^2/n/2 - 0.5 which is zero at -1. The happycat is instantiated below. The expo=0.25 makes the Happycat much more difficult by default.

>>> import cma
>>> val = cma.ff.ridgeoncircle([-1.01, -1.02])
>>> assert 2.12318 < val < 2.12319, val  # ~ 100 * 0.02
>>> import functools  # instantiate the happycat function
>>> happycat = functools.partial(cma.ff.ridgeoncircle,
...                              expo=0.25, factor=1, inner_expo=2)
>>> assert happycat([-1.2,3,4]) == cma.ff.happycat([-1.2,3,4])
>>> assert val != happycat([-1.01, -1.02]), val
def ridgeopt(self, x, ridge_exponent=1, factor=100, exponent0=2):

ridge with optimum in zero, by default a sharp ridge, namely return

(x[0]**2)**(2/2) + 100 * sum(x[1:]**2)**(1/2). In general, return (x[0]**2)**(exponent0 / 2) + 100 * sum(x[1:]**2)**(ridge_exponent / 2).

The exponents reflect the respective shapes with increasing |x|, like 1 for linear, 2 for quadratic, and 0.5 for square root.

See also ridgeoncircle.

def rosen(self, x, alpha=100.0, exponent=2):

Rosenbrock test objective function, x0=0

def rosen0(self, x, alpha=100.0):

Rosenbrock test objective function with optimum in all-zeros, x0=-1

def rosen_chained(self, x, alpha=100.0):

Undocumented

def rosen_nesterov(self, x, rho=100):

needs exponential number of steps in a non-increasing f-sequence.

x_0 = (-1,1,...,1) See Jarre (2011) "On Nesterov's Smooth Chebyshev-Rosenbrock Function"

def rosenelli(self, x):

Undocumented

def rot(self, x, fun, rot=1, args=()):

returns fun(rotation(x), *args), ie. fun applied to a rotated argument

def schaffer(self, x):

Schaffer function x0 in [-100..100]

def schwefel2_22(self, x):

Schwefel 2.22 function

def schwefelelli(self, x):

Undocumented

def schwefelmult(self, x, pen_fac=10000.0):

multimodal Schwefel function with domain -500..500

def sectorsphere(self, x):

asymmetric Sphere (squared norm) test objective function

def somenan(self, x, fun, p=0.1):

returns sometimes np.nan, otherwise fun(x)

def sphere(self, x, xoffset=None, effective_dimensions=None):

Sphere (norm squared) test objective function.

The optimum is at -xoffset=0.

effective_dimensions can be an int or a ratio -> max(1, int(ratio * len(x))).

See also cma.ff.noisysphere, cma.fitness_transformations.NoisyFitness, cma.fitness_transformations.LowEffectiveDimension and cma.fitness_transformations.NeutralVariables.

def sphere_pos(self, x):

Sphere (squared norm) test objective function

def spherew(self, x):

Sphere (squared norm) with sum x_i = 1 test objective function

def spherewithnconstraints(self, x):

Undocumented

def spherewithoneconstraint(self, x):

Undocumented

def styblinski_tang(self, x):

in [-5, 5] found also in Lazar and Jarre 2016, optimum in f(-2.903534...)=0

def subspace_sphere(self, x, visible_ratio=0.5, same_subspace=False):

random subspace sphere function, may be very difficult to solve

when the subspace changes with each evaluation.

same_subspace determines for how many evaluations the subspace remains the same, True means always. The call subspace_sphere('new subspace') resets the subspace such that a callback to fmin2 like:

fun = func_tools.partial(cma.ff.subspace_sphere, same_subspace=True)
mod = 1
cma.fmin2(..., callback=lambda es: (es.countiter % mod) or fun('new subspace'))

would reset the subspace every mod iterations.

Caveat: the same_subspace parameter has not been thoroughly tested.

>>> import functools, cma
>>> fun = functools.partial(cma.ff.subspace_sphere, same_subspace=True)
>>> val = fun(range(200))
>>> assert fun(range(200)) == val, val  # fun does not change the subspace
>>> perm = cma.ff._subspace_sphere_permutation
>>> val2 = cma.ff.subspace_sphere(range(200))  # changes the subspace
>>> assert perm is not cma.ff._subspace_sphere_permutation
>>> perm = cma.ff._subspace_sphere_permutation
>>> assert fun(range(200)) == val2  # fun does not change the subspace
>>> assert perm is cma.ff._subspace_sphere_permutation
>>> fun('new subspace')
>>> assert perm is not cma.ff._subspace_sphere_permutation
def tablet(self, x, cond=1000000.0, rot=0):

Tablet test objective function

def trid(self, x):

Undocumented

def twoaxes(self, y, cond=1000000.0):

Cigtab test objective function

def xinsheyang2(self, x, termination_friendly=True):

a multimodal function which is rather unsolvable in larger dimension.

>>> import functools
>>> import numpy as np
>>> import cma
>>> f = functools.partial(cma.ff.xinsheyang2, termination_friendly=False)
>>> X = [(i * [0] + (4 - i) * [1.24]) for i in range(5)]
>>> for x in X: print(x)
[1.24, 1.24, 1.24, 1.24]
[0, 1.24, 1.24, 1.24]
[0, 0, 1.24, 1.24]
[0, 0, 0, 1.24]
[0, 0, 0, 0]
>>> ' '.join(['{0:.3}'.format(f(x)) for x in X])  # [np.round(f(x), 3) for x in X]
'0.091 0.186 0.336 0.456 0.0'

One needs to solve a trinary deceptive function where f-value (to be minimized) is monotonuously decreasing with increasing distance to the global optimum >= 1. That is, the global optimum is surrounded by 3^n - 1 local optima that have the better values the further they are away from the global optimum.

Conclusion: it is a rather suspicious sign if an algorithm finds the global optimum of this function in larger dimension.

See also http://benchmarkfcns.xyz/benchmarkfcns/xinsheyangn2fcn.html

evaluations: int =

Undocumented

@property
BBOB =

Undocumented

_subspace_sphere_changed_count =

Undocumented

_subspace_sphere_count: int =

Undocumented

_subspace_sphere_permutation =

Undocumented