Example #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, []))
Example #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...
Example #3
0
def solve_problem(cap, allItems):
	def easyCase(aList):
		for item in aList:
			if item < cap/2:
				return False
		return True

	def checkInput(aList):
		for item in aList:
			if item > cap:
				print("SOME ITEM WON'T EVEN FIT IN ITS OWN BIN! ABORTING")
				sys.exit()
	print("\n\n")

	# Get the capacity for the bins from the user
	items = sorted(allItems)
	bigItem = items.pop() # We can reduce time by a factor of 2 by always putting the same item in bin0
	maxBins = len(allItems)
	minBins = int(math.ceil(sum(allItems)/cap))
	curMin = maxBins + 1
	config = []

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

	# Begin timing
	t1 = clock()

	# 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.capacity - sum(Binny.contents) >= 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...
			# If we've reached a new minimum of bins used, save the
			# minimum and keep a "proof" copy of the configuration that worked
			if len(bins) < curMin:
				curMin = len(bins)
				config = copy.deepcopy(bins)
			# If we used the true minimum number of bins, we're definitely done
			if len(bins) == minBins:
				break

	# End timing
	t2 = clock()
	print(t2-t1)

	# Put the item we removed back in so it looks pretty
	items.append(bigItem)
	print("True Bin Packing for", items, "with capacity", cap, "used", curMin, "bins")
	print("A configuration that worked was:", config)
	return config
Example #4
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...
Example #5
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.capacity - sum(Binny.contents) >= 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...