2

我目前正在使用 Q learning,并且我有一个字典 Q[state, action],其中每个状态都可以是任何东西,即字符串、数字、列表.. 取决于应用程序。每个状态有 3 或 4 个可能的动作。对于每个状态,我需要找到具有最高 Q 值的动作。问题是我不知道如何直接从具有两个键的字典中访问状态具有的所有可能操作,因此我尝试使用 for 循环:

for statex, actionx in self.array:
    if statex == state and (actionx != None):
         y[actionx] = self.array[statex, actionx]
y.argMax()

其中 argMax()

def argMax(self):
    """
    Returns the key with the highest value.
    """
    if len(self.keys()) == 0: return None
    all = self.items()
    values = [x[1] for x in all]
    maxIndex = values.index(max(values))
    return all[maxIndex][0]

问题是计算时间太长。有什么想法可以让它更快,可能是通过消除 for 循环?

4

1 回答 1

0

如果您使用字典字典会快得多:

    self.array[state][action]
于 2016-02-25T11:54:34.917 回答