Python Forum
IndexError: index 10 is out of bounds for axis 0 with size 1
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IndexError: index 10 is out of bounds for axis 0 with size 1
#1
Hello. I am posting for the first time on this site and I am asking my questions. I am implementing the following code and I will receive the error mentioned above. The original code is in MATLAB and I want to implement it in Python.Thank you for helping me to fix this error.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
     
import numpy as np
from numpy import random
  
 
 
def fobj(X,Lb,Ub):
     
    for s in range(3):
        if X[s] > Ub[s]:
            X[s] = Ub[s]
        if X[s] < Lb[s]:
            X[s] = Lb[s]
    g = [[0 for m in range(1)]for b in range(4)]   
     
    g[0] =  1 - (X[1] **3 * X[2]) / (71785 * X[0] ** 4)
    g[1] = (4 * X[1]** 2 - X[0] * X[1])/(12566 * (X[1] * X[0] ** 3 - X[0]**4)) + 1 / (51800 * X[0] ** 2) - 1
    g[2] = 1 - (140.45 * X[0]) / (X[1] ** 2 * X[2])
    g[3] = (X[0] + X[1]) / (1.5) - 1
     
   # fit = np.zeros((1))
    fit = (X[2] + 2) * X[1] * X[0] **2
         
        # print((X[2] + 2)) * X[1]
        
                                          
                                          
    nou = 10 ** 9
    penalty = 0
    for n in range(4):
        if g[n] > 0:
            penalty = penalty + nou * g[n]
   # print(X)                                    
    pfit = fit + penalty
 
    return X,fit,pfit
 
def environment(TO,t):
    envTO = np.zeros((20,3))
    for i in range(20):
         
        c1 = random.randint(0,2,1)
        c2 = random.randint(0,2,1)
        c = c1 + c2 * ( 1 - t)
        envTO[i,:] =(np.ones((1,nV)) - c *(np.random.random(nV)))* TO[i]
    return envTO
def Replacement(TO,Fit,PFit,TO_M,Fit_M,PFit_M):
    nTO = 20
    helpTO =np.concatenate((TO,TO_M),axis = 0)
    helpFit = np.concatenate((Fit,Fit_M),axis = 1)
    helpPFit = np.concatenate((PFit,PFit_M),axis = 1)
    order = np.argsort(helpFit)
    TO = np.take(helpTO, order)[0:nTO,:]
    Fit= np.take(helpFit, order)[0:nTO]
    PFit = np.take(helpPFit, order)[0:nTO]
     
    return TO,Fit,PFit
 
def Memory(TO,Fit,PFit,TO_M,Fit_M,PFit_M):
     
    nTO = 20
    STO_M = 5
    for i in range(nTO):
        for j in range(STO_M):
            if PFit[i] < PFit_M[j]:
                TO_M[j,:] = TO[i,:]
                Fit_M[j] = PFit[i]
                break
   
    return  TO_M,Fit_M,PFit_M
 
def Newtonslaw(TO,envTO,Fit,PFit,p,t,Lb,Ub):
    nTO = 20
    nV = 3
    order = np.argsort(PFit)
    TO = np.take(TO, order)[:]
    Fit = np.take(Fit, order)
   # print(envTO[:,:])
    PFit = np.take(PFit, order)
    envTO = np.take(envTO, order)[:]
    newTO = np.zeros((20,3))
     
    for i in range(nTO):
        beta = PFit[0,i] / PFit[0,nTO-1]
         
        if i <= nTO // 2:
            
            newTO[i,:]= envTO[nTO//2 + i,:] + (TO[i,:] -envTO[nTO // 2 + i,:]) * np.exp(beta * t)
             
            if np.random.random() < p:
                index = np.ceil(np.random.random() * nV)
                newTO[i,index] = Lb[index] + (Ub[index] - Lb[index]) * np.random.random()
        else:
            newTO[i,:] = envTO[ i - nTO // 2,:] + (TO[i,:] -envTO[i - nTO // 2,:] )* np.exp(-beta * t)
            if np.random.random() < p:
                index = np.ceil(np.random.random() * nV)
                newTO[i,index] = Lb[index ] + (Ub[index] - Lb[index] ) * np.random.random()
     
    return newTO
                 
             
 
nV = 3 # Number of design variables.
Lb = np.array( [0.05,0.25,2]) # Lower bounds of design variables.
Ub= np.array([2,1.3,15]) # Upper bounds of design variables.
nTO = 20 # Number of Thermal Objects.
maxNFEs=20000 # Maximum Number of Objective Function Evaluations.
p = 0.3 # With the probability of p a component of updated objects will be
#regenerated randomly within the search space.
STO_M = 5 # Size of the thermal objects memory.
 
TO = np.zeros((20,3))
zip_object = zip(Ub, Lb)
difference = []
for Ub, Lb in zip_object:
    difference.append(Ub-Lb) # append each difference to list
Lb = np.array( [0.05,0.25,2])
Ub= np.array([2,1.3,15])
 
 
 
for j in range(nTO):
    TO[j,:] += Lb + difference*np.random.rand(nV)
     
 
 
Fit =np.zeros((1,20))
PFit =np.zeros((1,20))
 
#X = np.zeros((1,3))
 
for k in range(nTO):
    X,fit,pfit = fobj(TO[k,:],Lb,Ub)
  #  print(fit)
    TO[k,:] = X
  #  print(X)
    Fit[0,k] = fit
  #  print(Fit)
    PFit[0,k] = pfit
 #   print(PFit)
   
#TO_M = np.zeros((5,3))   
index = np.argsort(PFit,axis=1)
TO_M = np.take(TO, index)[0:STO_M,:]
Fit_M = np.take(Fit, index)[0:STO_M]
PFit_M = np.take(PFit, index)[0:STO_M]
NFEs = 0
NITs = 0 #Number of algorithm iterations
maxNITs = maxNFEs / nTO
 
while NFEs < maxNFEs:
    NITs += 1
    t = NITs / maxNITs
     
    envTO = environment(TO, t)
    newTO = Newtonslaw (TO,envTO,Fit,PFit,p,t,Lb,Ub)
    for i in range(nTO):
        X,fit,pfit = fobj(newTO[i,:],Lb,Ub)
        TO[i,:] = X
        Fit[0,i] = fit
        PFit[0,i] = pfit
    NFEs = NFEs + nTO
    TO,Fit,PFit=Replacement(TO,Fit,PFit,TO_M,Fit_M,PFit_M)
    TO_M,Fit_M,PFit_M=Memory(TO,Fit,PFit,TO_M,Fit_M,PFit_M)
    MinPFit=PFit_M[1]
    MinFit=Fit_M[1]
    bestTO=TO_M[1,:]
    
    
         
I get the error in this line of code:
newTO[i,:]= envTO[nTO//2 + i,:] + (TO[i,:] -envTO[nTO // 2 + i,:]) * np.exp(beta * t)
Reply
#2
When posting questions about "Why do I get this error?" you should post the error. Not just the message, but the entire error trace with line numbers.
1
2
3
4
5
6
for i in range(nTO):
    beta = PFit[0,i] / PFit[0,nTO-1]
          
    if i <= nTO // 2:
             
        newTO[i,:]= envTO[nTO//2 + i,:] + (TO[i,:] -envTO[nTO // 2 + i,:]) * np.exp(beta * t)
This code loops from i == 0 to i == 19.

When i == 10, your do this:
1
newTO[10,:]= envTO[10 + 10,:]...
But envTO was created to be this:
1
envTO = np.zeros((20,3))
20 is not a valid index. You can get envTO[19], but not env[20]

Maybe all you need to do is change your test:
1
2
3
4
# from
if i <= nTO // 2:
# to
if i < nTO // 2:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  IndexError: index 31 is out of bounds for axis 0 with size 31 YL789 1 918 Sep-21-2024, 09:46 AM
Last Post: Gribouillis
  IndexError: index 10 is out of bounds for axis 0 with size 10 Mehboob 11 10,221 Sep-14-2023, 06:54 AM
Last Post: Mehboob
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 13,930 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  IndexError: list index out of range dolac 4 3,412 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  IndexError: list index out of range Anldra12 2 2,237 May-03-2022, 01:39 PM
Last Post: Anldra12
  For loop index out of bounds armitron121 2 4,281 Feb-08-2022, 04:23 PM
Last Post: armitron121
  Sample labels from excel file in order to put them on x-axis and y-axis of a plot hobbyist 11 6,913 Sep-14-2021, 08:29 AM
Last Post: hobbyist
  IndexError: list index out of range rf_kartal 6 4,598 Sep-07-2021, 02:36 PM
Last Post: Larz60+
  IndexError: list index out of range Laplace12 1 2,835 Jun-22-2021, 10:47 AM
Last Post: Yoriz
  IndexError: list index out of range brunolelli 11 13,118 Mar-25-2021, 11:36 PM
Last Post: brunolelli

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020