ankane / or-tools-ruby

Operations research tools for Ruby
Apache License 2.0
171 stars 20 forks source link

Add solution_info to CpSolver #43

Closed johnnymo87 closed 1 year ago

johnnymo87 commented 1 year ago

Hi @ankane, I'm not sure about this PR, but I figured that I'd open it to get your feedback.

I was playing around with division in the CP-SAT solver, and I ran into a problem where I got back a model_invalid status from the solver.

The docs suggest multiple things for debugging this, such as calling ValidateCpModel(model_proto) for C++ or model.Validate() for python.

Less documented, however, is the solution_info attribute in the CpSolverResponse proto, which SolveCpModel sets with an error message if there is one while doing validation before solving.

To debug my problem locally, I ultimately took this solution_info path. And I was curious about your thoughts on it. I'm not sure if we want to go down the path of exposing CpSolverResponse.solution_info() when to my casual reading it seems less documented compared to ValidateCpModel(model_proto). But I thought I'd offer this up here to start a discussion.

ankane commented 1 year ago

Thanks @johnnymo87, looks great! fwiw, Python has this method as well.

from ortools.sat.python import cp_model

model = cp_model.CpModel()

x = model.NewIntVar(0, 10, 'x')
y = model.NewIntVar(0, 10, 'y')
z = model.NewIntVar(0, 10, 'z')

model.AddDivisionEquality(x, y, z)

solver = cp_model.CpSolver()
status = solver.Solve(model)
print(status)
print(solver.SolutionInfo())