for循环只执行一次迭代python
for loop executing only one iteration python
大家好我正在 运行ning 一个程序,该程序应该 运行 'max_iter' 次,其中嵌套循环将 运行 'mv_max' ] 之后有一个 for 循环检查变量 'check' 是否为 True 它将增加 'mv' 计数器直到 'mv_max'。我的问题是 for 循环只执行一次并退出到算法的其他部分(此处未包括),如下图所示,我不知道问题的根源,它可能只是一个愚蠢的错误请帮我!
link 图片:
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)
大家好我正在 运行ning 一个程序,该程序应该 运行 'max_iter' 次,其中嵌套循环将 运行 'mv_max' ] 之后有一个 for 循环检查变量 'check' 是否为 True 它将增加 'mv' 计数器直到 'mv_max'。我的问题是 for 循环只执行一次并退出到算法的其他部分(此处未包括),如下图所示,我不知道问题的根源,它可能只是一个愚蠢的错误请帮我! link 图片: 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)