def cal_Fitness_Value(self, chromo): if self.memo_FV.get(tuple(chromo)): return self.memo_FV[(tuple(chromo))].eti_penalty p = copy.deepcopy(self.problem) p.earliness_penalties[ chromo[0]] = p.earliness_penalties[chromo[0]] + p.a p.tardiness_penalties[ chromo[0]] = p.tardiness_penalties[chromo[0]] - p.a p.earliness_penalties[chromo[ self.n - 1]] = p.earliness_penalties[chromo[self.n - 1]] - p.a p.tardiness_penalties[chromo[ self.n - 1]] = p.tardiness_penalties[chromo[self.n - 1]] + p.a # memos memo_BT = bt.init_BT_memo(chromo, p.due_dates, p.processing_times) memo_ET = et.init_ET_memo(chromo, p.due_dates, p.processing_times) et_global_solution = et.init_ET_global_solution(chromo, p) memo_ETI = dp.init_ETI_memo(chromo) block_lasts, end_times, eti_penalty, _ = dp.opt_ETI( memo_BT, memo_ET, memo_ETI, et_global_solution, utils.BIG_NUMBER, chromo, self.n - 1, p) # if self.iter == 0: # print(eti_penalty) real_obj, b_ratio = self.cal_Real_Objective(chromo, block_lasts, end_times, self.problem) self.memo_FV[tuple(chromo)] = utils.Solution(block_lasts, end_times, eti_penalty, 0) return eti_penalty
def get_one_child(self): index1 = random.randint(0, self.pop_size - 1) index2 = random.randint(0, self.pop_size - 1) while index1 == index2: index2 = random.randint(0, self.pop_size - 1) chromo1 = self.pop[index1] chromo2 = self.pop[index2] et_global_solution1 = et.init_ET_global_solution(chromo1, self.problem) et_global_solution2 = et.init_ET_global_solution(chromo2, self.problem) et_penalty1 = sum(et_global_solution1.block_objs) et_penalty2 = sum(et_global_solution2.block_objs) if et_penalty1 > et_penalty2: penalty2 = self.cal_Fitness_Value(chromo2, et_global_solution2) if et_penalty1 > penalty2: better_penalty = penalty2 new_chromo = chromo2[:] else: penalty1 = self.cal_Fitness_Value(chromo1, et_global_solution1) if penalty1 > penalty2: better_penalty = penalty2 new_chromo = chromo2[:] else: better_penalty = penalty1 new_chromo = chromo1[:] else: penalty1 = self.cal_Fitness_Value(chromo1, et_global_solution1) if et_penalty2 > penalty1: better_penalty = penalty1 new_chromo = chromo1[:] else: penalty2 = self.cal_Fitness_Value(chromo2, et_global_solution2) if penalty1 > penalty2: better_penalty = penalty2 new_chromo = chromo2[:] else: better_penalty = penalty1 new_chromo = chromo1[:] if (better_penalty <= self.memo_opt[self.iter]): self.memo_opt[self.iter] = better_penalty self.opt_chromo = new_chromo # cross r = random.random() if r < self.cross_rate: temp_chromo = self.pop[random.randint(0, self.pop_size - 1)] new_chromo = self.cross(new_chromo, temp_chromo) # print("iter", self.iter) # print("mutation count", self.mutation_count) # print("cross_count", self.cross_count) # print([a for a in new_chromo]) # mutation r = random.random() if r < self.mut_rate: new_chromo = self.mutation(new_chromo) return new_chromo
def run4(problem): jobs = [ 8, 9, 3, 2, 1, 4, 7, 5, 6, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 ] p = copy.deepcopy(problem) n = p.n p.earliness_penalties[jobs[0]] = p.earliness_penalties[jobs[0]] + p.a p.tardiness_penalties[jobs[0]] = p.tardiness_penalties[jobs[0]] - p.a p.earliness_penalties[jobs[n - 1]] = p.earliness_penalties[jobs[n - 1]] - p.a p.tardiness_penalties[jobs[n - 1]] = p.tardiness_penalties[jobs[n - 1]] + p.a start = time.process_time() memo_BT = bt.init_BT_memo(jobs, problem.due_dates, problem.processing_times) memo_ET = et.init_ET_memo(jobs, problem.due_dates, problem.processing_times) memo_ETI = dp.init_ETI_memo(jobs, problem.due_dates) block_lasts, end_times, eti_penalty1 = dp.opt_ETI(memo_BT, memo_ET, memo_ETI, utils.BIG_NUMBER, jobs, 0, n - 1, p) end = time.process_time() run_time1 = end - start print("**************** Main ****************") print("********* Basic DP Block Lasts: *********") for i in block_lasts: print(i) print("********* Basic DP End Times: *********") for i in end_times: print(i) print("********* Basic DP eti_penalty: *********") print("overall penalty of basic DP:", eti_penalty1) start = time.process_time() memo_BT = bt.init_BT_memo(jobs, problem.due_dates, problem.processing_times) memo_ET = et.init_ET_memo(jobs, problem.due_dates, problem.processing_times) memo_ETI_bounded = dpb.init_ETI_memo_bounded(jobs, problem.due_dates) block_lasts, end_times, eti_penalty2 = dpb.opt_ETI_Bounded( memo_BT, memo_ET, memo_ETI_bounded, utils.BIG_NUMBER, n - 1, jobs, 0, n - 1, p) end = time.process_time() run_time2 = end - start print("**************** Main ****************") print("********* Bounded DP Block Lasts: *********") for i in block_lasts: print(i) print("********* Bounded DP End Times: *********") for i in end_times: print(i) print("********* Bounded DP eti_penalty: *********") print("overall penalty of basic DP:", eti_penalty2) print("******************") print("runtime 1", run_time1) print("runtime 2", run_time2)
def generate_Initial(self): initial = [] chromo = range(self.n) # number of chromosomes by due dates in the initial population nCBD = 3 due = self.problem.due_dates due, chromo = zip(*sorted(zip(due, chromo))) chromo = list(chromo) CBD = chromo et_global_solution = et.init_ET_global_solution(CBD, self.problem) objCBD = self.cal_Fitness_Value(CBD, et_global_solution) for i in range(nCBD): CBD = list(CBD) initial.append(CBD) # number of chromosomes by expected start times in the initial population nCES = 3 chromo = list(range(self.n)) due = self.problem.due_dates process = self.problem.processing_times expected_starts = [] for j in range(len(due)): expected_starts.append(due[j] - process[j]) expected_starts, chromo = zip(*sorted(zip(expected_starts, chromo))) chromo = list(chromo) CES = chromo et_global_solution = et.init_ET_global_solution(CES, self.problem) objCES = self.cal_Fitness_Value(CES, et_global_solution) for i in range(nCES): CES = list(CES) initial.append(CES) if objCBD < objCES: self.opt_chromo = CBD self.memo_opt.append(objCBD) else: self.opt_chromo = CES self.memo_opt.append(objCES) for i in range(self.pop_size - nCBD - nCES): random.shuffle(chromo) tempChromo = chromo.copy() initial.append(tempChromo) # print("**************** initial pop ****************") # for j in range(self.pop_size): # print(initial[j]) # print("**************** initial pop ****************") return initial
def opt_ETI(memoBT, memo_ET, memo_ETI, et_global_solution, upper_bound, jobs, last, problem): total_cplex_time = 0 if memo_ETI[last].eti_penalty >= 0: eti_penalty = memo_ETI[last].eti_penalty if memo_ETI[last].end_times[last] >= upper_bound: eti_penalty = utils.BIG_NUMBER return list(memo_ETI[last].block_lasts), list( memo_ETI[last].end_times), eti_penalty, total_cplex_time block_lasts = list() end_times = list() et_slt = et.opt_ET(memo_ET, et_global_solution, jobs, problem, last) if et_slt.head_last == last: # if the optimal schedule of ET problem has only one block block_start = et_slt.tail_start eti_penalty = et_slt.et_penalty block_lasts.append(last) t = block_start for j in range(last + 1): t = t + problem.processing_times[jobs[j]] end_times.append(t) else: block_lasts, end_times, eti_penalty, cplex_time = dp(memoBT, memo_ET, memo_ETI, et_global_solution, et_slt.head_last, et_slt.tail_first, jobs, last, problem) total_cplex_time += cplex_time memo_ETI[last].block_lasts = list(block_lasts) memo_ETI[last].end_times = list(end_times) memo_ETI[last].eti_penalty = eti_penalty if end_times[last] >= upper_bound: eti_penalty = utils.BIG_NUMBER return block_lasts, end_times, eti_penalty, total_cplex_time
def run1(problem): # print('"*************** Single Machine Scheduling with E/T/I Penalties ***************"') # jobs=[3,2,1,4,0] # jobs = [4,7] # jobs = [8, 9, 3, 2, 1, 4, 7, 5, 6, 0,10,11,12,13,14,15,16,17,18,19] jobs = [1, 4, 2, 0, 3] # due2 = list(problem.due_dates) # due2, jobs = zip(*sorted(zip(due2, jobs))) # The variable part of idleness penalty is absorbed by the first job and the last job. p = copy.deepcopy(problem) p.earliness_penalties[jobs[0]] = p.earliness_penalties[jobs[0]] + p.a p.tardiness_penalties[jobs[0]] = p.tardiness_penalties[jobs[0]] - p.a p.earliness_penalties[jobs[n - 1]] = p.earliness_penalties[jobs[n - 1]] - p.a p.tardiness_penalties[jobs[n - 1]] = p.tardiness_penalties[jobs[n - 1]] + p.a start = time.process_time() memo_BT = bt.init_BT_memo(jobs, problem.due_dates, problem.processing_times) memo_ET = et.init_ET_memo(jobs, problem.due_dates, problem.processing_times) memo_ETI = dp.init_ETI_memo(jobs, problem.due_dates) block_lasts, end_times, eti_penalty1 = dp.opt_ETI(memo_BT, memo_ET, memo_ETI, utils.BIG_NUMBER, jobs, 0, n - 1, p) end = time.process_time() run_time1 = end - start print("**************** Main ****************") print("********* DP block lasts: *********") for i in block_lasts: print(i) print("********* DP end times: *********") for i in end_times: print(i) print("********* DP eti_penalty: *********") print("overall penalty of DP:", eti_penalty1) start = time.process_time() eti_penalty2, test_model = test.test_DP(jobs, problem.b, problem.due_dates, problem.processing_times, problem.earliness_penalties, problem.tardiness_penalties) end = time.process_time() run_time2 = end - start print("********* DP eti_penalty: *********") print("overall penalty of DP:", eti_penalty1) print("********* DP runtime: *********") print(run_time1) print("********* CPLEX eti_penalty: *********") print("overall penalty of CPLEX:", eti_penalty2) print("********* CPLEX runtime: *********") print(run_time2) return round(eti_penalty1), round(eti_penalty2), block_lasts, test_model
def run9(p, jobs): sys.setrecursionlimit(1200) start = time.process_time() memo_BT = bt.init_BT_memo(jobs, p.due_dates, p.processing_times) memo_ET = et.init_ET_memo(jobs, p.due_dates, p.processing_times) et_global_solution = et.init_ET_global_solution(jobs, p) memo_ETI = dpb.init_ETI_memo_bounded(jobs) block_lasts, _, eti_penalty1, cplex_time = dpb.opt_ETI_Bounded( memo_BT, memo_ET, memo_ETI, et_global_solution, utils.BIG_NUMBER, p.n - 1, jobs, p.n - 1, p) end = time.process_time() run_time1 = end - start num_idle = len(block_lasts) - 1 f = open('My_DP_Bounded_0725.txt', 'a') f.write( str(p.n) + "\t" + str(p.b) + "\t" + str(p.rho) + "\t" + str(run_time1) + "\t" + str(eti_penalty1) + "\t" + str(num_idle) + "\n") f.close()
def __init__(self, jobs, problem): self.jobs = jobs self.problem = problem self.vs = [] self.memo = [] head_last, tail_first, et_penalty_ET, num_idle_ET = et.opt_ET_no_memo( jobs, 0, problem.n - 1, problem) _, _, ub_penalty, _ = blockTiming.time_block_no_memo( jobs, 0, problem.n - 1, problem) self.idle_bound = min( num_idle_ET, math.floor(round(ub_penalty - et_penalty_ET, 4) / problem.b)) self.tail_first = tail_first self.head_last = head_last
def run9_0(p, jobs): # sys.setrecursionlimit(2000) start = time.process_time() memo_BT = bt.init_BT_memo(jobs, p.due_dates, p.processing_times) memo_ET = et.init_ET_memo(jobs, p.due_dates, p.processing_times) memo_ETI = dp.init_ETI_memo(jobs, p.due_dates) _, _, eti_penalty1, cplex_time = dp.opt_ETI(memo_BT, memo_ET, memo_ETI, utils.BIG_NUMBER, jobs, p.n - 1, p) end = time.process_time() run_time1 = end - start f = open('My_DP_results_0725.txt', 'a') f.write( str(p.n) + "\t" + str(p.b) + "\t" + str(p.rho) + "\t" + str(run_time1) + "\t" + str(cplex_time) + "\n") f.close()
def opt_ETI_Bounded(memoBT, memo_ET, memo_ETI_bounded, et_global_solution, upper_bound, idle_bound, jobs, last, problem): if memo_ETI_bounded[last].eti_penalty >= 0: eti_penalty = memo_ETI_bounded[last].eti_penalty if memo_ETI_bounded[last].end_times[last] >= upper_bound: eti_penalty = utils.BIG_NUMBER elif memo_ETI_bounded[last].num_idle > idle_bound: eti_penalty = utils.BIG_NUMBER return list(memo_ETI_bounded[last].block_lasts), list( memo_ETI_bounded[last].end_times), eti_penalty, 0 block_lasts = list() end_times = list() if idle_bound == 0: start_UB, end_UB, et_penalty_UB, cplex_time = bt.time_block( memoBT, jobs, 0, last, problem) block_lasts.append(last) t = start_UB for i in range(last + 1): t += problem.processing_times[jobs[i]] end_times.append(t) return block_lasts, end_times, et_penalty_UB, 0 et_slt = et.opt_ET(memo_ET, et_global_solution, jobs, problem, last) # new lemma 5 if et_slt.tail_end > upper_bound: et_penalty_boundary, _ = et.opt_ET_with_boundary( jobs, 0, last, problem, upper_bound) delta_et = et_penalty_boundary - et_slt.et_penalty if et_slt.num_idle * problem.b < delta_et: return block_lasts, end_times, utils.BIG_NUMBER, 0 if et_slt.tail_first <= et_global_solution.block_lasts[0]: block_start, end_UB, et_penalty_UB, cplex_time = bt.time_block( memoBT, jobs, 0, last, problem) eti_penalty = et_penalty_UB block_lasts.append(last) t = block_start for j in range(last + 1): t = t + problem.processing_times[jobs[j]] end_times.append(t) else: start_UB, end_UB, et_penalty_UB, cplex_time = bt.time_block( memoBT, jobs, 0, last, problem) idle_bound = min( idle_bound, et_slt.num_idle, math.floor( round(et_penalty_UB - et_slt.et_penalty, 4) / problem.b)) block_lasts, end_times, eti_penalty, cplex_time = dp_Bounded( memoBT, memo_ET, memo_ETI_bounded, et_global_solution, et_slt.head_last, et_slt.tail_first, jobs, last, idle_bound, problem) num_idle = len(block_lasts) - 1 memo_ETI_bounded[last].block_lasts = list(block_lasts) memo_ETI_bounded[last].end_times = list(end_times) memo_ETI_bounded[last].eti_penalty = eti_penalty memo_ETI_bounded[last].num_idle = num_idle if num_idle > idle_bound: eti_penalty = utils.BIG_NUMBER if end_times[last] >= upper_bound: eti_penalty = utils.BIG_NUMBER return block_lasts, end_times, eti_penalty, 0