twine_core/problems/optimization.rs
1/// Defines an optimization problem to be solved.
2///
3/// An optimization problem maps solver variables to a model input,
4/// then computes a scalar objective value from the model input and output.
5///
6/// The direction of optimization (minimize or maximize) is determined by
7/// the solver function chosen (e.g., [`golden_section::minimize`][gs-min] vs
8/// [`golden_section::maximize`][gs-max]).
9///
10/// The const generic `N` is the number of solver variables.
11/// For example, `N = 1` represents a scalar optimization problem.
12///
13/// [gs-min]: https://docs.rs/twine-solvers/latest/twine_solvers/optimization/golden_section/fn.minimize.html
14/// [gs-max]: https://docs.rs/twine-solvers/latest/twine_solvers/optimization/golden_section/fn.maximize.html
15pub trait OptimizationProblem<const N: usize> {
16 type Input;
17 type Output;
18 type Error: std::error::Error + Send + Sync + 'static;
19
20 /// Maps solver variables (`x`) into a model input.
21 ///
22 /// # Errors
23 ///
24 /// Returns [`Self::Error`] if the input cannot be constructed from `x`.
25 fn input(&self, x: &[f64; N]) -> Result<Self::Input, Self::Error>;
26
27 /// Computes a scalar objective value from model input/output.
28 ///
29 /// The solver determines whether to minimize or maximize this objective.
30 ///
31 /// # Errors
32 ///
33 /// Returns [`Self::Error`] if the objective cannot be computed.
34 fn objective(&self, input: &Self::Input, output: &Self::Output) -> Result<f64, Self::Error>;
35}