背包分支和绑定错误的结果

时间:2017-05-14 15:42:10

标签: python algorithm knapsack-problem

我已将this link中给出的代码转换为python版本。该代码应该计算要在权重W的背包中填充的最大值的正确值。我附上了以下代码:

#http://www.geeksforgeeks.org/branch-and-bound-set-2-implementation-of-01-knapsack/

from queue import Queue
class Node:
    def __init__(self):
        self.level = None
        self.profit = None
        self.bound = None
        self.weight = None

    def __str__(self):
        return "Level: %s Profit: %s Bound: %s Weight: %s" % (self.level, self.profit, self.bound, self.weight)


def bound(node, n, W, items):
    if(node.weight >= W):
        return 0

    profit_bound = int(node.profit)
    j = node.level + 1
    totweight = int(node.weight)

    while ((j < n) and (totweight + items[j].weight) <= W):
        totweight += items[j].weight
        profit_bound += items[j].value
        j += 1

    if(j < n):
        profit_bound += (W - totweight) * items[j].value / float(items[j].weight)

    return profit_bound

Q = Queue()

def KnapSackBranchNBound(weight, items, total_items):
    items = sorted(items, key=lambda x: x.value/float(x.weight), reverse=True)

    u = Node()
    v = Node()

    u.level = -1
    u.profit = 0
    u.weight = 0

    Q.put(u)
    maxProfit = 0;

    while not Q.empty():
        u = Q.get()
        if u.level == -1:
            v.level = 0

        if u.level == total_items - 1:
            continue

        v.level = u.level + 1
        v.weight = u.weight + items[v.level].weight
        v.profit = u.profit + items[v.level].value
        if (v.weight <= weight and v.profit > maxProfit):
            maxProfit = v.profit;

        v.bound = bound(v, total_items, weight, items)
        if (v.bound > maxProfit):
            Q.put(v)

        v.weight = u.weight
        v.profit = u.profit
        v.bound = bound(v, total_items, weight, items)
        if (v.bound > maxProfit):
            # print items[v.level]
            Q.put(v)

    return maxProfit

if __name__ == "__main__":
    from collections import namedtuple
    Item = namedtuple("Item", ['index', 'value', 'weight'])
    input_data = open("test.data").read()
    lines = input_data.split('\n')

    firstLine = lines[0].split()
    item_count = int(firstLine[0])
    capacity = int(firstLine[1])

    print "running from main"
    items = []

    for i in range(1, item_count+1):
        line = lines[i]
        parts = line.split()
        items.append(Item(i-1, int(parts[0]), float(parts[1])))
    kbb = KnapSackBranchNBound(capacity, items, item_count)
    print kbb

该程序应该为文件test.data中的以下项目计算235的值:

5 10
40 2
50 3.14
100 1.98
95 5
30 3

第一行显示number of itemsknapsack weight。第一行下方的行显示这些项目的valueweight。项目使用namedtuple制作,并根据值/重量进行排序。对于这个问题,我得到135而不是235.我在这里做错了什么?

修改 我已经解决了基于分支和绑定找到正确项目的问题。如果需要,可以查看here

1 个答案:

答案 0 :(得分:4)

问题是您要将多个对同一Array ( [123] => Manufacturer [456] => Model ) 对象的引用插入到队列中。修复是在while循环的每次迭代中初始化两个新的Node()对象,如下所示:

v

如果没有这些重新初始化,您将修改已插入队列的 while not Q.empty(): u = Q.get() v = Node() # Added line if u.level == -1: v.level = 0 if u.level == total_items - 1: continue v.level = u.level + 1 v.weight = u.weight + items[v.level].weight v.profit = u.profit + items[v.level].value if (v.weight <= weight and v.profit > maxProfit): maxProfit = v.profit; v.bound = bound(v, total_items, weight, items) if (v.bound > maxProfit): Q.put(v) v = Node() # Added line v.level = u.level + 1 # Added line v.weight = u.weight v.profit = u.profit v.bound = bound(v, total_items, weight, items) if (v.bound > maxProfit): # print(items[v.level]) Q.put(v) 对象。 这与C ++不同,其中Node对象是隐式复制到队列中的值,以避免出现诸如此类的别名问题。