From 3eca4e76d735ed5d3f5c30debcbc8d22e0b1deee Mon Sep 17 00:00:00 2001 From: Laurent Perron Date: Sun, 29 Dec 2024 08:19:47 +0100 Subject: [PATCH] [CP-SAT] fix --- ortools/sat/python/cp_model.py | 4 +++- ortools/sat/python/cp_model_test.py | 10 +--------- ortools/sat/python/swig_helper.cc | 6 ++++++ ortools/sat/python/swig_helper_test.py | 10 +++++----- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/ortools/sat/python/cp_model.py b/ortools/sat/python/cp_model.py index 1efe750d37..debc7dd0f3 100644 --- a/ortools/sat/python/cp_model.py +++ b/ortools/sat/python/cp_model.py @@ -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) diff --git a/ortools/sat/python/cp_model_test.py b/ortools/sat/python/cp_model_test.py index ff9027efd7..a9eb2f088b 100644 --- a/ortools/sat/python/cp_model_test.py +++ b/ortools/sat/python/cp_model_test.py @@ -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() @@ -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: diff --git a/ortools/sat/python/swig_helper.cc b/ortools/sat/python/swig_helper.cc index d0f784b625..9ab532ebb4 100644 --- a/ortools/sat/python/swig_helper.cc +++ b/ortools/sat/python/swig_helper.cc @@ -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; diff --git a/ortools/sat/python/swig_helper_test.py b/ortools/sat/python/swig_helper_test.py index bc06ce9809..e96e1c7cd2 100644 --- a/ortools/sat/python/swig_helper_test.py +++ b/ortools/sat/python/swig_helper_test.py @@ -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()) @@ -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()) @@ -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()) @@ -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()) @@ -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())