微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

首次拟合装箱算法

如何解决首次拟合装箱算法

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

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

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),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}")

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。