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.
    
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.
    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: index 10 is out of bounds for axis 0 with size 10 Mehboob 11 1,950 Sep-14-2023, 06:54 AM
Last Post: Mehboob
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 5,958 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  IndexError: list index out of range dolac 4 1,843 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  IndexError: list index out of range Anldra12 2 1,408 May-03-2022, 01:39 PM
Last Post: Anldra12
  For loop index out of bounds armitron121 2 2,617 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 4,232 Sep-14-2021, 08:29 AM
Last Post: hobbyist
  IndexError: list index out of range rf_kartal 6 2,761 Sep-07-2021, 02:36 PM
Last Post: Larz60+
  IndexError: list index out of range Laplace12 1 2,186 Jun-22-2021, 10:47 AM
Last Post: Yoriz
  IndexError: list index out of range brunolelli 11 6,345 Mar-25-2021, 11:36 PM
Last Post: brunolelli
  IndexError: list index out of range ramu4651 2 3,693 Jan-24-2021, 01:45 PM
Last Post: buran

Forum Jump:

User Panel Messages

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