0

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 ?

5
  • Please edit your question to include a minimum reproducible example with test data demonstrating your problem. Commented Nov 10, 2023 at 16:18
  • The code you listed is not the same as the code in the error text. Commented Nov 10, 2023 at 16:18
  • what is y.position? why do you expect it to work with ind, an array produced by argwhere? What is ind? Commented Nov 10, 2023 at 16:29
  • Careful about using np.int. That may be deprecated. Commented Nov 10, 2023 at 16:57
  • ind from argwhere will be a (n,1) array (n can be 0, 1 or more). I don't know what this scalar variable is, but clearly it can be indexed with an array like this (or possibly any index). Commented Nov 10, 2023 at 19:33

1 Answer 1

0

If your x.position was a scalar variables, i.e. a np.float64 constructed like:

In [101]: y=np.array([15.,20.])[0]; y
Out[101]: 15.0

argwhere works with an int of that, and produces a (n,1) array:

In [102]: ind=np.argwhere(np.random.rand(int(y))<.15)

In [103]: ind
Out[103]: 
array([[ 0],
       [11]], dtype=int64)

Using that as index to y produces your error:

In [104]: y[ind]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[104], line 1
----> 1 y[ind]

IndexError: invalid index to scalar variable.

Any attempt to index that scalar variable produces this error.

I don't know what of this is your code, and what's copied from somewhere elese (without much understanding?), but the key thing is look at the indexing operation in the problem line. What are the variables, the position and the ind.

y.position[ind]

I can't propose any fix because I don't have the big picture or any of your data. But the first step of any fix is to understand the error

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.