简体   繁体   English

for循环只执行一次迭代python

[英]for loop executing only one iteration python

Hi guys I'm running a program that is supposed to run for 'max_iter' times in which a nested loop will run for 'mv_max' times in this later there is a for loop that checks if variable 'check' is True it will increase 'mv' counter until 'mv_max'.大家好,我正在运行一个应该运行“max_iter”次的程序,其中嵌套循环将运行“mv_max”次,稍后有一个for循环检查变量“check”是否为真,它会增加'mv' 计数器直到 'mv_max'。 My problem is the for loop is executing for just one time and exits to other parts of the algorithm (not included here) as show in the image below, I don't know the source of the problem and it might be just a silly error Please help me!我的问题是 for 循环仅执行一次并退出到算法的其他部分(此处未包括),如下图所示,我不知道问题的根源,这可能只是一个愚蠢的错误请帮我! link for the image: https://drive.google.com/file/d/1bbTmLaFHZZlistVMyt6en4WPA3vVtMa2/view?usp=sharing图片链接: 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)

It was a silly error I forgot to reset the counter 'mv' here is the correct code:这是一个愚蠢的错误,我忘了重置计数器“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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM