Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- # Define the worker class
- class Worker:
- def __init__(self, name, shift_preferences):
- self.name = name
- self.shift_preferences = shift_preferences
- self.schedule = []
- self.off_days = 0
- self.consecutive_working_days = 0
- self.consecutive_night_shifts = 0
- self.consecutive_afternight_shifts = 0
- self.num_day_shifts = 0
- self.num_night_shifts = 0
- self.num_afternight_shifts = 0
- def assign_shift(self, shift):
- self.schedule.append(shift)
- self.consecutive_working_days += 1
- self.consecutive_night_shifts = 0
- self.consecutive_afternight_shifts = 0
- if shift == "OFF":
- self.off_days += 1
- self.consecutive_working_days = 0
- elif shift == "DAY":
- self.num_day_shifts += 1
- elif shift == "NIGHT":
- self.num_night_shifts += 1
- if len(self.schedule) > 1 and self.schedule[-2] == "NIGHT":
- self.consecutive_night_shifts += 1
- elif shift == "AFTERNOON":
- self.num_afternight_shifts += 1
- if len(self.schedule) > 1 and self.schedule[-2] == "NIGHT":
- self.consecutive_afternight_shifts += 1
- def can_work_shift(self, shift):
- if shift == "OFF":
- return self.off_days < 10
- elif shift == "DAY":
- return self.consecutive_working_days < 6 and self.num_day_shifts <= self.num_night_shifts + self.num_afternight_shifts
- elif shift == "NIGHT":
- return self.consecutive_working_days < 6 and self.num_night_shifts <= self.num_day_shifts + self.num_afternight_shifts and self.consecutive_afternight_shifts == 0
- elif shift == "AFTERNOON":
- return self.consecutive_working_days < 6 and self.num_afternight_shifts <= self.num_night_shifts and self.consecutive_night_shifts > 0
- # Define the shifts and their probabilities
- shifts = {
- "DAY": 0.6,
- "NIGHT": 0.2,
- "AFTERNOON": 0.2,
- "OFF": 0
- }
- # Define the workers and their shift preferences
- workers = [
- Worker("Alice", ["DAY", "NIGHT", "AFTERNOON"]),
- Worker("Bob", ["DAY", "NIGHT", "AFTERNOON"]),
- Worker("Charlie", ["DAY", "NIGHT"]),
- Worker("Dave", ["DAY", "NIGHT"]),
- Worker("Eve", ["DAY", "AFTERNOON"])
- ]
- # Assign shifts to workers
- for day in range(1, 31):
- print(f"Day {day}")
- for worker in workers:
- available_shifts = [shift for shift in shifts.keys() if worker.can_work_shift(shift)]
- if not available_shifts:
- worker.assign_shift("OFF")
- else:
- chosen_shift = random.choices(available_shifts, [shifts[shift] for shift in available_shifts])[0]
- worker.assign_shift(chosen_shift)
- print(f"{worker.name}: {worker.schedule[-1]}")
- print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement