我为具有4种可能状态和4种可能动作的电路板设置了一个简单的MDP。董事会和奖励设置如下所示:

python - 我尝试在mdptoolbox中使用值迭代算法时发生OverflowError-LMLPHP

在这里,S4是目标状态,S2是吸收状态。我在编写的代码中定义了转移概率矩阵和奖励矩阵,以获得该MDP的最佳值函数。但是在运行代码时,出现错误消息:OverflowError: cannot convert float infinity to integer。我不明白原因。

import mdptoolbox
import numpy as np

transitions = np.array([
    # action 1 (Right)
    [
        [0.1, 0.7, 0.1, 0.1],
        [0.3, 0.3, 0.3, 0.1],
        [0.1, 0.2, 0.2, 0.5],
        [0.1,  0.1,  0.1,  0.7]
    ],
    # action 2 (Down)
    [
        [0.1, 0.4, 0.4, 0.1],
        [0.3, 0.3, 0.3, 0.1],
        [0.4, 0.1, 0.4, 0.1],
        [0.1,  0.1,  0.1,  0.7]
    ],
    # action 3 (Left)
    [
        [0.4, 0.3, 0.2, 0.1],
        [0.2, 0.2, 0.4, 0.2],
        [0.5, 0.1, 0.3, 0.1],
        [0.1,  0.1,  0.1,  0.7]
    ],
    # action 4 (Top)
    [
        [0.1, 0.4, 0.4, 0.1],
        [0.3, 0.3, 0.3, 0.1],
        [0.4, 0.1, 0.4, 0.1],
        [0.1,  0.1,  0.1,  0.7]
    ]
])

rewards = np.array([
    [-1, -100, -1, 1],
    [-1, -100, -1, 1],
    [-1, -100, -1, 1],
    [1, 1, 1, 1]
])


vi = mdptoolbox.mdp.ValueIteration(transitions, rewards, discount=0.5)
vi.setVerbose()
vi.run()

print("Value function:")
print(vi.V)


print("Policy function")
print(vi.policy)


如果我将discount的值从1更改为0.5,则可以正常工作。值迭代不能与折扣值0.5或任何其他十进制值一起使用的原因可能是什么?

更新:我的奖励矩阵似乎有问题。我无法按照预期的方式编写它。因为如果我更改奖励矩阵中的某些值,该错误就会消失。

最佳答案

结果表明,我定义的奖励矩阵不正确。根据上图中定义的奖励矩阵,它应为documentation中给出的(S,A)类型,其中每一行对应于从S1S4的状态,每一列对应于动作从A1直到A4。新的奖励矩阵如下所示:

#(S,A)
rewards = np.array([
    [-1, -1, -1, -1],
    [-100, -100, -100, -100],
    [-1, -1, -1, -1],
    [1, 1, 1, 1]
])


它很好用。但是我仍然不确定,内部发生了什么导致溢出错误。

10-04 12:23