For 循环写入数组中前一个条目的一部分,但不是全部

For loop writing over part, but not all, of previous entry in array

我正在编写一个非常简单的随机游走函数。整个代码如下。当当前成本函数小于前一个时,我使用数组来跟踪参数值。但出于某种原因,跟踪输出的数组正在重写?以前的条目。我认为这与将参数附加为 "c" 有关,因此存在内存分配问题,因为附加了 "c" 而不是 c 的值,但我不知道如何解决此问题,也不知道为什么它会与成本函数值 psi 不同,它按预期附加和维护其值。

我正在尝试创建如下内容:

input: 
for n in N: 
if cost < previous cost: keep current parameters
write current parameters & cost to tracking list

output:
n = 0, parameters = [1,2,3], cost = 4.5, track = [([1,2,3], 4.5)]
n = 1, parameters = [1.1,2.4,2.7], cost = 3.9, track = [([1,2,3], 4.5), ([1.1, 2.4, 2.7], 3.9)]
etc

相反,我得到的是

output:
n = 0, parameters = [1,2,3], cost = 4.5, track = [([1,2,3], 4.5)]
n = 1, parameters = [1.1,2.4,2.7], cost = 3.9, track = [([1.1,2.4,2.7], 4.5), ([1.1, 2.4, 2.7], 3.9)]

所以它正在替换参数,但保持以前的成本。当跟踪功能也在成功(较低的成本)if 语句之外时,就会发生这种情况。

帮助?

#pure random walk where all c parameters are randomly updated at once; no restrictions on pos/neg of parameters
del track
del bigtrack

c = [5,5,-5]
cp = c
kB = -8

M = 10
N = 5000
bigtrack = []

psip = cost(LN, LB, dldtN, dldtB, c[2], kB, c[0], c[1]) #cost using initialized parameters

for m in range(M):

    track = []
    for n in range(N):

        for i in range(len(c)):
            rand = np.random.uniform(-1,1)
            c[i] = c[i] + rand
            #print(c[i])
        #print("parameters = ", c)

        psi = cost(LN, LB, dldtN, dldtB, c[2], kB, c[0], c[1])
        #print("new cost = ", psi)

        if psi < psip:
            cp = c
            resp = res
            psip = psi
            track.append((c, psi))
            print("tracking function: \n", track)
        else:
            c = cp #keep prior 
            res = resp
            psi = psip

        if psi <16:
            print("VICTORY")
            break


    #print(track)
    #if track != []:
    bigtrack.append((track, m))

这是一个示例输出

tracking function: 
 [([4.145180382591114, 5.194803875207629, -5.77417154313107], 457.37070071446328)]
tracking function: 
 [([4.366620027701061, 5.610886161756634, -6.300451985366614], 457.37070071446328), ([4.366620027701061, 5.610886161756634, -6.300451985366614], 406.27520117896)]
tracking function: 
 [([4.294900222966394, 6.104004008230176, -6.636360393709489], 457.37070071446328), ([4.294900222966394, 6.104004008230176, -6.636360393709489], 406.27520117896), ([4.294900222966394, 6.104004008230176, -6.636360393709489], 377.1702411076343)]

您将相同的 list 插入到结果中,同时在两次插入之间对其进行修改。实际上,您正在执行以下操作:

>>> track = []
>>> c = [1, 2, 3]
>>> track.append(c)
>>> track
[[1, 2, 3]]
>>> c[1] += 2
>>> track.append(c)
>>> track
[[1, 4, 3], [1, 4, 3]]

为了解决这个问题,您可以在每次将列表附加到结果时复制该列表:

track.append((c[:], psi)) # instead of track.append((c, psi))