def test_saving_and_retrieving_schedule(self): schedule1 = Schedule(event="New", start_time=datetime.time(11, 23, 44), end_time=datetime.time(11, 25, 44)) schedule1.save() schedule2 = Schedule(event="Next", start_time=datetime.time(8, 11, 10), end_time=datetime.time(8, 25, 12)) schedule2.save() saved_schedule = Schedule.objects.all() self.assertEqual(saved_schedule.count(), 2) self.assertEqual(saved_schedule[0], schedule1) self.assertEqual(saved_schedule[1], schedule2)
def callback_c(n, durationValue, amountValue, rvValue, scheduleRows, modeValue, rateValue, **kwargs): user = kwargs['user'] if n is None: user = kwargs['user'] return [ html.Div('Lease quote', className='h3 mb-0'), dbc.Button("Save quote", id="save_quote_button", className="d-none d-md-block btn btn-sm btn-primary"), ] return dash.no_update else: if n > 1: return [ html.Div('Lease quote', className='h3 mb-0'), ] customer = get_object_or_404(Customer, pk=1) contract = Contract() contract.customer = customer contract.user = user contract.status = 'Created' contract.creation_date = datetime.date.today() contract.status_date = datetime.date.today() contract.save() schedule = Schedule() schedule.contract = contract schedule.mode = modeValue schedule.rv = rvValue schedule.amount = amountValue schedule.start_date = startdate schedule.rate = rateValue / 120000 schedule.save() i = 0 for scheduleRow in scheduleRows: if (modeValue == '01'): date = startdate + relativedelta(months=i) else: date = startdate + relativedelta(months=i + 1) i = i + 1 step = Step() step.schedule = schedule step.rent = scheduleRow['rent'] step.balance = scheduleRow['balance'] step.date = date step.save() return [ html.Div('Lease quote', className='h3 mb-0 text-gray-800'), html.Div('Quote saved !', className='h3 mb-0 text-gray-800'), ]
def generate(self) -> Schedule: classes = [] for C in self.schedule_param.courses: assigned_instructors = self._get_assigned_Instructors_for( C, ) for sec_i in range(C.num_of_sections): instructor, timeslot, room = self._get_unique_Instr_Timeslot_Room( assigned_instructors, C, classes, ) section = Section(C, sec_i+1) classes.append(Class(section, instructor, room, timeslot)) return Schedule(classes)
def run(self, *args, **kwargs): # initial population population = [ self.schedule_generator.generate() for _ in range(self.population_size) ] new_population = [None for _ in range(self.population_size)] total_classes = len(self.schedule_param.sections) self.logger.write(self.logger.record_start_marker) for epoch in range(self.epochs): try: population = sorted( population, key=lambda sch: self.fitness_provider.fitness(sch), reverse=not self.fitness_provider.is_reverse()) best_fitness = self.fitness_provider.fitness(population[0]) self.logger.write(f"{epoch}\t\t{best_fitness}") if self.fitness_provider.compare(best_fitness, self.min_acceptable_fitness): return population[0] """ Dominance by elites """ elite_count = (self.elite_pct * self.population_size) // 100 for i in range(elite_count): new_population[i] = population[i] """ Crossover """ mateable_count = (self.mateable_pct * self.population_size) // 100 siblig_index = 1 \ if (self.population_size - elite_count) % 2 == 0 \ else -1 for i in range(elite_count, self.population_size, 2): parent1_idx = np.random.randint(mateable_count) parent2_idx = np.random.randint(mateable_count) # single point crossover crossover_point = np.random.randint(total_classes) # 2 children produced new_population[i + siblig_index] = Schedule( (population[parent1_idx].classes[:crossover_point] + population[parent2_idx].classes[crossover_point:])) new_population[i] = Schedule( (population[parent2_idx].classes[:crossover_point] + population[parent1_idx].classes[crossover_point:])) """ Mutation """ # self.mutable_pct mutable_count = (self.mutable_pct * self.population_size) // 100 for i in range(mutable_count): # NOTE: research on optimal choice of `schedule_idx` range schedule_idx = np.random.randint(self.population_size) # schedule_idx = np.random.randint(elite_count, population_size) class_idx = np.random.randint(total_classes) param_idx = np.random.randint(3) tmp_sch = deepcopy(new_population[schedule_idx]) if param_idx == 0: """ mutate room """ tmp_sch.classes[class_idx].room = random.choice( self.schedule_param.rooms) elif param_idx == 1: """ mutate instructor """ tmp_sch.classes[class_idx].instructor = \ random.choice(self.schedule_param.instructors) else: # param_idx == 2 """ mutate timeslot """ tmp_sch.classes[class_idx].timeslot = \ random.choice(self.schedule_param.timeslots) new_population[schedule_idx] = tmp_sch """ Local Search using Smart Mutation """ lcl_search_count = (self.lcl_search_pct * self.population_size) // 100 for i in range(lcl_search_count): schedule_idx = np.random.randint(self.population_size) class_idx = np.random.randint(total_classes) tmp_sch = deepcopy(new_population[schedule_idx]) for j in range(self.lcl_search_iters): param_idx = np.random.randint(3) if param_idx == 0: """ mutate room """ tmp_sch.classes[class_idx].room = random.choice( self.schedule_param.rooms) elif param_idx == 1: """ mutate instructor """ tmp_sch.classes[class_idx].instructor = \ random.choice(self.schedule_param.instructors) else: # param_idx == 2 """ mutate timeslot """ tmp_sch.classes[class_idx].timeslot = \ random.choice(self.schedule_param.timeslots) tmp_fitness = self.fitness_provider.fitness(tmp_sch) current_fitness = self.fitness_provider.fitness( new_population[schedule_idx]) if self.fitness_provider.compare( tmp_fitness, current_fitness): new_population[schedule_idx] = tmp_sch break population = new_population except KeyboardInterrupt: print("Solver stopped by user") break self.logger.write(self.logger.record_end_marker) best_fitness = self.fitness_provider.fitness(population[0]) best_fit_idx = 0 for i in range(1, len(population)): f = self.fitness_provider.fitness(population[i]) if self.fitness_provider.compare(f, best_fitness): best_fitness = f best_fit_idx = i return population[best_fit_idx]
def setUp(self): import datetime schedule1 = Schedule(event="New", start_time=datetime.time(11, 23), end_time=datetime.time(11, 25)) schedule1.save()