@@ -256,6 +256,20 @@ impl SCIPProblem {
256256 pub fn get_option < T : ScipOptionGetValue > ( self , option : & str ) -> T {
257257 T :: get_for ( self . model , option)
258258 }
259+
260+ /// Set an initial solution for the problem.
261+ /// Returns an error if the solution is not feasible.
262+ fn try_with_initial_solution (
263+ self ,
264+ solution : impl IntoIterator < Item = ( Variable , f64 ) > ,
265+ ) -> Result < Self , russcip:: SolError > {
266+ let sol = self . model . create_orig_sol ( ) ;
267+ for ( var, val) in solution {
268+ sol. set_val ( & self . id_for_var [ & var] , val) ;
269+ }
270+ self . model . add_sol ( sol) ?;
271+ Ok ( self )
272+ }
259273}
260274
261275impl CardinalityConstraintSolver for SCIPProblem {
@@ -335,13 +349,14 @@ impl SolverModel for SCIPProblem {
335349}
336350
337351impl WithInitialSolution for SCIPProblem {
352+ /// Set an initial solution for the problem.
353+ ///
354+ /// # Panics
355+ ///
356+ /// Panics if the solution is not feasible.
338357 fn with_initial_solution ( self , solution : impl IntoIterator < Item = ( Variable , f64 ) > ) -> Self {
339- let sol = self . model . create_sol ( ) ;
340- for ( var, val) in solution {
341- sol. set_val ( & self . id_for_var [ & var] , val) ;
342- }
343- self . model . add_sol ( sol) . expect ( "could not set solution" ) ;
344- self
358+ self . try_with_initial_solution ( solution)
359+ . expect ( "could not set solution" )
345360 }
346361}
347362impl WithMipGap for SCIPProblem {
@@ -487,7 +502,7 @@ mod tests {
487502 . unwrap ( ) ;
488503 // Recreate same problem with initial values slightly off
489504 let initial_x = solution. value ( x) - 0.1 ;
490- let initial_y = solution. value ( x ) - 1.0 ;
505+ let initial_y = solution. value ( y ) - 1.0 ;
491506 let mut vars = variables ! ( ) ;
492507 let x = vars. add ( variable ( ) . clamp ( 0 , 2 ) ) ;
493508 let y = vars. add ( variable ( ) . clamp ( 1 , 3 ) ) ;
@@ -516,7 +531,7 @@ mod tests {
516531 . unwrap ( ) ;
517532 // Recreate same problem with initial values slightly off
518533 let initial_x = solution. value ( x) - 0.1 ;
519- let initial_y = solution. value ( x ) - 1.0 ;
534+ let initial_y = solution. value ( y ) - 1.0 ;
520535 let mut vars = variables ! ( ) ;
521536 let x = vars. add ( variable ( ) . clamp ( 0 , 2 ) . initial ( initial_x) ) ;
522537 let y = vars. add ( variable ( ) . clamp ( 1 , 3 ) . initial ( initial_y) ) ;
0 commit comments