如何按条件重新启动循环

2024-06-01 01:52:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码是:


for i in [data1, data2]:
    pbest_A = i.iloc[:, 0]
    pbest_B = i.iloc[:, 3]

    gbest_score_cycle = i['score'].max()  # max score in cycle
    gbest_score = np.where(gbest_score_cycle > gbest_score, gbest_score_cycle, gbest_score)  # update gbest score
    if gbest_score == gbest_score_cycle:  # row of gbest 
        gbest = i.loc[i['score'].idxmax()] 

    gbest_A = gbest[0]
    gbest_B = gbest[3]

    save_A = []
    save_B = []

    for j in range(5):
        R1 = random.uniform(0,1)
        R2 = random.uniform(0,1)

        Xid_A = New_Xid_A
        Xid_B = New_Xid_B
        Vid_A = New_Vid_A
        Vid_B = New_Vid_B

        New_Vid_A = w*Vid_A + c1*R1*(pbest_A[i]- Xid_A) + c2*R2*(gbest_A - Xid_A)
        New_Vid_B = w*Vid_B + c1*R1*(pbest_B[i] - Xid_B) + c2*R2*(gbest_B - Xid_B)
        New_Xid_A= Xid_A + New_Vid_A
        New_Xid_B= Xid_B + New_Vid_B
        # get result: New_Xid_A, New_Xid_B
        # *** if New_Xid_A > 10 or New_Xid_B > 20, restart this loop (same j in [for j in range(5)])

        save_A.append(New_Xid_A)
        save_B.append(New_Xid_B)

    print(save_A)
    print(save_B)

我一直在寻找这种问题。我正在考虑如何在for循环中使用while。因为现在我的问题是当条件符合时,再重新启动循环(不要附加到save\u A和save\u B)。有什么办法吗?你知道吗


Tags: innewforifsavemaxscorer2
2条回答

您可以使用两个while循环,第一个while循环是一个无限循环,只有在内部while循环完成后,它才会中断并移动到for循环中的下一个元素。你知道吗

代码如下

for i in [data1, data2]:

    #Outer while loop runs till inner while loop is finished
    while True:
        j = 0
        #Inner while loop
        while j < 5:
            #If condition is met, reset inner counter and break inner while loop
            if New_Xid_A > 10 or New_Xid_B > 20:
                j = 0
                break
            j += 1

        #If inner while loop is successful, break out of infinite loop
        if j == 5:
            break

“关闭”功能+“异常”方法(pythonic):

def outer():
    # ... all the needed variables

    for i in [data1, data2]:
        pbest_A = i.iloc[:, 0]
        pbest_B = i.iloc[:, 3]

        gbest_score_cycle = i['score'].max()  # max score in cycle
        gbest_score = np.where(gbest_score_cycle > gbest_score, gbest_score_cycle, gbest_score)  # update gbest score
        if gbest_score == gbest_score_cycle:  # row of gbest 
            gbest = i.loc[i['score'].idxmax()]

        gbest_A = gbest[0]
        gbest_B = gbest[3]

        save_A, save_B = [], []

        def inner():
            try:
                for j in range(5):
                    R1 = random.uniform(0, 1)
                    R2 = random.uniform(0, 1)

                    Xid_A = New_Xid_A
                    Xid_B = New_Xid_B
                    Vid_A = New_Vid_A
                    Vid_B = New_Vid_B

                    New_Vid_A = w * Vid_A + c1 * R1 * (pbest_A[i] - Xid_A) + c2 * R2 * (gbest_A - Xid_A)
                    New_Vid_B = w * Vid_B + c1 * R1 * (pbest_B[i] - Xid_B) + c2 * R2 * (gbest_B - Xid_B)
                    New_Xid_A = Xid_A + New_Vid_A
                    New_Xid_B = Xid_B + New_Vid_B

                    # get result: New_Xid_A, New_Xid_B
                    if New_Xid_A > 10 or New_Xid_B > 20:
                        raise ValueError    # terminate current function call

                    save_A.append(New_Xid_A)
                    save_B.append(New_Xid_B)

            except ValueError:
                inner()     # back to the same function with a new call

        inner()

        print(save_A)
        print(save_B)

outer()

相关问题 更多 >