class documentation

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:

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_hit_at Undocumented
Property function_names_to_evaluate_first_found attributes which are searched for to be called if no function was given to __init__.
Class Variable _function_names_to_evaluate_first_found Undocumented
Instance Variable __callable Undocumented
Instance Variable __initialized Undocumented
def initialize(self, fitness_function):

initialization of Function

evaluations: int =

Undocumented

ftarget =

Undocumented

target_hit_at =

Undocumented

@property
function_names_to_evaluate_first_found =

attributes which are searched for to be called if no function was given to __init__.

The first present match is used.

_function_names_to_evaluate_first_found: list[str] =

Undocumented

__callable =

Undocumented

__initialized: bool =

Undocumented