【问题标题】:TypeError: 'NoneType' object is not iterable From beginner 2TypeError:'NoneType' 对象不可迭代从初学者 2
【发布时间】:2018-05-19 23:12:10
【问题描述】:

我有一个代码和一条错误消息说 [Q_table, pre_s, pre_a, s, a]= select_action(x,x_dot,theta,theta_dot,R, Q_table, pre_s, s, pre_a, a, alpha, beta, gamma) TypeError: 'NoneType' 对象不可迭代

基本上我试图在其他地方调用这个函数: 在我之前的帖子中,我在另一个代码上遇到了类似的问题,我的错误是缺少返回语句,但是现在,我有一个返回语句,我无法理解这个问题。请帮我。谢谢各位:)

[Q_table, pre_s, pre_a, s, a]= select_action(x,x_dot,theta,theta_dot,R, Q_table, pre_s, s, pre_a, a, alpha, beta, gamma)


def select_action(x,x_dot,theta,theta_dot,R,Q_table,pre_s,s,pre_a,a,alpha,beta,gamma):
pre_s = s
pre_a = a
s = select_box(x, x_dot, theta, theta_dot)


if (pre_a != -1):    # Update Q value. If previous action been taken
   if (s == -1):        # Current state is failed
      predicted_value = 0        # fail state's value is zero
   elif (Q_table[s, 0] <= Q_table[s, 1]):        # Left Q<= Right Q
        predicted_value = Q_table[s, 1]        #  set Q to bigger one
   else:
        predicted_value = Q_table[s, 1]

        Q_table[pre_s, pre_a] = Q_table[pre_s, pre_a] + alpha * (R + gamma * predicted_value - Q_table[pre_s, pre_a])

       # Determine best action
b=beta*random()
if ((Q_table[s, 0] + b <= Q_table[s, 1]).all()):
    a = 2            # push right
else:
    a = 1
    return [Q_table, pre_s, pre_a, s, a]

【问题讨论】:

  • 很难说,因为您发布的代码缩进不正确(def 之后的行应该缩进),但是如果您的 return 确实缩进到最后一个 else 下,那么您将返回None 如果if 为真。

标签: python-3.6


【解决方案1】:

清除两个's'。

[Q_table, pre_s, pre_a, s, a]= select_action(x,x_dot,theta,theta_dot,R, Q_table, pre_s, s, pre_a, a, alpha, beta, gamma)


def select_action(x,x_dot,theta,theta_dot,R,Q_table,pre_s,s,pre_a,a,alpha,beta,gamma):
    pre_s = s
    pre_a = a
    s_another = select_box(x, x_dot, theta, theta_dot)


    if (pre_a != -1):    # Update Q value. If previous action been taken
       if (s_another == -1):        # Current state is failed
          predicted_value = 0        # fail state's value is zero
       else:
          if (Q_table[s_another, 0] <= Q_table[s_another, 1]):        # Left Q<= Right Q
            predicted_value = Q_table[s_another, 1]        #  set Q to bigger one
          else:
            predicted_value = Q_table[s_another, 0]

            Q_table[pre_s, pre_a] = Q_table[pre_s, pre_a] + alpha * (R + gamma * predicted_value - Q_table[pre_s, pre_a])

        # Determine best action
        b=beta*random()
        if ((Q_table[s_another, 0] + b <= Q_table[s_another, 1]).all()):
            a = 2            # push right
        else:
            a = 1
    return [Q_table, pre_s, pre_a, s_another, a]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-25
    • 1970-01-01
    • 2017-08-29
    相关资源
    最近更新 更多