Edit: Solution
The answer was posted below, using list prod from itertools greatly reduces memory usage as it is a list object, something I overlooked in the thread I linked.
Code:
n = 2 #dimension
r = np.arange(0.2,2.4,0.1) #range
grid = product(r,repeat=n)
for g in grid:
y = np.array(g)
if np.all(np.diff(g) > 0) == True:
print(f(y)) #or whatever you want to do
I try to evalute a function with n parameters in a certain range. And I want to be able to change n in a certain range, so the user can determine it with his input.
I got a working code with help from here an it looks like this:
import numpy as np
n = 2 #determine n
r = np.arange(0.2,2.4,0.1) #determine range
grid=np.array(np.meshgrid(*[r]*n)).T.reshape(-1,n) #creates a meshgrid for the range**n
#reduces the number of used points in the grid (the function is symmetrical in its input parameters)
for i in range(0,n-1):
grid = np.array([g for g in grid if g[i]<g[i+1]])
y = np.zeros((grid.shape[0]))
for i in range(grid.shape[0]):
y[i] = f(grid[i,:]) #evaluating function for all lines in grid
This only works up to n equals 5 or 6 then the grid-array and the ouput array just gets to big for Spyder to handle it (~10 MB).
Is there a way to only create one line of the meshgrid (combination of parameters) at a time to evaluate the function with? Then I could save those values (grid,y) in a textfile and overwrite them in the next step.
Or is there a way to create all n-dimensional combinations in range without meshgrid but with a variable n and one at a time?