Cost Functions#

class staliro.cost_func.CostFunc#

The transformation from a Sample to a cost value.

This class is parameterized by two type variables, C and E. C is the type of the cost returned by this class and the value attribute in Result return value. E is the type of the annotation data in the extra attribute of the return value.

abstract evaluate(sample)#

Evaluate the given Sample into a cost value.

Parameters:

sample (Sample) – The sample to evaluate

Returns:

The cost value associated with the sample and any provided annotation data

Return type:

Result[C, E]

class staliro.cost_func.Wrapper(func)#

Wrapper to transform a raw python function into a CostFunc.

Parameters:

func (Callable[[Sample], Result[C, E]]) – The user function to wrap

evaluate(sample)#

Apply the provided function to evaluate the given Sample.

Parameters:

sample (Sample) – The sample to evaluate with the user function

Returns:

The cost value associated with the sample and any provided annotation data

Return type:

Result[C, E]

@staliro.cost_func.costfunc(func: Callable[[Sample], Result[C, E]]) Wrapper[C, E]#
@staliro.cost_func.costfunc(func: Callable[[Sample], C]) Wrapper[C, None]
@staliro.cost_func.costfunc(func: None = None) Decorator

Transform a python function into a CostFunc.

If the provided function returns any value other than a Result, the value will be wrapped in a Result with the extra field set to None. This decorator can be called with or without parentheses.

Parameters:

func – The function to transform

Returns:

A cost function or a decorator