Reputation: 1945
I am simulating an inventory management system for a retail shop; therefore, I have a (15,15) matrix of zeros in which states are rows and actions columns:
Q = np.matrix(np.zeros([15, 15]) )
Specifically, 0 is the minimum and 14 the maximum inventory level, states are current inventory level and actions stock orders (quantity).
Consequently, I would like to substitute zeros with "-1", where the sum of state and action > 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]]
(...)
I tried implementing that manually, but how can I get the final matrix automatically?
Upvotes: 1
Views: 55
Reputation: 4767
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.]])
Upvotes: 1
Reputation: 1945
# 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, :]
It is definitely not computationally effective but it works.
Upvotes: 1