# n - liczba grup # k_i - liczba zajęć dla danej grupy i = 1..n # pl - prowadzący # timeslot = 8-16 * 5 # 1 - k_i typów zajęć - wpierdolić w timeslot # DNA - wszystkie grupy - wszystkie zajecia """ [ # 1..k_i typów zajęć - RR/MD [ # timesloty dla MD [obiekt_zajecie-GRUPA/PROWADZACY], # zajecie ["D2,Prowadzacy"], # zajecie ["D1,Prowadzacy"], [], ], [ ] ] """ from pprint import pprint import random from itertools import groupby class Lecture: def __init__(self, group, lecturer): self.group = group self.lecturer = lecturer def __repr__(self): return f"({self.group}, {self.lecturer})" n = 5 k_i = 10 pl = 1 timeslots = 5*8 def generate_initial_population(count): out = [] for i in range(count): state = [[[] for i in range(timeslots)] for l in range(k_i)] for lecture_type in range(k_i): for group in range(n): chosen_timeslot = random.choice(state[lecture_type]) chosen_timeslot.append(Lecture(group, lecture_type)) out.append(state) return out def count_in_group_collisions(state): collisions = 0 for timeslot in range(timeslots): for group in range(n): lectures_for_group_in_timeslot_count = 0 for lecture_type in range(k_i): timeslot_list = state[lecture_type][timeslot] for lecture in timeslot_list: if lecture.group == group: lectures_for_group_in_timeslot_count += 1 if lectures_for_group_in_timeslot_count > 1: collisions += 1 return collisions def count_lecturer_collisions(state): collisions = 0 for lecture_type in range(k_i): for timeslot in range(timeslots): if len(state[lecture_type][timeslot]) > 1: if any(filter(lambda x: x > 1, [len(list(group)) for key, group in groupby(sorted(state[lecture_type][timeslot], key=lambda x: x.lecturer), key=lambda x: x.lecturer)])): collisions += 1 return collisions def count_okienkas(state): pass population = generate_initial_population(5) print (population[0]) for state in population: print(count_lecturer_collisions(state)) print(count_in_group_collisions(state))