Skip to content

Commit 4ba2ee2

Browse files
committed
feat: allow defining initial values for individual variables
1 parent b58170a commit 4ba2ee2

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/variable.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ impl FormatWithVars for Variable {
118118
pub struct VariableDefinition {
119119
pub(crate) min: f64,
120120
pub(crate) max: f64,
121+
pub(crate) initial: Option<f64>,
121122
pub(crate) name: String,
122123
pub(crate) is_integer: bool,
123124
}
@@ -128,6 +129,7 @@ impl VariableDefinition {
128129
VariableDefinition {
129130
min: f64::NEG_INFINITY,
130131
max: f64::INFINITY,
132+
initial: None,
131133
name: String::new(),
132134
is_integer: false,
133135
}
@@ -177,6 +179,27 @@ impl VariableDefinition {
177179
self
178180
}
179181

182+
/// Set the initial value of the variable. This may help the solver to find a solution significantly faster.
183+
///
184+
/// **Warning**: not all solvers support integer variables.
185+
/// Refer to the documentation of the solver you are using.
186+
///
187+
/// ```
188+
/// # use good_lp::{ProblemVariables, variable, default_solver, SolverModel, Solution};
189+
/// let mut problem = ProblemVariables::new();
190+
/// let x = problem.add(variable().max(3).initial(3));
191+
/// let y = problem.add(variable().max(5).initial(5));
192+
/// if cfg!(not(any(feature="clarabel"))) {
193+
/// let solution = problem.maximise(x + y).using(default_solver).solve().unwrap();
194+
/// assert_eq!(solution.value(x), 3.);
195+
/// assert_eq!(solution.value(y), 5.);
196+
/// }
197+
/// ```
198+
pub fn initial<N: Into<f64>>(mut self, value: N) -> Self {
199+
self.initial = Some(value.into());
200+
self
201+
}
202+
180203
/// Set the name of the variable. This is useful in particular when displaying the problem
181204
/// for debugging purposes.
182205
///

0 commit comments

Comments
 (0)