Using while loop to repeat a step of loop to reach the proper answer

你说的曾经没有我的故事 提交于 2019-12-13 03:26:11

问题


I have the following code which I just write the final part of it. For some reasons we use random.normal values. As it is obvious the func function has three variables.

My question is: The steps of calculations is 101and exactly we have 100 values for each parameter in output. How should I tell Python, If you see Wo[i]>0 and Omo[i]<0 recalculate that step (which would use another random number) until we have the proper answer (Wo[i]<0 and Omo[i]>0.

We have to do this because, If we print outputs and then remove those values which do not satisfy our condition, we have for example, 60 values instead of 100. I know we can use while loop but I do not know how.

N=101
for i in range (1,N):
    R=np.random.uniform(0,1)

    Omn[i] = Omo[i-1] + 0.05 * np.random.normal()
    Wn[i] = Wo[i-1] + 0.05 * np.random.normal()
    Mn[i] = Mo[i-1] + 0.1 * np.random.normal()

    L = exp(-0.5 * ( func(Omn[i], Wn[i], Mn[i] ) - func( Omo[i-1], Wo[i-1], Mo[i-1] )))

    if L>R:
        Wo[i]=Wn[i]
        Mo[i]=Mn[i]
        Omo[i]=Omn[i]

    else:
        Wo[i]=Wo[i-1]
        Mo[i]=Mo[i-1]
        Omo[i]=Omo[i-1]

    print(Wo[i],Mo[i],Omo[i])

回答1:


You can use a normal while loop and check the condition after calculating, for example:

for i in range (1,N):
    R=np.random.uniform(0,1)
    while True:
        Omn[i] = Omo[i-1] + 0.05 * np.random.normal()
        Wn[i] = Wo[i-1] + 0.05 * np.random.normal()
        Mn[i] = Mo[i-1] + 0.1 * np.random.normal()
        if Wo[i] <= 0 and Omo[i] >= 0:
            break
    # rest of code



回答2:


Insert a while loop to repeat the calculations you require.

When L > R, Wo and Omo are updated with the Omn and Wn values. So if this values are Omn<0 or Wn>0 you need to re-calulate them. When L <= R, Wo and Omo are calculated from the previous iteration. Since the previous iteration already is Wo[i]<=0 and Omo[i]>=0 you don't need to repeat those calculations.

Thus, as shown in the code below, you only need to re-calculate the Omn and Wn variables:

Omo[0] = 0.24
Wo[0] = -0.2
Mo[0] = 1.0

N = 101
for i in range (1,N):
    is_valid = False
    if __debug__:
        print "Calculating position " + str(i)
    while (not is_valid):
        Omn[i] = Omo[i-1] + 0.05 * np.random.normal()
        Wn[i] = Wo[i-1] + 0.05 * np.random.normal()
        Mn[i] = Mo[i-1] + 0.1 * np.random.normal()

        if __debug__:
            print "- is_valid iteration values: " + str(Wn[i]) + " " + str(Omn[i])
            print "- is_valid previous values: " + str(Wo[i-1]) + " " + str(Omo[i-1])

        is_valid = Omn[i] >= 0 and Wn[i] <= 0 

    R = np.random.uniform(0,1)
    L = exp(-0.5 * ( func(Omn[i], Wn[i], Mn[i] ) - func( Omo[i-1], Wo[i-1], Mo[i-1] )))

    if L > R:
        Omo[i] = Omn[i]
        Wo[i] = Wn[i]
        Mo[i] = Mn[i]
    else:
        Omo[i] = Omo[i-1]
        Wo[i] = Wo[i-1]
        Mo[i] = Mo[i-1]

    print(Wo[i], Mo[i], Omo[i])


来源:https://stackoverflow.com/questions/49916212/using-while-loop-to-repeat-a-step-of-loop-to-reach-the-proper-answer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!