0

Thanks in advance.

So I am trying to make a recursive traveling salesman solution in Python as I need the shortest path from point 'a' to point 'b', and I want a general solution for any number of 'towns' in between a and b.

My problem comes about because I want two variables to be the same, but because of scoping they are not. I am not that familiar with how scoping works although I know what it means to have different scopes.

The actual code is quite long and interconnected, so here is the simplified version(I have left out exactly how I find values for 'path', 'cure_dist' and a few other variables):

def tsp_recursive(count, minim, path):    # line 1
    if count == 0:
        path = [1, 2, 3, 4]            # there is code to evaluate path; line 3
        curr_dist = 0                  # there is code to evaluate curr_dist
        if curr_dist < minim:          # this is an unimportant statement; line 5
            minim = curr_dist          # line 6. minim should be the same as on lines 1, 5 and 11
            path = ["Some other list"] # line 7. path should be the same as on lines 1, 3 and 11
        return
    else:
        for i in range(5):
            tsp_recursive(count - 1, minim, path) # line 11

The problem is that the minim and path variable on lines 1, 3, 5, 6, 7 and 11 should all be the same, but my PyCharm says that 'Local variable is not used' on lines 6 and 7

Thank you so much.

(in case it is needed, here is the full code)

def dist_adder(array, indexes):
    total = 0
    for i in range(len(indexes)-1):
        total += array[indexes[i]][indexes[i+1]]
    return total

def tsp_recursive(count, distances, start, finish, indexes, minim, path):
    """
    :param count:       int  | number of times to for-loop, this is len(distances-2)
    :param distances:   [[]] | list of lists of distances from a to b, i.e:
    distances = [[-1, 3, 5, 40],
                 [3, -1, 2, 6],
                 [5, 2, -1, 1],
                 [40, 6, 1, -1]]
    :param start:       int  | index of the starting point in distances
    :param finish:      int  | index of the finishing point in distances
    :param indexes:     []   | list of the indexes of all the for loops; len(indexes) == len(distances) and for i in indexes: i = -1 at start
    :param minim:       int  | the minimum path length, starts out at 'infinity'
    :param path:        list | a list of the shortest path, in order, by the number of the different points
    :return:
    """
    assert isinstance(count, int)
    assert isinstance(distances, list)
    assert isinstance(indexes, list)
    if count == 0:
        path = [start, finish]
        path[1:1] = indexes
        curr_dist = dist_adder(distances, path)

        if curr_dist < minim:
            minim = curr_dist           # this line
            path = indexes.copy()       # and this line are where the problem is
        return
    else:
        for i in range(len(distances)):
            if i not in indexes and i != start and i != finish:
                indexes[len(indexes) - count] = i
                tsp_recursive(count - 1, distances, start, finish, indexes, minim, path)
Beyarkay
  • 25
  • 1
  • 10
  • 1
    Variables are local to a specific function *call*, not a specific *function*, and you can't pass variables around. You can only pass objects, which variables refer to. – user2357112 Dec 15 '15 at 18:04
  • PyCharm is saying that because after you define `minim` and `path` in lines 6 and 7 (you are overwriting the variables, so you are defining them again) you `return` nothing (`None`) and they go nowhere, they were not used, hence that message. Just for illustration, if you `return` them, that message will go away. – iled Dec 15 '15 at 18:11

0 Answers0