我的 Python 代码中的 while 循环中有多个 for 循环。对于第一次迭代,一切正常。对于 while 循环的第二次迭代,我收到以下错误:
File ".\simulated_annealing.py", line 209, in <module>
for ii in range(0, 8, 1) :
TypeError: 'float' object is not callable
以下是我的代码的相关部分:
而 numpy.absolute(FcostOld) >= 0.001 和 l <= L :
Fx = []
Fy = []
Fz = []
V = []
ii = 0
for ii in range(0, 8, 1) : # ... LINE 209
Fx.append([])
Fy.append([])
Fz.append([])
for j in range(0, 13, 1) :
Fx[ii].append(0)
Fy[ii].append(0)
Fz[ii].append(0)
V.append(0.0)
print "\n l = ", l
j = 0
print "j = ", j
i = 0
for i in range(0, 8) :
print "i = ", i
"""
V[i] = 0.0
for j in range(0, 13, 1) :
Fx[i][j] = 0.0
Fy[i][j] = 0.0
Fz[i][j] = 0.0
"""
for j in range(0, 6, 1) :
for k in range(0, Natoms, 1) :
dxxC = x[i][j] - Layer15[k][0]
dyyC = y[i][j] - Layer15[k][1]
dzzC = zC - Layer15[k][2]
rrC = numpy.sqrt(dxxC*dxxC + dyyC*dyyC + dzzC*dzzC)
if rrC <= rcut :
V[i] = V[i] + VrealC(rrC, DeC, alphaC, rC, s6)
Fx[i][j] = Fx[i][j] + frealC(rrC, DeC, alphaC, rC, dxxC, s6)
Fy[i][j] = Fy[i][j] + frealC(rrC, DeC, alphaC, rC, dyyC, s6)
Fz[i][j] = Fz[i][j] + frealC(rrC, DeC, alphaC, rC, dzzC, s6)
for j in range(6, 12, 1) :
for k in range(0, Natoms, 1) :
dxxH = x[i][j] - Layer15[k][0]
dyyH = y[i][j] - Layer15[k][1]
dzzH = zH - Layer15[k][2]
rrH = numpy.sqrt(dxxH*dxxH + dyyH*dyyH +dzzH*dzzH)
if rrH <= rcut :
V[i] = V[i] + VrealH(rrH, DeH, alphaH, rH, s6)
Fx[i][j] = Fx[i][j] + frealH(rrH, DeH, alphaH, rH, dxxH, s6)
Fy[i][j] = Fy[i][j] + frealH(rrH, DeH, alphaH, rH, dyyH, s6)
Fz[i][j] = Fz[i][j] + frealH(rrH, DeH, alphaH, rH, dzzH, s6)
j = 12
for k in range(0, Natoms, 1) :
dxxX = x[i][j] - Layer15[k][0]
dyyX = y[i][j] - Layer15[k][1]
dzzX = zX - Layer15[k][2]
rrX = numpy.sqrt(dxxX*dxxX + dyyX*dyyX +dzzX*dzzX)
if rrX <= rcutX :
V[i] = V[i] + VrealX(rrH, DeH, alphaH, rH, s6)
Fx[i][j] = Fx[i][j] + frealX(rrX, DeX, alphaX, rX, dxxX, s6)
Fy[i][j] = Fy[i][j] + frealX(rrX, DeX, alphaX, rX, dyyX, s6)
Fz[i][j] = Fz[i][j] + frealX(rrX, DeX, alphaX, rX, dzzX, s6)
print "i = ", i
if flag == False :
FcostOld_V = 0 * numpy.sqrt( sum(numpy.power(V[n]-VTarget[n], 2) for n in range(0,8)) ) / numpy.sqrt( sum((V[n]*V[n]) for n in range(0,8)) )
FcostOld_F = numpy.absolute( sum(Fz[n][m] for n in range(0,8) for m in range(0,13)) ) / 8
FcostOld = FcostOld_V + FcostOld_F
if flag == True :
FcostNew_V = 0 * numpy.sqrt( sum(numpy.power(V[n]-VTarget[n], 2) for n in range(0,8)) ) / numpy.sqrt( sum((V[n]*V[n]) for n in range(0,8)) )
FcostNew_F = numpy.absolute( sum(Fz[n][m] for n in range(0,8) for m in range(0,13)) ) / 8
FcostNew = FcostNew_V + FcostNew_F
if (FcostNew - FcostOld) < 0:
s6Old = s6
FcostOld = FcostNew
DeCOld = DeC
alphaCOld = alphaC
rCOld = rC
DeHOld = DeH
alphaHOld = alphaH
rHOld = rH
DeXOld = DeX
alphaXOld = alphaX
rXOld = rX
if (FcostNew - FcostOld) >= 0 :
P = numpy.exp( -(FcostNew-FcostOld) / kT )
r0 = numpy.random.rand()
if r0 < P :
s6Old = s6
FcostOld = FcostNew
DeCOld = DeC
alphaCOld = alphaC
rCOld = rC
DeHOld = DeH
alphaHOld = alphaH
rHOld = rH
DeXOld = DeX
alphaXOld = alphaX
rXOld = rX
flag = True
range = 0.1
DeC = NewParameter(DeCOld, range)
alphaC = NewParameter(alphaCOld, 2*range)
rC = NewParameter(rC, range/2)
DeH = NewParameter(DeHOld, 5*range)
alphaH = NewParameter(alphaHOld, 2*range)
rH = NewParameter(rH, range/3)
DeX = NewParameter(DeXOld, range)
alphaX = NewParameter(alphaXOld, 2*range)
rX = NewParameter(rX, range/2)
s6 = NewParameter(s6Old, range)
if numpy.mod(l,1) == 0 :
print "\nFcost = ", FcostOld
print Fx[7]
print " l = ", l
l = l + 1