Skip to content

Commit

Permalink
[CP-SAT] fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lperron committed Dec 29, 2024
1 parent 8eb9983 commit 3eca4e7
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
4 changes: 3 additions & 1 deletion ortools/sat/python/cp_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2410,7 +2410,9 @@ def solve(
if self.best_bound_callback is not None:
self.__solve_wrapper.add_best_bound_callback(self.best_bound_callback)

self.__response_wrapper = self.__solve_wrapper.solve(model.proto)
self.__response_wrapper = (
self.__solve_wrapper.solve_and_return_response_wrapper(model.proto)
)

if solution_callback is not None:
self.__solve_wrapper.clear_solution_callback(solution_callback)
Expand Down
10 changes: 1 addition & 9 deletions ortools/sat/python/cp_model_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,14 +358,6 @@ def testLtVar(self) -> None:
self.assertEqual(cp_model.INT_MIN, ct.linear.domain[0])
self.assertEqual(0, ct.linear.domain[1])

def testSimplification1(self) -> None:
print("testSimplification1")
model = cp_model.CpModel()
x = model.new_int_var(-10, 10, "x")
prod = (x * 2) * 2
self.assertEqual(x, prod.expression())
self.assertEqual(4, prod.coefficient())

def testLinearNonEqualWithConstant(self) -> None:
print("testLinearNonEqualWithConstant")
model = cp_model.CpModel()
Expand Down Expand Up @@ -1908,7 +1900,7 @@ def testIssue4376MinimizeModel(self) -> None:
if status == cp_model.OPTIMAL:
self.assertLess(
time.time(),
max(best_bound_callback.last_time, solution_callback.last_time) + 5.0,
max(best_bound_callback.last_time, solution_callback.last_time) + 9.0,
)

def testIssue4434(self) -> None:
Expand Down
6 changes: 6 additions & 0 deletions ortools/sat/python/swig_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,12 @@ PYBIND11_MODULE(swig_helper, m) {
arg("best_bound_callback"))
.def("set_parameters", &SolveWrapper::SetParameters, arg("parameters"))
.def("solve",
[](SolveWrapper* solve_wrapper,
const CpModelProto& model_proto) -> CpSolverResponse {
::pybind11::gil_scoped_release release;
return solve_wrapper->Solve(model_proto);
})
.def("solve_and_return_response_wrapper",
[](SolveWrapper* solve_wrapper,
const CpModelProto& model_proto) -> ResponseWrapper {
::py::gil_scoped_release release;
Expand Down
10 changes: 5 additions & 5 deletions ortools/sat/python/swig_helper_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def testSimpleSolve(self):
self.assertTrue(text_format.Parse(model_string, model))

solve_wrapper = swig_helper.SolveWrapper()
response_wrapper = solve_wrapper.solve(model)
response_wrapper = solve_wrapper.solve_and_return_response_wrapper(model)

self.assertEqual(cp_model_pb2.OPTIMAL, response_wrapper.status())
self.assertEqual(30.0, response_wrapper.objective_value())
Expand Down Expand Up @@ -153,7 +153,7 @@ def testSimpleSolveWithCore(self):

solve_wrapper = swig_helper.SolveWrapper()
solve_wrapper.set_parameters(parameters)
response_wrapper = solve_wrapper.solve(model)
response_wrapper = solve_wrapper.solve_and_return_response_wrapper(model)

self.assertEqual(cp_model_pb2.OPTIMAL, response_wrapper.status())
self.assertEqual(30.0, response_wrapper.objective_value())
Expand All @@ -175,7 +175,7 @@ def testSimpleSolveWithProtoApi(self):
model.objective.scaling_factor = -1

solve_wrapper = swig_helper.SolveWrapper()
response_wrapper = solve_wrapper.solve(model)
response_wrapper = solve_wrapper.solve_and_return_response_wrapper(model)

self.assertEqual(cp_model_pb2.OPTIMAL, response_wrapper.status())
self.assertEqual(30.0, response_wrapper.objective_value())
Expand All @@ -197,7 +197,7 @@ def testSolutionCallback(self):
params = sat_parameters_pb2.SatParameters()
params.enumerate_all_solutions = True
solve_wrapper.set_parameters(params)
response_wrapper = solve_wrapper.solve(model)
response_wrapper = solve_wrapper.solve_and_return_response_wrapper(model)

self.assertEqual(5, callback.solution_count())
self.assertEqual(cp_model_pb2.OPTIMAL, response_wrapper.status())
Expand Down Expand Up @@ -226,7 +226,7 @@ def testBestBoundCallback(self):
params.linearization_level = 2
params.log_search_progress = True
solve_wrapper.set_parameters(params)
response_wrapper = solve_wrapper.solve(model)
response_wrapper = solve_wrapper.solve_and_return_response_wrapper(model)

self.assertEqual(2.6, best_bound_callback.best_bound)
self.assertEqual(cp_model_pb2.OPTIMAL, response_wrapper.status())
Expand Down

0 comments on commit 3eca4e7

Please sign in to comment.