Beispiel #1
0
def first_fit(list_items, max_size):
    """ Returns list of bins with input items inside. """
    list_bins = []
    list_bins.append(Bin())  # Add first empty bin to list

    for item in list_items:
        # Go through bins and try to allocate
        alloc_flag = False

        for bin in list_bins:
            if bin.sum() + item <= max_size:
                bin.addItem(item)
                alloc_flag = True
                break

        # If item not allocated in bins in list, create new bin
        # and allocate it to it.
        if alloc_flag == False:
            newBin = Bin()
            newBin.addItem(item)
            list_bins.append(newBin)

    # Turn bins into list of items and return
    list_items = []
    for bin in list_bins:
        list_items.append(bin.show())

    return (list_items)
    def findBin(self, item, capacity):
        current = self.bst.root
        best = None
        
        while current is not None:
            _bin = current.key
             
            if item.fitsInto(_bin):
                best = _bin
                if _bin.residual() == item.size:
                    current = None
                else:
                    current = current.right
            else:
                current = current.left

        _bin = None
        if best is None:
            _bin = Bin(capacity)
            _bin.addItem(item)
        else:
            self.bst.remove(best)
            _bin = best
            _bin.addItem(item)

        self.bst.insert(_bin)

        return _bin
Beispiel #3
0
def first_fit(list_items, max_size):
	""" Returns list of bins with input items inside. """
	list_bins = []
	list_bins.append(Bin()) # Add first empty bin to list

	for item in list_items:
		# Go through bins and try to allocate
		alloc_flag = False

		for bin in list_bins:
			if bin.sum() + item <= max_size:
				bin.addItem(item)
				alloc_flag = True
				break
		
		# If item not allocated in bins in list, create new bin
		# and allocate it to it.
		if alloc_flag == False:
			newBin = Bin()
			newBin.addItem(item)
			list_bins.append(newBin)

	# Turn bins into list of items and return
	list_items = []
	for bin in list_bins:
		list_items.append(bin.show())

	return(list_items)
    def findBin(self, item, capacity):
        current = self.bst.root

        _bin = self.findWorst(item)
        if _bin is None:
            _bin = Bin(capacity)
        else:
            self.bst.remove(_bin)            

        _bin.addItem(item)
        self.bst.insert(_bin)

        return _bin
    def findBin(self, item, capacity):
        if len(self.bins) == 0:
            self.bins.append(Bin(capacity))

        bin_ = self.bins.pop()
        self.bins.append(bin_)

        if not item.fitsInto(bin_):
            bin_ = Bin(capacity)
            self.bins.append(bin_)

        bin_.addItem(item)

        return bin_
    def findBin(self, item, capacity):
        bin_ = self.bst.select(1)

        if bin_ is not None and item.fitsInto(bin_):
            self.bst.remove(bin_)
        else:
            bin_ = self.bst.select(0)

            if bin_ is not None and item.fitsInto(bin_):
                self.bst.remove(bin_)
            else:
                bin_ = Bin(capacity)

        bin_.addItem(item)
        self.bst.insert(bin_)

        return bin_
    def findBin(self, item, capacity):
        current = self.bst.root
        bin_ = None

        while current is not None:
            if item.fitsInto(current.key):
                bin_ = current.key

            if current.left is not None and current.left.minLoading + item.size <= capacity:
                current = current.left
            elif bin_ is None and current.right is not None and current.right.minLoading + item.size <= capacity:
                current = current.right
            else:
                current = None

        if not bin_:
            bin_ = Bin(capacity)
        else:
            self.bst.remove(bin_)

        bin_.addItem(item)
        self.bst.insert(bin_)

        return bin_