Skip to main content

oximo_core/
objective.rs

1use std::fmt;
2
3use oximo_expr::ExprId;
4
5/// Whether the model minimizes or maximizes its objective.
6#[derive(Copy, Clone, Debug, PartialEq, Eq)]
7pub enum ObjectiveSense {
8    Minimize,
9    Maximize,
10}
11
12impl fmt::Display for ObjectiveSense {
13    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14        f.write_str(match self {
15            Self::Minimize => "minimize",
16            Self::Maximize => "maximize",
17        })
18    }
19}
20
21/// The model's objective: an expression to optimize and the direction.
22#[derive(Clone, Debug)]
23pub struct Objective {
24    /// Root node of the objective expression in the model's [`oximo_expr::ExprArena`].
25    pub expr: ExprId,
26    pub sense: ObjectiveSense,
27}
28
29#[cfg(test)]
30mod tests {
31    use super::ObjectiveSense;
32
33    #[test]
34    fn display_uses_user_facing_ascii_labels() {
35        let labels = [ObjectiveSense::Minimize.to_string(), ObjectiveSense::Maximize.to_string()];
36        assert_eq!(labels, ["minimize", "maximize"]);
37        assert!(labels.iter().all(|label| label.is_ascii()));
38    }
39}