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