def geneticAlgorithm(self): self.pop = [] # 种群记录 self.length_record = [] # 记录高度 self.lowest_length_record = [] # 记录全局高度 self.global_best_sequence = [] # 全局最优序列 self.global_lowest_length = 9999999999 # 全局最低高度 # 初步的随机数组 for i in range(0, self.pop_size): _list=copy.deepcopy(self.poly_list) random.shuffle(_list) self.pop.append(_list) # 持续获得下一代 for i in range(0, self.generations): print("############################ Compute the ",i+1,"th generation #######################################") self.getLengthRanked() # 高度排列 self.getNextGeneration() # 获得下一代 # 高度记录与最低高度处理 self.length_record.append(self.fitness_ranked[0][1]) if self.fitness_ranked[0][1]<self.global_lowest_length: self.global_lowest_length=self.fitness_ranked[0][1] self.global_best_sequence=self.pop[self.fitness_ranked[0][0]] self.lowest_length_record.append(self.global_lowest_length) # print(self.global_lowest_length) # print("Final length: " + str(self.global_lowest_length)) blf=BottomLeftFill(self.width,PolyListProcessor.getPolysVertices(self.global_best_sequence),NFPAssistant=self.NFPAssistant) blf.showAll()
def __init__(self,width,poly_list,nfp_asst=None,generations=10,pop_size=20): self.width=width self.minimal_rotation=360 # 最小的旋转角度 self.poly_list=poly_list self.ga_multi=False # 开了多进程反而更慢 if self.ga_multi: multiprocessing.set_start_method('spawn',True) self.elite_size=10 # 每一代选择个数 self.mutate_rate=0.1 # 变异概率 self.generations=generations # 代数 self.pop_size=pop_size # 每一代的个数 self.history_index_list=[] self.history_length_list=[] if nfp_asst: self.NFPAssistant=nfp_asst else: self.NFPAssistant=NFPAssistant(PolyListProcessor.getPolysVertices(poly_list),get_all_nfp=True) self.geneticAlgorithm() self.plotRecord()
def __init__(self,poly_list): self.min_angle=360 # 允许旋转的最小角度 self.width=1500 # 排列的宽度 self.temp_now=200 # 起始温度 2000 self.temp_end=1e-5 # 结束温度 1e-20 self.dec_rate=0.7 # 降温速率 0.995 self.loop_times=5 # 内循环次数 self.cur_poly_list=poly_list # 当前的序列 self.new_poly_list=poly_list # 生成新的序列 self.history_index_list=[] # 运行过的index序列 self.history_length_list=[] # 运行结果 self.NFPAssistant=NFPAssistant(PolyListProcessor.getPolysVertices(poly_list),get_all_nfp=True) self.run()
def __init__(self,poly_list): # 初始设置 self.width=1500 # 初始化数据,NFP辅助函数 polys=PolyListProcessor.getPolysVertices(poly_list) self.NFPAssistant=NFPAssistant(polys,get_all_nfp=False) # 获得最优解 blf=BottomLeftFill(self.width,polys,NFPAssistant=self.NFPAssistant) self.best_height=blf.contain_height self.cur_height=blf.contain_height # 当前的poly_list均为已经排样的情况 self.best_poly_list=copy.deepcopy(poly_list) self.cur_poly_list=copy.deepcopy(poly_list) self.run()
def packingLength(poly_list,history_index_list,history_length_list,width,**kw): polys=PolyListProcessor.getPolysVertices(poly_list) index_list=PolyListProcessor.getPolyListIndex(poly_list) length=0 check_index=PolyListProcessor.getIndex(index_list,history_index_list) if check_index>=0: length=history_length_list[check_index] else: try: if 'NFPAssistant' in kw: blf=BottomLeftFill(width,polys,NFPAssistant=kw['NFPAssistant']) # blf.showAll() length=blf.contain_length else: length=BottomLeftFill(width,polys).contain_length except: print('出现Self-intersection') length=99999 history_index_list.append(index_list) history_length_list.append(length) return length