float对象不可调用

时间:2013-08-28 04:17:21

标签: python numpy

我的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)&gt; = 0.001且l&lt; = 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

2 个答案:

答案 0 :(得分:10)

不要将range用作变量名称。它影响内置函数range

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

答案 1 :(得分: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

()用于函数调用。由于上面示例中的xfloat,因此您无法调用float,因此会失败。