【问题标题】:q-learning: ValueError: 'a' cannot be empty unless no samples are takenq-learning: ValueError: 'a' 不能为空,除非没有采样
【发布时间】:2019-10-29 10:51:44
【问题描述】:

我尝试开发用于强化学习的 q-learning 算法,这是我的代码:

import numpy as np
R = np.matrix ([[-1, 0, -1, -1, 0, -1, -1, -1, -1], 
            [-1, -1, 100, 0, -1, -1, -1, -1, -1], 
            [-1, -1, 100, -1, -1, -1, -1, -1, -1],
            [-1, -1, -1, -1, -1, -1, -1, -1, -1], 
            [-1, -1, -1, -1, -1, 100, 0, -1, -1], 
            [-1, -1, -1, -1, -1, 100, -1, -1, -1], 
            [-1, -1, -1, -1, -1, -1, -1, 100, 0], 
            [-1, -1, -1, -1, -1, -1, -1, 100, -1],
            [-1, -1, -1, -1, -1, -1, -1, -1, -1]])
    # Q matrix
Q = np.matrix(np.zeros([9,9]))

# Gamma (learning parameter)
gamma = 0.4

# Initial state. (Usually to be chosen at random)
initial_state = 1

# This function returns all available actions in the state given as an argument
def available_actions(state):
    current_state_row = R[state,]
    av_act = np.where(current_state_row >= 0) [1]
    return av_act

# Get available actions in the current state
available_act = available_actions(initial_state)

# This function chooses at random which action to be performed within the range of all the available actions.
def sample_next_action(available_actions_range):
    next_action = int(np.random.choice(available_act, 1))
    return next_action

#sample next action to be performed
action = sample_next_action(available_act)

# This function updates the Q matrix according to the path selected and the Q learning algorithm
def update(current_state, action, gamma):
    max_index = np.where(Q[action,] == np.max(Q[action,]))[1]

    if max_index.shape[0] > 1:
        max_index = int(np.random.choice(max_index, size=1))
    else:
        max_index = int(max_index)
    max_value = Q[action, max_index]

    # Q learning formula
    Q[current_state, action] = R[current_state, action] + gamma * max_value

# Update Q matrix
update(initial_state, action, gamma)

# Training
# Train over 10000 iterations. (Re-iterate the process above)
for i in range(10000):
    current_state = np.random.randint(0, int(Q.shape[0]))
    available_act = available_actions(current_state)
    action = sample_next_action(available_act)
    update(current_state, action, gamma)

# Normalize the trained Q matrix
print ("Trained Q matrix:")
print (Q / np.max(Q) * 100)

# Testing 

# Goal state = 2

current_state = 1
steps = [current_state]

while current_state != 2:
    next_step_index = np.where(Q[current_state,] == np.max(Q[current_state,]))[1]

    if next_step_index.shape[0] > 1:
        next_step_index = int(np.random.choice(next_step_index, size=1))
    else:
        next_step_index = int(next_step_index)

    steps.append(next_step_index)
    current_state = next_step_index

# Print selected sequence of steps
print("Selected path:")
print(steps)

但我总是遇到这个我不明白的错误:

ValueError Traceback(最近调用 最后)在 46 current_state = np.random.randint(0, int(Q.shape[0])) 47 可用_act = 可用_actions(current_state) ---> 48 动作 = sample_next_action(available_act) 49 更新(当前状态,动作,伽玛) 50

在 sample_next_action(available_actions_range) 19 # 该函数在所有可用动作的范围内随机选择要执行的动作。 20 def sample_next_action(available_actions_range): ---> 21 next_action = int(np.random.choice(available_act, 1)) 22 返回下一个动作 23

mtrand.RandomState.choice()中的mtrand.pyx

ValueError: 'a' 不能为空,除非没有采样

请帮忙!

【问题讨论】:

  • 尝试打印available_act 好像它可能是空的。
  • 是的,这就是问题所在,而在矩阵中,当我处于状态 1 时,有两个可能的操作是 2 或 3
  • R = np.matrix ([[-1, -1, -1, -1, 0, -1], # [-1, -1, -1, 0, -1, 100], # [-1, -1, -1, 0, -1, -1], # [-1, 0, 0, -1, 0, -1], # [0, -1, -1 , 0, -1, 100], # [-1, 0, -1, -1, 0, 100]]) 并且当我使用这个矩阵时,我没有这个错误!!
  • 可能R[state,] 应该是R[state]。尝试打印current state row,看看是否符合您的预期。
  • 我不确定 R 的含义,这是奖励矩阵吗?什么是状态?一个索引?一维索引还是二维?

标签: python reinforcement-learning q-learning


【解决方案1】:

代码有不少缺陷:

  1. 将R和Q的数据结构改为:

    R = np.array... Q = np.zeros([9, 9])

  2. 更改状态 3 和状态 8 的 R 矩阵,以便至少有一个操作可用。因此,只需在这些行中添加一个大于零的值。

  3. 将 available_actions 定义更改为:

    def available_actions(state):
         current_state_row = R[state, :]
         av_act = np.where(current_state_row >= 0)[0]
    
  4. 更改第 39 行以正确索引

    max_index = np.where(Q[action,] == np.max(Q[action, :]))[0]

  5. 更改第 73 行以进行正确的索引

    next_step_index = np.where(Q[current_state,:] == np.max(Q[current_state,:]))[0]

通过这些更改,您应该能够获得价值。
最终结果是:

所选路径:[1, 2]

import numpy as np
R = np.array([[-1, 0, -1, -1, 0, -1, -1, -1, -1],
            [-1, -1, 100, 0, -1, -1, -1, -1, -1],
            [-1, -1, 100, -1, -1, -1, -1, -1, -1],
            [-1, -1, -1, -1, -1, 0, -1, -1, -1],
            [-1, -1, -1, -1, -1, 100, 0, -1, -1],
            [-1, -1, -1, -1, -1, 100, -1, -1, -1],
            [-1, -1, -1, -1, -1, -1, -1, 100, 0],
            [-1, -1, -1, -1, -1, -1, -1, 100, -1],
            [-1, -1, -1, -1, 0, -1, -1, -1, -1]])
    # Q matrix
Q = np.zeros([9,9])

# Gamma (learning parameter)
gamma = 0.4

# Initial state. (Usually to be chosen at random)
initial_state = 1

# This function returns all available actions in the state given as an argument
def available_actions(state):
    current_state_row = R[state, :]
    av_act = np.where(current_state_row >= 0)[0]
    return av_act

# Get available actions in the current state
available_act = available_actions(initial_state)

# This function chooses at random which action to be performed within the range of all the available actions.
def sample_next_action(available_actions_range):
    next_action = int(np.random.choice(available_act, 1))
    return next_action

#sample next action to be performed
action = sample_next_action(available_act)

# This function updates the Q matrix according to the path selected and the Q learning algorithm
def update(current_state, action, gamma):
    max_index = np.where(Q[action,] == np.max(Q[action, :]))[0]

    if max_index.shape[0] > 1:
        max_index = int(np.random.choice(max_index, size=1))
    else:
        max_index = int(max_index)
    max_value = Q[action, max_index]

    # Q learning formula
    Q[current_state, action] = R[current_state, action] + gamma * max_value

# Update Q matrix
update(initial_state, action, gamma)

# Training
# Train over 10000 iterations. (Re-iterate the process above)
for i in range(10000):
    current_state = np.random.randint(0, int(Q.shape[0]))
    available_act = available_actions(current_state)
    action = sample_next_action(available_act)
    update(current_state, action, gamma)

# Normalize the trained Q matrix
print ("Trained Q matrix:")
print (Q / np.max(Q) * 100)

# Testing

# Goal state = 2

current_state = 1
steps = [current_state]

while current_state != 2:
    next_step_index = np.where(Q[current_state,:] == np.max(Q[current_state,:]))[0]

    if next_step_index.shape[0] > 1:
        next_step_index = int(np.random.choice(next_step_index, size=1))
    else:
        next_step_index = int(next_step_index)

    steps.append(next_step_index)
    current_state = next_step_index

# Print selected sequence of steps
print("Selected path:")
print(steps)

【讨论】:

  • 是的,没问题
猜你喜欢
  • 1970-01-01
  • 2019-07-11
  • 1970-01-01
  • 2014-04-29
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多