大家好,我正在运行一个程序,该程序应该运行“max_iter”次,其中嵌套循环将运行“mv_max”次,稍后有一个 for loop 检查变量“check”是否为 True 它会增加“mv”计数器直到“mv_max”。我的问题是 for loop 仅执行一次并退出到算法的其他部分(此处未包括),如下图所示,我不知道问题的根源,这可能只是一个愚蠢的问题错误请帮帮我!图片链接: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)
回答1
这是一个愚蠢的错误,我忘了重置计数器“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)