⭐️ 前言

小编之前写过一篇博文:求解三维装箱问题的启发式深度优先搜索算法(python),详述了基于空间选择的三维装箱算法。本文考虑了一个事实:在某些情况下,我们在摆放物品时,总是优先选择较低的平面,基于这个常识,本文提出一种基于平面选择的三维装箱算法。废话不多说,开始算法之旅吧。

⭐️ 块

块的定义及生成可以参考博文:求解三维装箱问题的启发式深度优先搜索算法(python),这里就不在赘述。

⭐️ 平面

“平面”指可用于摆放货物的面。初始平面就是箱的整个底面,放入第一批货物后,“平面”包括了同批货物顶面形成的面和箱底面空余的部分。
本文算法采用由底向上的方式完成物品的装载,既优先铺满底面,然后再向上堆放物品。大体过程是:首先由完全相同的货物组成“块”,然后用块自底向上依次填充所选择的目标平面,并重新生成若干新的平面,然后不断重复上述过程来完成最终的摆放方案。下图演示了一个用4个“块”进行填充的简单过程,每个块顶部都生成了一个的平面。在这个例子中,填充完毕后,从原先的1个平面,分成了8个新的平面。

⭐️ 选择平面

当所用容器仅有一个时,初始情况下只有一个备选平面,即箱底面。
选择装载目标平面时,按照以下几条准则依次进行判断:

1) 为避免堆积过高,影响货物堆放的稳定性,优先选择空间位置较低(即参考点z坐标较小)的平面;
2) 若几个平面参考点z坐标相同,则优先选择面积较小的平面,因为面积小的平面在后面可能更难使用;
3) 若这些平面的参考点z坐标与面积均相同,则优先选择相对较狭长的平面,同样因为狭长的平面在后面可能更加难以利用;
4) 若以上3点均相同,先比较它们参考点的x坐标,选择x坐标最小的一个。若x坐标相同,则选择y坐标最小的一个。

⭐️ 填充平面

为能够充分利用空间,物品块与目标平面组合的保留规则依次为:

1) 货物组成的“块”能最大限度地利用目标平面,即放入块后目标平面剩余面积最小;
2) 若剩余面积相同,比较块的体积,保留最大体积的块。

下文将该准则简称为准则。

⭐️ 合并平面

当所选择的平面不能容纳任何一个货物时,更有效地利用空间,需进行平面合并。一个平面只能和其相邻的平面合并,相邻平面指高度相同,即具有相同参考点z坐标,至少有一边平齐,如下图所示类似情况的平面(图中蓝色部分表示目标平面(gs),白色部分表示其相邻平面(ns)):

首先目标平面(gs)先后在平面列表和备用平面列表中依次顺序查找是否有相邻平面(ns)。平面列表指还未使用的新平面列表,备用平面列表指已经过计算不可能放入任何物品的平面的列表。当一个目标平面找到与其相邻的平面时,通过试合并决定是否保留这一合并,并进行正式合并。保留某次试合并的基本准则包括:

(1) 可以装入至少一个仍有剩余的物品(种类、方向不限);
(2) 合并后新平面的面积是否比原先两个平面都大。

设试合并后新生产的两块平面分别为ms1和 ms2,判断是否保留该次试合并及正式合并的步骤为:

  1. 判断 ns 是否满足准则(1)。若不满足则执行第 2)步,若满足执行第 3)步;
  2. 判断 ms1 和 ms2 是否满足准则(1)。将满足的一个平面插入平面列表,不满足的放入备用平面列表。若均不满足准则(1),则判断 ms1 和 ms2 中是否有能够足准则(2)的,若有,则将ms1和ms2均放入备用平面列表;否则不保留此次试合并,重新开始寻找相邻平面;
  3. 计算在放入物品后gs和ns将浪费的总面积(ws1)。根据ms1和ms2满足准则(1)的情况,计算合并后可能浪费的面积之和(ws2)。设平面 xs 的面积为Sqr(xs),在该平面上放入物品后将浪费的面积为WsSqr(xs),则根据ms1和ms2的满足准则(1)的情况,ws2有以下几种情况:

    若ws1>ws2,则执行平面合并程序,并更新平面和备用平面列表;若ws1<=ws2,则不保留此次试合并,继续寻找其他相邻平面;
  4. 若对平面列表和备用平面列表搜索完毕侯,最终仍没有找到可合并的平面,则将目标平面gs从平面列表移入备用平面列表。

⭐️ 产生新平面

在放入一“块”后,原目标平面被分成3个子平面,一个位于块的顶部(下图中深色部分),另外两个可有下面两种方式(下图中Rsa1和Rsa2)。我们选择产生的子平面面积较大的一个方式,即比较两种方式中面积较大的两个子平面,选择有最大面积子平面的生成方式。以下图为例,若(a)Rsa1>Rsa2,(b)Rsb2>Rsb1,则比较 Rsa1与Rsb2,若Rsa1>Rsb2,选择(a)方式。产生的子平面放入平面列表中。

⭐️ 程序及运行结果(笔者python运行环境为python3.7)

import copy
import sys
from itertools import product
import math
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import numpy as npMAX_GAP = 0# 绘图相关函数
def plot_linear_cube(ax, x, y, z, dx, dy, dz, color='red', linestyle=None):xx = [x, x, x+dx, x+dx, x]yy = [y, y+dy, y+dy, y, y]kwargs = {"alpha": 1, "color": color, "linewidth":2.5, "zorder":2}if linestyle:kwargs["linestyle"] = linestyleax.plot3D(xx, yy, [z]*5, **kwargs)ax.plot3D(xx, yy, [z+dz]*5, **kwargs)ax.plot3D([x, x], [y, y], [z, z+dz], **kwargs)ax.plot3D([x, x], [y+dy, y+dy], [z, z+dz], **kwargs)ax.plot3D([x+dx, x+dx], [y+dy, y+dy], [z, z+dz], **kwargs)ax.plot3D([x+dx, x+dx], [y, y], [z, z+dz], **kwargs)def cuboid_data(o, size=(1, 1, 1)):X = [[[0, 1, 0], [0, 0, 0], [1, 0, 0], [1, 1, 0]],[[0, 0, 0], [0, 0, 1], [1, 0, 1], [1, 0, 0]],[[1, 0, 1], [1, 0, 0], [1, 1, 0], [1, 1, 1]],[[0, 0, 1], [0, 0, 0], [0, 1, 0], [0, 1, 1]],[[0, 1, 0], [0, 1, 1], [1, 1, 1], [1, 1, 0]],[[0, 1, 1], [0, 0, 1], [1, 0, 1], [1, 1, 1]]]X = np.array(X).astype(float)for i in range(3):X[:, :, i] *= size[i]X += np.array(o)return Xdef plotCubeAt(positions, sizes=None, colors=None, **kwargs):if not isinstance(colors, (list, np.ndarray)):colors = ["C0"] * len(positions)if not isinstance(sizes, (list, np.ndarray)):sizes = [(1, 1, 1)] * len(positions)g = []for p, s, c in zip(positions, sizes, colors):g.append(cuboid_data(p, size=s))return Poly3DCollection(np.concatenate(g), facecolors=np.repeat(colors, 6), **kwargs)# 箱子类
class Box:def __init__(self, lx, ly, lz, weight=0.0, type=0):# 长self.lx = lx# 宽self.ly = ly# 高self.lz = lz# 重self.weight = weight# 类型self.type = typedef __str__(self):return "lx: {}, ly: {}, lz: {}, weight: {}, type: {}".format(self.lx, self.ly, self.lz, self.weight, self.type)# 块类
class Block:def __init__(self, lx, ly, lz, require_list=[], weight=0.0, box_rotate=False):# 长self.lx = lx# 宽self.ly = ly# 高self.lz = lz# 需要的物品数量self.require_list = require_list# 需要的物品重量self.weight = weight# 体积self.volume = 0# 是否旋转self.box_rotate = box_rotatedef __str__(self):return "lx: %s, ly: %s, lz: %s, volume: %s, require: %s, weight: %s, box_rotate: %a" % (self.lx, self.ly, self.lz, self.volume, self.require_list, self.weight, self.box_rotate)def __eq__(self, other):return self.lx == other.lx and self.ly == other.ly and self.lz == other.lz and self.box_rotate == other.box_rotate and (np.array(self.require_list) == np.array(other.require_list)).all()# 平面类
class Plane:def __init__(self, x, y, z, lx, ly, height_limit=0):# 坐标self.x = xself.y = yself.z = z# 长self.lx = lx# 宽self.ly = ly# 限高self.height_limit = height_limitself.origin = Nonedef __str__(self):return "x:{}, y:{}, z:{}, lx:{}, ly:{}, height_limit:{}".format(self.x, self.y, self.z, self.lx, self.ly, self.height_limit)def __eq__(self, other):return self.x == other.x and self.y == other.y and self.z == other.z and self.lx == other.lx and self.ly == other.ly# 判断是否与另一个平面相邻(z坐标相同,至少有一个边平齐),并返回合并后的两个平面def adjacent_with(self, other):if self.z != other.z:return False, None, None# 矩形中心my_center = (self.x + self.lx / 2, self.y + self.ly / 2)other_center = (other.x + other.lx / 2, other.y + other.ly / 2)# 矩形相邻时的中心距离x_adjacent_measure = self.lx / 2 + other.lx / 2y_adjacent_measure = self.ly / 2 + other.ly / 2# 宽边相邻,长边对齐if x_adjacent_measure + MAX_GAP >= math.fabs(my_center[0] - other_center[0]) >= x_adjacent_measure:if self.y == other.y and self.ly == other.ly:ms1 = Plane(min(self.x, other.x), self.y, self.z, self.lx + other.lx, self.ly)return True, ms1, Noneif self.y == other.y:ms1 = Plane(min(self.x, other.x), self.y, self.z, self.lx + other.lx, min(self.ly, other.ly))if self.ly > other.ly:ms2 = Plane(self.x, self.y + other.ly, self.z, self.lx, self.ly - other.ly)else:ms2 = Plane(other.x, self.y + self.ly, self.z, other.lx, other.ly - self.ly)return True, ms1, ms2if self.y + self.ly == other.y + other.ly:ms1 = Plane(min(self.x, other.x), max(self.y, other.y), self.z, self.lx + other.lx, min(self.ly, other.ly))if self.ly > other.ly:ms2 = Plane(self.x, self.y, self.z, self.lx, self.ly - other.ly)else:ms2 = Plane(other.x, other.y, self.z, other.lx, other.ly - self.ly)return True, ms1, ms2# 长边相邻,宽边对齐if y_adjacent_measure + MAX_GAP >= math.fabs(my_center[1] - other_center[1]) >= y_adjacent_measure:if self.x == other.x and self.lx == other.lx:ms1 = Plane(self.x, min(self.y, other.y), self.z, self.lx, self.ly + other.ly)return True, ms1, Noneif self.x == other.x:ms1 = Plane(self.x, min(self.y, other.y), self.z, min(self.lx, other.lx), self.ly + other.ly)if self.lx > other.lx:ms2 = Plane(self.x + other.lx, self.y, self.z, self.lx - other.lx, self.ly)else:ms2 = Plane(self.x + self.lx, other.y, self.z, other.lx - self.lx, other.ly)return True, ms1, ms2if self.x + self.lx == other.x + other.lx:ms1 = Plane(max(self.x, other.x), min(self.y, other.y), self.z, min(self.lx, other.lx), self.ly + other.ly)if self.lx > other.lx:ms2 = Plane(self.x, self.y, self.z, self.lx - other.lx, self.ly)else:ms2 = Plane(other.x, other.y, self.z, other.lx - self.lx, other.ly)return True, ms1, ms2return False, None, None# 问题类
class Problem:def __init__(self, container: Plane, height_limit=sys.maxsize, weight_limit=sys.maxsize, box_list=[], num_list=[], rotate=False):# 初始最低水平面self.container = container# 限高self.height_limit = height_limit# 限重self.weight_limit = weight_limit# 箱体列表self.box_list = box_list# 箱体数量self.num_list = num_list# 是否考虑板材旋转self.rotate = rotate# 放置类
class Place:def __init__(self, plane: Plane, block: Block):self.plane = planeself.block = blockdef __eq__(self, other):return self.plane == other.plane and self.block == other.block# 装箱状态类
class PackingState:def __init__(self, plane_list=[], avail_list=[], weight=0.0):# 装箱计划self.plan_list = []# 可用箱体数量self.avail_list = avail_list# 可用平面列表self.plane_list = plane_list# 备用平面列表self.spare_plane_list = []# 当前排样重量self.weight = weight# 当前排样体积self.volume = 0# 选择平面
def select_plane(ps: PackingState):# 选最低的平面min_z = min([p.z for p in ps.plane_list])temp_planes = [p for p in ps.plane_list if p.z == min_z]if len(temp_planes) == 1:return temp_planes[0]# 相同高度的平面有多个的话,选择面积最小的平面min_area = min([p.lx * p.ly for p in temp_planes])temp_planes = [p for p in temp_planes if p.lx * p.ly == min_area]if len(temp_planes) == 1:return temp_planes[0]# 较狭窄的min_narrow = min([p.lx/p.ly if p.lx <= p.ly else p.ly/p.lx for p in temp_planes])new_temp_planes = []for p in temp_planes:narrow = p.lx/p.ly if p.lx <= p.ly else p.ly/p.lxif narrow == min_narrow:new_temp_planes.append(p)if len(new_temp_planes) == 1:return new_temp_planes[0]# x坐标较小min_x = min([p.x for p in new_temp_planes])new_temp_planes = [p for p in new_temp_planes if p.x == min_x]if len(new_temp_planes) == 1:return new_temp_planes[0]# y坐标较小min_y = min([p.y for p in new_temp_planes])new_temp_planes = [p for p in new_temp_planes if p.y == min_y]return new_temp_planes[0]# 将某平面从可用平面列表转移到备用平面列表
def disable_plane(ps: PackingState, plane: Plane):ps.plane_list.remove(plane)ps.spare_plane_list.append(plane)# 生成简单块
def gen_simple_block(init_plane: Plane, box_list, num_list, max_height, can_rotate=False):block_table = []for box in box_list:for nx in np.arange(num_list[box.type]) + 1:for ny in np.arange(num_list[box.type] / nx) + 1:for nz in np.arange(num_list[box.type] / nx / ny) + 1:if box.lx * nx <= init_plane.lx and box.ly * ny <= init_plane.ly and box.lz * nz <= max_height - init_plane.z:# 该简单块需要的立体箱子数量requires = np.full_like(num_list, 0)requires[box.type] = int(nx) * int(ny) * int(nz)# 简单块block = Block(lx=box.lx * nx, ly=box.ly * ny, lz=box.lz * nz, require_list=requires)# 简单块填充体积block.volume = box.lx * nx * box.ly * ny * box.lz * nz# 简单块重量block.weight = int(nx) * int(ny) * int(nz) * box.weightblock_table.append(block)if can_rotate:# 物品朝向选择90度进行堆叠if box.ly * nx <= init_plane.lx and box.lx * ny <= init_plane.ly and box.lz * nz <= max_height - init_plane.z:requires = np.full_like(num_list, 0)requires[box.type] = int(nx) * int(ny) * int(nz)# 简单块block = Block(lx=box.ly * nx, ly=box.lx * ny, lz=box.lz * nz, require_list=requires, box_rotate=True)# 简单块填充体积block.volume = box.ly * nx * box.lx * ny * box.lz * nz# 简单块重量block.weight = int(nx) * int(ny) * int(nz) * box.weightblock_table.append(block)return block_table# 生成可行块列表
def gen_block_list(plane: Plane, avail, block_table, max_height, avail_weight=sys.maxsize):block_list = []for block in block_table:# 块中需要的箱子数量必须小于最初的待装箱的箱子数量# 块的尺寸必须小于放置空间尺寸# 块的重量必须小于可放置重量if (np.array(block.require_list) <= np.array(avail)).all() and block.lx <= plane.lx and block.ly <= plane.ly \and block.lz <= max_height - plane.z and block.weight <= avail_weight:block_list.append(block)return block_list# 查找下一个可行块
def find_block(plane: Plane, block_list, ps: PackingState):# 平面的面积plane_area = plane.lx * plane.ly# 放入块后,剩余的最小面积min_residual_area = min([plane_area - b.lx * b.ly for b in block_list])# 剩余面积相同,保留最大体积的块candidate = [b for b in block_list if plane_area - b.lx * b.ly == min_residual_area]# 可用平面最大高度max_plane_height = min([p.z for p in ps.plane_list])_candidate = sorted(candidate, key=lambda x: x.volume, reverse=True)# if max_plane_height == 0:#     # 第一次放置体积最大的块#     _candidate = sorted(candidate, key=lambda x: x.volume, reverse=True)# else:#     # 选择平面时尽量使放置物品后与已经放置的物品平齐#     _candidate = sorted(candidate, key=lambda x: x.lz + plane.z - max_plane_height)#     _candidate = sorted(candidate, key=lambda x: x.volume + ps.volume - 2440*1220*1000)return _candidate[0]# 裁切出新的剩余空间(有稳定性约束)
def gen_new_plane(plane: Plane, block: Block):# 块顶部的新平面rs_top = Plane(plane.x, plane.y, plane.z + block.lz, block.lx, block.ly)# 底部平面裁切if block.lx == plane.lx and block.ly == plane.ly:return rs_top, None, Noneif block.lx == plane.lx:return rs_top, Plane(plane.x, plane.y + block.ly, plane.z, plane.lx, plane.ly - block.ly), Noneif block.ly == plane.ly:return rs_top, Plane(plane.x + block.lx, plane.y, plane.z, plane.lx - block.lx, block.ly), None# 比较两种方式中面积较大的两个子平面,选择有最大面积子平面的生成方式rsa1 = Plane(plane.x, plane.y + block.ly, plane.z, plane.lx, plane.ly - block.ly)rsa2 = Plane(plane.x + block.lx, plane.y, plane.z, plane.lx - block.lx, block.ly)rsa_bigger = rsa1 if rsa1.lx * rsa1.ly >= rsa2.lx * rsa2.ly else rsa2rsb1 = Plane(plane.x, plane.y + block.ly, plane.z, block.lx, plane.ly - block.ly)rsb2 = Plane(plane.x + block.lx, plane.y, plane.z, plane.lx - block.lx, plane.ly)rsb_bigger = rsb1 if rsb1.lx * rsb1.ly >= rsb2.lx * rsb2.ly else rsb2if rsa_bigger.lx * rsa_bigger.ly >= rsb_bigger.lx * rsb_bigger.ly:return rs_top, rsa1, rsa2else:return rs_top, rsb1, rsb2# 计算平面浪费面积
def plane_waste(ps: PackingState, plane: Plane, block_table, max_height, max_weight=sys.maxsize):# 浪费面积waste = 0if plane:block_list = gen_block_list(plane, ps.avail_list, block_table, max_height, max_weight - ps.weight)if len(block_list) > 0:block = find_block(plane, block_list, ps)waste = plane.lx * plane.ly - block.lx * block.lyelse:waste = plane.lx * plane.lyreturn waste# 判断平面是否可以放置物品
def can_place(ps: PackingState, plane: Plane, block_table, max_height, max_weight=sys.maxsize):if plane is None:return Falseblock_list = gen_block_list(plane, ps.avail_list, block_table, max_height, max_weight - ps.weight)return True if len(block_list) > 0 else False# 用块填充平面
def fill_block(ps: PackingState, plane: Plane, block: Block):# 更新可用箱体数目ps.avail_list = (np.array(ps.avail_list) - np.array(block.require_list)).tolist()# 更新放置计划place = Place(plane, block)ps.plan_list.append(place)# 更新体积利用率ps.volume = ps.volume + block.volume# 产生三个新的平面rs_top, rs1, rs2 = gen_new_plane(plane, block)# 移除裁切前的平面ps.plane_list.remove(plane)# 装入新的可用平面if rs_top:ps.plane_list.append(rs_top)if rs1:ps.plane_list.append(rs1)if rs2:ps.plane_list.append(rs2)# 合并平面
def merge_plane(ps: PackingState, plane: Plane, block_table, max_height, max_weight=sys.maxsize):# print("合并平面开始了")for ns in ps.plane_list + ps.spare_plane_list:# 不和自己合并if plane == ns:continue# 找相邻平面is_adjacent, ms1, ms2 = plane.adjacent_with(ns)if is_adjacent:# print("有相邻平面呦")block_list = gen_block_list(ns, ps.avail_list, block_table, max_height, max_weight - ps.weight)# 相邻平面本身能放入至少一个剩余物品if len(block_list) > 0:block = find_block(ns, block_list, ps)# 计算相邻平面和原平面浪费面积的总和ws1 = ns.lx * ns.ly - block.lx * block.ly + plane.lx * plane.ly# 计算合并后平面的总浪费面积ws2 = plane_waste(ps, ms1, block_table, max_height, max_weight) + plane_waste(ps, ms2, block_table, max_height, max_weight)# 合并后,浪费更小,则保留合并if ws1 > ws2:# 保留平面合并ps.plane_list.remove(plane)if ns in ps.plane_list:ps.plane_list.remove(ns)else:ps.spare_plane_list.remove(ns)if ms1:ps.plane_list.append(ms1)if ms2:ps.plane_list.append(ms2)returnelse:# 放弃平面合并,寻找其他相邻平面continue# 相邻平面本身无法放入剩余物品else:# 合共后产生一个平面if ms2 is None:# 能放物品,则保留平面合并if can_place(ps, ms1, block_table, max_height, max_weight):ps.plane_list.remove(plane)if ns in ps.plane_list:ps.plane_list.remove(ns)else:ps.spare_plane_list.remove(ns)ps.plane_list.append(ms1)returnelif ms1.lx * ms1.ly > plane.lx * plane.ly and ms1.lx * ms1.ly > ns.lx * ns.ly:ps.plane_list.remove(plane)if ns in ps.plane_list:ps.plane_list.remove(ns)else:ps.spare_plane_list.remove(ns)# ps.spare_plane_list.append(ms1)ps.plane_list.append(ms1)returnelse:continue# 合并后产生两个平面else:if (not can_place(ps, ms1, block_table, max_height, max_weight)) and (not can_place(ps, ms2, block_table, max_height, max_weight)):if (ms1.lx * ms1.ly > plane.lx * plane.ly and ms1.lx * ms1.ly > ns.lx * ns.ly) or (ms2.lx * ms2.ly > plane.lx * plane.ly and ms2.lx * ms2.ly > ns.lx * ns.ly):ps.plane_list.remove(plane)if ns in ps.plane_list:ps.plane_list.remove(ns)else:ps.spare_plane_list.remove(ns)ps.spare_plane_list.append(ms1)ps.spare_plane_list.append(ms2)returnelse:continueelse:ps.plane_list.remove(plane)if ns in ps.plane_list:ps.plane_list.remove(ns)else:ps.spare_plane_list.remove(ns)if can_place(ps, ms1, block_table, max_height, max_weight):ps.plane_list.append(ms1)else:ps.spare_plane_list.append(ms1)if can_place(ps, ms2, block_table, max_height, max_weight):ps.plane_list.append(ms2)else:ps.spare_plane_list.append(ms2)return# 若对平面列表和备用平面列表搜索完毕后,最终仍没有找到可合并的平面,则将目标平面从平面列表移入备用平面列表disable_plane(ps, plane)# 构建箱体坐标,用于绘图
def build_box_position(block, init_pos, box_list):# 箱体类型索引box_idx = (np.array(block.require_list) > 0).tolist().index(True)if box_idx > -1:# 所需箱体box = box_list[box_idx]# 箱体的相对坐标if block.box_rotate:nx = block.lx / box.lyny = block.ly / box.lxx_list = (np.arange(0, nx) * box.ly).tolist()y_list = (np.arange(0, ny) * box.lx).tolist()else:nx = block.lx / box.lxny = block.ly / box.lyx_list = (np.arange(0, nx) * box.lx).tolist()y_list = (np.arange(0, ny) * box.ly).tolist()nz = block.lz / box.lzz_list = (np.arange(0, nz) * box.lz).tolist()# 箱体的绝对坐标dimensions = (np.array([x for x in product(x_list, y_list, z_list)]) + np.array([init_pos[0], init_pos[1], init_pos[2]])).tolist()# 箱体的坐标及尺寸if block.box_rotate:return sorted([d + [box.ly, box.lx, box.lz] for d in dimensions], key=lambda x: (x[0], x[1], x[2])), box_idxelse:return sorted([d + [box.lx, box.ly, box.lz] for d in dimensions], key=lambda x: (x[0], x[1], x[2])), box_idxreturn None, None# 绘制排样结果
def draw_packing_result(problem: Problem, ps: PackingState):# 绘制结果fig = plt.figure()ax1 = fig.gca(projection='3d')# 绘制容器plot_linear_cube(ax1, 0, 0, 0, problem.container.lx, problem.container.ly, problem.height_limit)for p in ps.plan_list:# 绘制箱子box_pos, _ = build_box_position(p.block, (p.plane.x, p.plane.y, p.plane.z), problem.box_list)positions = []sizes = []colors = ["yellow"] * len(box_pos)for bp in sorted(box_pos, key=lambda x: (x[0], x[1], x[2])):positions.append((bp[0], bp[1], bp[2]))sizes.append((bp[3], bp[4], bp[5]))pc = plotCubeAt(positions, sizes, colors=colors, edgecolor="k")ax1.add_collection3d(pc)plt.title('Cube{}'.format(0))plt.show()# plt.savefig('3d_lowest_plane_packing.png', dpi=800)# 基本启发式算法
def basic_heuristic(problem: Problem):# 生成简单块block_table = gen_simple_block(problem.container, problem.box_list, problem.num_list, problem.height_limit, problem.rotate)# 初始化排样状态ps = PackingState(avail_list=problem.num_list)# 开始时,剩余空间堆栈中只有容器本身ps.plane_list.append(Plane(problem.container.x, problem.container.y, problem.container.z, problem.container.lx,problem.container.ly))max_used_high = 0# 循环直到所有平面使用完毕while ps.plane_list:# 选择平面plane = select_plane(ps)# 查找可用块block_list = gen_block_list(plane, ps.avail_list, block_table, problem.height_limit, problem.weight_limit - ps.weight)if block_list:# 查找下一个近似最优块block = find_block(plane, block_list, ps)# 填充平面fill_block(ps, plane, block)# 更新排样重量ps.weight += block.weight# 更新最大使用高度if plane.z + block.lz > max_used_high:max_used_high = plane.z + block.lzelse:# 合并相邻平面merge_plane(ps, plane, block_table, problem.height_limit, problem.weight_limit)# 板材的位置信息box_pos_info = [[] for _ in problem.num_list]for p in ps.plan_list:box_pos, box_idx = build_box_position(p.block, (p.plane.x, p.plane.y, p.plane.z), problem.box_list)for bp in box_pos:box_pos_info[box_idx].append((bp[0], bp[1], bp[2]))# 计算容器利用率used_volume = problem.container.lx * problem.container.ly * max_used_highused_ratio = round(float(ps.volume) * 100 / float(used_volume), 3) if used_volume > 0 else 0# # 绘制排样结果图draw_packing_result(problem, ps)return ps.avail_list, used_ratio, max_used_high, box_pos_info, ps# 主算法
def simple_test():# 容器底面container = Plane(0, 0, 0, 2440, 1220)# 箱体列表box_list = [Box(lx=2390, ly=70, lz=10, weight=3001, type=0),Box(lx=2390, ly=50, lz=10, weight=10, type=1),Box(lx=625, ly=210, lz=10, weight=5, type=2),Box(lx=625, ly=110, lz=10, weight=5, type=3),Box(lx=2160, ly=860, lz=10, weight=5, type=4),Box(lx=860, ly=140, lz=10, weight=5, type=5),Box(lx=860, ly=120, lz=10, weight=5, type=6)]num_list = [200, 3, 200, 180, 20, 5, 10]# 问题problem = Problem(container=container, height_limit=300, weight_limit=4000, box_list=box_list, num_list=copy.copy(num_list))# 具体计算new_avail_list, used_ratio, used_high, box_pos_, _ = basic_heuristic(problem)# 箱体原始数量print(num_list)# 剩余箱体print(new_avail_list)# 利用率print(used_ratio)if __name__ == "__main__":simple_test()

算法运行结果如下:

⭐️ 写在最后

本文理论部分参考上海交通大学硕士论文《三维装箱问题的混合遗传算法研究》,论文作者和导师都很牛,读者可以自行百度获取论文原文

基于最低水平面的三维装箱问题的启发式算法相关推荐

  1. 基于禁忌搜索算法的三维装箱问题

    装箱问题 装箱问题是复杂的离散组合最优化问题.所谓组合优化,是指在离散的.有限的数学结构上,寻找一个满足给定条件,并使其目标函数值达到最大或最小的解.经典的装箱问题要求把一定数量的物品放入容量相同的一 ...

  2. 【三维装箱】基于粒子群算法求解三维装箱问题matlab源码

    1 简介 针对约束条件下三维装箱问题复杂性,为提高装箱利用率,本文提出 了混合粒子群算法,该算法采用BF启发式算法配合改进的自适应权重粒子群算法实现.通过仿真试验,结果表明该混合粒子群算法对解决部分约 ...

  3. 【三维装箱】基于遗传和模拟退火的三维装箱问题matlab源码

    1 简介 集装箱装载是货物运输过程中重要的一步,其属于NP-hard问题.为了提高效率,降低成本,提出了以集装 箱体积利用率最大化为目标建立三维装载模型,同时考虑体积约束.重量约束.重心约束.方向约束 ...

  4. 基于Q-learning的无人机三维路径规划(含完整C++代码)

    目录 1.实验目标 2.相关原理 3.实验过程 3.1基于Q-learning的三维模型创建 3.2无人机类.环境类和障碍物类的建立 3.3继承和多态的实现 3.4训练 3.5测试 4.完整代码 ma ...

  5. python:关于三维装箱问题的算法研究-3

    基于 三维装箱问题的算法研究-2 的基础,对整个过程进行了优化 因为后面研究的视图依赖于Three.js进行成像,需要写一些简单的vue页面,所以把整个算法包括数据格式的调用做成了django后端系统 ...

  6. 【GA三维路径规划】基于matlab遗传算法无人机三维路径规划【含Matlab源码 1526期】

    一.无人机简介 0 引言 随着现代技术的发展,飞行器种类不断变多,应用也日趋专一化.完善化,如专门用作植保的大疆PS-X625无人机,用作街景拍摄与监控巡察的宝鸡行翼航空科技的X8无人机,以及用作水下 ...

  7. 基于深度学习的三维语义理解(分割)综述列表

    基于深度学习的三维语义理解(分割)综述列表 文章目录 基于深度学习的三维语义理解(分割)综述列表 前言 基于深度学习的三维语义理解(分割)综述列表 一. 从单一三维模型中进行深度学习 1.1基于点云的 ...

  8. 图形处理(十三)基于可变形模板的三维人脸重建-学习笔记

    基于可变形模板的三维人脸重建-学习笔记 原文地址:http://blog.csdn.net/hjimce/article/details/50331423 作者:hjimce 一.数据库处理: 我们通 ...

  9. orb特征 稠密特征_一种基于ORB-SLAM2的双目三维稠密建图方法技术

    本发明专利技术公开了一种基于ORB‑SLAM2的双目稠密建图方法,涉及机器人同步定位与地图创建领域,该方法主要由跟踪线程.局部地图线程.闭环检测线程和稠密建图线程组成.其中稠密建图线程包含以下步骤:1 ...

最新文章

  1. 图解系列之垃圾收集标记整理算法
  2. 【C基础】指针/指针运算/二级指针/函数指针
  3. 教你如何开发一款实用的完整Android App
  4. hadoop常见问题汇总
  5. 网络工程师Day3--PPPoE配置实验
  6. 【转】宝贝,我要出嫁了……
  7. java程序(1016)
  8. mono:利用wxWindows开发界面程序
  9. 阿里云Maven配置方案
  10. 每日工作记录——任意小数分频研究
  11. Mac 配置maven
  12. 截止到某天的汇总报表_excel表格日数据汇总-excel表中如何将每日的数据汇总到每周...
  13. Power BI----各类切片器的使用
  14. 什么是垂直搜索? 推荐几个网站
  15. 教你如何使用SwipeRefreshLayout来构建一个上拉加载下拉刷新框架
  16. CIC详细设计说明文档
  17. 照度/感光度(Lux)
  18. Pico4VR一体机游戏资源下载安装教程,Pico4无线串流PCVR游戏教程pico4串流游戏下载
  19. 百度云此链接内容可能涉及侵权,分享的文件已经被取消等屏蔽问题的解决方法
  20. Qt 网络发送带中文字符串

热门文章

  1. 在线SQL转文本工具
  2. matlab删除三维数组中全零行或列
  3. Ubuntu 音效均衡器,网易云音乐均衡器插件
  4. c语言实型数据的运算-分期付款
  5. 刷题之完全二叉树的权值和小字辈及根据后序和中序遍历输出先序遍历
  6. RateLimiter google限流组件试析(SmoothBursty/SmoothWarmingUp)
  7. sqlserver中的表值函数和标量值函数
  8. 物联网宠物饮水机解决方案
  9. 实验:Wireshark 抓包软件的使用及MAC协议分析
  10. 使用Squirrel连接Phoenix