def run(self, in_data: Instance) -> Solution: machines = in_data.machine_speeds machines2 = [[], [], [], [], []] for i in range(len(machines)): machines2[i] = [i, 0, machines[i]] #real machine index, time, delay sorted_machines = self.rearange_machines(machines2) tasks_weird = in_data.tasks tasks_unsorted = [] for task_index, task in enumerate(tasks_weird): tasks_unsorted.append([task_index + 1, task.duration, task.ready]) sorted_tasks = self.double_sort( tasks_unsorted) #((real_task_index, p, r), ()) tasks_to_machine_allocation, sorted_machines, F = self.algorytm( sorted_tasks, sorted_machines) n = len(tasks_weird) # print(tasks_to_machine_allocation) schedule = Schedule(n=n, m=len(machines), schedule=tasks_to_machine_allocation) return Solution(score=round(F / n, 6), schedule=schedule)
def run(self, in_data: Instance) -> Solution: current_time = 0.0 total_flow = 0.0 tasks = self.init_tasks(in_data.tasks) machines = self.init_machines(in_data.machine_speeds) available_tasks_left = len(tasks) while available_tasks_left > 0: available_machines = self.get_available_machines(machines, current_time) for machine in available_machines: chosen_task = self.select_task(machine, tasks) tasks[chosen_task.task_id - 1].mark_finished() total_flow += machine.assign_task(chosen_task) available_tasks_left -= 1 if available_tasks_left == 0: break current_time = machines[0].machine_time for i in range(1, len(machines)): if machines[i].machine_time < current_time: current_time = machines[i].machine_time order = [] for machine in machines: order.append(machine.schedule) schedule = Schedule(len(tasks), len(machines), order) return Solution(round(total_flow / in_data.no_tasks, 6), schedule)
def alg1(in_data: Instance) -> Solution: schedule = [[] for _ in range(in_data.no_machines)] tasks = pd.DataFrame(data=[[t.ready, t.duration] for t in in_data.tasks]) # print(tasks) # in_data.tasks.sort(key=lambda x: x.ready) tasks = tasks.sort_values(by=[0, 1], ascending=[True, False]) # tasks = tasks.sort_values(by=[1, 0], ascending=[True, False]) # print(tasks) machines = dict() for inx, m in enumerate(in_data.machine_speeds): machines[inx] = m srt_mach = sorted(machines.items(), key=lambda it: it[1]) # print(srt_mach) act_machine = 0 for i, task in tasks.iterrows(): schedule[srt_mach[act_machine][0]].append(int(task.name) + 1) act_machine += 1 act_machine %= in_data.no_machines # print(schedule) result = Algorithm136811.calc_score(in_data, schedule) return Solution( result, Schedule(in_data.no_tasks, in_data.no_machines, schedule))
def run(self, in_data: Instance) -> Solution: score = 0 n = in_data.no_tasks m = in_data.no_machines schedule =[[] for i in range(m)] machines = [i for i in zip(range(m), m*[0], in_data.machine_speeds)] tasks = [i for i in zip(range(1, n+1), in_data.tasks)] for task in sorted(tasks, key=lambda task: task[1].ready): task_id = task[0] ready = task[1].ready duration = task[1].duration machine = min(machines, key=lambda machine: machine[1] + duration * machine[2]) machine_id = machine[0] time = machine[1] speed = machine[2] schedule[machine_id].append(task_id) C = max(ready+duration*speed, time+duration*speed) machines[machine_id] = (machine_id, C, speed) score += C - ready score /= n return Solution(score=score, schedule=Schedule(n, m, schedule))
def run(self, in_data: Instance) -> Solution: def find_best(task_checked): solutions = {} timer = [0] * in_data.no_machines for machine_num, machine in machines: timer[machine_num] = max(local_timers[machine_num], task_checked.ready) timer[machine_num] += task_checked.duration * machine solutions[machine_num] = timer[machine_num] - task.ready #choose best machine chosen_machine = min(solutions, key=solutions.get) #increment timer and solution on local chosen machine local_solutions[chosen_machine] += solutions[chosen_machine] local_timers[chosen_machine] = timer[chosen_machine] return chosen_machine sorted_tasks = sorted(enumerate(in_data.tasks, 1), key=lambda x: (x[1].ready,x[1].duration)) machines = list(enumerate(in_data.machine_speeds)) # split tasks into equal parts to machines local_solutions = [0.0] * in_data.no_machines local_timers = [0.0] * in_data.no_machines local_schedules = [] [local_schedules.append([]) for i in range(5)] #for every task find where is minimum value and add this to schedule. for ind, task in sorted_tasks: #find best machine for this solution append_task_to = find_best(task) #append this tasks to chosen machine local_schedules[append_task_to].append(ind) global_solution = sum(local_solutions) / in_data.no_tasks return Solution(score=global_solution, schedule=Schedule(n=in_data.no_tasks, m=in_data.no_machines, schedule=local_schedules))
def sort_available(self, instance: Instance, key): tasks = instance.tasks.copy() n = instance.no_tasks m = instance.no_machines times = [0] * m schedule = [[] for i in range(0, m)] while True: next_machine = self.get_next_machine(times, instance.machine_speeds) time = times[next_machine] available = self.get_available(tasks, time) if len(available) == 0: ready_times = [ task.ready for task in tasks if task.ready >= time ] if len(ready_times) == 0: break time = min(ready_times) for i in range(0, m): times[i] = max(time, times[i]) next_machine = self.get_next_machine(times, instance.machine_speeds) available = self.get_available(tasks, time) available.sort(key=key) task = available[0] schedule[next_machine].append(task.no) speed = instance.machine_speeds[next_machine] times[next_machine] = max(times[next_machine], task.ready) + task.duration * speed tasks.remove(task) return Schedule(n, m, schedule)
def run(AjbFo, in_data: Instance): AjbFa = [Machine(AjbFy) for AjbFy in in_data.machine_speeds] AjbFv = sorted(enumerate(in_data.tasks), key=lambda AjbFM: (AjbFM[1].ready, AjbFM[1].duration)) for AjbFi, AjbFM in AjbFv: AjbFD = lmap(lambda machine: machine.finish_time(AjbFM), AjbFa) AjbFU = np.argmin(AjbFD).item() AjbFa[AjbFU].do_schedule(AjbFM, AjbFi + 1, AjbFD[AjbFU]) AjbFe = AjbFu AjbFP = -1 for i, machine in enumerate(AjbFa): if machine.time > AjbFM.ready and i != AjbFU: AjbFx = machine.criterium_change_on_swap(AjbFM) AjbFO = AjbFa[AjbFU].criterium_change_on_swap( machine.schedule_tasks[-1]) AjbFg = AjbFx + AjbFO if AjbFe > AjbFg and AjbFg < 0: AjbFe = AjbFg AjbFP = (i, AjbFx, AjbFO) if AjbFP != -1: AjbFE, AjbFn = AjbFa[AjbFP[0]].schedule_tasks[-1], AjbFa[ AjbFP[0]].schedule_ids[-1] AjbFa[AjbFP[0]].swap(AjbFP[1], AjbFa[AjbFU].schedule_tasks[-1], AjbFa[AjbFU].schedule_ids[-1]) AjbFa[AjbFU].swap(AjbFP[2], AjbFE, AjbFn) AjbFt = [machine.schedule_ids for machine in AjbFa] AjbFQ = calc_score(in_data, AjbFt) return Solution(schedule=Schedule(schedule=AjbFt, m=in_data.no_machines, n=in_data.no_tasks), score=AjbFQ)
def run(E, in_data): A = in_data B, C = read_instance(A) D = E.minimal_expected_loss_first(C, B) F = compute_loss(D, C, B) return Solution(score=F, schedule=Schedule(n=A.no_tasks, m=A.no_machines, schedule=[[B + 1 for B in A] for A in D]))
def lxpDwtEkCfBQUiy(lxpDwtEkCfBQUiJ,lxpDwtEkCfBQUiq): lxpDwtEkCfBQUiG=0 for lxpDwtEkCfBQUiW in lxpDwtEkCfBQUHs(lxpDwtEkCfBQUiJ.no_machines): lxpDwtEkCfBQUir=0 for lxpDwtEkCfBQUiI in lxpDwtEkCfBQUiq[lxpDwtEkCfBQUiW]: lxpDwtEkCfBQUir+=lxpDwtEkCfBQUHo(lxpDwtEkCfBQUiJ.tasks[lxpDwtEkCfBQUiI-1].ready-lxpDwtEkCfBQUir,0) lxpDwtEkCfBQUir+=lxpDwtEkCfBQUiJ.machine_speeds[lxpDwtEkCfBQUiW]*lxpDwtEkCfBQUiJ.tasks[lxpDwtEkCfBQUiI-1].duration lxpDwtEkCfBQUiG+=lxpDwtEkCfBQUir-lxpDwtEkCfBQUiJ.tasks[lxpDwtEkCfBQUiI-1].ready lxpDwtEkCfBQUiG=lxpDwtEkCfBQUiG/lxpDwtEkCfBQUiJ.no_tasks return Solution(lxpDwtEkCfBQUiG,Schedule(lxpDwtEkCfBQUiJ.no_tasks,lxpDwtEkCfBQUiJ.no_machines,lxpDwtEkCfBQUiq))
def dummy_schedule(self, instance: Instance): tasks_per_machine = instance.no_tasks // instance.no_machines schedule = [] task_num = 1 for i in range(0, instance.no_machines - 1): tasks = range(task_num, task_num + tasks_per_machine) schedule.append(list(tasks)) task_num += tasks_per_machine tasks = range(task_num, instance.no_tasks + 1) schedule.append(list(tasks)) return Schedule(instance.no_tasks, instance.no_machines, schedule)
def run(self, in_data: Instance) -> Solution: n = in_data.no_tasks m = in_data.no_machines machine_speeds = sorted(in_data.machine_speeds) r = range(m) r = sorted(r, key=in_data.machine_speeds.__getitem__) schedule = [[] for _ in range(m)] ready_tasks = [] done = [0 for _ in range(m)] tasks_by_readiness = [ IndexedTask(task.duration, task.ready, index) for index, task in enumerate(in_data.tasks, 1) ] tasks_by_readiness = sorted(tasks_by_readiness, key=lambda x: x.ready) mean_speed = sum(machine_speeds) / m time = tasks_by_readiness[0].ready while len(tasks_by_readiness) > 0 or len(ready_tasks) != 0: while len(tasks_by_readiness ) > 0 and tasks_by_readiness[0].ready <= time: ready_tasks.append(tasks_by_readiness.pop(0)) task_finish = min(done) machine = 0 for t in done: if t == task_finish: break machine += 1 if len(ready_tasks) == 0: continue best_index = 0 reverse = -1 if machine_speeds[machine] < mean_speed else 1 best_task_duration = 1e18 for index, task in enumerate(ready_tasks): if reverse * task.duration < best_task_duration: best_task_duration = reverse * task.duration best_index = index schedule[machine].append(ready_tasks.pop(best_index).index) done[ machine] = time + reverse * best_task_duration * machine_speeds[ machine] time = max( min(done), -1 if len(tasks_by_readiness) == 0 else tasks_by_readiness[0].ready) for i, c in zip(r, list(schedule)): schedule[i] = c schedule = Schedule(n, m, schedule) solution = Solution(0, schedule) return Solution(Evaluator132211().evaluate(in_data, solution).value, schedule)
def sort_global(self, instance: Instance, key): n = instance.no_tasks m = instance.no_machines times = [0] * m schedule = [[] for i in range(0, m)] tasks = sorted(instance.tasks, key=key) for task in tasks: next_machine = self.get_next_machine(times, instance.machine_speeds) schedule[next_machine].append(task.no) speed = instance.machine_speeds[next_machine] times[next_machine] = max(times[next_machine], task.ready) + speed * task.duration return Schedule(n, m, schedule)
def run(self, in_data: Instance) -> Solution: n = in_data.no_tasks m = in_data.no_machines tasks = [ ScheduledTask(i + 1, t.ready, t.duration, 0) for i, t in enumerate(in_data.tasks) ] tasks.sort(key=lambda t: t.p) c_m_i = [0] * m ans = [[] for _ in range(m)] for i, task in enumerate(tasks): min_m = 0 min_cost = 1e7 tmp_last_competed = [0.0] * m for m_i, machine, speed in zip(range(m), ans, in_data.machine_speeds): if len(machine) == 0: cost = task.p * speed if cost < min_cost: min_m = m_i min_cost = cost tmp_last_competed[m_i] = task.r + cost continue tmp_last_competed[m_i] = max(c_m_i[m_i], task.r) + task.p * speed cost = tmp_last_competed[m_i] - task.r if cost < min_cost: min_m = m_i min_cost = cost ans[min_m].append(task.id) task.end = max(c_m_i[min_m], task.r) + task.p * in_data.machine_speeds[min_m] c_m_i[min_m] = task.end obj = 0 for num, machine in enumerate(ans): c = 0 for idx in machine: task = in_data.tasks[idx - 1] c = max(c, task.ready) + (task.duration * in_data.machine_speeds[num]) obj += c - task.ready obj = obj / n return Solution(obj, Schedule(n, m, ans))
def run(self, in_data: Instance) -> Solution: self.get_instance_data(in_data.machine_speeds, in_data.tasks) self.sort_lists() self.clock, self.flow_time = 0.0, 0.0 task = self.assign_task() while task: machine = self.pick_machine() if machine != None: self.flow_time += machine.add_task(task) else: ready_machine = min(self.machines, key=lambda x: x.clock) self.clock = ready_machine.clock self.flow_time += ready_machine.add_task(task) task = self.assign_task() sequence = [machine.list_of_tasks for machine in self.machines] final_sequence = Schedule(len(self.tasks), len(self.machines), sequence) mean_flow_time = round(self.flow_time / in_data.no_tasks, 6) return Solution(mean_flow_time, final_sequence)
def alg2(in_data: Instance) -> Solution: schedule = [[] for _ in range(in_data.no_machines)] tasks = pd.DataFrame(data=[[t.ready, t.duration] for t in in_data.tasks]) # tasks = tasks.sort_values(by=[0, 1], ascending=[True, False]) tasks = tasks.sort_values(by=[0], ascending=[True]) machines = dict() for inx, m in enumerate(in_data.machine_speeds): machines[inx] = [m, 0] srt_mach = sorted(machines.items(), key=lambda it: it[1][0]) # print(srt_mach) # sum_p = sum([t.duration for t in in_data.tasks]) # sum_mach_pow = sum([1.0 / b for b in in_data.machine_speeds]) # opt_end = sum_p / sum_mach_pow for i, task in tasks.iterrows(): added = False for machine in range(5): if srt_mach[machine][1][1] <= task[0]: schedule[srt_mach[machine][0]].append(int(task.name) + 1) srt_mach[machine][1][1] = max(task[0], srt_mach[machine][1][1]) + \ task[1] * in_data.machine_speeds[srt_mach[machine][0]] added = True break if not added: tmp = [] for m in range(5): tmp.append( (m, max(srt_mach[m][1][1], task[0]) + task[1] * in_data.machine_speeds[srt_mach[m][0]])) best = min(tmp, key=lambda it: it[1]) schedule[srt_mach[best[0]][0]].append(int(task.name) + 1) srt_mach[best[0]][1][1] = best[1] result = Algorithm136811.calc_score(in_data, schedule) return Solution( result, Schedule(in_data.no_tasks, in_data.no_machines, schedule))
def run(self, cCLno: Instance) -> Solution: class MST(NamedTuple): cCLnI: cCLnV t: cCLnV index: cCLnQ cCLnz: cCLnV = 0 cCLnP = [] cCLnJ = [[] for _ in cCLni(cCLno.no_machines)] cCLnb = [MST(cCLnI, 0, i) for i, cCLnI in cCLnK(cCLno.machine_speeds)] cCLnN = cCLno.tasks cCLnN.sort(key=lambda task: task.ready) cCLnm = [[i, val] for i, val in cCLnK(cCLnN, start=1)] while cCLnv(cCLnP) < cCLno.no_tasks: cCLnu = [cCLnq for cCLnq in cCLnb if cCLnq.t <= cCLnz] cCLnE = cCLnR( takewhile(lambda task: task[cCLnG].ready <= cCLnz, cCLnm)) if cCLnv(cCLnu) > 0 and cCLnv(cCLnE) > 0: cCLne(cCLnu, cCLnE, cCLnP, cCLnb, cCLnJ, cCLnm, cCLnz) cCLnz += 1 elif cCLnv(cCLnu) == 0: cCLnz = cCLnr(cCLnF(cCLnb, key=lambda cCLnq: cCLnq.t).t, cCLnz) elif cCLnv(cCLnE) == 0: cCLnz = cCLnF(cCLnm, key=lambda task: task[cCLnG].ready)[cCLnG].ready else: cCLnz += 1 cCLnd = 0 for cCLnx in cCLni(cCLno.no_machines): cCLnz = 0 for cCLnO in cCLnJ[cCLnx]: cCLnz += cCLnr(cCLno.tasks[cCLnO - 1].ready - cCLnz, 0) cCLnz += cCLno.machine_speeds[cCLnx] * cCLno.tasks[cCLnO - 1].duration cCLnd += cCLnz - cCLno.tasks[cCLnO - 1].ready cCLnd = cCLnd / cCLno.no_tasks cCLnJ = Schedule(cCLno.no_tasks, cCLno.no_machines, cCLnJ) return Solution(cCLnd, cCLnJ)
def getSolution(in_data: Instance, schedule: List[List[int]]): schedule = Schedule(in_data.no_tasks, in_data.no_machines, schedule) tmpSolution = Solution(0, schedule) criterium = Criterium(in_data, tmpSolution) return Solution(criterium.getCriterium(), schedule)