我试图把一个大的长方体(L,W,H)切成N (N可以是任意数,不需要指定)小长方体,其中长方体的长度和宽度是大长方体的20%到50%,并且小长方体的高度小于H的30%。小立方体的大小不应该都一样,理想情况下,每个长方体都应该是不同的。算法的输入应该是大长方体的L、W、H,算法的返回应该是一组小方框尺寸,它们可以按顺序叠加回大长方体中。
一个相关的问题,我可以想到的是三维股票切割问题,虽然在这个问题上,你指定每个小长方体,但在这里,小盒子的尺寸可以是随机的,只要他们在一个范围内。任何帮助都将不胜感激!
发布于 2022-05-05 09:58:07
rng = np.random.default_rng(0)
def split_dimension(length, low, high):
'''
Attempt to partition a segment of the given length in segments
of length between low and high
'''
acc = 0
while length - acc > high:
assert length - acc >= low, \
'Failed to get a solution, maybe it was bad luck, maybe there is no solution'
# of course this choice could be made in a more clever way
v = rng.uniform(low, high)
yield acc, v
acc += v
yield acc, length - acc
def split_hypercuboid(dimension, dim_ranges):
'''
Attempt to split a given cuboid in sub cuboids with dimensions
'''
low, high = dim_ranges[-1]
length = dimension[-1]
if len(dimension) == 1:
for c,v in split_dimension(length, low, high):
yield (c,), (v,)
else:
for c,v in split_dimension(length, low, high):
for corner, size in split_hypercuboid(dimension[:-1], dim_ranges[:-1]):
yield corner + (c,), size + (v,)
def solve(L, W, H):
''' Apply it to the original question '''
return list(split_hypercuboid((L, W, H), ((0.2*L, 0.5*L), (0.2*W, 0.5*W), (0.0, 0.3*H))))绘制第一层
plt.figure(figsize=(5,5))
for (z, x, y), (l, w, h) in solve(100, 100, 100):
if z == 0:
plt.plot([x, x, x+w, x+w, x], [y, y+h, y+h, y, y], '-k')

https://stackoverflow.com/questions/72020655
复制相似问题