I will be grateful to anyone who can help me write some Python code to enumerate the 21×2×3 arrays, indexed with i, j and k, which are two-thirds filled with 0's and one-third filled with the values 'Ava', 'Bob', 'Joe', 'Mia', 'Sam', 'Tom', 'Zoe' in such a way that:
fixed the index i you have exactly two empty 2-tuples and one 2-tuple with different non-zero values;
fixed the index k you have exactly fourteen empty 2-tuples and seven 2-tuple with different non-zero values;
fixed the indexes j and k you have a 21-tuple with fourteen zero values and exactly one occurrence of each of the non-zero values, respecting the following constraints:
a) "Ava" can appear only in a row with index 0, 1, 4, 6, 10, 11, 13, 14, 15, 19 or 20;
b) "Bob" can appear only in a row with index 2, 3, 5, 7, 8, 9, 12, 16, 17 or 18;
c) "Joe" can appear only in a row with index 2, 4, 5, 7, 8, 10, 14, 15, 18 or 20;
d) "Mia" can appear only in a row with index 0, 1, 3, 6, 9, 12, 13, 16, 17 or 19;
e) "Sam" can appear only in a row with index 1, 2, 7, 9, 15, 17 or 20;
f) "Tom" can appear only in a row with index 0, 3, 8, 10, 12, 16 or 19;
g) "Zoe" can appear only in a row with index 4, 5, 6, 11, 13, 14 or 18.
As a result I would like to obtain something like this:
[ 0 0 [Tom Mia [ 0 0
0 0 Ava Sam 0 0
0 0 Sam Bob 0 0
0 0 Bob Tom 0 0
0 0 0 0 Joe Zoe
0 0 Joe Zoe 0 0
0 0 0 0 Zoe Ava
Joe Sam 0 0 0 0
0 0 0 0 Tom Bob
0 0 0 0 Mia Sam
Tom Ava 0 0 0 0
Ava Zoe 0 0 0 0
Bob Mia 0 0 0 0
0 0 Mia Ava 0 0
0 0 Zoe Joe 0 0
Sam Joe 0 0 0 0
0 0 0 0 Bob Tom
0 0 0 0 Sam Mia
Zoe Bob 0 0 0 0
Mia Tom 0 0 0 0
0 0 ] 0 0 ] Ava Joe]
Rows represent school classes, columns represent school terms (there are 2 of them), tubes represent class days (there are 3 of them: Monday, Wednesday and Friday). So the first horizontal slice of the above solution means that class 1A has lesson only on Wednesday, in the the first term with teacher Tom and in the second term with teacher Mia. (Teachers can only work in some classes and not in others.)
Thanks in advance!
Update n. 1
As a starting point, I tried to attack the following toy problem:
Enumerate all arrays with a given number of rows and 3 columns which are two-thirds filled with "0" and one-third filled with "1" in such a way that summing the values in each row you always get 1 and summing the values in each column you always get rows / 3.
Finally, after struggling a bit, I think I managed to get a solution with the following code, that I kindly ask you to correct or improve. (I have set rows = 6 because the number of permutations of the obvious solution is 6!/(2!*2!*2!) = 90, whereas setting rows = 21 I would have got 21!/(7!*7!*7!) = 399,072,960 solutions.)
from ortools.sat.python import cp_model
# Create model.
model = cp_model.CpModel()
# Create variables.
rows = 6
columns = 3
x = []
for i in range(rows):
x.append([model.NewBoolVar(f'x[{i}][{j}]') for j in range(columns)])
# Add constraints.
for i in range(rows):
model.Add(sum(x[i]) == 1)
# Uncomment the following four lines of code if you want to solve the slightly more general problem that asks to enumerate
# all boolean arrays, with a given number of rows and columns, filled in such a way that summing the values in each
# row you always get 1 and summing the values in each column you always get no more than the ceiling of (rows / columns).
# if rows % columns != 0:
# for j in range(columns):
# model.Add(sum(x[i][j] for i in range(rows)) <= rows // columns + 1)
# else:
for j in range(columns):
model.Add(sum(x[i][j] for i in range(rows)) == rows // columns)
class MyPrintedSolution():
def __init__(self, sol, sol_number):
self.sol = sol
self.sol_number = sol_number
def PrintReadableTable(self):
print(f'Solution {self.sol_number}, printed in readable form:')
counter = 0
for v in self.sol:
if counter % columns != columns-1:
print(v, end = ' ')
else:
print(v)
counter += 1
print()
def PrintRawSolution(self):
print(f'Solution {self.sol_number}, printed in raw form:')
counter = 0
for v in self.sol:
print(f'{v}', end = '')
counter += 1
print('\n')
class VarArraySolutionPrinter(cp_model.CpSolverSolutionCallback):
def __init__(self, variables, limit):
cp_model.CpSolverSolutionCallback.__init__(self)
self.__variables = variables
self.__solution_count = 0
self.__solution_limit = limit
def solution_count(self):
return self.__solution_count
def on_solution_callback(self):
self.__solution_count += 1
solution = [self.Value(v) for v in self.__variables]
myprint = MyPrintedSolution(solution, self.__solution_count)
myprint.PrintReadableTable()
# myprint.PrintRawSolution()
if self.__solution_count >= self.__solution_limit:
print(f'Stop search after {self.__solution_limit} solutions')
self.StopSearch()
# Create solver and solve model.
solver = cp_model.CpSolver()
# solver.parameters.num_workers = 16 # Solver works better with more workers. (At least 8, 16 if enough cores.)
# solver.parameters.log_search_progress = True
solver.parameters.enumerate_all_solutions = True
# solver.parameters.max_time_in_seconds = 10.0
solution_limit = 100000
solution_printer = VarArraySolutionPrinter([x[i][j] for i in range(rows) for j in range(columns)], solution_limit)
solver.Solve(model, solution_printer)
Update n. 2
Following #Christopher Hamkins' initial roadmap and subsequent precious suggestions, I think I finally got what I wanted, using the following code (although I am of course always open to corrections or further suggestions).
from ortools.sat.python import cp_model
# Create model.
model = cp_model.CpModel()
# Create variables.
classes = 21 # indexed with "i", but one could as well have chosen "c"
terms = 2 # indexed with "j", but one could as well have chosen "t"
days = 3 # indexed with "k", but one could as well have chosen "d"
persons = 8 # indexed with "p"
persons_names = [' 0 ', 'Ava', 'Bob', 'Joe', 'Mia', 'Sam', 'Tom', 'Zoe']
classes_names = ['1A', '1B', '1C', '1D', '1E', '1F', '1G', '2A', '2B', '2C', '2D', '2E', '2F', '2G', '3A', '3B', '3C', '3D', '3E', '3F', '3G']
classes_p = [[] for _ in range(persons)]
classes_p[0] = list(range(classes))
classes_p[1] = [0, 1, 4, 6, 10, 11, 13, 14, 15, 19, 20] # list of classes in which person 1 can work
classes_p[2] = [2, 3, 5, 7, 8, 9, 12, 16, 17, 18] # list of classes in which person 2 can work
classes_p[3] = [2, 4, 5, 7, 8, 10, 14, 15, 18, 20] # list of classes in which person 3 can work
classes_p[4] = [0, 1, 3, 6, 9, 12, 13, 16, 17, 19] # list of classes in which person 4 can work
classes_p[5] = [1, 2, 7, 9, 15, 17, 20] # list of classes in which person 5 can work
classes_p[6] = [0, 3, 8, 10, 12, 16, 19] # list of classes in which person 6 can work
classes_p[7] = [4, 5, 6, 11, 13, 14, 18] # list of classes in which person 7 can work
x = {}
for i in range(classes):
for j in range(terms):
for k in range(days):
for p in range(persons):
x[i, j, k, p] = model.NewBoolVar(f'x[{i}, {j}, {k}, {p}]')
# Add constraints.
"""
For all i, j, k constrain the sum of x[i, j, k, p] over p in the range of people to be equal to 1,
so exactly nobody or one person is selected at a given slot.
"""
for i in range(classes):
for j in range(terms):
for k in range(days):
model.Add(sum(x[i, j, k, p] for p in range(persons)) == 1)
"""
For all i constrain the sum of x[i, j, k, p] over all j, k, p in their respective ranges (except p = 0)
to be exactly equal to 2, so exactly two people are in a given row.
"""
for i in range(classes):
model.Add(sum(x[i, j, k, p] for j in range(terms) for k in range(days) for p in range(1, persons)) == 2)
"""
For all i, k, and for p = 0, add the implications
x[i, 0, k, 0] == x[i, 1, k, 0]
"""
for i in range(classes):
for k in range(days):
model.Add(x[i, 0, k, 0] == x[i, 1, k, 0])
"""
For all i, p (except p = 0), constrain the sum of x[i, j, k, p] over all j and k
to be at most 1.
"""
for i in range(classes):
for p in range(1, persons):
model.Add(sum(x[i, j, k, p] for j in range(terms) for k in range(days)) <= 1)
# for k in range(days): # Equivalent alternative to the previous line of code
# model.AddBoolOr([x[i, 0, k, p].Not(), x[i, 1, k, p].Not])
"""
For all j, k constrain the sum of x[i, j, k, p] over all i, p in their respective ranges (except p = 0)
to be exactly equal to 7, so exactly seven people are in a given column.
"""
for j in range(terms):
for k in range(days):
model.Add(sum(x[i, j, k, p] for i in range(classes) for p in range(1, persons)) == 7)
"""
For all j, k, p (except p = 0) constrain the sum of x[i, j, k, p] over all i
to be exactly equal to 1, so each person appears exactly once in the column.
"""
for j in range(terms):
for k in range(days):
for p in range(1, persons):
model.Add(sum(x[i, j, k, p] for i in range(classes)) == 1)
"""
For all j and k, constrain x[i, j, k, p] == 0 for the row i in which each person p can't appear.
"""
for p in range(persons):
for i in enumerate(set(range(classes)) - set(classes_p[p])):
for j in range(terms):
for k in range(days):
model.Add(x[i[1], j, k, p] == 0)
class MyPrintedSolution():
def __init__(self, sol, sol_number):
self.sol = sol
self.sol_number = sol_number
def PrintReadableTable1(self):
print(f'Solution {self.sol_number}, printed in first readable form:')
print(' | Mon | Wed | Fri ')
print(' Cl | Term1 Term2 | Term1 Term2 | Term1 Term2')
print('----------------------------------------------------', end='')
q = [_ for _ in range(8)] + [_ for _ in range(24, 32)] + [_ for _ in range(8, 16)] + [_ for _ in range(32, 40)] + [_ for _ in range(16, 24)] + [_ for _ in range(40, 48)]
r = []
for i in range(21):
r += [n+48*i for n in q]
shuffled_sol = [self.sol[m] for m in tuple(r)]
counter = 0
for w in shuffled_sol:
if (counter % (persons * days * terms)) == 0:
print('\n ', classes_names[counter // (terms * days * persons)], sep='', end=' |')
if w:
print(' ', persons_names[counter % persons], sep='', end=' ')
counter += 1
print('\n')
def PrintReadableTable2(self):
print(f'Solution {self.sol_number}, printed in second readable form:')
print(' Cl | Term1 Term2 ')
print(' Cl | Mon Wed Fri Mon Wed Fri ')
print('----------------------------------------', end = '')
counter = 0
for v in self.sol:
if (counter % (persons * days * terms)) == 0:
print('\n ', classes_names[counter // (terms * days * persons)], sep = '', end = ' |')
if v:
print(' ', persons_names[counter % persons], sep = '', end = ' ')
counter += 1
print('\n')
def PrintRawSolution(self):
print(f'Solution {self.sol_number}, printed in raw form:')
counter = 0
for v in self.sol:
print(f'{v}', end = '')
counter += 1
print('\n')
class VarArraySolutionPrinter(cp_model.CpSolverSolutionCallback):
def __init__(self, variables, limit):
cp_model.CpSolverSolutionCallback.__init__(self)
self.__variables = variables
self.__solution_count = 0
self.__solution_limit = limit
def solution_count(self):
return self.__solution_count
def on_solution_callback(self):
self.__solution_count += 1
solution = [self.Value(v) for v in self.__variables]
myprint = MyPrintedSolution(solution, self.__solution_count)
myprint.PrintReadableTable1()
# myprint.PrintReadableTable2()
# myprint.PrintRawSolution()
if self.__solution_count >= self.__solution_limit:
print(f'Stop search after {self.__solution_limit} solutions')
self.StopSearch()
# Create solver and solve model.
solver = cp_model.CpSolver()
# solver.parameters.num_workers = 16 # Solver works better with more workers. (At least 8, 16 if enough cores.)
# solver.parameters.log_search_progress = True
solver.parameters.enumerate_all_solutions = True
# solver.parameters.max_time_in_seconds = 10.0
solution_limit = 20
solution_printer = VarArraySolutionPrinter([x[i, j, k, p] for i in range(classes) for j in range(terms) for k in range(days) for p in range(persons)], solution_limit)
status = solver.Solve(model, solution_printer)
Update n. 3
#AirSquid proposed a solution using PuLP which is to me almost as valuable as the one using CP-SAT. It provides only one solution at a time, but (it has other advantages and) one can always get around this by adding some ad hoc further constraints, for example to see a different solution with a certain person in a specific position.
Your "toy" problem is definitely going in the right direction.
For your actual problem, try making a 21×2×3x8 array x indexed with i, j, k and p (for person) of BoolVar's. The last index represents the person, it will need 0 to represent "nobody" and for the rest Ava = 1, Bob = 2, etc., so its max value will be one more than the number of people. If the variable X[i,j,k,p] is true (1) it means that the given person p is present at the index i, j, k. If X[i,j,k,0] is true, it means a 0 = nobody is present at i, j, k.
For all i, j, k, constrain the sum of x[i, j, k, p] for p in the range of people to be equal to 1, so exactly nobody or one person is selected at a given slot.
For point 1: fixed the index i you have exactly two empty 2-tuples and one 2-tuple with different non-zero values:
For all i constrain the sum of x[i, j, k, p] for all j, k, p in their respective ranges (except p = 0) to be exactly equal to 2, so exactly two people are in a given row.
For all i, k, and for p = 0, add the implications
x[i, 0, k, 0] == x[i, 1, k, 0]
This will ensure that if one of the pair is 0, so is the other.
For all i, k and p except p = 0, add the implications
x[i, 0, k, p] implies x[i, 1, k, p].Not and
x[i, 1, k, p] implies x[i, 0, k, p].Not
(Actually one of these alone should be sufficient)
You can directly add an implication with the AddImplication(self, a, b) method, or you can realize that "a implies b" means the same thing as "b or not a" and add the implication with the AddBoolOr method. For the first implication, with x[i, 0, k, p] as a, and x[i, 1, k, p].Not as b, therefore adding:
AddBoolOr([x[i, 0, k, p].Not(), x[i, 1, k, p].Not])
Note that both variables are negated with Not in the expression.
Since the other implication assigns x[i, 1, k, p] as a, and x[i, 0, k, p].Not as b, the resulting expression is exactly the same
AddBoolOr([x[i, 0, k, p].Not(), x[i, 1, k, p].Not])
so you only need to add it once.
This will ensure a tuple will consist of two different people.
Alternative formulation of the last part:
For all i and p except p = 0, constraint the sum of x[i, j, k, p] for all j and k to be exactly equal to 1.
For point 2: fixed the index k you have exactly fourteen empty 2-tuples and seven 2-tuple with different non-zero values;
For all j and k constrain the sum of x[i, j, k, p] for all i and p (except p=0) in their respective ranges to be exactly equal to 7, so exactly seven people are in a given column.
For all j, k, and p (except p = 0) constrain the sum of x[i, j, k, p] over all i to be exactly equal to 1, so each person appears exactly once in the column (that is, once for each value of the indices j and k, for some value of i).
For point 3:
For all j and k, Constrain x[i, j, k, p] == 0 for the row i in which each person p can't appear.
Let us know how it works.
You're taking a pretty big swing if you are new to the trifecta of python, linear programming, and pulp, but the problem you describe is very doable...perhaps the below will get you started. It is a smaller example that should work just fine for the data you have, I just didn't type it all in.
A couple notes:
The below is a linear program. It is "naturally integer" as coded, preventing the need to restrict the domain of the variables to integers, so it is much easier to solve. (A topic for you to research, perhaps).
You could certainly code this up as a constraint problem as well, I'm just not as familiar. You could also code this up like a matrix as you are doing with i, j, k, but most frameworks allow more readable names for the sets.
The teaching day M/W/F is arbitrary and not linked to anything else in the problem, so you can (externally to the problem), just pick 1/3 of the assignments per day from the solution for each course & term.
The transition from the verbiage to the constraint formulation is most of the magic in linear programming and you'd be well suited with an introductory text if you continue along!
Code:
# teacher assignment
import pulp
from itertools import chain
# some data...
teach_days = {'M', 'W', 'F'}
terms = {'Spring', 'Fall'}
courses = {'Math 101', 'English 203', 'Physics 201'}
legal_asmts = { 'Bob': {'Math 101', 'Physics 201'},
'Ann': {'Math 101', 'English 203'},
'Tim': {'English 203'},
'Joe': {'Physics 201'}}
# quick sanity check
assert courses == set.union(*chain(legal_asmts.values())), 'course mismatch'
# set up the problem
prob = pulp.LpProblem('teacher_assignment', pulp.LpMaximize)
# make a 3-tuple index of the term, class, teacher
idx = [(term, course, teacher) for term in terms for course in courses for teacher in legal_asmts.keys()]
assn = pulp.LpVariable.dicts('assign', idx, cat=pulp.LpContinuous, lowBound=0)
# OBJECTIVE: teach as many courses as possible within constraints...
prob += pulp.lpSum(assn)
# CONSTRAINTS
# teach each class no more than once per term
for term in terms:
for course in courses:
prob += pulp.lpSum(assn[term, course, teacher] for teacher in legal_asmts.keys()) <= 1
# each teacher no more than 1 course per term
for term in terms:
for teacher in legal_asmts.keys():
prob += pulp.lpSum(assn[term, course, teacher] for course in courses) <= 1
# each teacher can only teach within legal assmts, and if legal, only teach it once
for teacher in legal_asmts.keys():
for course in courses:
if course in legal_asmts.get(teacher):
prob += pulp.lpSum(assn[term, course, teacher] for term in terms) <= 1
else: # it is not legal assignment
prob += pulp.lpSum(assn[term, course, teacher] for term in terms) <= 0
prob.solve()
#print(prob)
# Inspect results...
for i in idx:
if assn[i].varValue: # will be true if value is non-zero
print(i, assn[i].varValue)
Output:
Coin0008I MODEL read with 0 errors
Option for timeMode changed from cpu to elapsed
Presolve 16 (-10) rows, 12 (-12) columns and 32 (-40) elements
Perturbing problem by 0.001% of 1 - largest nonzero change 0.00010234913 ( 0.010234913%) - largest zero change 0
0 Obj -0 Dual inf 11.99913 (12)
10 Obj 5.9995988
Optimal - objective value 6
After Postsolve, objective 6, infeasibilities - dual 0 (0), primal 0 (0)
Optimal objective 6 - 10 iterations time 0.002, Presolve 0.00
Option for printingOptions changed from normal to all
Total time (CPU seconds): 0.00 (Wallclock seconds): 0.00
('Spring', 'Math 101', 'Bob') 1.0
('Spring', 'Physics 201', 'Joe') 1.0
('Spring', 'English 203', 'Ann') 1.0
('Fall', 'Math 101', 'Ann') 1.0
('Fall', 'Physics 201', 'Bob') 1.0
('Fall', 'English 203', 'Tim') 1.0
EDIT: corrected form of problem
Misunderstood part of the problem statement. The below is fixed. Needed to introduce a binary indicator variable for the day assignment per form and had some fun with tabulate.
Using an LP has the advantage that (with the included obj statement) it will do the best possible within the constraints to teach as much as possible, even if there is a teacher shortage, where a CP will not. A CP on the other hand can enumerate all the combos that satisfy the constraints, the LP cannot.
Code
# teacher assignment
import pulp
from tabulate import tabulate
# some data...
teach_days = {'M', 'W', 'F'}
terms = {'Spring', 'Fall'}
forms = list(range(20))
teach_capable = { "Ava" : [ 0, 1, 4, 6, 10, 11, 13, 14, 15, 19, 20],
"Bob" : [ 2, 3, 5, 7, 8, 9, 12, 16, 17, 18],
"Joe" : [ 2, 4, 5, 7, 8, 10, 14, 15, 18, 20],
"Mia" : [ 0, 1, 3, 6, 9, 12, 13, 16, 17, 19],
"Sam" : [ 1, 2, 7, 9, 15, 17, 20],
"Tom" : [ 0, 3, 8, 10, 12, 16, 19],
"Zoe" : [ 4, 5, 6, 11, 13, 14, 18],}
# set up the problem
prob = pulp.LpProblem('teacher_assignment', pulp.LpMaximize)
# make a 4-tuple index of the day, term, class, teacher
idx = [(day, term, form, teacher)
for day in teach_days
for term in terms
for form in forms
for teacher in teach_capable.keys()]
# variables
assn = pulp.LpVariable.dicts('assign', idx, cat=pulp.LpContinuous, lowBound=0)
form_day = pulp.LpVariable.dicts('form-day',
[(form, day) for form in forms for day in teach_days],
cat=pulp.LpBinary) # inidicator for which day the form uses
# OBJECTIVE: teach as many courses as possible within constraints...
prob += pulp.lpSum(assn)
# CONSTRAINTS
# 1. Teach each form on no more than 1 day
for form in forms:
prob += pulp.lpSum(form_day[form, day] for day in teach_days) <= 1 # limit to 1 day per form
for form in forms:
for day in teach_days:
for term in terms:
# no more than 1 assignment, if this day is the designated "form-day"
prob += pulp.lpSum(assn[day, term, form, teacher] for teacher in teach_capable.keys()) \
<= form_day[form, day]
# 2. Each teacher can only teach within legal assmts, and limit them to teaching that form once
for teacher in teach_capable.keys():
for form in forms:
if form in teach_capable.get(teacher):
prob += pulp.lpSum(assn[day, term, form, teacher] for day in teach_days for term in terms) <= 1
else: # it is not legal assignment
prob += pulp.lpSum(assn[day, term, form, teacher] for day in teach_days for term in terms) <= 0
# 3. Each teacher can only teach on once per day per term
for teacher in teach_capable.keys():
for term in terms:
for day in teach_days:
prob += pulp.lpSum(assn[day, term, form, teacher] for form in forms) <= 1
prob.solve()
print("Status = %s" % pulp.LpStatus[prob.status])
#print(prob)
# gather results...
selections = []
for i in idx:
if assn[i].varValue: # will be true if value is non-zero
selections.append(i)
#print(i, assn[i].varValue)
# Let's try to make some rows for tabulate... hacky but fun
def row_index(label):
"""return the form, column index, and name"""
col = 1
if 'W' in label: col += 2
elif 'F' in label: col += 4
if 'Fall' in label: col += 1
return label[2], col, label[-1]
headers = ['Form', 'Mon-1', 'Mon-2', 'Wed-1', 'Wed-2', 'Fri-1', 'Fri-2']
row_data = [[f,'','','','','',''] for f in forms]
for selection in selections:
form, col, name = row_index(selection)
row_data[form][col] = name
print(tabulate(row_data, headers=headers, tablefmt='grid'))
Output:
Status = Optimal
+--------+---------+---------+---------+---------+---------+---------+
| Form | Mon-1 | Mon-2 | Wed-1 | Wed-2 | Fri-1 | Fri-2 |
+========+=========+=========+=========+=========+=========+=========+
| 0 | | | Ava | Tom | | |
+--------+---------+---------+---------+---------+---------+---------+
| 1 | Mia | Sam | | | | |
+--------+---------+---------+---------+---------+---------+---------+
| 2 | | | | | Sam | Joe |
+--------+---------+---------+---------+---------+---------+---------+
| 3 | | | | | Bob | Mia |
+--------+---------+---------+---------+---------+---------+---------+
| 4 | Ava | Zoe | | | | |
+--------+---------+---------+---------+---------+---------+---------+
| 5 | Zoe | Joe | | | | |
+--------+---------+---------+---------+---------+---------+---------+
| 6 | | | Mia | Zoe | | |
+--------+---------+---------+---------+---------+---------+---------+
| 7 | | | Joe | Sam | | |
+--------+---------+---------+---------+---------+---------+---------+
| 8 | Bob | Tom | | | | |
+--------+---------+---------+---------+---------+---------+---------+
| 9 | | | Sam | Mia | | |
+--------+---------+---------+---------+---------+---------+---------+
| 10 | | | | | Ava | Tom |
+--------+---------+---------+---------+---------+---------+---------+
| 11 | | | Zoe | Ava | | |
+--------+---------+---------+---------+---------+---------+---------+
| 12 | | | Tom | Bob | | |
+--------+---------+---------+---------+---------+---------+---------+
| 13 | | | | | Zoe | Ava |
+--------+---------+---------+---------+---------+---------+---------+
| 14 | | | | | Joe | Zoe |
+--------+---------+---------+---------+---------+---------+---------+
| 15 | Joe | Ava | | | | |
+--------+---------+---------+---------+---------+---------+---------+
| 16 | | | | | Tom | Bob |
+--------+---------+---------+---------+---------+---------+---------+
| 17 | Sam | Bob | | | | |
+--------+---------+---------+---------+---------+---------+---------+
| 18 | | | Bob | Joe | | |
+--------+---------+---------+---------+---------+---------+---------+
| 19 | Tom | Mia | | | | |
+--------+---------+---------+---------+---------+---------+---------+
[Finished in 165ms]
So, here's a funny little programming challenge. I was writing a quick method to determine all the market holidays for a particular year, and then I started reading about Easter and discovered just how crazy* the logic is for determining its date--the first Sunday after the Paschal Full Moon following the spring equinox! Does anybody know of an existing function to calculate the date of Easter for a given year?
Granted, it's probably not all that hard to do; I just figured I'd ask in case somebody's already done this. (And that seems very likely.)
UPDATE: Actually, I'm really looking for the date of Good Friday (the Friday before Easter)... I just figured Easter would get me there. And since I'm in the U.S., I assume I'm looking for the Catholic Easter? But perhaps someone can correct me on that if I'm wrong.
*By "crazy" I meant, like, involved. Not anything offensive...
Python: using dateutil's easter() function.
>>> from dateutil.easter import *
>>> print easter(2010)
2010-04-04
>>> print easter(2011)
2011-04-24
The functions gets, as an argument, the type of calculation you like:
EASTER_JULIAN = 1
EASTER_ORTHODOX = 2
EASTER_WESTERN = 3
You can pick the one relevant to the US.
Reducing two days from the result would give you Good Friday:
>>> from datetime import timedelta
>>> d = timedelta(days=-2)
>>> easter(2011)
datetime.date(2011, 4, 24)
>>> easter(2011)+d
datetime.date(2011, 4, 22)
Oddly enough, someone was iterating this, and published the results in Wikipedia's article about the algorithm:
in SQL Server Easter Sunday would look like this, scroll down for Good Friday
CREATE FUNCTION dbo.GetEasterSunday
( #Y INT )
RETURNS SMALLDATETIME
AS
BEGIN
DECLARE #EpactCalc INT,
#PaschalDaysCalc INT,
#NumOfDaysToSunday INT,
#EasterMonth INT,
#EasterDay INT
SET #EpactCalc = (24 + 19 * (#Y % 19)) % 30
SET #PaschalDaysCalc = #EpactCalc - (#EpactCalc / 28)
SET #NumOfDaysToSunday = #PaschalDaysCalc - (
(#Y + #Y / 4 + #PaschalDaysCalc - 13) % 7
)
SET #EasterMonth = 3 + (#NumOfDaysToSunday + 40) / 44
SET #EasterDay = #NumOfDaysToSunday + 28 - (
31 * (#EasterMonth / 4)
)
RETURN
(
SELECT CONVERT
( SMALLDATETIME,
RTRIM(#Y)
+ RIGHT('0'+RTRIM(#EasterMonth), 2)
+ RIGHT('0'+RTRIM(#EasterDay), 2)
)
)
END
GO
Good Friday is like this and it uses the Easter function above
CREATE FUNCTION dbo.GetGoodFriday
(
#Y INT
)
RETURNS SMALLDATETIME
AS
BEGIN
RETURN (SELECT dbo.GetEasterSunday(#Y) - 2)
END
GO
From here: http://web.archive.org/web/20070611150639/http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-an-auxiliary-calendar-table.html
When it came for me to write this (traffic prediction based on day of week and holiday),
I gave up on trying to write it by myself. I found it somewhere on the net. The code was public domain, but...
sigh
see for yourself.
void dateOfEaster(struct tm* p)
{
int Y = p->tm_year;
int a = Y % 19;
int b = Y / 100;
int c = Y % 100;
int d = b / 4;
int e = b % 4;
int f = (b + 8) / 25;
int g = (b - f + 1) / 3;
int h = (19 * a + b - d - g + 15) % 30;
int i = c / 4;
int k = c % 4;
int L = (32 + 2 * e + 2 * i - h - k) % 7;
int m = (a + 11 * h + 22 * L) / 451;
p->tm_mon = ((h + L - 7 * m + 114) / 31 ) - 1;
p->tm_mday = ((h + L - 7 * m + 114) % 31) + 1;
p->tm_hour = 12;
const time_t tmp = mktime(p);
*p = *localtime(&tmp); //recover yday from mon+mday
}
Some questions are better left unasked.
I feel lucky that all moving holidays in my country are a fixed offset from the date of Easter.
The SQL Server function below is more general than the accepted answer
The accepted answer is only correct for the range (inclusive) : 1900-04-15 to 2099-04-12
It uses the algorithm provided by The United States Naval Observatory (USNO)
http://aa.usno.navy.mil/faq/docs/easter.php
CREATE FUNCTION dbo.GetEasterSunday (#Y INT)
RETURNS DATETIME
AS
BEGIN
-- Source of algorithm : http://aa.usno.navy.mil/faq/docs/easter.php
DECLARE #c INT = #Y / 100
DECLARE #n INT = #Y - 19 * (#Y / 19)
DECLARE #k INT = (#c - 17) / 25
DECLARE #i INT = #c - #c / 4 - (#c - #k) / 3 + 19 * #n + 15
SET #i = #i - 30 * (#i / 30)
SET #i = #i - (#i / 28) * (1 - (#i / 28) * (29 / (#i + 1)) * ((21 - #n) / 11))
DECLARE #j INT = #Y + #Y / 4 + #i + 2 - #c + #c / 4
SET #j = #j - 7 * (#j / 7)
DECLARE #l INT = #i - #j
DECLARE #m INT = 3 + (#l + 40) / 44
DECLARE #d INT = #l + 28 - 31 * (#m / 4)
RETURN
(
SELECT CONVERT
( DATETIME,
RTRIM(#Y)
+ RIGHT('0'+RTRIM(#m), 2)
+ RIGHT('0'+RTRIM(#d), 2)
)
)
END
GO
VB .NET Functions for Greek Orthodox and Catholic Easter:
Public Shared Function OrthodoxEaster(ByVal Year As Integer) As Date
Dim a = Year Mod 19
Dim b = Year Mod 7
Dim c = Year Mod 4
Dim d = (19 * a + 16) Mod 30
Dim e = (2 * c + 4 * b + 6 * d) Mod 7
Dim f = (19 * a + 16) Mod 30
Dim key = f + e + 3
Dim month = If((key > 30), 5, 4)
Dim day = If((key > 30), key - 30, key)
Return New DateTime(Year, month, day)
End Function
Public Shared Function CatholicEaster(ByVal Year As Integer) As DateTime
Dim month = 3
Dim a = Year Mod 19 + 1
Dim b = Year / 100 + 1
Dim c = (3 * b) / 4 - 12
Dim d = (8 * b + 5) / 25 - 5
Dim e = (5 * Year) / 4 - c - 10
Dim f = (11 * a + 20 + d - c) Mod 30
If f = 24 Then f += 1
If (f = 25) AndAlso (a > 11) Then f += 1
Dim g = 44 - f
If g < 21 Then g = g + 30
Dim day = (g + 7) - ((e + g) Mod 7)
If day > 31 Then
day = day - 31
month = 4
End If
Return New DateTime(Year, month, day)
End Function
The below code determines Easter through powershell:
function Get-DateOfEaster {
param(
[Parameter(ValueFromPipeline)]
$theYear=(Get-Date).Year
)
if($theYear -lt 1583) {
return $null
} else {
# Step 1: Divide the theYear by 19 and store the
# remainder in variable A. Example: If the theYear
# is 2000, then A is initialized to 5.
$a = $theYear % 19
# Step 2: Divide the theYear by 100. Store the integer
# result in B and the remainder in C.
$c = $theYear % 100
$b = ($theYear -$c) / 100
# Step 3: Divide B (calculated above). Store the
# integer result in D and the remainder in E.
$e = $b % 4
$d = ($b - $e) / 4
# Step 4: Divide (b+8)/25 and store the integer
# portion of the result in F.
$f = [math]::floor(($b + 8) / 25)
# Step 5: Divide (b-f+1)/3 and store the integer
# portion of the result in G.
$g = [math]::floor(($b - $f + 1) / 3)
# Step 6: Divide (19a+b-d-g+15)/30 and store the
# remainder of the result in H.
$h = (19 * $a + $b - $d - $g + 15) % 30
# Step 7: Divide C by 4. Store the integer result
# in I and the remainder in K.
$k = $c % 4
$i = ($c - $k) / 4
# Step 8: Divide (32+2e+2i-h-k) by 7. Store the
# remainder of the result in L.
$l = (32 + 2 * $e + 2 * $i - $h - $k) % 7
# Step 9: Divide (a + 11h + 22l) by 451 and
# store the integer portion of the result in M.
$m = [math]::floor(($a + 11 * $h + 22 * $l) / 451)
# Step 10: Divide (h + l - 7m + 114) by 31. Store
# the integer portion of the result in N and the
# remainder in P.
$p = ($h + $l - 7 * $m + 114) % 31
$n = (($h + $l - 7 * $m + 114) - $p) / 31
# At this point p+1 is the day on which Easter falls.
# n is 3 for March and 4 for April.
$DateTime = New-Object DateTime $theyear, $n, ($p+1), 0, 0, 0, ([DateTimeKind]::Utc)
return $DateTime
}
}
$eastersunday=Get-DateOfEaster 2015
Write-Host $eastersunday
Found this Excel formula somewhere
Assuming cell A1 contains year e.g. 2020
ROUND(DATE(A1;4;1)/7+MOD(19*MOD(A1;19)-7;30)*0,14;0)*7-6
Converted to T-SQL lead me to this:
DECLARE #yr INT=2020
SELECT DATEADD(dd, ROUND(DATEDIFF(dd, '1899-12-30', DATEFROMPARTS(#yr, 4, 1)) / 7.0 + ((19.0 * (#yr % 19) - 7) % 30) * 0.14, 0) * 7.0 - 6, -2)
In JS, taken from here.
var epoch=2444238.5,elonge=278.83354,elongp=282.596403,eccent=.016718,sunsmax=149598500,sunangsiz=.533128,mmlong=64.975464,mmlongp=349.383063,mlnode=151.950429,minc=5.145396,mecc=.0549,mangsiz=.5181,msmax=384401,mparallax=.9507,synmonth=29.53058868,lunatbase=2423436,earthrad=6378.16,PI=3.141592653589793,epsilon=1e-6;function sgn(x){return x<0?-1:x>0?1:0}function abs(x){return x<0?-x:x}function fixAngle(a){return a-360*Math.floor(a/360)}function toRad(d){return d*(PI/180)}function toDeg(d){return d*(180/PI)}function dsin(x){return Math.sin(toRad(x))}function dcos(x){return Math.cos(toRad(x))}function toJulianTime(date){var year,month,day;year=date.getFullYear();var m=(month=date.getMonth()+1)>2?month:month+12,y=month>2?year:year-1,d=(day=date.getDate())+date.getHours()/24+date.getMinutes()/1440+(date.getSeconds()+date.getMilliseconds()/1e3)/86400,b=isJulianDate(year,month,day)?0:2-y/100+y/100/4;return Math.floor(365.25*(y+4716)+Math.floor(30.6001*(m+1))+d+b-1524.5)}function isJulianDate(year,month,day){if(year<1582)return!0;if(year>1582)return!1;if(month<10)return!0;if(month>10)return!1;if(day<5)return!0;if(day>14)return!1;throw"Any date in the range 10/5/1582 to 10/14/1582 is invalid!"}function jyear(td,yy,mm,dd){var z,f,alpha,b,c,d,e;return f=(td+=.5)-(z=Math.floor(td)),b=(z<2299161?z:z+1+(alpha=Math.floor((z-1867216.25)/36524.25))-Math.floor(alpha/4))+1524,c=Math.floor((b-122.1)/365.25),d=Math.floor(365.25*c),e=Math.floor((b-d)/30.6001),{day:Math.floor(b-d-Math.floor(30.6001*e)+f),month:Math.floor(e<14?e-1:e-13),year:Math.floor(mm>2?c-4716:c-4715)}}function jhms(j){var ij;return j+=.5,ij=Math.floor(86400*(j-Math.floor(j))+.5),{hour:Math.floor(ij/3600),minute:Math.floor(ij/60%60),second:Math.floor(ij%60)}}function jwday(j){return Math.floor(j+1.5)%7}function meanphase(sdate,k){var t,t2;return 2415020.75933+synmonth*k+1178e-7*(t2=(t=(sdate-2415020)/36525)*t)-155e-9*(t2*t)+33e-5*dsin(166.56+132.87*t-.009173*t2)}function truephase(k,phase){var t,t2,t3,pt,m,mprime,f,apcor=!1;if(pt=2415020.75933+synmonth*(k+=phase)+1178e-7*(t2=(t=k/1236.85)*t)-155e-9*(t3=t2*t)+33e-5*dsin(166.56+132.87*t-.009173*t2),m=359.2242+29.10535608*k-333e-7*t2-347e-8*t3,mprime=306.0253+385.81691806*k+.0107306*t2+1236e-8*t3,f=21.2964+390.67050646*k-.0016528*t2-239e-8*t3,phase<.01||abs(phase-.5)<.01?(pt+=(.1734-393e-6*t)*dsin(m)+.0021*dsin(2*m)-.4068*dsin(mprime)+.0161*dsin(2*mprime)-4e-4*dsin(3*mprime)+.0104*dsin(2*f)-.0051*dsin(m+mprime)-.0074*dsin(m-mprime)+4e-4*dsin(2*f+m)-4e-4*dsin(2*f-m)-6e-4*dsin(2*f+mprime)+.001*dsin(2*f-mprime)+5e-4*dsin(m+2*mprime),apcor=!0):(abs(phase-.25)<.01||abs(phase-.75)<.01)&&(pt+=(.1721-4e-4*t)*dsin(m)+.0021*dsin(2*m)-.628*dsin(mprime)+.0089*dsin(2*mprime)-4e-4*dsin(3*mprime)+.0079*dsin(2*f)-.0119*dsin(m+mprime)-.0047*dsin(m-mprime)+3e-4*dsin(2*f+m)-4e-4*dsin(2*f-m)-6e-4*dsin(2*f+mprime)+.0021*dsin(2*f-mprime)+3e-4*dsin(m+2*mprime)+4e-4*dsin(m-2*mprime)-3e-4*dsin(2*m+mprime),pt+=phase<.5?.0028-4e-4*dcos(m)+3e-4*dcos(mprime):4e-4*dcos(m)-.0028-3e-4*dcos(mprime),apcor=!0),!apcor)throw"Error calculating moon phase!";return pt}function phasehunt(sdate,phases){var adate,k1,k2,nt1,nt2,yy,mm,dd,jyearResult=jyear(adate=sdate-45,yy,mm,dd);for(yy=jyearResult.year,mm=jyearResult.month,dd=jyearResult.day,adate=nt1=meanphase(adate,k1=Math.floor(12.3685*(yy+1/12*(mm-1)-1900)));nt2=meanphase(adate+=synmonth,k2=k1+1),!(nt1<=sdate&&nt2>sdate);)nt1=nt2,k1=k2;return phases[0]=truephase(k1,0),phases[1]=truephase(k1,.25),phases[2]=truephase(k1,.5),phases[3]=truephase(k1,.75),phases[4]=truephase(k2,0),phases}function kepler(m,ecc){var e,delta;e=m=toRad(m);do{e-=(delta=e-ecc*Math.sin(e)-m)/(1-ecc*Math.cos(e))}while(abs(delta)>epsilon);return e}function getMoonPhase(julianDate){var Day,N,M,Ec,Lambdasun,ml,MM,MN,Ev,Ae,MmP,mEc,lP,lPP,NP,y,x,MoonAge,MoonPhase,MoonDist,MoonDFrac,MoonAng,F,SunDist,SunAng;return N=fixAngle(360/365.2422*(Day=julianDate-epoch)),Ec=kepler(M=fixAngle(N+elonge-elongp),eccent),Ec=Math.sqrt((1+eccent)/(1-eccent))*Math.tan(Ec/2),Lambdasun=fixAngle((Ec=2*toDeg(Math.atan(Ec)))+elongp),F=(1+eccent*Math.cos(toRad(Ec)))/(1-eccent*eccent),SunDist=sunsmax/F,SunAng=F*sunangsiz,ml=fixAngle(13.1763966*Day+mmlong),MM=fixAngle(ml-.1114041*Day-mmlongp),MN=fixAngle(mlnode-.0529539*Day),MmP=MM+(Ev=1.2739*Math.sin(toRad(2*(ml-Lambdasun)-MM)))-(Ae=.1858*Math.sin(toRad(M)))-.37*Math.sin(toRad(M)),lPP=(lP=ml+Ev+(mEc=6.2886*Math.sin(toRad(MmP)))-Ae+.214*Math.sin(toRad(2*MmP)))+.6583*Math.sin(toRad(2*(lP-Lambdasun))),NP=MN-.16*Math.sin(toRad(M)),y=Math.sin(toRad(lPP-NP))*Math.cos(toRad(minc)),x=Math.cos(toRad(lPP-NP)),toDeg(Math.atan2(y,x)),NP,toDeg(Math.asin(Math.sin(toRad(lPP-NP))*Math.sin(toRad(minc)))),MoonAge=lPP-Lambdasun,MoonPhase=(1-Math.cos(toRad(MoonAge)))/2,MoonDist=msmax*(1-mecc*mecc)/(1+mecc*Math.cos(toRad(MmP+mEc))),MoonAng=mangsiz/(MoonDFrac=MoonDist/msmax),mparallax/MoonDFrac,{moonIllumination:MoonPhase,moonAgeInDays:synmonth*(fixAngle(MoonAge)/360),distanceInKm:MoonDist,angularDiameterInDeg:MoonAng,distanceToSun:SunDist,sunAngularDiameter:SunAng,moonPhase:fixAngle(MoonAge)/360}}function getMoonInfo(date){return null==date?{moonPhase:0,moonIllumination:0,moonAgeInDays:0,distanceInKm:0,angularDiameterInDeg:0,distanceToSun:0,sunAngularDiameter:0}:getMoonPhase(toJulianTime(date))}function getEaster(year){var previousMoonInfo,moonInfo,fullMoon=new Date(year,2,21),gettingDarker=void 0;do{previousMoonInfo=getMoonInfo(fullMoon),fullMoon.setDate(fullMoon.getDate()+1),moonInfo=getMoonInfo(fullMoon),void 0===gettingDarker?gettingDarker=moonInfo.moonIllumination<previousMoonInfo.moonIllumination:gettingDarker&&moonInfo.moonIllumination>previousMoonInfo.moonIllumination&&(gettingDarker=!1)}while(gettingDarker&&moonInfo.moonIllumination<previousMoonInfo.moonIllumination||!gettingDarker&&moonInfo.moonIllumination>previousMoonInfo.moonIllumination);for(fullMoon.setDate(fullMoon.getDate()-1);0!==fullMoon.getDay();)fullMoon.setDate(fullMoon.getDate()+1);return fullMoon}
Then run getEaster(2020); // -> Sun Apr 12 2020