繁体   English   中英

首次拟合装箱算法

[英]First Fit Bin Packing Algorithm

我正在尝试创建一个首次拟合算法。 我采用的方法是创建一个空列表列表,这些列表代表垃圾箱,然后它们将被某些区域值填充,这些值加起来就是垃圾箱区域。 我希望这种情况一直持续到大部分区域都可以填满为止。

这是我的问题出现的地方:

lists.append([])
for i in lists:
    for box in boxes:
        l = box[0]
        w = box[1]
        area = l * w
        if area <= bin_area:
            bin_area = bin_area - area
            lists[0].append(area)
        else:
            bin_area = 15
            if area <= bin_area:
                bin_area = bin_area - area
                lists[1].append(area)
                # Here I want to then create a new empty list 
                # where I can add more values that add up to the bin value. 

因此,在上述代码的末尾,我想创建一个新的空列表,我可以在其中添加更多与 bin 值相加的值。

我通过猜测尝试lists[ i ].append([area]) ,但索引必须是整数。

我该如何实现?

另外,这是我的完整代码:

def FirstFitAlg():
    box1 = (3,2)
    box2 = (1,4)
    box3 = (2,1)
    box4 = (4,3)
    box5 = (1,2)
    boxes = [box1,box2,box3,box4,box5]
    num_of_boxes = len(boxes)
    bin_area = 15
    n_bin = 0
    lists = []
    lists.append([])
    lists.append([])
    #for i in lists:
    for box in boxes:
        l = box[0]
        w = box[1]
        area = l * w
        if area <= bin_area:
            bin_area = bin_area - area
            lists[0].append(area)
        else:
            bin_area = 15
            if area <= bin_area:
                bin_area = bin_area - area
                lists[1].append(area)
                # Here I want to then create a new empty list 
                # where I can add more values that add up to the bin value. 

    print(lists)
    for i in lists:
        if len(i) >= 1:
            n_bin += 1

    print(n_bin)
    efficiency = (n_bin/num_of_boxes) * 100
    print(efficiency)

保持打印出您的函数,并将框信息作为参数传递给它。 这样它就更通用了。

这是它的工作原理:

def firstFitAlg(boxes, bin_area):
    bins = []
    current_bin_area = 0
    total_occupied = 0
    for box in boxes:
        l, w = box
        area = l * w
        total_occupied += area
        if area > current_bin_area:  # Overflow. Need new bin
            current_bin = []  # Create new bin
            current_bin_area = bin_area  # All space is available in it
            bins.append(current_bin)  # This bin is part of the solution
        current_bin.append(box)  # Add box in this bin
        current_bin_area -= area  # and reduce the available space in it

    return bins, total_occupied


boxes = [(3,2),(1,4),(2,1),(4,3),(1,2)]
bin_area = 15
bins, total_occupied = firstFitAlg(boxes, bin_area)

print(bins)
print(f"Bumber of bins: {len(bins)}")
efficiency = (total_occupied/(bin_area * len(bins))) * 100
print(f"Efficiency: {efficiency}")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM