ulvis.paste.net

Paste Search Dynamic
Recent pastes
provided schedule
  1.   # This function receives a schedule (list of classes objects with attributes: id, course, room, timeslot)
  2.     # Using Peewee ORM
  3.     # Normally, len(self.classes) > 6000 records. It takes a great amount of time to complete the execution of this function
  4.     def calculate_fitness(self):
  5.         # The following variable is used to accumulate the number of conflicts present on each solution provided to the function
  6.         number_of_conflicts = 0
  7.  
  8.         # For each class in the provided schedule
  9.         for i, x in enumerate(self.classes):
  10.             # Filter related classes to check timeslot conflicts (classes from the same program, department, group, semester and day)
  11.  
  12.             filtered_classes = list(filter(lambda z: (x.id != z.id and
  13.                                                       x.course.subject.program == z.course.subject.program and
  14.                                                       x.course.subject.department == z.course.subject.department and
  15.                                                       x.course.subject.semester == z.course.subject.semester and
  16.                                                       x.timeslot.day == z.timeslot.day and
  17.                                                       x.course.group.id == z.course.group.id), self.classes[i:]))
  18.  
  19.             # For each filtered class
  20.             for y in filtered_classes:
  21.                 if x.timeslot.start_time in range(y.timeslot.start_time, y.timeslot.end_time+1) or
  22.                                     y.timeslot.start_time in range(x.timeslot.start_time, x.timeslot.end_time+1):
  23.                     number_of_conflicts += 1
  24.  
  25.         # Return a normalized fitness value between [0, 1]
  26.         return 1 / (number_of_conflicts + 1)
Parsed in 0.010 seconds