20 lines
659 B
Python
20 lines
659 B
Python
from ortools.sat.python import cp_model
|
|
|
|
|
|
class IntermediateSolutionPrinter(cp_model.CpSolverSolutionCallback):
|
|
"""Callback that prints intermediate solutions."""
|
|
|
|
def __init__(self, variables, *, scale=1.0):
|
|
cp_model.CpSolverSolutionCallback.__init__(self)
|
|
self._variables = variables
|
|
self._solution_count = 0
|
|
|
|
def on_solution_callback(self):
|
|
"""Called each time an improving solution is found."""
|
|
print("\n--- Solution ---")
|
|
for name, var in self._variables.items():
|
|
print(f"{name} = {self.Value(var)}")
|
|
|
|
@property
|
|
def solution_count(self):
|
|
return self._solution_count
|