Python算法,不返回它应该返回的内容

时间:2016-10-25 20:28:59

标签: python algorithm return

python中的TSP随机迭代算法

我太蟒蛇了。我随身携带它。我现在所遇到的问题可能是一个非常武断的问题;但我已经放弃了。当从我的所有迭代路径中返回“最佳”时,它返回最后发布的路径。我不明白为什么会这样做。 if语句应该在while语句结束之前对最佳路径和开销进行排序。

算法为我的矩阵创建随机不同的路径,然后计算每个游览的成本,如果所述游览比上一次游览便宜,则新游览也设置为最佳游览。

但它没有。

from random import randint
from random import shuffle
from time import *

class main():

def calc(self, path, matrix, w):

    pathen = path
    matrix = matrix
    nCost = 0

    for x in range(int(w)):
        if pathen[x] == 0:
            first = x
            prev = x

    for x in range(int(w)):
        for y in range(int(w)):
            if pathen[y] == x:
                next = y

        nCost += matrix[prev][next]
        prev = next

    nCost += matrix[prev][first]

    return nCost

def ranIterative(self, matrix, w, stop):
    best = 9999
    path = [0 for x in range(int(w))]
    bestPath = path

    print(path)

    while stop:

        cost = 0

        for x in range(int(w)):
            path[x] = x

        shuffle(path)

        print(path)

        for x in range(int(w)):
            if path[x] == 0:
                first = x
                prev = x

        for x in range(1, int(w)):
            for y in range(int(w)):
                if path[y] == x:
                    next = y

            cost += matrix[prev][next]

            prev = next

        cost += matrix[prev][first]

        print("Cost: " + str(cost))
        print("Bst: " + str(best))

        if cost < best:
            best = cost
            bestPath = path
            bestest = bestPath
            print(path)
            print("Best path1: " + str(bestPath))

        stop -= 1
        print("Best path2: " + str(bestPath))

    print("Best path3: " + str(bestPath))
    print("Best path3: " + str(bestest))
    return bestest


def matrix(self, w):
    matrix = [[0 for x in range(int(w))] for y in range(int(w))]

    for x in range(int(w)):
        for y in range(int(w)):
            if y < x:
                matrix[x][y] = randint(1, 9)
                matrix[y][x] = matrix[x][y]
    return matrix


main = main()
print("How many cities?")
w = input()
matrix = main.matrix(int(w))
print("How many iterations?")
stop = input()
path1 = main.ranIterative(matrix, int(w), int(stop))
cost = main.calc(path1, matrix, int(w))
print("With " + str(stop) + " iterations on " + str(w) + " cities gives:")
print("Best path :" + str(path1))
print("Costs: " + str(cost))

输出:

How many cities?
5
How many iterations?
10
[0, 0, 0, 0, 0]
[0, 1, 3, 4, 2]
Cost: 30
Bst: 9999
[0, 1, 3, 4, 2]
Best path1: [0, 1, 3, 4, 2]
Best path2: [0, 1, 3, 4, 2]
[0, 3, 4, 2, 1]
Cost: 28
Bst: 30
[0, 3, 4, 2, 1]
Best path1: [0, 3, 4, 2, 1]
Best path2: [0, 3, 4, 2, 1]
[4, 3, 0, 1, 2]
Cost: 29
Bst: 28
Best path2: [4, 3, 0, 1, 2]
[3, 1, 4, 0, 2]
Cost: 25
Bst: 28
[3, 1, 4, 0, 2]
Best path1: [3, 1, 4, 0, 2]
Best path2: [3, 1, 4, 0, 2]
[0, 4, 3, 1, 2]
Cost: 33
Bst: 25
Best path2: [0, 4, 3, 1, 2]
[2, 0, 1, 3, 4]
Cost: 26
Bst: 25
Best path2: [2, 0, 1, 3, 4]
[3, 0, 2, 4, 1]
Cost: 28
Bst: 25
Best path2: [3, 0, 2, 4, 1]
[2, 3, 0, 4, 1]
Cost: 32
Bst: 25
Best path2: [2, 3, 0, 4, 1]
[0, 1, 3, 2, 4]
Cost: 32
Bst: 25
Best path2: [0, 1, 3, 2, 4]
[2, 1, 4, 0, 3]
Cost: 32
Bst: 25
Best path2: [2, 1, 4, 0, 3]
Best path3: [2, 1, 4, 0, 3]
Best path3: [2, 1, 4, 0, 3]
With 10 iterations on 5 cities gives:
Best path :[2, 1, 4, 0, 3]
Costs: 32

如你所见,我的结果对于这么简单的任务来说是非常错误的。 我似乎无法找出解决这个问题的方法。

1 个答案:

答案 0 :(得分:0)

你必须在 mutable listdictset,...)和什么不是&#之间做出改变39; t(intfloatstrtuple,...)

由于pathlist所以它是 mutable ,所以经典错误是这样的:

bestPath = path

认为您将最佳路径(并将其冻结)存储到bestPath变量,但您只是获得对path的另一个引用数组。 path上的后续更改反映在bestPath上,您始终会获得最后计算的值。

(这适用于像整数,浮点数,字符串这样的值,因为它们是由不可变的类型存储的:修改原始文件不会修改副本)< / p>

通过创建副本来修复它,例如:

bestPath = list(path)