class Function(object):
Known subclasses: cma.fitness_transformations.ComposedFunction
, cma.fitness_transformations.Expensify
, cma.fitness_transformations.GlueArguments
, cma.fitness_transformations.NoisyFitness
, cma.fitness_transformations.SomeNaNFitness
, cma.fitness_transformations.StackFunction
Constructor: Function(fitness_function)
a declarative base class, indicating that a derived class instance "is" a (fitness/objective) function.
A callable
passed to __init__
is called as the fitness
Function
, otherwise the _eval
method is called, if defined in the
derived class, when the Function
instance is called. If the input
argument is a matrix or a list of vectors, the method is called for
each vector individually like
_eval(numpy.asarray(vector)) for vector in matrix.
>>> import cma >>> from cma.fitness_transformations import Function >>> f = Function(cma.ff.rosen) >>> assert f.evaluations == 0 >>> assert f([2, 3]) == cma.ff.rosen([2, 3]) >>> assert f.evaluations == 1 >>> assert f([[1], [2]]) == [cma.ff.rosen([1]), cma.ff.rosen([2])] >>> assert f.evaluations == 3 >>> class Fsphere(Function): ... def _eval(self, x): ... return sum(x**2) >>> fsphere = Fsphere() >>> assert fsphere.evaluations == 0 >>> assert fsphere([2, 3]) == 4 + 9 and fsphere([[2], [3]]) == [4, 9] >>> assert fsphere.evaluations == 3 >>> Fsphere.__init__ = lambda self: None # overwrites Function.__init__ >>> assert Fsphere()([2]) == 4 # which is perfectly fine to do
Details:
- When called, a class instance calls either the function passed to
__init__
or, if none was given, tries to call any of thefunction_names_to_evaluate_first_found
, first come first serve. By default, function_names_to_evaluate_first_found == ["_eval"]. - This class cannot move to module
fitness_functions
, because the latter usesfitness_transformations.rotate
.
Method | __call__ |
Undocumented |
Method | __init__ |
allows to define the fitness_function to be called, doesn't need to be ever called |
Method | initialize |
initialization of Function |
Instance Variable | evaluations |
Undocumented |
Instance Variable | ftarget |
Undocumented |
Instance Variable | target |
Undocumented |
Property | function |
attributes which are searched for to be called if no function was given to __init__ . |
Class Variable | _function |
Undocumented |
Instance Variable | __callable |
Undocumented |
Instance Variable | __initialized |
Undocumented |
cma.fitness_transformations.ComposedFunction
, cma.fitness_transformations.Expensify
, cma.fitness_transformations.GlueArguments
, cma.fitness_transformations.NoisyFitness
, cma.fitness_transformations.SomeNaNFitness
, cma.fitness_transformations.StackFunction
allows to define the fitness_function to be called, doesn't need to be ever called
attributes which are searched for to be called if no function
was given to __init__
.
The first present match is used.