##### IndexError: index 10 is out of bounds for axis 0 with size 1
 IndexError: index 10 is out of bounds for axis 0 with size 1 vahid89 Unladen Swallow Posts: 1 Threads: 1 Joined: Jan 2021 Reputation: Jan-06-2021, 10:01 PM (This post was last modified: Jan-06-2021, 10:01 PM by vahid89.) 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.``` 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 deanhystad So-and-so of the Yard Posts: 2,013 Threads: 11 Joined: Feb 2020 Reputation: Jan-07-2021, 06:19 PM 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. ``` 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: `newTO[10,:]= envTO[10 + 10,:]...`But envTO was created to be this: `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: ```# from if i <= nTO // 2: # to if i < nTO // 2:``` Reply

 Possibly Related Threads… Thread Author Replies Views Last Post IndexError: list index out of range brunolelli 11 847 Mar-25-2021, 11:36 PM Last Post: brunolelli IndexError: list index out of range ramu4651 2 519 Jan-24-2021, 01:45 PM Last Post: buran when this error rise?index 28 is out of bounds for axis 0 with size 13 abbaszandi 1 578 Nov-10-2020, 08:46 PM Last Post: deanhystad code with no tuple gets : IndexError: tuple index out of range Aggam 4 625 Nov-04-2020, 11:26 AM Last Post: Aggam Difference Between Figure Axis and Sub Plot Axis in MatplotLib JoeDainton123 2 611 Aug-21-2020, 10:17 PM Last Post: JoeDainton123 String index out of bounds ( Python : Dict ) kommu 2 704 Jun-25-2020, 08:52 PM Last Post: menator01 IndexError: index out of range: 0 tomX 1 1,275 May-01-2020, 10:32 AM Last Post: Larz60+ IndexError: index 0 is out of bounds for axis 0 with size 0 error tmhsa 5 1,421 Apr-25-2020, 01:15 PM Last Post: tmhsa IndexError: index 8 is out of bounds for axis 0 with size 8 Help_me_Please 6 15,904 Jan-03-2020, 01:51 PM Last Post: Help_me_Please IndexError: List index out of range issue Adem 1 1,742 Nov-01-2019, 10:47 PM Last Post: ichabod801

Forum Jump:

### User Panel Messages

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