Skip to content

Commit

Permalink
Improved solution printer
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Oct 6, 2024
1 parent b19773c commit c2c52b6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/or_tools/cp_solver_solution_callback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions test/solution_printer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c2c52b6

Please sign in to comment.