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.
newTO[i,:]= envTO[nTO//2 + i,:] + (TO[i,:] -envTO[nTO // 2 + i,:]) * np.exp(beta * t)
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)