我的代码是:
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)。有什么办法吗?你知道吗
您可以使用两个while循环,第一个while循环是一个无限循环,只有在内部while循环完成后,它才会中断并移动到for循环中的下一个元素。你知道吗
代码如下
“关闭”功能+“异常”方法(pythonic):
相关问题 更多 >
编程相关推荐