lp_types/vars/
mod.rs

1use crate::{utils::DisplayWrapper, LinearConstraint};
2use std::fmt::{Debug, Display, Formatter};
3
4mod convert;
5mod display;
6
7pub struct LinearVariable<T> {
8    kind: LinearVariableKind,
9    symbol: String,
10    bound: LinearConstraint<T>,
11}
12
13#[derive(Debug)]
14pub enum LinearVariableKind {
15    Boolean,
16    Decimal,
17    Integer,
18}
19
20/// Create a new variable with constraints
21impl<T> LinearVariable<T> {
22    /// Creates a new variable with no constraints.
23    pub fn new<S>(symbol: S) -> Self
24    where
25        S: Into<String>,
26    {
27        Self { kind: LinearVariableKind::Decimal, symbol: symbol.into(), bound: LinearConstraint::None }
28    }
29    /// Creates a new variable with the given lower bound.
30    pub fn ge<S>(symbol: S, lower: T) -> Self
31    where
32        S: Into<String>,
33    {
34        Self { kind: LinearVariableKind::Decimal, symbol: symbol.into(), bound: LinearConstraint::ge(lower) }
35    }
36    /// Creates a new variable with the given lower bound.
37    pub fn geq<S>(symbol: S, lower: T) -> Self
38    where
39        S: Into<String>,
40    {
41        Self { kind: LinearVariableKind::Decimal, symbol: symbol.into(), bound: LinearConstraint::geq(lower) }
42    }
43    /// Creates a new variable with the given upper bound.
44    pub fn le<S>(symbol: S, upper: T) -> Self
45    where
46        S: Into<String>,
47    {
48        Self { kind: LinearVariableKind::Decimal, symbol: symbol.into(), bound: LinearConstraint::le(upper) }
49    }
50    /// Creates a new variable with the given upper bound.
51    pub fn leq<S>(symbol: S, upper: T) -> Self
52    where
53        S: Into<String>,
54    {
55        Self { kind: LinearVariableKind::Decimal, symbol: symbol.into(), bound: LinearConstraint::leq(upper) }
56    }
57    /// Creates a new variable with the given lower and upper bounds.
58    pub fn bounds<S>(symbol: S, bound: LinearConstraint<T>) -> Self
59    where
60        S: Into<String>,
61    {
62        Self { kind: LinearVariableKind::Decimal, symbol: symbol.into(), bound }
63    }
64}
65
66impl<T> LinearVariable<T> {
67    pub fn get_symbol(&self) -> &str {
68        &self.symbol
69    }
70    pub fn get_kind(&self) -> &LinearVariableKind {
71        &self.kind
72    }
73
74    pub fn set_kind(&mut self, kind: LinearVariableKind) {
75        self.kind = kind;
76    }
77
78    pub fn with_kind(mut self, kind: LinearVariableKind) -> Self {
79        self.kind = kind;
80        self
81    }
82}
83
84impl<T: PartialOrd> LinearVariable<T> {
85    pub fn contains(&self, other: &T) -> bool {
86        self.bound.contains(other)
87    }
88}