This commit is contained in:
Laurent Perron
2024-02-20 12:20:30 +01:00
parent 7e5f884e8f
commit d17ee64819

View File

@@ -24,6 +24,8 @@
"""
# [START import]
import weakref
from ortools.constraint_solver import routing_enums_pb2
from ortools.constraint_solver import pywrapcp
# [END import]
@@ -98,20 +100,21 @@ class SolutionCallback:
model: pywrapcp.RoutingModel,
limit: int,
):
self._routing_manager = manager
self._routing_model = model
# We need a weak ref on the routing model to avoid a cycle.
self._routing_manager_ref = weakref.ref(manager)
self._routing_model_ref = weakref.ref(model)
self._counter = 0
self._counter_limit = limit
self.objectives = []
def __call__(self):
objective = int(self._routing_model.CostVar().Value())
objective = int(self._routing_model_ref().CostVar().Value())
if not self.objectives or objective < self.objectives[-1]:
self.objectives.append(objective)
print_solution(self._routing_manager, self._routing_model)
print_solution(self._routing_manager_ref(), self._routing_model_ref())
self._counter += 1
if self._counter > self._counter_limit:
self._routing_model.solver().FinishCurrentSearch()
self._routing_model_ref().solver().FinishCurrentSearch()
# [END solution_callback]