为 Q-Learning 构建可用操作矩阵
Build a matrix of available actions for Q-Learning
我正在模拟一家零售店的库存管理系统;因此,我有一个 (15,15) 零矩阵,其中状态是行和操作列:
Q = np.matrix(np.zeros([15, 15]) )
具体来说,0 是最小库存水平,14 是最大库存水平,状态是当前库存水平和操作库存订单(数量)。
因此,我想用“-1”代替零,其中状态和动作的总和 > 14:
print(final_Q)
#First row, from which I can order everything (since 0 + 14 == 14)
[[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
#Second row, from which I can order max. 13 products (1 + 14 > 14)
[[0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1]]
#Third row, from which the max is 12
[[0 0 0 0 0 0 0 0 0 0 0 0 0 -1 -1]]
(...)
我尝试手动实现它,但如何才能自动获得 final matrix?
# Q matrix
Q = np.matrix(np.zeros([15+1, 15+1] ))
# Create a diagonal of -1s
Q = Q[0:15][0:15]
il1 = np.tril_indices(15)
Q[il1] = -1
Q = np.rot90(Q)
# Adjust single values
Q[parameters["max_products"]-1][0, 1:] = Q[parameters["max_products"]][0, 1:]
Q = Q[:15, :]
它在计算上绝对不是有效的,但它确实有效。
Q = np.tril(-1*np.ones(15), -1)[:, ::-1]
>>> Q
array([[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., -1.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,-1., -1.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., -1.,-1., -1.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., -1., -1.,-1., -1.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., -1., -1., -1.,-1., -1.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., -1., -1., -1., -1.,-1., -1.],
[ 0., 0., 0., 0., 0., 0., 0., 0., -1., -1., -1., -1., -1.,-1., -1.],
[ 0., 0., 0., 0., 0., 0., 0., -1., -1., -1., -1., -1., -1.,-1., -1.],
[ 0., 0., 0., 0., 0., 0., -1., -1., -1., -1., -1., -1., -1.,-1., -1.],
[ 0., 0., 0., 0., 0., -1., -1., -1., -1., -1., -1., -1., -1.,-1., -1.],
[ 0., 0., 0., 0., -1., -1., -1., -1., -1., -1., -1., -1., -1.,-1., -1.],
[ 0., 0., 0., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1.,-1., -1.],
[ 0., 0., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1.,-1., -1.],
[ 0., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1.,-1., -1.]])
我正在模拟一家零售店的库存管理系统;因此,我有一个 (15,15) 零矩阵,其中状态是行和操作列:
Q = np.matrix(np.zeros([15, 15]) )
具体来说,0 是最小库存水平,14 是最大库存水平,状态是当前库存水平和操作库存订单(数量)。
因此,我想用“-1”代替零,其中状态和动作的总和 > 14:
print(final_Q)
#First row, from which I can order everything (since 0 + 14 == 14)
[[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
#Second row, from which I can order max. 13 products (1 + 14 > 14)
[[0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1]]
#Third row, from which the max is 12
[[0 0 0 0 0 0 0 0 0 0 0 0 0 -1 -1]]
(...)
我尝试手动实现它,但如何才能自动获得 final matrix?
# Q matrix
Q = np.matrix(np.zeros([15+1, 15+1] ))
# Create a diagonal of -1s
Q = Q[0:15][0:15]
il1 = np.tril_indices(15)
Q[il1] = -1
Q = np.rot90(Q)
# Adjust single values
Q[parameters["max_products"]-1][0, 1:] = Q[parameters["max_products"]][0, 1:]
Q = Q[:15, :]
它在计算上绝对不是有效的,但它确实有效。
Q = np.tril(-1*np.ones(15), -1)[:, ::-1]
>>> Q
array([[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., -1.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,-1., -1.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., -1.,-1., -1.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., -1., -1.,-1., -1.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., -1., -1., -1.,-1., -1.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., -1., -1., -1., -1.,-1., -1.],
[ 0., 0., 0., 0., 0., 0., 0., 0., -1., -1., -1., -1., -1.,-1., -1.],
[ 0., 0., 0., 0., 0., 0., 0., -1., -1., -1., -1., -1., -1.,-1., -1.],
[ 0., 0., 0., 0., 0., 0., -1., -1., -1., -1., -1., -1., -1.,-1., -1.],
[ 0., 0., 0., 0., 0., -1., -1., -1., -1., -1., -1., -1., -1.,-1., -1.],
[ 0., 0., 0., 0., -1., -1., -1., -1., -1., -1., -1., -1., -1.,-1., -1.],
[ 0., 0., 0., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1.,-1., -1.],
[ 0., 0., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1.,-1., -1.],
[ 0., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1.,-1., -1.]])