I've copied a code example that works fine by itself, and does work in my version as well. The problem is after asking the user the number of employees, shifts, and days being scheduled, the program won't display the results of the solver. It will do this for the example code alone, but not with the code I have written to control it. If anyone can take a look at this to see why, it would be greatly appreciated.
from ortools.sat.python import cp_model
def sup_functions(): # supervisor functions
sup_task = input('I want to: ')
# scheduling employees
if sup_task == 'schedule employees':
class EmpsPartialSolutionPrinter(cp_model.CpSolverSolutionCallback):
def __init__(self, shifts, num_emps, num_days, num_shifts, sols):
cp_model.CpSolverSolutionCallback.__init__(self)
self._shifts = shifts
self._num_emps = num_emps
self._num_days = num_days
self._num_shifts = num_shifts
self._solutions = set(sols)
self._solution_count = 1
def on_solution_callback(self):
if self._solution_count in self._solutions:
print('Solution %i' % self._solution_count)
for d in range(self._num_days):
print('Day %i' % d)
for n in range(self._num_emps):
is_working = False
for s in range(self._num_shifts):
if self.Value(self._shifts[(n, d, s)]):
is_working = True
print(' Employee %i works shift %i' % (n, s))
if not is_working:
print(' Employee {} does not work'.format(n))
print()
self._solution_count += 1
def solution_count(self):
return self._solution_count
def main():
# Data.
num_emps = int(input("How many employees are you scheduling? "))
num_days = int(input("How many days are you scheduling for? "))
num_shifts = int(input(f"How many shifts are you scheduling for each employees for {num_days} days? "))
all_emps = range(num_emps)
all_shifts = range(num_shifts)
all_days = range(num_days)
# Creates the model.
model = cp_model.CpModel()
# Creates shift variables.
# shifts[(n, d, s)]: nurse 'n' works shift 's' on day 'd'.
shifts = {}
for n in all_emps:
for d in all_days:
for s in all_shifts:
shifts[(n, d,
s)] = model.NewBoolVar('shift_n%id%is%i' % (n, d, s))
# Each shift is assigned to exactly one employee in the schedule period.
for d in all_days:
for s in all_shifts:
model.Add(sum(shifts[(n, d, s)] for n in all_emps) == 1)
# Each emmployee works at most one shift per day.
for n in all_emps:
for d in all_days:
model.Add(sum(shifts[(n, d, s)] for s in all_shifts) <= 1)
# Try to distribute the shifts evenly, so that each employee works
# min_shifts_per_emp shifts. If this is not possible, because the total
# number of shifts is not divisible by the number of employees, some employees will
# be assigned one more shift.
min_shifts_per_emp = (num_shifts * num_days) // num_emps
if num_shifts * num_days % num_emps == 0:
max_shifts_per_emp = min_shifts_per_emp
else:
max_shifts_per_emp = min_shifts_per_emp + 1
for n in all_emps:
num_shifts_worked = 0
for d in all_days:
for s in all_shifts:
num_shifts_worked += shifts[(n, d, s)]
model.Add(min_shifts_per_emp <= num_shifts_worked)
model.Add(num_shifts_worked <= max_shifts_per_emp)
# Creates the solver and solve.
solver = cp_model.CpSolver()
solver.parameters.linearization_level = 0
# Display the first five solutions.
a_few_solutions = range(5)
solution_printer = EmpsPartialSolutionPrinter(shifts, num_emps,
num_days, num_shifts,
a_few_solutions)
solver.SearchForAllSolutions(model, solution_printer)
if __name__ == '__main__':
main()
# lets program know which functions to call
def emporsup():
emp_or_sup = input('Are you an employee or supervisor? ') # determines if user is an employee or the owner
if emp_or_sup == "supervisor":
sup_functions()
#elif emp_or_sup == "employee":
#emp_functions()
else:
print("not a valid response")
emporsup()
emporsup()