def getLengthRanked(self): length_results = [] self.fitness_sum = 0 if self.ga_multi == True: tasks = [[pop] for pop in self.pop] pool = multiprocessing.Pool() results = pool.starmap(self.getLength, tasks) for i in range(0, len(self.pop)): # print("length:",results[i]) self.fitness_sum += 1000 / results[i] length_results.append([ i, results[i], 1000 / results[i], PolyListProcessor.getPolyListIndex(self.pop[i]) ]) else: for i in range(0, len(self.pop)): length = self.getLength(self.pop[i]) self.fitness_sum += 1000 / length length_results.append([ i, length, 1000 / length, PolyListProcessor.getPolyListIndex(self.pop[i]) ]) self.fitness_ranked = sorted(length_results, key=operator.itemgetter(1)) # 排序,包含index
def breed(self,parent1, parent2): geneA,geneB = random.randint(0,len(parent1)-1), random.randint(0,len(parent1)-1) start_gene,end_gene = min(geneA, geneB),max(geneA, geneB) parent1_index = PolyListProcessor.getPolyListIndex(parent1) parent2_index = PolyListProcessor.getPolyListIndex(parent2) child1_index = parent1_index[start_gene:end_gene] # 截取一部分 child2_index = [item for item in parent2_index if item not in child1_index] # 截取剩余部分 return PolyListProcessor.getPolysByIndex(child1_index,self.poly_list) + PolyListProcessor.getPolysByIndex(child2_index,self.poly_list)
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