Home > Enterprise >  for loop executing only one iteration python
for loop executing only one iteration python

Time:05-24

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'. 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! link for the image: 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)

CodePudding user response:

It was a silly error I forgot to reset the counter 'mv' here is the correct code:

    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)
  • Related