@@ -23,15 +23,20 @@ pub fn coin_cbc(to_solve: UnsolvedProblem) -> CoinCbcProblem {
23
23
variables,
24
24
} = to_solve;
25
25
let mut model = Model :: default ( ) ;
26
+ let mut initial_solution = vec ! [ ] ;
26
27
let columns: Vec < Col > = variables
27
- . into_iter ( )
28
+ . iter_variables_with_def ( )
28
29
. map (
29
- |VariableDefinition {
30
- min,
31
- max,
32
- is_integer,
33
- ..
34
- } | {
30
+ |(
31
+ var,
32
+ & VariableDefinition {
33
+ min,
34
+ max,
35
+ initial,
36
+ is_integer,
37
+ ..
38
+ } ,
39
+ ) | {
35
40
let col = model. add_col ( ) ;
36
41
// Variables are created with a default min of 0
37
42
model. set_col_lower ( col, min) ;
@@ -41,6 +46,9 @@ pub fn coin_cbc(to_solve: UnsolvedProblem) -> CoinCbcProblem {
41
46
if is_integer {
42
47
model. set_integer ( col) ;
43
48
}
49
+ if let Some ( val) = initial {
50
+ initial_solution. push ( ( var, val) ) ;
51
+ } ;
44
52
col
45
53
} ,
46
54
)
@@ -52,12 +60,16 @@ pub fn coin_cbc(to_solve: UnsolvedProblem) -> CoinCbcProblem {
52
60
ObjectiveDirection :: Maximisation => Sense :: Maximize ,
53
61
ObjectiveDirection :: Minimisation => Sense :: Minimize ,
54
62
} ) ;
55
- CoinCbcProblem {
63
+ let mut problem = CoinCbcProblem {
56
64
model,
57
65
columns,
58
66
has_sos : false ,
59
67
mip_gap : None ,
68
+ } ;
69
+ if initial_solution. len ( ) > 0 {
70
+ problem = problem. with_initial_solution ( initial_solution) ;
60
71
}
72
+ problem
61
73
}
62
74
63
75
/// A coin-cbc model
@@ -234,7 +246,7 @@ impl WithMipGap for CoinCbcProblem {
234
246
235
247
#[ cfg( test) ]
236
248
mod tests {
237
- use crate :: { variables, Solution , SolverModel , WithInitialSolution } ;
249
+ use crate :: { variable , variables, Solution , SolverModel , WithInitialSolution } ;
238
250
use float_eq:: assert_float_eq;
239
251
240
252
#[ test]
@@ -261,4 +273,23 @@ mod tests {
261
273
let sol = pb. solve ( ) . unwrap ( ) ;
262
274
assert_float_eq ! ( sol. value( v) , limit, abs <= 1e-8 ) ;
263
275
}
276
+
277
+ #[ test]
278
+ fn solve_problem_with_initial_variable_values ( ) {
279
+ let limit = 3.0 ;
280
+ // Solve problem once
281
+ variables ! {
282
+ vars:
283
+ 0.0 <= v <= limit;
284
+ } ;
285
+ let pb = vars. maximise ( v) . using ( super :: coin_cbc) ;
286
+ let sol = pb. solve ( ) . unwrap ( ) ;
287
+ assert_float_eq ! ( sol. value( v) , limit, abs <= 1e-8 ) ;
288
+ // Recreate problem and solve with initial solution
289
+ let mut vars = variables ! ( ) ;
290
+ let v = vars. add ( variable ( ) . min ( 0 ) . max ( limit) . initial ( 2 ) ) ;
291
+ let pb = vars. maximise ( v) . using ( super :: coin_cbc) ;
292
+ let sol = pb. solve ( ) . unwrap ( ) ;
293
+ assert_float_eq ! ( sol. value( v) , limit, abs <= 1e-8 ) ;
294
+ }
264
295
}
0 commit comments