def placePoly(self,index): ''' 放置某一个index的形状进去 ''' adjoin=self.polygons[index] ifp=self.getInnerFitRectangle(self.polygons[index]) differ_region=Polygon(ifp) multi=True if multi==True: if self.choose_nfp==True and index>8: target_polygon=self.polygons[index-8:index] bottom_height=self.getMinTop(target_polygon)+300 differ_region=differ_region.difference(Polygon([[0,0],[self.width,0],[self.width,bottom_height],[0,bottom_height]])) else: target_polygon=self.polygons[0:index] tasks=[(main,adjoin) for main in target_polygon] res=pool.starmap(NFP,tasks) for item in res: nfp=item.nfp differ_region=differ_region.difference(Polygon(nfp)) else: for main_index in range(0,index): main=self.polygons[main_index] nfp=NFP(main,adjoin).nfp differ_region=differ_region.difference(Polygon(nfp)) # print(differ_region) differ=GeoFunc.polyToArr(differ_region) differ_index=self.getBottomLeft(differ) refer_pt_index=GeoFunc.checkTop(adjoin) GeoFunc.slideToPoint(self.polygons[index],adjoin[refer_pt_index],differ[differ_index])
def getMinTop(self,polygons): min_top=9999999999 for poly in polygons: bt_index=GeoFunc.checkTop(poly) if poly[bt_index][1]<min_top: min_top=poly[bt_index][1] return min_top
def getHeight(self): _max=0 for i in range(1,len(self.polygons)): top_index=GeoFunc.checkTop(self.polygons[i]) top=self.polygons[i][top_index][1] if top>_max: _max=top self.contain_height=_max PltFunc.addLine([[0,self.contain_height],[self.width,self.contain_height]],color="blue")
def shrink(self): self.new_height = self.height * 0.95 self.cur_polys = [] for poly in self.polys: top_index = GeoFunc.checkTop(poly) delta = self.new_height - poly[top_index][1] if delta >= 0: self.cur_polys.append(GeoFunc.copyPoly(poly)) else: self.cur_polys.append(GeoFunc.getSlide(poly, 0, delta))
def placeFirstPoly(self): ''' 放置第一个形状进去,并平移到left-bottom ''' poly=self.polygons[0] poly_index=GeoFunc.checkTop(poly) left_index,bottom_index,right_index,top_index=GeoFunc.checkBound(poly) move_x=poly[left_index][0] move_y=poly[bottom_index][1] GeoFunc.slidePoly(poly,0,-move_y)
def getInnerFitRectangle(self,poly): ''' 获得IFR,同时平移到left-bottom ''' poly_index=GeoFunc.checkTop(poly) left_index,bottom_index,right_index,top_index=GeoFunc.checkBound(poly) move_x=poly[left_index][0] move_y=poly[bottom_index][1] GeoFunc.slidePoly(poly,-move_x,-move_y) refer_pt=[poly[poly_index][0],poly[poly_index][1]] width=self.width-poly[right_index][0] height=self.height-poly[top_index][1] IFR=[refer_pt,[refer_pt[0]+width,refer_pt[1]],[refer_pt[0]+width,refer_pt[1]+height],[refer_pt[0],refer_pt[1]+height]] return IFR
def MinimizeOverlap(self, oris, v, o): ''' oris: 允许旋转的角度集合 v: 多边形位置 实际已通过self.polygons得到 o: 旋转的角度 后期可考虑把多边形封装成类 ''' n_polys = self.n_polys it = 0 fitness = 999999 while it < self.n_mo: Q = np.random.permutation(range(n_polys)) for i in range(n_polys): curPoly = self.polygons[Q[i]] # 记录原始位置 top_index = GeoFunc.checkTop(curPoly) top = list(curPoly[top_index]) F = self.evaluate(Q[i]) # 以后考虑旋转 print('F of', Q[i], ':', F) v_i = self.CuckooSearch(Q[i]) self.evaluate(Q[i], v_i) F_new = v_i.getF() print('new F of', Q[i], ':', F) if F_new < F: print('polygon', Q[i], v_i.getXY()) else: # 平移回原位置 GeoFunc.slideToPoint(curPoly, curPoly[top_index], top) fitness_new = self.evaluateAll() if fitness_new == 0: return it # 可行解 elif fitness_new < fitness: fitness = fitness_new it = 0 self.updatePenalty() it = it + 1 return it
def slidePolytoMe(self, poly): top_index = GeoFunc.checkTop(poly) top = poly[top_index] GeoFunc.slideToPoint(poly, top, self.getXY())