pub trait CostFunction {
    type Param;
    type Output;

    fn cost(&self, param: &Self::Param) -> Result<Self::Output, Error>;

    fn bulk_cost<'a, P>(
        params: &'a [P]
    ) -> Result<Vec<Self::Output>, Error>
        P: Borrow<Self::Param> + SyncAlias,
        Self::Output: SendAlias,
        Self: SyncAlias
, { ... } fn parallelize(&self) -> bool { ... } }
Expand description

Defines computation of a cost function value


use argmin::core::{CostFunction, Error};
use argmin_testfunctions::rosenbrock;

struct Rosenbrock {}

impl CostFunction for Rosenbrock {
    type Param = Vec<f64>;
    type Output = f64;

    /// Compute Rosenbrock function
    fn cost(&self, param: &Self::Param) -> Result<Self::Output, Error> {
        Ok(rosenbrock(param, 1.0, 100.0))

Required Associated Types

Type of the parameter vector

Tyope of the return value of the cost function

Required Methods

Compute cost function

Provided Methods

Compute cost in bulk. If the rayon feature is enabled, multiple calls to cost will be run in parallel using rayon, otherwise they will execute sequentially. If the rayon feature is enabled, parallelization can still be turned off by overwriting parallelize to return false. This can be useful in cases where it is preferable to parallelize only certain parts. Note that even if parallelize is set to false, the parameter vectors and the problem are still required to be Send and Sync. Those bounds are linked to the rayon feature. This method can be overwritten.

Indicates whether to parallelize calls to cost when using bulk_cost. By default returns true, but can be set manually to false if needed. This allows users to turn off parallelization for certain traits implemented on their problem. Note that parallelization requires the rayon feature to be enabled, otherwise calls to cost will be executed sequentially independent of how parallelize is set.