0

我正在编写一个非常简单的随机游走函数。整个代码如下。当当前成本函数小于前一个时,我使用数组来跟踪参数值。但是由于某种原因,跟踪输出的数组正在重写?以前的条目。我认为这与将参数附加为“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)]
4

1 回答 1

0

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))
于 2017-01-06T02:42:30.480 回答