Skip to main content

optimization_engine/core/
problem.rs

1//! An optimization problem
2//!
3//! This struct defines an optimization problem in terms of its cost function
4//! (cost function and its gradient) and constraints
5//!
6//! Cost functions are user defined. They can either be defined in Rust or in
7//! C (and then invoked from Rust via an interface such as icasadi).
8//!
9use crate::{constraints, FunctionCallResult};
10use std::marker::PhantomData;
11
12/// Definition of an optimisation problem
13///
14/// The definition of an optimisation problem involves:
15/// - the gradient of the cost function
16/// - the cost function
17/// - the set of constraints, which is described by implementations of
18///   [Constraint](../../panoc_rs/constraints/trait.Constraint.html)
19pub struct Problem<'a, GradientType, ConstraintType, CostType, T = f64>
20where
21    GradientType: Fn(&[T], &mut [T]) -> FunctionCallResult,
22    CostType: Fn(&[T], &mut T) -> FunctionCallResult,
23    ConstraintType: constraints::Constraint<T>,
24{
25    /// constraints
26    pub(crate) constraints: &'a ConstraintType,
27    /// gradient of the cost
28    pub(crate) gradf: GradientType,
29    /// cost function
30    pub(crate) cost: CostType,
31    marker: PhantomData<T>,
32}
33
34impl<'a, GradientType, ConstraintType, CostType, T>
35    Problem<'a, GradientType, ConstraintType, CostType, T>
36where
37    GradientType: Fn(&[T], &mut [T]) -> FunctionCallResult,
38    CostType: Fn(&[T], &mut T) -> FunctionCallResult,
39    ConstraintType: constraints::Constraint<T>,
40{
41    /// Construct a new instance of an optimisation problem
42    ///
43    /// ## Arguments
44    ///
45    /// - `constraints` constraints
46    /// - `cost_gradient` gradient of the cost function
47    /// - `cost` cost function
48    ///
49    /// ## Returns
50    ///
51    /// New instance of `Problem`
52    pub fn new(
53        constraints: &'a ConstraintType,
54        cost_gradient: GradientType,
55        cost: CostType,
56    ) -> Problem<'a, GradientType, ConstraintType, CostType, T> {
57        Problem {
58            constraints,
59            gradf: cost_gradient,
60            cost,
61            marker: PhantomData,
62        }
63    }
64}