Skip to content

Commit 266fa72

Browse files
authored
feat: add getters to inspect variables (#104)
1 parent 08e0898 commit 266fa72

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

src/variable.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,21 @@ impl VariableDefinition {
157157
self
158158
}
159159

160+
/// Returns `true` the variable has an integer constraint, and `false`
161+
/// otherwise. Note that an integer constraint can be set by adding either
162+
/// an integer or a binary constraint.
163+
///
164+
/// ```
165+
/// # use good_lp::variable;
166+
/// let var = variable();
167+
/// assert_eq!(var.is_integer(), false);
168+
/// let var = var.binary();
169+
/// assert_eq!(var.is_integer(), true);
170+
/// ```
171+
pub fn is_integer(&self) -> bool {
172+
self.is_integer
173+
}
174+
160175
/// Define the variable as an integer that can only take the value 0 or 1.
161176
///
162177
/// **Warning**: not all solvers support integer variables.
@@ -207,6 +222,33 @@ impl VariableDefinition {
207222
self
208223
}
209224

225+
/// Returns the optional initial value of this variable.
226+
///
227+
/// ```
228+
/// # use good_lp::variable;
229+
/// let var = variable();
230+
/// assert_eq!(var.get_initial(), None);
231+
/// let var = var.initial(42);
232+
/// assert_eq!(var.get_initial(), Some(42.0));
233+
/// ```
234+
pub fn get_initial(&self) -> Option<f64> {
235+
self.initial
236+
}
237+
238+
/// Returns `true` the variable has an initial solution, and `false`
239+
/// otherwise.
240+
///
241+
/// ```
242+
/// # use good_lp::variable;
243+
/// let var = variable();
244+
/// assert_eq!(var.has_initial(), false);
245+
/// let var = var.initial(42);
246+
/// assert_eq!(var.has_initial(), true);
247+
/// ```
248+
pub fn has_initial(&self) -> bool {
249+
self.initial.is_some()
250+
}
251+
210252
/// Set the name of the variable. This is useful in particular when displaying the problem
211253
/// for debugging purposes.
212254
///
@@ -221,6 +263,19 @@ impl VariableDefinition {
221263
self
222264
}
223265

266+
/// Returns the name of the variable.
267+
///
268+
/// ```
269+
/// # use good_lp::variable;
270+
/// let var = variable();
271+
/// assert_eq!(var.get_name(), "");
272+
/// let var = var.name("my variable");
273+
/// assert_eq!(var.get_name(), "my variable");
274+
/// ```
275+
pub fn get_name(&self) -> &str {
276+
&self.name
277+
}
278+
224279
/// Set the lower and/or higher bounds of the variable
225280
///
226281
/// ## Examples
@@ -261,12 +316,39 @@ impl VariableDefinition {
261316
self.min = min.into();
262317
self
263318
}
319+
320+
/// Returns the minimum bound of the variable.
321+
///
322+
/// ```
323+
/// # use good_lp::variable;
324+
/// let var = variable();
325+
/// assert_eq!(var.get_min(), f64::NEG_INFINITY);
326+
/// let var = var.min(0);
327+
/// assert_eq!(var.get_min(), 0.0);
328+
/// ```
329+
pub fn get_min(&self) -> f64 {
330+
self.min
331+
}
332+
264333
/// Set the higher bound of the variable
265334
pub fn max<N: Into<f64>>(mut self, max: N) -> Self {
266335
self.max = max.into();
267336
self
268337
}
269338

339+
/// Returns the maximum bound of the variable.
340+
///
341+
/// ```
342+
/// # use good_lp::variable;
343+
/// let var = variable();
344+
/// assert_eq!(var.get_max(), f64::INFINITY);
345+
/// let var = var.max(0);
346+
/// assert_eq!(var.get_max(), 0.0);
347+
/// ```
348+
pub fn get_max(&self) -> f64 {
349+
self.max
350+
}
351+
270352
/// Set both the lower and higher bounds of the variable
271353
pub fn clamp<N1: Into<f64>, N2: Into<f64>>(self, min: N1, max: N2) -> Self {
272354
self.min(min).max(max)

0 commit comments

Comments
 (0)