Ejemplo n.º 1
0
def BruteForce(locations):
    start_time = time.time()
    # Gets all the path combinations
    p = permutations(locations)
    p = list(p)
    smallestIndex = 0
    smallestDistance = 999999

    counter = 0
    # Go through all the paths
    for x in p:
        x = list(x)
        x.append(x[0])
        if counter % 1000 == 0:
            print("%.2f%% complete" % (counter / len(p) * 100), end='\r')
        # gets the path distance
        tempDistance = ListFunctions.TotalDistance(x)
        # if this path is shorter than previous best, remember this
        if smallestDistance > tempDistance:
            smallestDistance = tempDistance
            smallestIndex = counter
        counter += 1

    path = []
    for x in p[smallestIndex]:
        path.append(x)
    path.append(p[smallestIndex][0])

    # prints smallest distance found
    print(path, " is the path, the distance is ", smallestDistance)
    print("This process took %.4f seconds to complete" %
          int(time.time() - start_time))

    V.DisplayRoute(path)