Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何实现回溯打印背包中的物品(允许重复物品)?_Python_Dynamic Programming_Backtracking_Knapsack Problem - Fatal编程技术网

Python 如何实现回溯打印背包中的物品(允许重复物品)?

Python 如何实现回溯打印背包中的物品(允许重复物品)?,python,dynamic-programming,backtracking,knapsack-problem,Python,Dynamic Programming,Backtracking,Knapsack Problem,我有以下清单: 第一个数字是产品id, 第二个数字是55,10,。。。价格和价格是多少 第三个数字16,6…是利润。 使用下面的代码,如果我输入一个价格限制,我应该通过项目的最佳组合获得最高利润。网站可以多次出售 这是我的代码: 现在我需要修改它,以便它还返回一个列表chosenProduct,表示要销售的所选产品ID 例如。​如果两次选择产品冷蓝色马雷拉水壶,一次选择减肥包,则列表应为chosenProduct=[0,0,1] 每当我发现一个新的最优值时,我就尝试将选择的产品存储在一个列表中,

我有以下清单:

第一个数字是产品id, 第二个数字是55,10,。。。价格和价格是多少 第三个数字16,6…是利润。 使用下面的代码,如果我输入一个价格限制,我应该通过项目的最佳组合获得最高利润。网站可以多次出售

这是我的代码:

现在我需要修改它,以便它还返回一个列表chosenProduct,表示要销售的所选产品ID

例如。​如果两次选择产品冷蓝色马雷拉水壶,一次选择减肥包,则列表应为chosenProduct=[0,0,1]

每当我发现一个新的最优值时,我就尝试将选择的产品存储在一个列表中,但它会存储它找到的从值1到价格限制的每一个最优值。我希望它只存储选择的最新产品,并使用回溯从那里列出所有选择的产品,构成利润。我该怎么做

def dp_pricelimit(product_list, price_limit):
    #to store results
    chosenProduct=[None]*(price_limit+1)
    memo=[0]*(price_limit+1)
    memo[0]=0
    for price in range(1, price_limit+1):
        for item in product_list:#go through the items
            if item[2]<=price:
                balance=price-item[2]

                profit=item[3] + memo[balance]
                if profit>memo[price]:#if found new optimal
                    memo[price]=profit
                    chosenProduct[price]=item[0]

    #use list to store list of item
    items = []
    #set i, the current price, to max price
    i = price_limit

    #while i is not a negative price
    while i >= 0:
        if chosenProduct[i] == None:
            break
        #append the item that was last added to make the optimal profit at price i.
        items.append(chosenProduct[i])
        #then jump back to before we added this item by decrementing the i, the current price, by the price of the last item that was added.
        i-=product_list[items[-1]][2]




    return memo[price_limit],items


print(dp_pricelimit([[0, 'Cool Blue Marella Jug', 33, 15],[1, 'Weight Loss Pack', 55, 16], [2, 'Natural Black Vogue Lashes', 10, 2], [3, 'Paris Age Perfect Intense Nutrition Serum', 45, 22]],88))

基本上,使用chosenproduct数组向后迭代。最后添加的项目,以创建最佳的;它的成本可以被减去,以在它被添加之前的价格获得最佳价值。然后在下一个价格中,我们添加了最后一项,以在chosenproduct数组中获得当前价格。祝你好运

欢迎来到StackOverflow。请按照您创建此帐户时的建议,阅读并遵循帮助文档中的发布指南。适用于这里。在您发布MCVE代码并准确描述问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中,并重现您描述的问题。当您发布的代码尝试比较int和string类型时,它将失效。在我解决了这个问题后,我发现不管价格如何,它只选择了一个项目。我想你的下一步是学习递归和回溯的教程,这样你就能找到一个有效的答案了。然后您可以担心跟踪购物清单。您可能还想将利润变量更改为收入。好的,谢谢,但我只是想知道为什么我们需要[2]在I-=product_list[items[-1]][2]?我尝试了没有它的情况,并且不支持操作数类型-=:'int'和'list'。[2]是指价格吗?那么[2]是该特定项目的价格指数。所以product_list[items[-1]]表示一个项目,product_list[items[-1]][2]表示该项目的价格。此外,items[-1]表示最后一个元素也是附加在items数组中的最后一个元素,它是一个项目ID列表,我们使用product_list作为给定项目ID号的项目其余信息的查找表。
def dp_pricelimit(product_list, price_limit):
    #to store results
    chosenProduct=[0]*(price_limit+1)
    memo=[0]*(price_limit+1)
    memo[0]=0
    for price in range(1, price_limit+1):
        for item in product_list:#go through the items
            if item[2]<=price:
                balance=price-item[2]
                profit=item[3] + memo[balance]
                if profit>memo[price]:#if found new optimal
                    memo[price]=profit
                    chosenProduct[price]=item[0]

    return memo[price_limit],chosenProduct
def dp_pricelimit(product_list, price_limit):
    #to store results
    chosenProduct=[None]*(price_limit+1)
    memo=[0]*(price_limit+1)
    memo[0]=0
    for price in range(1, price_limit+1):
        for item in product_list:#go through the items
            if item[2]<=price:
                balance=price-item[2]

                profit=item[3] + memo[balance]
                if profit>memo[price]:#if found new optimal
                    memo[price]=profit
                    chosenProduct[price]=item[0]

    #use list to store list of item
    items = []
    #set i, the current price, to max price
    i = price_limit

    #while i is not a negative price
    while i >= 0:
        if chosenProduct[i] == None:
            break
        #append the item that was last added to make the optimal profit at price i.
        items.append(chosenProduct[i])
        #then jump back to before we added this item by decrementing the i, the current price, by the price of the last item that was added.
        i-=product_list[items[-1]][2]




    return memo[price_limit],items


print(dp_pricelimit([[0, 'Cool Blue Marella Jug', 33, 15],[1, 'Weight Loss Pack', 55, 16], [2, 'Natural Black Vogue Lashes', 10, 2], [3, 'Paris Age Perfect Intense Nutrition Serum', 45, 22]],88))