找到8个元素的最佳组合,每个元素来自8个列表

时间:2016-01-26 01:21:53

标签: python algorithm

我的问题是我有8个大小的列表(10,10,10,10,10,50,50,50)。每个列表中的每个元素都具有所需的成本和值。我想要做的是找到8个元素(每个列表中有1个)的组合,它们具有最高的组合值,而不会超过成本上限。我想出的就是蛮力,因为我觉得它在完成之前1小时后会耗尽内存。是否值得尝试实现类似apriori算法的东西,或者是否有更好的方法。到目前为止我所拥有的:

import itertools

foo = open("test.txt", "r")

L1 = []
L2 = []
L3 = []
L4 = []
L5 = []
L6 = []
L7 = []
L8 = []

costCap = 10000 #max cost allowed
perfValue = 0 #
final = []

for line in foo:
    line = ','.join(line.split())
    line2 = line.split(",")[1] #values

    if(line2 == "A1"):
        L3.append(line)
    elif(line2 == "A2"):
        L2.append(line)
    elif(line2 == "A3"):
        L5.append(line)
    elif(line2 == "A4"):
        L1.append(line)
    elif(line2 == "A5"):
        L4.append(line)

    L6.append(line)
    L7.append(line)
    L8.append(line)


l1 = list(itertools.product(L1, L2, L5, L4, L3, L6, L7, L8))
for k in range(len(l1)):
    s1 = l1[k][0].split(",")[5]
    s2 = l1[k][1].split(",")[5]
    s3 = l1[k][2].split(",")[5]
    s4 = l1[k][3].split(",")[5]
    s5 = l1[k][4].split(",")[5]
    s6 = l1[k][5].split(",")[5]
    s7 = l1[k][6].split(",")[5]
    s8 = l1[k][7].split(",")[5]
    temp = int(s1[1:]) + int(s2[1:]) + int(s3[1:]) + int(s4[1:]) + int(s5[1:]) + int(s6[1:]) + int(s7[1:]) + int(s8[1:])
    if ((temp > perfValue) and (temp < costCap)):
        perfRating = temp
        final = l1[k]

print(final)

Edit1:很抱歉很多是文本文件解析。所有真正发生的事情都是l1 = list(itertools.product(L1, L2, L5, L4, L3, L6, L7, L8))找到所有可能的组合,然后for循环只检查哪个组合具有最高值,同时低于上限。

2 个答案:

答案 0 :(得分:3)

您遇到的内存限制是由于您的循环编写方式,它应该迭代产品生成器而不是从该生成器创建列表:

for k in itertools.product(L1, L2, L5, L4, L3, L6, L7, L8):
    s1 = k[0].split(",")[5]
    s2 = k[1].split(",")[5]
    s3 = k[2].split(",")[5]
    s4 = k[3].split(",")[5]
    s5 = k[4].split(",")[5]
    s6 = k[5].split(",")[5]
    s7 = k[6].split(",")[5]
    s8 = k[7].split(",")[5]
    temp = int(s1[1:]) + int(s2[1:]) + int(s3[1:]) + int(s4[1:]) + int(s5[1:]) + int(s6[1:]) + int(s7[1:]) + int(s8[1:])
    if ((temp > perfValue) and (temp < costCap)):
        perfRating = temp
        final = k

答案 1 :(得分:1)

您正试图解决Knapsack Problem看一看,例如here寻找可能的方法(动态编程算法)