This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ortools.sat.python import cp_model | |
# Instantiate model and solver | |
model = cp_model.CpModel() | |
solver = cp_model.CpSolver() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 1. Variable | |
army = model.NewIntVar(1, 10000, 'army') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 2. Constraints | |
# variable % mod = target → (target, variable, mod) | |
model.AddModuloEquality(0, army, 13) | |
model.AddModuloEquality(0, army, 19) | |
model.AddModuloEquality(0, army, 37) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Find the variable that satisfies these constraints | |
status = solver.Solve(model) | |
# If a solution has been found, print results | |
if status == cp_model.OPTIMAL or status == cp_model.FEASIBLE: | |
print('================= Solution =================') | |
print(f'Solved in {solver.WallTime():.2f} milliseconds') | |
print() | |
print(f'🪖 Army = {solver.Value(army)}') | |
print() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Instantiate model and solver | |
model = cp_model.CpModel() | |
solver = cp_model.CpSolver() | |
# 1. Variables | |
capacity = 19 | |
bread = model.NewIntVar(0, capacity, 'bread') | |
meat = model.NewIntVar(0, capacity, 'meat') | |
beer = model.NewIntVar(0, capacity, 'beer') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 2. Constraints | |
model.Add(1 * bread | |
+ 3 * meat | |
+ 7 * beer <= capacity) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 3. Objective | |
model.Maximize(3 * bread | |
+ 10 * meat | |
+ 26 * beer) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Solve problem | |
status = solver.Solve(model) | |
# If an optimal solution has been found, print results | |
if status == cp_model.OPTIMAL: | |
print('================= Solution =================') | |
print(f'Solved in {solver.WallTime():.2f} milliseconds') | |
print() | |
print(f'Optimal value = {3*solver.Value(bread)+10*solver.Value(meat)+26*solver.Value(beer)} popularity') | |
print('Food:') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CountSolutions(cp_model.CpSolverSolutionCallback): | |
"""Count the number of solutions.""" | |
def __init__(self): | |
cp_model.CpSolverSolutionCallback.__init__(self) | |
self.__solution_count = 0 | |
def on_solution_callback(self): | |
self.__solution_count += 1 |
OlderNewer