def course_scheduler(course_descriptions, goal_conditions, initial_state): """ State consists of a conjunction of courses/high-level requirements that are to be achieved. When conjunction set is empty a viable schedule should be in the schedule_set. :param course_descriptions: Course catalog. A Python dictionary that uses Course as key and CourseInfo as value :param goal_conditions: A list of courses or high-level requirements that a viable schedule would need to fulfill :param initial_state: A list of courses the student has already taken :return: A List of scheduled courses in format (course, scheduled_term, course_credits) """ depths = range(1, 9) best_schedule = None best_schedule_num = float('inf') schedule = Schedule(course_descriptions, initial_state, goal_conditions) empty_schedule = schedule.copy() for depth in depths: schedule.max_semester = depth schedule.assign(empty_schedule) frontier = [] append_to_queue(frontier, goal_conditions, schedule) search(frontier, schedule) if 0 < schedule.num_of_courses_scheduled() < best_schedule_num: best_schedule = schedule.copy() best_schedule_num = schedule.num_of_courses_scheduled() schedule.assign(best_schedule) return schedule.get_plan()
def _reproduce(self): population = np.empty_like(self._population) for idx in range(self.parameters.population_size - 1): # crossover parent1, parent2 = self._tournament_selection( ), self._tournament_selection() while parent1 is parent2: parent2 = self._tournament_selection() child = Schedule(self.resources, self.parameters) if np.random.binomial(1, p=self.parameters.crossover_rate): child.crossover(parent1, parent2) else: child.copy(np.random.choice([parent1, parent2])) # mutation if np.random.binomial(1, self.parameters.mutation_rate): child.mutate() child.calculate_fitness() population[idx] = child # preserve the best child = Schedule(self.resources, self.parameters) if self.best_schedule is not None: child.copy(self.best_schedule) else: child.initialize() population[-1] = child self._population = population self._track_best()
class TestClass(unittest.TestCase): """ Test the schedule.Schedule class """ def setUp(self): self.interpreters = [interpreter1, interpreter2] self.appts = [appt1, appt2, appt3] self.patients = [patient1, patient2] self.sched = Schedule(appts=self.appts, interpreters=self.interpreters) def test(self): # calc_impact self.assertEqual(self.sched.calc_impact(), 150) # copy copied = self.sched.copy() self.assertIsInstance(copied, Schedule) self.assertEqual(copied, self.sched) self.assertTrue(appt1 in copied.appts) self.assertTrue(appt2 in copied.appts) self.assertTrue(appt3 in copied.appts) # gen_intervals generated = self.sched.gen_intervals() expected = { "i": [1, 2, 3], "s": ["08:00", "08:25", "08:45"], "f": ["08:10", "09:05", "09:25"], "v": [100, 30, 20], "x": [3, 0, 4], "y": [4, 0, -4] } self.assertEqual(generated, expected) # brief brief_str_appt1 = (str(appt1.idnum) + "|" + str(appt1.start) + "|" + str(appt1.finish) + "|" + str(appt1.patient) + "|" + str(appt1.location.coordinates) + "|" + str(appt1.interpreter)) brief_str_appt2 = (str(appt2.idnum) + "|" + str(appt2.start) + "|" + str(appt2.finish) + "|" + str(appt2.patient) + "|" + str(appt2.location.coordinates) + "|" + str(appt2.interpreter)) brief_str_appt3 = (str(appt3.idnum) + "|" + str(appt3.start) + "|" + str(appt3.finish) + "|" + str(appt3.patient) + "|" + str(appt3.location.coordinates) + "|" + str(appt3.interpreter)) brief_str = (brief_str_appt1 + "\n" + brief_str_appt2 + "\n" + brief_str_appt3 + "\n") self.assertEqual(self.sched.brief(), brief_str) # __eq__ copied_schedule = self.sched.copy() self.assertEqual(self.sched, copied_schedule) # __ne__ copied_schedule = self.sched.copy() copied_schedule.appts[0].idnum = 100 self.assertNotEqual(self.sched, copied_schedule)