Manish Tr
Manish Tr

Reputation: 1

Any method to run the number of iteration using itertools .product in python

need to calculate sum of large series in the form s= a1*tf1+a2*tf2+......+an*tfn where n=50 for my problem and each and each value of a1,a2,...,an= range(25,100,25) and tf = random.randint(low=-10, high=10, size=51). The program need to get max value of s for different combination of a1,a2,a3... an.

The issue with current code is the total number of combination of a is 3^51 value which take huge amount of time to solve and memory issue is encountered.

Is there any method to solve this issue?

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)

Upvotes: -1

Views: 45

Answers (1)

Kenzo Staelens
Kenzo Staelens

Reputation: 413

as your code and question note different ranges for a values i'll take the liberty to use the ones provided in your code

given your constraints range(10, 80,20) and low=-10, high=10 and maximizing your value, a good bet is maximizing the multiplier for the positive numbers and minimizing the multiplier for negative numbers (anything goes for 0 so i'll take the liberty of not maximizing those to match your output)

your solution for example gives these results at a lower K (see bottom for output)

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() # random_array is just iterable as is
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_array,a)
    if spl > splm:
        splm = spl
        amax=a

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

only 10 and 70 are values chosen, so we can optimize like this

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_array:
    
    if item>0:
        amax_result.append(temp_range[1]) # max
    else:
        amax_result.append(temp_range[0]) #min
spl_result = calculate_s(random_array,amax_result)
print(spl_result, amax_result)
#>>> [-1, 9, 3, 7, -7]
#>>> 1250 [10, 70, 70, 70, 10]

Upvotes: 0

Related Questions