for subject in all_subjects:
for t in all_teachers:
name = 'C:{%i} S:{%i} T:{%i}' % (course, subject, teacher)
teacher_courses[course, subject, t] = self.model.NewBoolVar(name)
temp_array = [
self.assignment[course, subject, t, slot] for slot in all_slots
]
self.model.AddMaxEquality(teacher_courses[course, subject, t],
temp_array)
self.model.Add(
sum(teacher_courses[course, subject, t]
for t in all_teachers) == 1)
# Solution collector
self.collector = None
def solve(self):
print('Solving')
solver = cp_model.CpSolver()
solution_printer = SchoolSchedulingSatSolutionPrinter()
status = solver.SearchForAllSolutions(self.model, solution_printer)
print()
print('Branches', solver.NumBranches())
print('Conflicts', solver.NumConflicts())
print('WallTime', solver.WallTime())
def print_status(self):
pass
class SchoolSchedulingSatSolutionPrinter(cp_model.CpSolverSolutionCallback):
def NewSolution(self):
print('Found Solution!')
def main():
# DATA
subjects = ['English', 'Math', 'History']
levels = ['1-', '2-', '3-']
sections = ['A']
teachers = ['Mario', 'Elvis', 'Donald', 'Ian']
teachers_work_hours = [18, 12, 12, 18]
working_days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
periods = ['08:00-09:30', '09:45-11:15', '11:30-13:00']
curriculum = {
('1-', 'English'): 3,
('1-', 'Math'): 3,
('1-', 'History'): 2,
('2-', 'English'): 4,
('2-', 'Math'): 2,
('2-', 'History'): 2,
('3-', 'English'): 2,
('3-', 'Math'): 4,
('3-', 'History'): 2
}
# Subject -> List of teachers who can teach it
specialties_idx_inverse = [
[1, 3], # English -> Elvis & Ian
[0, 3], # Math -> Mario & Ian
[2, 3] # History -> Donald & Ian
]
problem = SchoolSchedulingProblem(
subjects, teachers, curriculum, specialties_idx_inverse, working_days,
periods, levels, sections, teachers_work_hours)
solver = SchoolSchedulingSatSolver(problem)
solver.solve()
solver.print_status()
if __name__ == '__main__':
main()