在Python中使用itertools.product运行迭代次数的任何方法

问题描述 投票:0回答:1

需要以

s= a1*tf1+a2*tf2+......+an*tfn
的形式计算大序列的总和,其中我的问题的 n=50 以及
a1,a2,...,an= range(25,100,25)
tf = random.randint(low=-10, high=10, size=51)
的每个值。 程序需要针对
a1,a2,a3... an
的不同组合获取 s 的最大值。

当前代码的问题是a的组合总数为3^51个值,这需要大量时间来解决并且遇到内存问题。

有什么方法可以解决这个问题吗?

from itertools import product
import numpy as np
import pandas as pd
import random
def calculate_s(coefficients, variables):
    if len(coefficients) != len(variables):
        raise ValueError("Number of coefficients must match the number of variables.")

    s = sum(f * a for f, a in zip(coefficients, variables))
    return s

K = 51
random_array = np.random.randint(low=-10, high=10, size=K)
random_set = random_array.tolist()
#print("Random integer set:", random_array)
#print(type(random_set))
tf=list(random_set)
print(tf)
splm =0
amax=[]


temp = [list(range(10, 80,20) )for X in range(K)]

res = list(product(*temp))
for a in res:
    a = list(map(lambda x: x, a))
    spl=calculate_s(tf,a)
    if spl > splm:
        splm = spl
        amax=a

print(splm,amax)
python runtime-error out-of-memory python-itertools large-data-volumes
1个回答
0
投票

由于您的代码和问题注释了不同的

a
值范围,我将随意使用代码中提供的值

考虑到你的约束

range(10, 80,20)
low=-10, high=10
并最大化你的价值,一个好的选择是最大化正数的乘数并最小化负数的乘数(任何东西都适合0,所以我会冒昧地不最大化那些以匹配您的输出)

例如,您的解决方案以较低的 K 给出这些结果(请参阅底部的输出)

from itertools import product
import numpy as np

K=5 # for proof of concept
random_array = np.random.randint(low=-10, high=10, size=K)
random_set = random_array.tolist() # no extra cast to list is needed, it's already a list
temp = [list(range(10, 80,20) )for X in range(K)]

def calculate_s(coefficients, variables):
    if len(coefficients) != len(variables):
        raise ValueError("Number of coefficients must match the number of variables.")
    s = sum(f * a for f, a in zip(coefficients, variables))
    return s

splm = 0
amax = None
for a in product(*temp): #there's no need to make a list first
    # this operation does not do anything useful; just convertinga tuple to a list
    # a = list(map(lambda x: x, a))
    spl=calculate_s(random_set,a)
    if spl > splm:
        splm = spl
        amax=a

print(random_set)
print(splm,amax)
#>>> [-1, 9, 3, 7, -7]
#>>> 1250 (10, 70, 70, 70, 10)

只选择10和70的值,所以我们可以这样优化

amax_result = []
temp_range = list(range(10,80,20)) # in case you want different ranges
temp_range = [temp_range[0],temp_range[-1]] #min and max value
for item in random_set:
    
    if item>0:
        amax_result.append(temp_range[1]) # max
    else:
        amax_result.append(temp_range[0]) #min
spl_result = calculate_s(random_set,amax_result)
print(spl_result, amax_result)
#>>> [-1, 9, 3, 7, -7]
#>>> 1250 [10, 70, 70, 70, 10]
© www.soinside.com 2019 - 2024. All rights reserved.