for循环只执行一次迭代python



嗨,伙计们,我正在运行一个程序,应该为'max_iter'时间运行,其中嵌套循环将为'mv_max'时间运行,稍后有一个for循环检查变量'check'是否为True,它将增加'mv'计数器直到'mv_max'。我的问题是for循环只执行一次,并退出到算法的其他部分(不包括在这里)如下图所示,我不知道问题的来源,它可能只是一个愚蠢的错误,请帮助我!图片链接:https://drive.google.com/file/d/1bbTmLaFHZZlistVMyt6en4WPA3vVtMa2/view?usp=sharing

mv = 1
tabu_list = []
for i in range(1, max_iter + 1):
move_history = tabu_list.copy()
while mv <= mv_max:
prohibited = []
print("-----------------------------------------------------------")
print('[LS] -> generating move number', mv, 'for neighbor', i)
check = False
while not check:
move = genrationDesMouvment(sequence, move_type=mv_type)
if move not in move_history and move not in prohibited:
seq = applymove(sequence, move, move_type=mv_type)
check = isfeasable(seq, bound, data_matrix, vehicle_capacity, demand_data, operation_data)
print('check', check, ' ls move is:', move, 'the tabu list is', move_history)
if mv_type == 'relocation': move = [
(move[0][0], move[0][2])] 
if not check: seq = applymove(seq, move, move_type=mv_type)
prohibited.append(move.copy())
else:
continue
else:
print('LS move succeeded', check, move)
sequence = seq.copy()
move_history += move.copy()
mv += 1
tabu_list += move_history.copy()
tabu_list = check_tenure(tabu_list, tenure)

这是一个愚蠢的错误,我忘记重置计数器'mv'这里是正确的代码:

for i in range(1, neighbors + 1):
mv=1
move_history = tabu_list.copy()
while mv <= mv_max:
prohibited = []
print("-----------------------------------------------------------")
print('[LS] -> generating move number', mv, 'for neighbor', i)
check = False
while not check:
move = genrationDesMouvment(sequence, move_type=mv_type)
if move not in move_history and move not in prohibited:
seq = applymove(sequence, move, move_type=mv_type)
check = isfeasable(seq, bound, data_matrix, vehicle_capacity, demand_data, operation_data)
print('check', check, ' ls move is:', move, 'the tabu list is', move_history)
if mv_type == 'relocation': move = [
(move[0][0], move[0][2])]  # in relocation, we need to apply anti-
# -move which we need to construct here because the returned value of generated move returns a
# special form which contains at the 3rd position the original index of the moved node
if not check: seq = applymove(seq, move, move_type=mv_type)
prohibited.append(move.copy())
else:
continue
else:
print('LS move succeeded', check, move)
sequence = seq.copy()
move_history += move.copy()
mv += 1
tabu_list += move_history.copy()
tabu_list = check_tenure(tabu_list, tenure)

最新更新