1use crate::expr::{AttachModel, Attached, LinExpr, QuadExpr};
5use crate::prelude::*;
6use crate::Result;
7use std::fmt;
8#[derive(Debug, Clone)]
13pub struct IneqExpr {
14 pub lhs: Expr,
16 pub sense: ConstrSense,
18 pub rhs: Expr,
20}
21
22impl IneqExpr {
23 pub(crate) fn into_normalised_linear(self) -> Result<(LinExpr, ConstrSense, f64)> {
24 let IneqExpr { lhs, rhs, sense } = self;
25 let mut lhs: LinExpr = (lhs - rhs).into_linexpr()?;
26 let rhs = -lhs.set_offset(0.0);
27 Ok((lhs, sense, rhs))
28 }
29
30 pub(crate) fn into_normalised_quad(self) -> (QuadExpr, ConstrSense, f64) {
31 let IneqExpr { lhs, rhs, sense } = self;
32 let mut lhs = (lhs - rhs).into_quadexpr();
33 let rhs = -lhs.set_offset(0.0);
34 (lhs, sense, rhs)
35 }
36}
37
38impl AttachModel for IneqExpr {}
39
40impl fmt::Debug for Attached<'_, IneqExpr> {
41 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42 let cmp = match self.inner.sense {
43 ConstrSense::Less => "≤",
44 ConstrSense::Greater => "≥",
45 ConstrSense::Equal => "=",
46 };
47 write!(
48 f,
49 "{:?} {} {:?}",
50 self.inner.lhs.attach(self.model),
51 cmp,
52 self.inner.rhs.attach(self.model)
53 )
54 }
55}
56
57#[derive(Debug, Clone)]
63pub struct RangeExpr {
64 pub expr: Expr,
66 pub ub: f64,
68 pub lb: f64,
70}
71
72impl RangeExpr {
73 pub(crate) fn into_normalised(self) -> Result<(LinExpr, f64, f64)> {
74 let RangeExpr {
75 expr,
76 mut ub,
77 mut lb,
78 } = self;
79 let mut expr = expr.into_linexpr()?;
80 let offset = expr.set_offset(0.0);
81 ub -= offset;
82 lb -= offset;
83 Ok((expr, lb, ub))
84 }
85}
86
87impl AttachModel for RangeExpr {}
88
89impl fmt::Debug for Attached<'_, RangeExpr> {
90 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
91 write!(
92 f,
93 "{:?} ∈ [{}, {}]",
94 self.inner.expr.attach(self.model),
95 self.inner.lb,
96 self.inner.ub
97 )
98 }
99}
100
101