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 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 placePoly(self, index): ''' 放置某一个index的形状进去 ''' adjoin = self.polygons[index] ifp = self.getInnerFitRectangle(self.polygons[index]) differ_region = Polygon(ifp) # 求解NFP和IFP的资料 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 __init__(self, poly1, poly2, **kw): self.stationary = poly1 self.sliding = poly2 start_point_index = geoFunc.checkBottom(self.stationary) self.start_point = [ poly1[start_point_index][0], poly1[start_point_index][1] ] self.locus_index = geoFunc.checkTop(self.sliding) # 如果不加list则original_top是指针 self.original_top = list(self.sliding[self.locus_index]) geoFunc.slideToPoint(self.sliding, self.sliding[self.locus_index], self.start_point) self.start = True # 判断是否初始 self.nfp = [] self.error = 1 self.main() if 'show' in kw: if kw["show"] == True: self.showResult() # 计算完成之后平移回原始位置 geoFunc.slideToPoint(self.sliding, self.sliding[self.locus_index], self.original_top)
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())