From c2c52b68d2d99c1cd46a42ec54eade95d6d646fd Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Sun, 6 Oct 2024 14:40:41 -0700 Subject: [PATCH] Improved solution printer --- lib/or_tools/cp_solver_solution_callback.rb | 6 +++--- test/solution_printer_test.rb | 6 ++++++ 2 files changed, 9 insertions(+), 3 deletions(-) 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