Skip to content

Instantly share code, notes, and snippets.

@mlabonne
Last active April 30, 2022 14:51
Show Gist options
  • Save mlabonne/5391d243f757d6f62cf28aedd021ccd7 to your computer and use it in GitHub Desktop.
Save mlabonne/5391d243f757d6f62cf28aedd021ccd7 to your computer and use it in GitHub Desktop.
model = cp_model.CpModel()
solver = cp_model.CpSolver()
# 1. Variable
army = model.NewIntVar(1, 100000, 'army')
# 2. Constraints
model.AddModuloEquality(0, army, 13)
model.AddModuloEquality(0, army, 19)
model.AddModuloEquality(0, army, 37)
class PrintSolutions(cp_model.CpSolverSolutionCallback):
"""Callback to print every solution."""
def __init__(self, variable):
cp_model.CpSolverSolutionCallback.__init__(self)
self.__variable = variable
def on_solution_callback(self):
print(self.Value(self.__variable))
# Solve with callback
solution_printer = PrintSolutions(army)
solver.parameters.enumerate_all_solutions = True
status = solver.Solve(model, solution_printer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment