One big retail store has racks of different capacity to keep goods of different category. Assume that there is no limitation of which commodity goes to which rack, any commodity can be put in any rack. The challenge is to optimally allocate commodities of different weights in each racks where the rack capacity will not exceed. So, if one rack capacity is 500kg, we need to put commodities of different weights, so that the total sum of weights of these n commodities is <=500kg.
This can be treated as a problem of finding sum combinations for a predefined value. I have followed this question & got one solution, written by thefourtheye.
Below is the code snippet I used;
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)
So, 1000000 is the target capacity of one rack that needs to be met by putting commodities of different weights. Running the above mentioned code, b
provides the output is;
[[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]]
So, here we are only getting the different commodity weights, sum of each array meets the target capacity.
- The code is supposed to run faster, but to get the output I had to wait for nearly 2 minutes. Is the target_number(1000000) is causing the delay? Or there is other efficient way?
- Here I am only getting the commodity weights. The
retail_data
has multiple columns, two of them arecommodity_weights
&commodity_id
. In the result displayed above, we can only see the weights, but I need thecommodity_id
as well. So my result should produce bothcommodity_id
& associatedcommodity_weights
. How can I get that?- From the list of id & weights, is there any way to achieve optimality in terms of allocation?
Thanks in advance!!