Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 这本词典的理解与这本“词典”有什么不同;至于;环_Python_Python 3.x_For Loop_Dictionary Comprehension - Fatal编程技术网

Python 这本词典的理解与这本“词典”有什么不同;至于;环

Python 这本词典的理解与这本“词典”有什么不同;至于;环,python,python-3.x,for-loop,dictionary-comprehension,Python,Python 3.x,For Loop,Dictionary Comprehension,我曾经尝试过用字典理解和for循环实现一个算法,我相信这两种方法都能获得相同的结果 词典理解 for循环 当我的算法运行时,它们会得到非常不同的结果。有人能告诉我两者的区别在哪里吗 细节 完整的算法如下所示 # parameters gamma = 0.9 # discount for MDP num_iter = 100 # maximum iterations, excluding initialization min_difference = 0.001 # stop VI if n

我曾经尝试过用字典理解和for循环实现一个算法,我相信这两种方法都能获得相同的结果

词典理解 for循环 当我的算法运行时,它们会得到非常不同的结果。有人能告诉我两者的区别在哪里吗

细节 完整的算法如下所示

# parameters
gamma = 0.9  # discount for MDP
num_iter = 100  # maximum iterations, excluding initialization
min_difference = 0.001  # stop VI if new values are this close to old values (or closer)

# initialize V(s)
state_values = {s: 0 for s in mdp.get_all_states()}

for i in range(num_iter):
    new_state_values = {s: get_new_state_value(mdp, state_values, s, gamma) for s in mdp.get_all_states()}

    # Compute difference
    diff = max(abs(new_state_values[s] - state_values[s]) for s in mdp.get_all_states())
    state_values = new_state_values

    if diff < min_difference:
        print("Terminated")
        break
#参数
gamma=0.9#MDP折扣
num_iter=100#最大迭代次数,不包括初始化
最小差值=0.001#如果新值与旧值如此接近(或更接近),则停止VI
#初始化V(s)
state_value={s:0表示mdp中的s.get_all_states()}
对于范围内的i(数值):
new_state_values={s:get_new_state_value(mdp,state_values,s,gamma)用于mdp中的s。get_all_states()}
#计算差分
diff=max(mdp中s的abs(新状态值[s]-状态值[s])。获取所有状态()
状态值=新的状态值
如果差值<最小差值:
打印(“终止”)
打破
“for-loop”版本几乎不会运行任何迭代,而dictionary-comprehension则会运行更多的迭代


更新:上述代码可以工作并聚合(我认为这是最具python风格的,依我看)。公认的答案提供了对不同方法的深入了解

非理解版本累积值,而不丢弃以前运行的外部循环中的值。如果您希望它是等效的,则需要更改:

for i in range(num_iter):
    for s in mdp.get_all_states():
        new_state_values[s] = get_new_state_value(mdp, state_values, s, gamma)
致:

新状态值重新初始化为干净的
dict


在您的完整代码中,非理解解决方案会将
状态值
新状态值
作为同一
dict
的别名(因此
状态值
在您使用时会发生变化),使问题变得更糟;
dict
理解通过构建一个新的
dict
来修复它,而无需在构建过程中修改
state\u值。

你确定
mdp.get\u all\u states()
是两个循环的相同迭代器吗?我建议在简化的示例中删除
for
循环,看看发生了什么@我有一些断言测试(由本练习的创建者提供)来测试所有底层函数的操作。所有函数都会传递断言,直到运行完整的算法为止。我认为这个问题与词典是如何创建的有关,但也许我忽略了什么;我意识到我在这里发帖时犯了一个错误。在试图清除任何无关的注释时,我省略了一行代码,在外部
for
循环开始之前初始化
state\u值。我现在已经更新了。不幸的是,进行这些更改仍然会导致相同的行为(这可能是因为我在之前的帖子中遗漏了一行代码)。@Hanzy:即使在编辑之后,在非
dict
理解的情况下,
new\u state\u值每次都没有显式初始化为空的
dict
,当您执行
state\u values=new\u state\u values
时,这两个值都是相同的
dict
,如果您在下一个循环中不将
new\u state\u values
重新绑定到新的
dict
,它们将无限期保留别名。第一次进入循环时,它们是不同的,但在循环2和以后的循环中,它们并没有保持不同。关键是,你的错误无关紧要,重要的是
state\u values=new\u state\u values
以及
new\u state\u values
是否反弹。对不起,我之前误解了你的意思。您是正确的,这确实会导致两个方法具有相同的输出。感谢您的耐心,此解决方案非常有用。我还是不能让它收敛。。。但是有一个问题!
# parameters
gamma = 0.9  # discount for MDP
num_iter = 100  # maximum iterations, excluding initialization
min_difference = 0.001  # stop VI if new values are this close to old values (or closer)

# initialize V(s)
state_values = {s: 0 for s in mdp.get_all_states()}

for i in range(num_iter):
    new_state_values = {s: get_new_state_value(mdp, state_values, s, gamma) for s in mdp.get_all_states()}

    # Compute difference
    diff = max(abs(new_state_values[s] - state_values[s]) for s in mdp.get_all_states())
    state_values = new_state_values

    if diff < min_difference:
        print("Terminated")
        break
for i in range(num_iter):
    for s in mdp.get_all_states():
        new_state_values[s] = get_new_state_value(mdp, state_values, s, gamma)
for i in range(num_iter):
    new_state_values = {}  # NEW!!!
    for s in mdp.get_all_states():
        new_state_values[s] = get_new_state_value(mdp, state_values, s, gamma)