I am trying to use the following function to make mutation step in agametic algorithm. Unfortunately, I have got an error as follows:
IndexError Traceback (most recent call last)
<ipython-input-20-35511e994420> in <cell line: 2>()
1 #Run the GA algorithm.
----> 2 out_1 = myGA_pv.run_ga_pv_simulation(problem, params)
3 #out_2 = myGA_wind.run_ga_simulation(problem, params)
4
1 frames
/content/myGA_pv.py in mutate(x, mu, sigma)
151 flag = np.random.rand(np.int(np.nan_to_num(x.position))) <= mu
152 ind = np.argwhere(flag)
--> 153 y.position[ind] += sigma*(np.random.rand(ind.any().shape))
154 return y
155
IndexError: invalid index to scalar variable.
The code of this function is like follows:
def mutate(x, mu, sigma):
y = x.deepcopy()
flag = np.random.rand(np.int(np.nan_to_num(x.position))) <= mu
ind = np.argwhere(flag)
y.position[ind] += sigma*(np.random.rand(ind.shape))
return y
How to overcome such error ?
y.position? why do you expect it to work withind, an array produced byargwhere? What isind?np.int. That may be deprecated.indfromargwherewill be a (n,1) array (ncan be 0, 1 or more). I don't know what thisscalar variableis, but clearly it can be indexed with an array like this (or possibly any index).