gpt4 book ai didi

python-2.7 - 使用 random.choice 列出索引超出范围错误

转载 作者:行者123 更新时间:2023-12-04 17:49:14 25 4
gpt4 key购买 nike

当我运行我的程序时,我收到以下错误,其中定义了下面的函数。我认为这是

valid_actions = filter(lambda x: x != random.choice(maxQactions)

导致错误的部分。有没有人看到问题是什么,或建议如何解决它?谢谢。

错误:
choose_action
action = random.choice(valid_actions)
File "/Users/UserName/anaconda/lib/python2.7/random.py", line 275, in choice
return seq[int(self.random() * len(seq))] # raises IndexError if seq is empty
IndexError: list index out of range

代码:
def choose_action(self, state):


self.state = state
self.next_waypoint = self.planner.next_waypoint()

action_selections = self.Q[state]

maxQ = max(action_selections.items(), key=lambda x: x[1])[1]

maxQactions = []
for action, Q in self.Q[state].items():
if Q == maxQ:
maxQactions.append(action)


if self.learning:
choose_using_epsilon = random.random() < 1 - self.epsilon
if not choose_using_epsilon:
valid_actions = filter(lambda x: x != random.choice(maxQactions),
Environment.valid_actions)
action = random.choice(valid_actions)
else:
action = random.choice(maxQactions) #maxQaction
else:
action = random.choice(Environment.valid_actions)
return action

最佳答案

引用 https://docs.python.org/2/library/random.html

如果 seq 为空,random.choice(seq) 会引发 IndexError。在您的情况下,IndexError 发生在

'action = random.choice(valid_actions)'

我怀疑 valid_actions 是否为空。

关于python-2.7 - 使用 random.choice 列出索引超出范围错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46461335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com