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 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 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 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()