代码不能正确迭代

时间:2016-05-05 19:05:15

标签: python-2.7

我知道这个网站上有很多类似的问题,但不幸的是我无法用现有的答案解决问题。我希望你不介意帮助我。 我有一个代码,我试图在迭代代码时将值附加到列表中。但是,在这个过程中出了问题,我最终得到了值0。 我不是很擅长调试,所以我找不到问题。 我在下面用较少的变量创建了一个更简单的代码版本:

    `import math
    from pylab import *
    import numpy as np

    #---------------------------INITIALIZATION-----------------------

    #initializing the parameters of the model
    Dt = 1
    time_step = 50

    #initializing the connection weights
    w1 = 1

    #initializing parameter values for the alogistic function

    steepness_SS_a=1

    speed_SS_a=1

    threshold_SS_a=1

    #--------------------THE FUNCTIONS---------------------------

    def initialize():
        global t, timesteps, WS_a, SS_a, new_WS_a, new_SS_a
        #initialize model states

        WS_a=1
        SS_a=0


        # initialize lists to update all states

        new_SS_a=np.array([SS_a])


        #initializing time list
        t=0.
        timesteps=[t]

    def observe():
        global t, timesteps, WS_a, SS_a, new_WS_a, new_SS_a



        np.append(SS_a, new_SS_a)


        timesteps.append(t)

    def update():
        global t, timesteps, WS_a, SS_a, new_WS_a, new_SS_a

        # for each state activation value, write how to update it in the form of new_WS(a)=function(WS(a))

        SS_a = SS_a + speed_SS_a * (((1/(1+math.exp(-steepness_SS_a * (w1 *         WS_a - threshold_SS_a))))-(1/(1+math.exp(steepness_SS_a *threshold_SS_a))))*        (1+math.exp(-steepness_SS_a*threshold_SS_a))-SS_a)

         # for each state activation value now move the state value to new value

        SS_a=new_SS_a 


        #updating the timestep
        t = t + Dt 

    #--------------------THE PROGRAM-----------------

    initialize()

    while t<30.:
        update()
        observe()

    #--------------------PLOTTING--------------------
    print SS_a
    plot(new_SS_a)

    show()`

1 个答案:

答案 0 :(得分:0)

怎么样:

import math
import numpy as np
from pylab import plot, show

def model():
    Dt = 1
    time_step = 50

    #initializing the connection weights
    w1 = 1

    #initializing parameter values for the alogistic function
    steepness_SS_a=1
    speed_SS_a=1
    threshold_SS_a=1

    #initialize model states
    WS_a=1
    SS_a=0
    # initialize lists to update all states
    new_SS_a=np.array([SS_a])
    #initializing time list
    t=0.0
    timesteps=[t]    

    while t<30.0:
        ## update()
        # for each state activation value, write how to update it in the form of new_WS(a)=function(WS(a))
        new = SS_a + speed_SS_a * (
                    (
                        (1/(1+math.exp(-steepness_SS_a * (w1 * WS_a - threshold_SS_a))))
                        -
                        (1/(1+math.exp(steepness_SS_a *threshold_SS_a)))
                    )
                    *        
                    (1+math.exp(-steepness_SS_a*threshold_SS_a))
                    -
                    SS_a
                )
        new_SS_a = np.append(new_SS_a,new)
        # for each state activation value now move the state value to new value
        SS_a=new 
        #updating the timestep
        t = t + Dt     

        ##observe()
        #np.append(SS_a, new_SS_a)
        timesteps.append(t)

    print( SS_a )
    print( new_SS_a )
    print( timesteps )
    plot(new_SS_a)
    show()        

model()   

市长更改位于SS_a = SS_a + speed_SS_a *...我首先将该值保存为new = SS_a + speed_SS_a *...,然后我将该值添加到new_SS_a new_SS_a = np.append(new_SS_a,new)(您需要捕获此结果) ,否则你就失去了它,并覆盖SS_a=new

中的旧旧的

另一件事是我把所有这些放在一起是因为以这种方式使用全局变量是一种不好的做法,全局应该用作常量而不是修改,如果你需要将它更改为函数结果,然后使该函数返回该值,然后更改为全局常量。另外import *是另一个不好的做法,因为用不需要的东西污染你的命名空间,在某些情况下额外的东西可以在你不知道的情况下覆盖你需要的其他东西,更糟糕的是你使用多个那些因为调试更难如果你不知道或不记得,每个东西强迫你进行额外查找以确定哪些东西属于每个库,所以总是使用显式导入

相关问题