一家大型零售商店的货架容量不同,可以存放不同类别的商品。假设对哪个商品去哪个架子没有限制,那么任何商品都可以放在任何架子中。面临的挑战是如何在不超过机架容量的每个机架中优化分配不同重量的商品。 因此,如果一个架子的容量为500kg,则需要放置不同重量的商品,因此这n种商品的总重量之和为<= 500kg。
这可以看作是查找预定义值的总和组合的问题。我遵循了this的问题,并找到了一个由thefourtheye编写的解决方案。
下面是我使用的代码段;
import pandas as pd
def a(lst, target, with_replacement=False):
def _a(idx, l, r, t, w):
if t == sum(l): r.append(l)
elif t < sum(l): return
for u in range(idx, len(lst)):
_a(u if w else (u + 1), l + [lst[u]], r, t, w)
return r
return _a(0, [], [], target, with_replacement)
commodity_weights = pd.np.array(retail_data.commodity_weights)
b = a(commodity_weights,1000000)
因此,1000000是一个目标机架的目标容量,需要放置不同重量的商品才能达到目标容量。运行上面提到的代码,b
提供的输出是;
[[150319, 176282, 125877, 112750, 118113, 151850, 164809],
[150319, 226013, 154886, 112438, 205017, 151327],
[150319, 226013, 106055, 255872, 261741],
[150319, 125877, 164094, 301341, 145931, 112438],
[150319, 285096, 169215, 230561, 164809],
[150319, 118113, 327682, 122286, 130273, 151327],
[150319, 149649, 230561, 321987, 147484],
[478629, 207277, 145931, 168163],
[266632, 112750, 169215, 164809, 130273, 156321],
[266632, 106055, 118081, 173942, 130273, 205017],
[486415, 125877, 106055, 169215, 112438],
[279641, 280289, 232793, 207277],
[279641, 208577, 112750, 112438, 130273, 156321],
[343704, 118113, 151850, 218170, 168163],
[280289, 112750, 118081, 320717, 168163],
[249551, 176282, 287475, 112750, 173942],
[249551, 232793, 122286, 230561, 164809],
[249551, 112750, 118113, 112438, 238985, 168163],
[249551, 118113, 106055, 118081, 169215, 238985],
[249551, 340878, 149649, 112438, 147484],
[249551, 151850, 301341, 145931, 151327],
[176282, 287475, 145931, 238985, 151327],
[176282, 236223, 260192, 122286, 205017],
[176282, 112750, 356097, 106055, 118081, 130735],
[176282, 151850, 118081, 431501, 122286],
[176282, 382377, 164094, 112438, 164809],
[303818, 125877, 215434, 106055, 118081, 130735],
[232793, 118081, 169215, 218170, 261741],
[232793, 173942, 130735, 164094, 130273, 168163],
[208577, 118113, 118081, 320717, 234512],
[208577, 154886, 239828, 396709],
[208577, 106055, 122286, 301341, 261741],
[208577, 178415, 239828, 205017, 168163],
[413029, 321406, 118081, 147484],
[287475, 125877, 356097, 118113, 112438],
[432142, 125877, 154886, 122286, 164809],
[125877, 215434, 260192, 118113, 149649, 130735],
[125877, 118113, 154886, 130735, 239828, 230561],
[125877, 172848, 151850, 149649, 169215, 230561],
[125877, 151850, 149649, 118081, 173942, 112438, 168163],
[125877, 151850, 149649, 118081, 122286, 164094, 168163],
[125877, 151850, 207277, 122286, 145931, 246779],
[125877, 106055, 169215, 200747, 246779, 151327],
[285096, 154886, 313239, 246779],
[285096, 149649, 118081, 178415, 112438, 156321],
[236223, 112750, 260192, 151850, 238985],
[236223, 215434, 118113, 172848, 106055, 151327],
[112750, 378095, 106055, 246779, 156321],
[112750, 151850, 118081, 317373, 299946],
[112750, 149649, 122286, 200747, 267084, 147484],
[112750, 106055, 122286, 382377, 164094, 112438],
[112750, 118081, 130735, 239828, 164094, 234512],
[112750, 118081, 317373, 246779, 205017],
[356097, 321406, 172848, 149649],
[215434, 118113, 207277, 164094, 164809, 130273],
[215434, 154886, 172848, 118081, 173942, 164809],
[118113, 151850, 118081, 130735, 145931, 130273, 205017],
[118113, 149649, 207277, 122286, 234512, 168163],
[118113, 106055, 207277, 118081, 173942, 164094, 112438],
[340878, 313239, 106055, 239828],
[151850, 169215, 164094, 145931, 200747, 168163],
[149649, 164094, 301341, 145931, 238985],
[106055, 118081, 130735, 230561, 267084, 147484],
[320717, 164094, 145931, 238985, 130273],
[308185, 112438, 164809, 267084, 147484],
[327152, 200747, 267084, 205017]]
因此,在这里,我们仅获得不同的商品权重,每个阵列的总和满足目标容量。
retail_data
有多个列,其中两个是commodity_weights
和commodity_id
。在上面显示的结果中,我们只能看到权重,但是我也需要commodity_id
。因此,我的结果应同时产生commodity_id
和关联的commodity_weights
。我该怎么办?
提前谢谢!