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};
10
11/// Definition of an optimisation problem
12///
13/// The definition of an optimisation problem involves:
14/// - the gradient of the cost function
15/// - the cost function
16/// - the set of constraints, which is described by implementations of
17/// [Constraint](../../panoc_rs/constraints/trait.Constraint.html)
18pub struct Problem<'a, GradientType, ConstraintType, CostType>
19where
20 GradientType: Fn(&[f64], &mut [f64]) -> FunctionCallResult,
21 CostType: Fn(&[f64], &mut f64) -> FunctionCallResult,
22 ConstraintType: constraints::Constraint,
23{
24 /// constraints
25 pub(crate) constraints: &'a ConstraintType,
26 /// gradient of the cost
27 pub(crate) gradf: GradientType,
28 /// cost function
29 pub(crate) cost: CostType,
30}
31
32impl<'a, GradientType, ConstraintType, CostType> Problem<'a, GradientType, ConstraintType, CostType>
33where
34 GradientType: Fn(&[f64], &mut [f64]) -> FunctionCallResult,
35 CostType: Fn(&[f64], &mut f64) -> FunctionCallResult,
36 ConstraintType: constraints::Constraint,
37{
38 /// Construct a new instance of an optimisation problem
39 ///
40 /// ## Arguments
41 ///
42 /// - `constraints` constraints
43 /// - `cost_gradient` gradient of the cost function
44 /// - `cost` cost function
45 ///
46 /// ## Returns
47 ///
48 /// New instance of `Problem`
49 pub fn new(
50 constraints: &'a ConstraintType,
51 cost_gradient: GradientType,
52 cost: CostType,
53 ) -> Problem<'a, GradientType, ConstraintType, CostType> {
54 Problem {
55 constraints,
56 gradf: cost_gradient,
57 cost,
58 }
59 }
60}