简体   繁体   English

寻找8个元素的最佳组合,从8个列表中各取一个

[英]Finding best possible combination of 8 elements, one from each of 8 lists

My problem is I have 8 lists of size (10, 10, 10, 10, 10, 50, 50, 50) respectively. 我的问题是我分别有8个大小列表(10、10、10、10、10、50、50、50)。 Each element in each of the lists has a required cost and a value. 每个列表中的每个元素都有所需的成本和值。 What I am trying to do is find the combination of 8 elements (1 from each list) that has the highest combined value while not going over the cost cap. 我正在尝试做的是找到8个元素的组合(每个列表中的1个),它们具有最高的组合值,同时又没有超出成本上限。 What I came up with is as brute force as it gets and as I figured runs out of memory after 1hr before it finishes. 我想到的就是它所获得的蛮力,并且我认为它在完成1小时后就会耗尽内存。 Is it worth trying to implement something like an apriori algorithm or is there a better way. 是值得尝试实现类似apriori算法的东西,还是有更好的方法? What I have so far: 到目前为止,我有:

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: Sorry alot of that is text file parsing. Edit1:抱歉,很多是文本文件解析。 All that really happens is l1 = list(itertools.product(L1, L2, L5, L4, L3, L6, L7, L8)) which finds all possible combinations, and then the for loop just checks to see which of the combinations has the highest value while being under the cap. 真正发生的是l1 = list(itertools.product(L1, L2, L5, L4, L3, L6, L7, L8))找到所有可能的组合,然后for循环仅检查以查看哪些组合具有低于上限时的最高值。

The memory limit you run into is due to how your loop is written it should iterate over the generator of product and not create a list from that generator: 您遇到的内存限制是由于循环的编写方式而引起的,它应遍历产品的生成器,而不是从该生成器创建列表:

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

您正在尝试解决背包问题 ,例如, 在此处查看可能的方法(动态编程算法)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM