Advertisement
Clarata

Untitled

Mar 26th, 2023 (edited)
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VisualBasic 2.82 KB | Source Code | 0 0
  1. import random
  2.  
  3. # Define the worker class
  4. class Worker:
  5.     def __init__(self, name, shift_preferences):
  6.         self.name = name
  7.         self.shift_preferences = shift_preferences
  8.         self.schedule = []
  9.         self.off_days = 0
  10.         self.consecutive_working_days = 0
  11.         self.consecutive_night_shifts = 0
  12.         self.consecutive_afternight_shifts = 0
  13.         self.num_day_shifts = 0
  14.         self.num_night_shifts = 0
  15.         self.num_afternight_shifts = 0
  16.  
  17.     def assign_shift(self, shift):
  18.         self.schedule.append(shift)
  19.         self.consecutive_working_days += 1
  20.         self.consecutive_night_shifts = 0
  21.         self.consecutive_afternight_shifts = 0
  22.  
  23.         if shift == "OFF":
  24.             self.off_days += 1
  25.             self.consecutive_working_days = 0
  26.         elif shift == "DAY":
  27.             self.num_day_shifts += 1
  28.         elif shift == "NIGHT":
  29.             self.num_night_shifts += 1
  30.             if len(self.schedule) > 1 and self.schedule[-2] == "NIGHT":
  31.                 self.consecutive_night_shifts += 1
  32.         elif shift == "AFTERNOON":
  33.             self.num_afternight_shifts += 1
  34.             if len(self.schedule) > 1 and self.schedule[-2] == "NIGHT":
  35.                 self.consecutive_afternight_shifts += 1
  36.  
  37.     def can_work_shift(self, shift):
  38.         if shift == "OFF":
  39.             return self.off_days < 10
  40.         elif shift == "DAY":
  41.             return self.consecutive_working_days < 6 and self.num_day_shifts <= self.num_night_shifts + self.num_afternight_shifts
  42.         elif shift == "NIGHT":
  43.             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
  44.         elif shift == "AFTERNOON":
  45.             return self.consecutive_working_days < 6 and self.num_afternight_shifts <= self.num_night_shifts and self.consecutive_night_shifts > 0
  46.  
  47. # Define the shifts and their probabilities
  48. shifts = {
  49.     "DAY": 0.6,
  50.     "NIGHT": 0.2,
  51.     "AFTERNOON": 0.2,
  52.     "OFF": 0
  53. }
  54.  
  55. # Define the workers and their shift preferences
  56. workers = [
  57.     Worker("Alice", ["DAY", "NIGHT", "AFTERNOON"]),
  58.     Worker("Bob", ["DAY", "NIGHT", "AFTERNOON"]),
  59.     Worker("Charlie", ["DAY", "NIGHT"]),
  60.     Worker("Dave", ["DAY", "NIGHT"]),
  61.     Worker("Eve", ["DAY", "AFTERNOON"])
  62. ]
  63.  
  64. # Assign shifts to workers
  65. for day in range(1, 31):
  66.     print(f"Day {day}")
  67.     for worker in workers:
  68.         available_shifts = [shift for shift in shifts.keys() if worker.can_work_shift(shift)]
  69.         if not available_shifts:
  70.             worker.assign_shift("OFF")
  71.         else:
  72.             chosen_shift = random.choices(available_shifts, [shifts[shift] for shift in available_shifts])[0]
  73.             worker.assign_shift(chosen_shift)
  74.         print(f"{worker.name}: {worker.schedule[-1]}")
  75.     print()
  76.  
Tags: Gpt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement