Exemplo n.º 1
0
print "\n\n"

# Get the capacity for the bins from the user
cap = binModule.getCap()
# Get the items from the user
items = binModule.getItems()

maxBins = len(items)
minBins = int(math.ceil(sum(items) / cap))
bins = []

print "Your items are:", items, "\nYour bins have capacity", cap, "\n"
print "MIN number of bins feasible:", int(
    minBins), "\nMAX number of bins (i.e. # items)", maxBins, "\n\n"

bins.append(Bin(cap, []))  # we need at least one bin to begin

t1 = clock()
for item in items:
    # Add the item to the first bin that can hold it
    # If no bin can hold it, make a new bin
    if item > cap:
        print "SOME ITEM WON'T EVEN FIT IN ITS OWN BIN! ABORTING"
        sys.exit()
    for xBin in bins:
        if xBin.capacity - sum(xBin.contents) >= item:
            xBin.add(item)
            break
        if bins.index(xBin) == len(bins) - 1:
            bins.append(Bin(cap, []))
Exemplo n.º 2
0
# Make sure no item is too large
checkInput(allItems)

# Check if we're in the case where all items need their own bins
if easyCase(allItems):
    print "Easy case"
    curMin = len(allItems)
    for item in allItems:
        config.append([item])

else:
    # Iterate through the permutations of the items
    for x in itertools.permutations(items):
        bins = []  # Clear the list of bins out after each new permuatation
        Binny = Bin(cap, [bigItem])  # A bin to begin your packing
        bins.append(Binny)
        # Iterate through each item in this permutation
        for item in x:
            # Don't bother finding out how to fit items if it's not better
            if len(bins) >= curMin:
                break
            # Still room in this bin?
            if Binny.free_capacity() >= item:
                Binny.add(item)
            # No...we need a fresh bin
            else:
                Binny = Bin(cap, [])
                Binny.add(item)
                bins.append(Binny)
        # We've put all the items in the perm into bins...