def slideToOrigin(self, poly): bottom_pt, min_y = [], 999999999 for pt in poly: if pt[1] < min_y: min_y = pt[1] bottom_pt = [pt[0], pt[1]] GeoFunc.slidePoly(poly, -bottom_pt[0], -bottom_pt[1])
def slideToContainer(self): # 平移部分形状 for index, poly in enumerate(self.polys): right_pt = LPAssistant.getRightPoint(poly) if right_pt[0] > self.cur_length: delta_x = self.cur_length - right_pt[0] GeoFunc.slidePoly(poly, delta_x, 0) top_pt = self.poly_status[index][1] self.poly_status[index][1] = [top_pt[0] + delta_x, top_pt[1]]
def placeFirstPoly(self): poly = self.polygons[0] left_index, bottom_index, right_index, top_index = GeoFunc.checkBound( poly) # 获得边界 GeoFunc.slidePoly(poly, -poly[left_index][0], -poly[bottom_index][1]) # 平移到左下角
def slideToBottomLeft(self): '''移到最左下角位置''' for poly in self.cur_polys: GeoFunc.slidePoly(poly, -self.border_left, -self.border_bottom)
def minimizeOverlap(self): start_time = time.time() # 记录引导检索的相关内容 self.miu = [[1] * len(self.polys) for _ in range(len(self.polys))] self.initialOverlap() # 记录重叠变化情况 self.overlap_reocrd = [] # 检索次数限制/超出倍数退出 it, N = 0, 50 minimal_overlap = self.getTotalOverlap() cur_overlap = minimal_overlap print("初始重叠:", cur_overlap) # 限定计算次数 print("开始一次检索") while it < N: print("it:", it) # 获得随机序列并逐一检索 permutation = np.arange(len(self.polys)) np.random.shuffle(permutation) for i in range(len(self.polys)): # 选择特定形状 choose_index = permutation[i] # 通过重叠判断是否需要计算 with_overlap = False for item in self.pair_overlap[choose_index]: if item > 0: with_overlap = True break if with_overlap == False: continue # 获得当前的最小的深度(调整后),如果没有重叠,直接下一个 self.getPrerequisite(choose_index, self.poly_status[choose_index][2], offline=True) cur_min_depth = self.getPolyDepeth(choose_index) # 记录最优情况,默认是当前情况 original_position = self.poly_status[choose_index][1] best_position, best_orientation, best_depth = self.poly_status[ choose_index][1], self.poly_status[choose_index][ 2], cur_min_depth # print("当前最低高度:",best_depth) print("测试第", i, "个形状") # 遍历四个角度的最优值 for orientation in [0, 1, 2, 3]: # print("测试角度:",90*orientation,"度") self.getPrerequisite(choose_index, orientation, offline=True) self.getProblemLP() new_position, new_depth = self.searchBestPosition( choose_index) # 获得最优位置 if new_depth < best_depth: best_position, best_orientation, best_depth = copy.deepcopy( new_position), orientation, new_depth # 如果有变化状态则需要更新overlap以及移动形状 if best_position != original_position: print("本次检索最低深度:", best_depth) # 更新记录的位置 self.poly_status[choose_index][1] = copy.deepcopy( best_position) self.poly_status[choose_index][2] = best_orientation # 获取形状顶部位置并平移过去 new_poly = copy.deepcopy( self.all_polygons[choose_index][best_orientation]) top_point = LPAssistant.getTopPoint(new_poly) GeoFunc.slidePoly(new_poly, best_position[0] - top_point[0], best_position[1] - top_point[1]) # 更新形状与重叠情况 self.polys[choose_index] = new_poly self.updateOverlap(choose_index) # self.showPolys() # 计算新方案的重叠情况 cur_overlap = self.getTotalOverlap() self.overlap_reocrd.append(cur_overlap) if cur_overlap < bias: print("没有重叠,本次检索结束") break elif cur_overlap < minimal_overlap: minimal_overlap = cur_overlap it = 0 print("\n当前重叠:", cur_overlap, "\n") it = it + 1 self.updateMiu() # 超出检索次数 if it == N: print("超出更新次数/超出倍数") # self.showPolys() end_time = time.time() print("本轮耗时:", end_time - start_time) print("最终结果:", self.polys) print("当前状态:", self.poly_status) with open( "/Users/sean/Documents/Projects/Packing-Algorithm/record/fu_result.csv", "a+") as csvfile: writer = csv.writer(csvfile) writer.writerows([[ time.asctime(time.localtime(time.time())), end_time - start_time, self.cur_length, self.total_area / (self.cur_length * self.width), cur_overlap, self.poly_status, self.polys ]]) self.showPolys() self.plotRecord("Overlap Record:", self.overlap_reocrd)