[英]Calculating items included in branch and bound knapsack
Using a branch and bound algorithm I have evaluated the optimal profit from a given set of items, but now I wish to find out which items are included in this optimal solution. 使用分支定界算法,我已经评估了给定项目集的最佳利润,但现在我想知道哪个项目包含在这个最优解决方案中。 I'm evaluating the profit value of the optimal knapsack as follows (adapted from here ): 我正在评估最佳背包的利润值如下(从这里改编):
import Queue
class Node:
def __init__(self, level, profit, weight):
self.level = level # The level within the tree (depth)
self.profit = profit # The total profit
self.weight = weight # The total weight
def solveKnapsack(weights, profits, knapsackSize):
numItems = len(weights)
queue = Queue.Queue()
root = Node(-1, 0, 0)
queue.put(root)
maxProfit = 0
bound = 0
while not queue.empty():
v = queue.get() # Get the next item on the queue
uLevel = v.level + 1
u = Node(uLevel, v.profit + e[uLevel][1], v.weight + e[uLevel][0])
bound = getBound(u, numItems, knapsackSize, weights, profits)
if u.weight <= knapsackSize and u.profit > maxProfit:
maxProfit = uProfit
if bound > maxProfit:
queue.put(u)
u = Node(uLevel, v.profit, v.weight)
bound = getBound(u, numItems, knapsackSize, weights, profits)
if (bound > maxProfit):
queue.put(u)
return maxProfit
# This is essentially the brute force solution to the fractional knapsack
def getBound(u, numItems, knapsackSize, weight, profit):
if u.weight >= knapsackSize: return 0
else:
upperBound = u.profit
totalWeight = u.weight
j = u.level + 1
while j < numItems and totalWeight + weight[j] <= C:
upperBound += profit[j]
totalWeight += weights[j]
j += 1
if j < numItems:
result += (C - totalWeight) * profit[j]/weight[j]
return upperBound
So, how can I get the items that form the optimal solution , rather than just the profit? 那么, 我如何才能获得构成最佳解决方案的项目 ,而不仅仅是利润?
I got this working using your code as the starting point. 我使用您的代码作为起点让我的工作。 I defined my Node
class as: 我将我的Node
类定义为:
class Node:
def __init__(self, level, profit, weight, bound, contains):
self.level = level # current level of our node
self.profit = profit
self.weight = weight
self.bound = bound # max (optimistic) value our node can take
self.contains = contains # list of items our node contains
I then started my knapsack solver similarly, but initalized root = Node(0, 0, 0, 0.0, [])
. 然后我开始类似我的背包解算器,但初始化root = Node(0, 0, 0, 0.0, [])
0,0,0,0.0 root = Node(0, 0, 0, 0.0, [])
。 The value root.bound
could be a float, which is why I initalized it to 0.0
, while the other values (at least in my problem) are all integers. 值root.bound
可以是一个浮点数,这就是为什么我将它root.bound
为0.0
,而其他值(至少在我的问题中)都是整数。 The node contains nothing so far, so I started it off with an empty list. 到目前为止,该节点没有任何内容,所以我用一个空列表开始它。 I followed a similar outline to your code, except that I stored the bound in each node (not sure this was necessary), and updated the contains
list using: 我跟你的代码大致相似,除了我在每个节点中存储了绑定(不确定这是必要的),并使用以下方法更新了contains
列表:
u.contains = v.contains[:] # copies the items in the list, not the list location
# Initialize u as Node(uLevel, uProfit, uWeight, 0.0, uContains)
u.contains.append(uLevel) # add the current item index to the list
Note that I only updated the contains
list in the "taking the item" node. 请注意,我只更新了“获取项目”节点中的contains
列表。 This is the first initialization in your main loop, preceding the first if bound > maxProfit:
statement. 这是主循环中的第一个初始化,位于第一个if bound > maxProfit:
语句之前。 I updated the contains
list in the if:
statement right before this, when you update the value of maxProfit
: 在更新maxProfit
的值时,我在此之前更新了if:
语句中的contains
列表:
if u.weight <= knapsackSize and u.value > maxProfit:
maxProfit = u.profit
bestList = u.contains
This stores the indices of the items you are taking to bestList
. 这会将您正在拍摄的项目的索引存储到bestList
。 I also added the condition if v.bound > maxProfit and v.level < items-1
to the main loop right after v = queue.get()
so that I do not keep going after I reach the last item, and I do not loop through branches that are not worth exploring. 我还在v = queue.get()
之后将if v.bound > maxProfit and v.level < items-1
到主循环中,以便在我到达最后一个项目后不继续操作,而且我没有循环通过不值得探索的分支。
Also, if you want to get a binary list output showing which items are selected by index, you could use: 此外,如果要获取显示通过索引选择哪些项目的二进制列表输出,您可以使用:
taken = [0]*numItems
for item in bestList:
taken[item] = 1
print str(taken)
I had some other differences in my code, but this should enable you to get your chosen item list out. 我的代码中还有其他一些差异,但这应该可以让您获得所选的项目列表。
I have been thinking about this for some time. 我一直在考虑这个问题。 Apparently, you have to add some methods inside your Node class that will assign the node_path and add the current level to it. 显然,您必须在Node类中添加一些方法,这些方法将分配node_path并向其添加当前级别。 You call your methods inside your loop and assign the path_list to your optimal_item_list when your node_weight is less than the capacity and its value is greater than the max_profit, ie where you assign the maxProfit. 您可以在循环内调用方法,并在node_weight小于容量并且其值大于max_profit(即分配maxProfit的位置)时将path_list分配给optimal_item_list。 You can find the java implementation here 你可以在这里找到java实现
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.