3

我的 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
4

2 回答 2

13

不要range用作变量名。它隐藏了内置函数range

>>> range = 0.1
>>> range(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'float' object is not callable
于 2013-08-28T04:19:40.177 回答
4

range是一个内置函数,它返回要迭代的值列表。在第 317 行,你有,它用一个值range = 0.1替换了内置range函数。float

因此,第二次通过时range,您的程序没有使用普通函数,而是看到一个浮点数,这就像说for ii in 0.1(0, 8, 1):这显然没有意义。

range要解决此问题,只需使用与示例不同的变量名称即可interval = 0.1

您收到的错误消息来自您尝试调用浮点数。

>>> x = 1.1
>>> x()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'float' object is not callable

用于()函数调用。由于xis afloat在上面的例子中,它失败是因为你不能调用 a float

于 2013-08-28T04:22:46.710 回答