我有以下问题,我解决不了:我在更新使用两个变量for循环,我从其他减去一个计算值当我在我的代码中的不同点进行计算时不同,即使两个变量之间没有变化。计算错误在我的代码做在不同的点计算时
特别,我计算值co[t] = max([0, est[t][ch] - est[t][sug] + eps])
,前一次为我设置est[t+1]
价值观和后一次。下面是一个示例输出,其中问题可以看出:
494: Accepted
494: co[t] pre = 2.11863208054
494: co[t] post = 1.69490566443
494: est[t][ch] = 2.49012790412
494: est[t][sug[t]] = 0.805222239686
显然,值co[t] post
是正确的,而co[t] pre
不正确。下面是用来产生该输出的代码:
sug = [0 for i in time]
co = [0 for i in time]
est = [[0 for i in op] for j in time]
eps = 0.01
alph = 0.2
for t in time:
ch = random.choice(op)
sug[t] = random.choice(op)
co[t] = max([0, est[t][ch] - est[t][sug[t]] + eps])
if t < T-1:
est[t+1] = est[t]
if ac[ch] >= ac[sug[t]] + co[t]:
print '%s: Declined' % t
est[t+1][ch] += alph*co[t]
elif ac[ch] < ac[sug[t]] + co[t]:
print '%s Accepted' % t
est[t+1][ch] -= alph*co[t]
else:
break
print '%s: co[t] pre = %s' % (t, co[t])
print '%s: co[t] post = %s' % (t, max([0, est[t][ch] - est[t][sug[t]] + eps]))
print '%s: est[t][ch] = %s' % (t, est[t][ch])
print '%s: est[t][sug[t]] = %s' % (t, est[t][sug[t]])
如可以看到的,co[t] pre
被计算之前if t < T-1
-clause,而co[t] post
之后计算的。请注意,if-clause中不会更改est[t][ch]
和est[t][sug]
。
我无法解释为什么会发生这种情况!
我道歉,如果这个问题已经被问过。在寻找它,我只发现其中大量的浮点数引起的问题的问题,但我不认为这是与此有关,因为我使用的是完全相同的变量(est[t][ch]
和est[t][sug]
)两次我计算co[t]
。
任何帮助非常感谢!提前致谢。
编辑:道歉,代码的缺失部分是:
import random
op = [i for i in range(4)]
ac = [3, 2, 1, 0]
T = 500
time = range(T)
代码中'time'和'op'的值是多少? –
...和'T' ..... – glibdud
时间设置为'时间=范围(T)',其中'T = 500' – Anwa