1use crate::error::{Result, ensure, ensure_finite};
4use crate::sparse::{Bounds, CsrMatrix};
5
6#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
9pub enum Sense {
10 #[default]
12 Minimize,
13 Maximize,
15}
16
17impl Sense {
18 pub fn as_str(self) -> &'static str {
20 match self {
21 Sense::Minimize => "minimize",
22 Sense::Maximize => "maximize",
23 }
24 }
25}
26
27#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
29pub enum VariableKind {
30 #[default]
32 Continuous,
33 Integer,
35 Binary,
37}
38
39impl VariableKind {
40 pub fn as_str(self) -> &'static str {
42 match self {
43 VariableKind::Continuous => "continuous",
44 VariableKind::Integer => "integer",
45 VariableKind::Binary => "binary",
46 }
47 }
48
49 pub fn is_integral(self) -> bool {
51 self != VariableKind::Continuous
52 }
53}
54
55#[derive(Clone, Debug, PartialEq)]
65pub struct ModelData {
66 pub sense: Sense,
68 pub objective: Vec<f64>,
70 pub objective_offset: f64,
72 pub variables: Bounds,
74 pub kinds: Vec<VariableKind>,
76 pub matrix: CsrMatrix,
78 pub rows: Bounds,
80}
81
82#[derive(Clone, Debug, PartialEq)]
94pub struct Model {
95 data: ModelData,
96}
97
98impl Model {
99 pub fn new(data: ModelData) -> Result<Self> {
101 let n = data.objective.len();
102 let m = data.rows.len();
103 ensure(n > 0, || "at least one variable is required".into())?;
104 ensure_finite(&data.objective, "objective")?;
105 ensure(data.objective_offset.is_finite(), || {
106 format!("objective offset is {}", data.objective_offset)
107 })?;
108 data.variables.validate(n, "variable bounds")?;
109 ensure(data.kinds.len() == n, || {
110 format!(
111 "variable kinds: expected {n} entries, got {}",
112 data.kinds.len()
113 )
114 })?;
115 for (j, kind) in data.kinds.iter().enumerate() {
116 if *kind == VariableKind::Binary {
117 let (l, u) = (data.variables.lower[j], data.variables.upper[j]);
118 ensure(l >= 0.0 && u <= 1.0, || {
119 format!("variable {j} is binary but has bounds [{l}, {u}] outside [0, 1]")
120 })?;
121 }
122 }
123 data.rows.validate(m, "row bounds")?;
124 ensure(data.matrix.rows() == m && data.matrix.cols() == n, || {
125 format!(
126 "matrix: shape is {} x {}, expected rows x variables = {m} x {n}",
127 data.matrix.rows(),
128 data.matrix.cols()
129 )
130 })?;
131 data.matrix.validate("matrix")?;
132 Ok(Self { data })
133 }
134
135 pub fn n(&self) -> usize {
137 self.data.objective.len()
138 }
139
140 pub fn m(&self) -> usize {
142 self.data.rows.len()
143 }
144
145 pub fn nnz(&self) -> usize {
147 self.data.matrix.nnz()
148 }
149
150 pub fn sense(&self) -> Sense {
152 self.data.sense
153 }
154
155 pub fn objective(&self) -> &[f64] {
157 &self.data.objective
158 }
159
160 pub fn objective_offset(&self) -> f64 {
162 self.data.objective_offset
163 }
164
165 pub fn variables(&self) -> &Bounds {
167 &self.data.variables
168 }
169
170 pub fn kinds(&self) -> &[VariableKind] {
172 &self.data.kinds
173 }
174
175 pub fn matrix(&self) -> &CsrMatrix {
177 &self.data.matrix
178 }
179
180 pub fn rows(&self) -> &Bounds {
182 &self.data.rows
183 }
184
185 pub fn is_mip(&self) -> bool {
187 self.data.kinds.iter().any(|k| k.is_integral())
188 }
189
190 pub fn data(&self) -> &ModelData {
192 &self.data
193 }
194
195 pub fn into_data(self) -> ModelData {
197 self.data
198 }
199}