最大化消耗能源

时间:2019-03-31 02:10:56

标签: python algorithm dynamic-programming

提供了三种类型的食物,即肉,蛋糕和比萨饼 和N家不同的商店出售食物,我只能从一种食物中选择一种 每个商店。另外,我只能以A,B和C编号购买商品,其中“ A”表示从不同商店的“ A”总数中购买肉类(请参见示例)。我的任务是 消耗食物,以便我可以拥有最大的能量。 例如,

10            <= number of stores <br>
5 3 2         <= out of 10 stores I can pick meat from 5 stores only. Similarly,
                 I can pick cake from 3 out of 10 stores...
56 44 41    1 <= Energy level of meat, cake and pizza - (56, 44, 41) for first store.<br> 
56 84 45    2
40 98 49    3
91 59 73    4
69 94 42    5
81 64 80    6
55 76 26    7
63 24 22    8
81 60 44    9
52 95 11    10

所以要最大限度地利用能量,我可以消耗...

  1. 商店编号中的肉:

    [1, 4, 7, 8, 9] => [56, 91, 55, 63, 81]
    
  2. 从商店编号开始结帐:

    [3, 5, 10] => [98, 94, 95]
    
  3. 商店编号中的披萨:

    [2, 6] => [45, 80]
    

这使我最终获得758的最大能量。


由于我是动态编程的新手,所以我尝试通过生成独特的组合(例如,

10 C 5 * (10-5) C 3 * (10-5 -3) C 2 = 2520

这是我的代码,

nStores = 10
a, b, c = 5, 3, 2
matrix = [
    [56,44,41],
    [56,84,45],
    [40,98,49],
    [91,59,73],
    [69,94,42],
    [81,64,80],
    [55,76,26],
    [63,24,22],
    [81,60,44],
    [52,95,11]
]

count = a + b + c
data = []
allOverCount = [i for i in range(count)]
def genCombination(offset, depth, passedData, reductionLevel = 3):
    if (depth == 0):
        first = set(data)
        if reductionLevel ==  3:
            return genCombination(0,b,[i for i in allOverCount if i not in first], reductionLevel=2)
        elif reductionLevel ==  2:
            return genCombination(0,c,[i for i in allOverCount if i not in first], reductionLevel=1)
        elif reductionLevel == 1:
            xAns = 0
            for i in range(len(data)):
                if i < a:
                    xAns += matrix[data[i]][0]
                elif i < a + b:
                    xAns += matrix[data[i]][1]
                else:
                    xAns += matrix[data[i]][2]
            return xAns
    oneData = 0
    for i in range(offset, len(passedData) - depth + 1 ):
        data.append(passedData[i])
        oneData = max(oneData, genCombination(i+1, depth-1, passedData, reductionLevel))
        del data[-1]
    return oneData
passedData = [i for i in range(count)]
finalOutput = genCombination(0,a,passedData)
print(finalOutput)

我知道这不是正确的方法。我该如何优化?

2 个答案:

答案 0 :(得分:4)

对背包的修改似乎可以解决它。

让我们将dp表定义为4维数组dp [N + 1] [A + 1] [B + 1] [C + 1]

现在有些单元格dp [n] [a] [b] [c]表示我们已经考虑了n家商店,我们从中选择了一家肉店, b的蛋糕店和c的比萨店,它存储着我们可以拥有的最大能量。

转换也很容易,从某些状态dp [n] [a] [b] [c]我们可以转到:

  • dp [n + 1] [a] [b] [c]如果我们跳过第n + 1个商店
  • dp [n + 1] [a + 1] [b] [c]如果我们购买 n + 1号店里的肉
  • dp [n + 1] [a] [b + 1] [c]如果我们从n + 1商店购买蛋糕
  • dp [n + 1] [a] [b] [c + 1](如果我们从n + 1商店购买披萨)

剩下的就是填写dp表。示例代码:

N = 10
A,B,C = 5,3,2
energy = [
[56, 44, 41],
[56, 84, 45],  
[40, 98, 49],  
[91, 59, 73], 
[69, 94, 42], 
[81, 64, 80], 
[55, 76, 26], 
[63, 24, 22], 
[81, 60, 44], 
[52, 95, 11] 
]

dp = {} 

for n in range(N+1):
    for a in range(A+1):
        for b in range(B+1):
            for c in range(C+1):
                dp[n,a,b,c]=0

answer = 0;
for n in range(N+1):
    for a in range(A+1):
        for b in range(B+1):
            for c in range(C+1):
                #Case 1, skip n-th shop
                if (n+1,a,b,c) in dp: dp[n+1,a,b,c] = max(dp[n+1,a,b,c], dp[n,a,b,c])
                #Case 2, buy meat from n-th shop
                if (n+1,a+1,b,c) in dp: dp[n+1,a+1,b,c] = max(dp[n+1,a+1,b,c], dp[n,a,b,c] + energy[n][0])
                #Case 3, buy cake from n-th shop
                if (n+1,a,b+1,c) in dp: dp[n+1,a,b+1,c] = max(dp[n+1,a,b+1,c], dp[n,a,b,c] + energy[n][1])
                #Case 4, buy pizza from n-th shop
                if (n+1,a,b,c+1) in dp: dp[n+1,a,b,c+1] = max(dp[n+1,a,b,c+1], dp[n,a,b,c] + energy[n][2])
                answer = max(answer,dp[n,a,b,c])

print(answer)

答案 1 :(得分:4)

这是通过纸浆(https://pypi.org/project/PuLP)使用线性编程的解决方案,为我提供了最佳解决方案

document

我认为该性能应优于手动编码的穷举求解器。

Maximum energy level: 758.0
Mapping of stores per foodtype: {1: [9, 2, 4], 0: [3, 8, 0, 6, 7], 2: [1, 5]}