Facebook
From Violet Cassowary, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 186
  1. # n - liczba grup
  2. # k_i - liczba zajęć dla danej grupy i = 1..n
  3. # pl - prowadzący
  4.  
  5. # timeslot = 8-16 * 5
  6.  
  7. # 1 - k_i typów zajęć - wpierdolić w timeslot
  8.  
  9. # DNA - wszystkie grupy - wszystkie zajecia
  10.  
  11. """
  12. [ # 1..k_i typów zajęć - RR/MD
  13.     [ # timesloty dla MD
  14.         [obiekt_zajecie-GRUPA/PROWADZACY], # zajecie
  15.         ["D2,Prowadzacy"], # zajecie
  16.         ["D1,Prowadzacy"],
  17.         [],
  18.     ],
  19.     [
  20.  
  21.     ]
  22. ]
  23. """
  24. from pprint import pprint
  25. import random
  26. from itertools import groupby
  27.  
  28.  
  29. class Lecture:
  30.     def __init__(self, group, lecturer):
  31.         self.group = group
  32.         self.lecturer = lecturer
  33.  
  34.     def __repr__(self):
  35.         return f"({self.group}, {self.lecturer})"
  36.  
  37.  
  38. n = 5
  39. k_i = 10
  40. pl = 1
  41. timeslots = 5*8
  42.  
  43. def generate_initial_population(count):
  44.     out = []
  45.     for i in range(count):
  46.         state = [[[] for i in range(timeslots)] for l in range(k_i)]
  47.         for lecture_type in range(k_i):
  48.             for group in range(n):
  49.                 chosen_timeslot = random.choice(state[lecture_type])
  50.                 chosen_timeslot.append(Lecture(group, lecture_type))
  51.         out.append(state)
  52.  
  53.     return out
  54.  
  55. def count_in_group_collisions(state):
  56.     collisions = 0
  57.     for timeslot in range(timeslots):
  58.         for group in range(n):
  59.             lectures_for_group_in_timeslot_count = 0
  60.             for lecture_type in range(k_i):
  61.                 timeslot_list = state[lecture_type][timeslot]
  62.                 for lecture in timeslot_list:
  63.                     if lecture.group == group:
  64.                         lectures_for_group_in_timeslot_count += 1
  65.                 if lectures_for_group_in_timeslot_count > 1:
  66.                     collisions += 1
  67.     return collisions
  68.  
  69.  
  70. def count_lecturer_collisions(state):
  71.     collisions = 0
  72.     for lecture_type in range(k_i):
  73.         for timeslot in range(timeslots):
  74.             if len(state[lecture_type][timeslot]) > 1:
  75.                 if any(filter(lambda x: x > 1,
  76.                               [len(list(group))
  77.                                for key, group in groupby(sorted(state[lecture_type][timeslot], key=lambda x: x.lecturer),
  78.                                                          key=lambda x: x.lecturer)])):
  79.                     collisions += 1
  80.     return collisions
  81.  
  82.  
  83. def count_okienkas(state):
  84.     pass
  85.  
  86.  
  87.  
  88.  
  89.  
  90. population = generate_initial_population(5)
  91. print (population[0])
  92.  
  93. for state in population:
  94.     print(count_lecturer_collisions(state))
  95.     print(count_in_group_collisions(state))
  96.  
  97.  
  98.  
  99.  
  100.