diff --git a/lib/or_tools/cp_solver_solution_callback.rb b/lib/or_tools/cp_solver_solution_callback.rb index c71509a..29bc3ba 100644 --- a/lib/or_tools/cp_solver_solution_callback.rb +++ b/lib/or_tools/cp_solver_solution_callback.rb @@ -5,16 +5,16 @@ class CpSolverSolutionCallback def value(expr) case expr when SatIntVar - @response.solution_integer_value(expr) + @response&.solution_integer_value(expr) when BoolVar - @response.solution_boolean_value(expr) + @response&.solution_boolean_value(expr) else raise "Unsupported type: #{expr.class.name}" end end def objective_value - @response.objective_value + @response&.objective_value end end end diff --git a/test/solution_printer_test.rb b/test/solution_printer_test.rb index aca63b2..e3129c0 100644 --- a/test/solution_printer_test.rb +++ b/test/solution_printer_test.rb @@ -10,11 +10,17 @@ def test_objective_solution_printer solver = ORTools::CpSolver.new solver.parameters.enumerate_all_solutions = true solution_printer = ORTools::ObjectiveSolutionPrinter.new + assert_nil solution_printer.objective_value + stdout, _ = capture_io do solver.solve(model, solution_printer) end assert_equal 15, solution_printer.solution_count assert_match "Solution 14", stdout + + # ensure @response is still valid after solve + GC.start + assert_equal 0, solution_printer.objective_value end def test_var_array_solution_printer