def readRecord(): line=122 df = pd.read_csv("/Users/sean/Documents/Projects/Packing-Algorithm/arrangements/record.csv") polygons=json.loads(df["result"][line]) for poly in polygons: PltFunc.addPolygon(poly) PltFunc.showPlt()
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 testData(): df = pd.read_csv("/Users/sean/Documents/Projects/Packing-Algorithm/record/convex.csv") poly1=json.loads(df['polygon'][4674]) poly2=json.loads(df['polygon'][3652]) nfp=NFP(poly1,poly2) # NFP计算,error<0表示错误 PltFunc.addPolygon(poly1) PltFunc.addPolygon(poly2) # PltFunc.addPolygonColor(nfp.nfp) # PltFunc.showPlt() bfp=bestFitPosition(nfp,False)
def getConvexRandom(): polygon=[] num=10 for i in range(0,num): # radian=(2/num)*math.pi*i+math.pi*random.randrange(0,5,1)/12 # convex 4的角度 radian=(2/num)*math.pi*i+math.pi*random.randrange(0,3,1)/(num*2) # convex num>4的角度 radius=random.randrange(200,500,100) pt=[radius*math.cos(radian),radius*math.sin(radian)] polygon.append(pt) GeoFunc.slidePoly(polygon,750,750) storePolygon(polygon,num=num) PltFunc.addPolygon(polygon) PltFunc.showPlt()
def showResult(self, **kw): if "current" in kw and kw["current"] == True: for poly in self.cur_polys: PltFunc.addPolygonColor(poly) PltFunc.addLine( [[0, self.new_height], [self.width, self.new_height]], color="blue") if "initial" in kw and kw["initial"] == True: for poly in self.polys: PltFunc.addPolygon(poly) PltFunc.addLine([[0, self.height], [self.width, self.height]], color="blue") PltFunc.showPlt()
def pltArea(self): A, B, C = 0, 0, 0 for i, coefficient in enumerate(self.break_points_list): print(coefficient) A = A + coefficient[1] B = A + coefficient[2] C = A + coefficient[3] if i > 0: x = [self.break_points_list[i - 1][0], coefficient[0]] else: x = [0, coefficient[0]] y0 = self.getQuadratic(x[0], A, B, C) y1 = self.getQuadratic(x[1], A, B, C) PltFunc.addLine([[x[0], y0], [x[1], y1]]) PltFunc.showPlt()
def showAll(self): for i in range(0,len(self.polygons)): PltFunc.addPolygon(self.polygons[i]) length=max(self.width,self.contain_height) PltFunc.addLine([[self.width,0],[self.width,self.contain_height]],color="blue") PltFunc.showPlt(width=length,height=length)
def CuckooSearch(self, poly_id, ori=''): ''' poly_id: 当前多边形index ori: 允许旋转的角度 ''' cuckoos = [] poly = self.polygons[poly_id] GL_Algo = BottomLeftFill(self.W, self.polygons) R = GL_Algo.getInnerFitRectangleNew(poly) # 为当前多边形计算inner-fit矩形 i = 0 while i < self.n_c: # 产生初始种群 c = Cuckoo(R) if self.censorCuckoo(c) == False: continue cuckoos.append(c) print(c.getXY()) i = i + 1 bestCuckoo = cuckoos[0] t = 0 while t < self.maxGen: # 开始搜索 c_i = random.choice(cuckoos) # 通过Levy飞行产生解 newCuckooFlag = False while newCuckooFlag == False: newX, newY = self.getCuckoos_Levy(1, bestCuckoo) c_i.setXY(newX[0], newY[0]) if self.censorCuckoo(c_i): newCuckooFlag = True self.evaluate(poly_id, c_i, ori) c_j = random.choice(cuckoos) self.evaluate(poly_id, c_j, ori) if c_i.getF() < c_j.getF(): c_j = c_i bestCuckoo = c_j # 丢弃一部分最坏的巢并在新位置建立新巢 cuckoos.sort(key=lambda x: x.getF(), reverse=True) newX, newY = self.getCuckoos_Levy( int(self.percentage * len(cuckoos)) + 1, bestCuckoo) newi = 0 for i in range(int(len(cuckoos) * self.percentage)): print('----- 第', str(t + 1), '代 // 第', str(i + 1), '只 ----') if newi >= len(newX): break c_new = Cuckoo(R) newCuckooFlag = False while newCuckooFlag == False: c_new.setXY(newX[newi], newY[newi]) if self.censorCuckoo(c_new) == False: newX, newY = self.getCuckoos_Levy( int(self.percentage * len(cuckoos)) + 1, bestCuckoo) newi = 0 else: newCuckooFlag = True self.evaluate(poly_id, c_new, ori) cuckoos[i] = c_new if c_new.getF() == 0: break newi = newi + 1 cuckoos.sort(key=lambda x: x.getF(), reverse=False) bestCuckoo = cuckoos[0] bestCuckoo.slidePolytoMe(poly) print(bestCuckoo.getF(), bestCuckoo.getXY()) self.bestF = bestCuckoo.getF() for i in range(0, self.n_polys): PltFunc.addPolygon(self.polygons[i]) t = t + 1 PltFunc.saveFig(str(t)) return bestCuckoo
def showAll(self): for i in range(0, self.n_polys): PltFunc.addPolygon(self.polygons[i]) PltFunc.showPlt()
def readPoly(): line=1000*10+random.randint(0,1000) df = pd.read_csv("/Users/sean/Documents/Projects/Packing-Algorithm/record/convex.csv") polygon=json.loads(df["polygon"][line]) PltFunc.addPolygon(polygon) PltFunc.showPlt()