I tried very hard to do this, here is my closest attempt.
from constraint import *
# Define variables
problem = Problem()
teams = range(1, 8)
days = range(1, 5)
slots = range(1, 7)
# Define domains
for day in days:
for slot in slots:
problem.addVariable(f"Day {day} Slot {slot}", teams)
# Define constraints
for team in teams:
# Each team should stir the pot of dodol for at least 8 hours during the entire dodol feast.
problem.addConstraint(SomeInSetConstraint({team}, 8), [f"Day {day} Slot {slot}" for day in days for slot in slots])
# Each team should stir the pot of dodol for no more than 4 hours a day.
for day in days:
problem.addConstraint(SomeInSetConstraint({team}, 4), [f"Day {day} Slot {slot}" for slot in slots])
# Each team should get sufficient rest before their next turn to stir the pot of dodol.
for day in days:
for slot in slots:
if slot < 6:
problem.addConstraint(lambda a, b, team=team: a == team and b != team, [f"Day {day} Slot {slot}", f"Day {day} Slot {slot+1}"])
else:
if day < 4: # to avoid index out of range
problem.addConstraint(lambda a, b, team=team: a == team and b != team, [f"Day {day} Slot {slot}", f"Day {day+1} Slot {1}"])
# Find a solution
solution = problem.getSolution()
# Print the solution
if solution:
for day in days:
print(f"Day {day}")
print("12AM > 4AM: Team", solution[f"Day {day} Slot 1"])
print("4AM > 8AM: Team", solution[f"Day {day} Slot 2"])
print("8AM > 12PM: Team", solution[f"Day {day} Slot 3"])
print("12PM > 4PM: Team", solution[f"Day {day} Slot 4"])
print("4PM > 8PM: Team", solution[f"Day {day} Slot 5"])
print("8PM > 12AM: Team", solution[f"Day {day} Slot 6"])
print()
else:
print("No solution found.")
Output:
No solution found.
Attempt#2
from constraint import Problem, InSetConstraint
# Create a problem instance
problem = Problem()
# Define the variables
# Each variable represents a 4-hour time slot over the 4 days
# The domain of each variable is the set of teams (1 to 7)
time_slots = ["Day{}_{}".format(day, slot) for day in range(1, 5) for slot in range(1, 7)]
problem.addVariables(time_slots, range(1, 8))
# Define the constraints
# Each team must stir the pot of dodol for at least 8 hours during the entire feast.
for team in range(1, 8):
problem.addConstraint(InSetConstraint([team]), ["Day{}_{}".format(day, slot) for day in range(1, 5) for slot in range(1, 3)])
# No team can stir the pot of dodol for more than 4 hours a day.
for day in range(1, 5):
for team in range(1, 8):
problem.addConstraint(InSetConstraint([team]), ["Day{}_{}".format(day, slot) for slot in range(1, 2)])
# Solve the problem
solutions = problem.getSolutions()
# Print the solutions
for solution in solutions:
print("Solution:")
for day in range(1, 5):
print("Day {}:".format(day))
for slot in range(1, 7):
print(" Slot {}: Team {}".format(slot, solution["Day{}_{}".format(day, slot)]))
Output:
No output
Attempt#3
import constraint
villagers_teams = ["team1", "team2", "team3",
"team4", "team5", "team6", "team7"]
allocated_time = ["D1_Hr1", "D1_Hr2", "D1_Hr3", "D1_Hr4", "D2_Hr1", "D2_Hr2", "D2_Hr3", "D2_Hr4",
"D3_Hr1", "D3_Hr2", "D3_Hr3", "D3_Hr4", "D4_Hr1", "D4_Hr2", "D4_Hr3", "D4_Hr4"]
problem = constraint.Problem()
# add the variables and the domain into the problem
for t in villagers_teams:
problem.addVariables([t + time for time in allocated_time], [0,1])
# define the constraints
def stir_for_four_hours(*args):
return sum(args) == 4
def max_four_hours_a_day(*args):
return sum(args) <= 4
def stir_at_least_eight_hours(*args):
return sum(args) >= 8
# Adding the constraints to the problem
for t in villagers_teams:
for day in range(1, 5):
problem.addConstraint(stir_for_four_hours, [t + f"D{day}_Hr{i}" for i in range(1, 5)])
problem.addConstraint(max_four_hours_a_day, [t + f"D{day}_Hr{i}" for i in range(1, 5)])
problem.addConstraint(stir_at_least_eight_hours, [t + f"D{day}_Hr{i}" for day in range(1, 5) for i in range(1, 5)])
# Solve the problem
solutions = problem.getSolutions()
# Print out the solutions
for i, s in enumerate(solutions, start = 1):
print(f"Solution {i}:")
for t in villagers_teams:
print(f"{t}: {[s[t + time] for time in allocated_time]}")
print()
Output:
Solution 1:
team1: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
team2: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
team3: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
team4: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
team5: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
team6: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
team7: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
(It is assigning 112 hours, need to make it to assign 96 hours)
Attempt #4
import constraint
villagers_teams = ["team1", "team2", "team3", "team4", "team5", "team6", "team7"]
allocated_time = ["D1_Hr1", "D1_Hr2", "D1_Hr3", "D1_Hr4", "D2_Hr1", "D2_Hr2", "D2_Hr3", "D2_Hr4",
"D3_Hr1", "D3_Hr2", "D3_Hr3", "D3_Hr4", "D4_Hr1", "D4_Hr2", "D4_Hr3", "D4_Hr4"]
problem = constraint.Problem()
# Add the variables and the domain into the problem
for t in villagers_teams:
problem.addVariables([t + '_' + time for time in allocated_time], [0, 1])
# Define the constraints
def stir_for_four_hours(*args):
return sum(args) == 4
def max_four_hours_a_day(*args):
return sum(args) <= 4
def stir_at_least_eight_hours(*args):
return sum(args) >= 8
def total_hours_constraint(*villagers_teams):
total_hours = sum(1 for team in villagers_teams if team is not None)
return total_hours == 96
# Adding the constraints to the problem
for day in range(1, 5):
for t in villagers_teams:
problem.addConstraint(stir_for_four_hours, [t + f"_D{day}_Hr{i}" for i in range(1, 5)])
problem.addConstraint(max_four_hours_a_day, [t + f"_D{day}_Hr{i}" for i in range(1, 5)])
problem.addConstraint(stir_at_least_eight_hours, [t + f"_D{day}_Hr{i}" for t in villagers_teams for i in range(1, 5)])
problem.addConstraint(total_hours_constraint, [f"{day}Hr{slot}" for day in days for slot in slots])
# Solve the problem
solutions = problem.getSolutions()
# Print out the solutions
for i, s in enumerate(solutions, start=1):
print(f"Solution {i}:")
for t in villagers_teams:
print(f"{t}: {[s[t + '_' + time] for time in allocated_time]}")
print()
Output:
Solution 1:
team1: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
team2: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
team3: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
team4: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
team5: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
team6: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
team7: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
Attempt #5
import constraint
# Define the teams and the time slots
teams = ["team1", "team2", "team3", "team4", "team5", "team6", "team7"]
time_slots = [f"D{day}_Hr{hour}" for day in range(1, 5) for hour in range(1, 7)]
# Create a problem instance
problem = constraint.Problem()
# Add variables (each time slot can be assigned to one team)
for slot in time_slots:
problem.addVariable(slot, teams)
# Each team must stir for at least 8 hours (two time slots)
for team in teams:
problem.addConstraint(lambda assignment, team=team: list(assignment.values()).count(team) >= 2, time_slots)
# No team can stir for more than 4 hours a day (one time slot per day)
for team in teams:
for day in range(1, 5):
problem.addConstraint(lambda assignment, team=team, day=day: sum(value == team and f"D{day}_" in slot for slot, value in assignment.items()) <= 1, time_slots)
# Solve the problem
solutions = problem.getSolutions()
# Print out the solutions
for i, solution in enumerate(solutions, start=1):
print(f"Solution {i}:")
for slot, team in solution.items():
print(f"{slot}: {team}")
print()
output:
TypeError: () takes from 1 to 2 positional arguments but 24 were given
Attempt #6
import constraint
# Define the problem
problem = constraint.Problem()
# Define the teams
teams = ["team1", "team2", "team3", "team4", "team5", "team6", "team7"]
# Define the time slots
time_slots = ["D1_Hr1", "D1_Hr2", "D1_Hr3", "D1_Hr4", "D2_Hr1", "D2_Hr2", "D2_Hr3", "D2_Hr4",
"D3_Hr1", "D3_Hr2", "D3_Hr3", "D3_Hr4"]
# Each time slot can be assigned to any team
for time_slot in time_slots:
problem.addVariable(time_slot, teams)
# Define a function to count the total time assigned to each team
def count_time(*assigned_teams):
from collections import Counter
team_counts = Counter(assigned_teams)
for team in teams:
if team_counts[team] > 24: # Each team should not be assigned more than 24 hours
return False
return True
# Add the constraint to the problem
problem.addConstraint(count_time, time_slots)
# Get the solutions
solutions = problem.getSolution()
# Print the first solution
if solutions:
for key, value in solutions.items():
print(key + ': ' + value)
else:
print("No solutions found.")
output:
D1_Hr1: team7
D1_Hr2: team7
D1_Hr3: team7
D1_Hr4: team7
D2_Hr1: team7
D2_Hr2: team7
D2_Hr3: team7
D2_Hr4: team7
D3_Hr1: team7
D3_Hr2: team7
D3_Hr3: team7
D3_Hr4: team7
Attempt #8
import constraint
# Define the problem
problem = constraint.Problem()
# Define the teams
teams = ["team1", "team2", "team3", "team4", "team5", "team6", "team7"]
# Define the time slots
time_slots = ["D1_Hr1", "D1_Hr2", "D1_Hr3", "D1_Hr4", "D1_Hr5", "D1_Hr6",
"D2_Hr1", "D2_Hr2", "D2_Hr3", "D2_Hr4","D2_Hr5", "D2_Hr6",
"D3_Hr1", "D3_Hr2", "D3_Hr3", "D3_Hr4","D3_Hr5", "D3_Hr6",
"D4_Hr1", "D4_Hr2", "D4_Hr3", "D4_Hr4","D4_Hr5", "D4_Hr6"]
# Each time slot can be assigned to any team
for time_slot in time_slots:
problem.addVariable(time_slot, teams)
# Define a function to count the total time assigned to each team
def count_time(*assigned_teams):
from collections import Counter
team_counts = Counter(assigned_teams)
for team in teams:
if team_counts[team] > 24: # Each team should not be assigned more than 24 hours
return False
return True
# Add the constraint to the problem
problem.addConstraint(count_time, time_slots)
# Get the solutions
solutions = problem.getSolution()
# Print the first solution
if solutions:
for key, value in solutions.items():
print(key + ': ' + value)
else:
print("No solutions found.")
Output:
D1_Hr1: team7
D1_Hr2: team7
D1_Hr3: team7
D1_Hr4: team7
D1_Hr5: team7
D1_Hr6: team7
D2_Hr1: team7
D2_Hr2: team7
D2_Hr3: team7
D2_Hr4: team7
D2_Hr5: team7
D2_Hr6: team7
D3_Hr1: team7
D3_Hr2: team7
D3_Hr3: team7
D3_Hr4: team7
D3_Hr5: team7
D3_Hr6: team7
D4_Hr1: team7
D4_Hr2: team7
D4_Hr3: team7
D4_Hr4: team7
D4_Hr5: team7
D4_Hr6: team7
Attempt #9
import constraint
# Define the problem
problem = constraint.Problem()
# Define the teams
teams = ["team1", "team2", "team3", "team4", "team5", "team6", "team7"]
# Define the time slots
time_slots = ["D{}_Hr{}".format(i, j) for i in range(1, 5) for j in range(1, 7)]
# Each time slot can be assigned to any team
for time_slot in time_slots:
problem.addVariable(time_slot, teams)
# Define a function to count the total time assigned to each team
def count_time(*assigned_teams):
from collections import Counter
team_counts = Counter(assigned_teams)
for team in teams:
if team_counts[team] > 8: # Each team should not be assigned more than 8 hours in total
return False
if any(sum(1 for t in assigned_teams[i:i+6] if t == team) > 4 for i in range(0, len(assigned_teams), 6)): # Each team should not be assigned more than 4 hours a day
return False
return True
# Add the constraint to the problem
problem.addConstraint(count_time, time_slots)
# Get the solutions
solutions = problem.getSolutions()
# Print the first solution
if solutions:
for key, value in sorted(solutions[0].items()):
print(key + ': ' + value)
else:
print("No solutions found.")
Output:
Keyboard Interrupt
(I gave up here)