I've modified the bin packing algorithm from here: First Fit Decreasing Algorithm. It now works for my particular application except that I noticed there's nothing to prevent items from being assigned to more than one bin.
In my program, I wish to return a list of lists where the sublists represent individual bins, and the sublist elements are the keys of the items placed in them.
I have tried using items.remove(item)
before and after both lines with .append()
but that has not prevented the item from being assigned to more than one bin.
def binpack(articles,bin_cap):
items = list(articles.items())
items.sort(key=lambda i: i[1], reverse=True)
bins = [[items[0]]]
for item in items:
for bin in bins:
# check to see if item will fit in bin
if sum(i[1] for i in bin) + item[1] <= bin_cap:
bin.append(item)
break
else:
# item didn't fit into any bin, start a new bin
bin = []
bin.append(item)
bins.append(bin)
bin_contents = [[item[0] for item in bin] for bin in bins]
print(bin_contents)
When tested:
articles = {0:20, 1:13, 2:22}
binpack(articles, bin_cap = 36)
Returns:[[2, 1], [2], [0]]
The item with key 2 is placed in the first and second bin when it should only be placed in one
Desired:[[2,1], [0]]