# How to do matrix multiplication in docplex in python?

Here's an applicative problem I'm trying to solve. Imagine there is a road that can be classified in 5 states. Can be something from good to bad etc. There are 4 maintenance actions possible. And the idea is to find a minimum cost policy for steady state. Which means if the road was 60% in state B and 40% in state C, and I were to apply action 1 on B and action 2 on C, they could swap but next year again, the distribution will be 60% and 40%.

s = 5 #states
a = 4 #actions
costs = [0, 25, 50, 100] #cost of actions

tpm_1 = np.array([
[0.9, 0.1, 0, 0, 0],
[0, 0.8, 0.2, 0, 0],
[0, 0, 0.6, 0.4, 0],
[0, 0, 0, 0.7, 0.3],
[0, 0, 0, 0, 1]
])  #transition probabilities for action 1: do nothing, meaning if nothing is done and the asset is in state 1, there's 90% chance that it stays there and 10% chance it goes to state B.

w = steady.continuous_var_matrix(s, a, lb=0, ub=1, name='a')


I next created these action variables, representing probabilities or fractions of roads in different categories on which different actions are to be taken. To apply steady state condition, I need to apply the constraint,

Current state*transition probabilities = current state.


However, the continuous_var_matrix() function creates a dictionary and not a matrix of variables. How do I go about then applying matrix multiplication on this? Why would you name something matrix, make a dict and not give it any functions of matrix?

• Found a work around. Defined list variables, passed them as numpy array and then used reshape command. Now I have a ndarray. However, the naming is a bit pain in the ass, because for first row its 0 to 3 then it is from 4 to 7 and then 8 to 11 and so on instead of standard matrix notations. Makes work a little bit tedious to interpret after the optimization but still works! Let me know if anyone still finds matrix multiplication possible with continuous_var_matrix() call. Jul 31, 2022 at 6:50
• You may try cvxpy as the modeling language.
– xd y
Nov 9, 2022 at 5:30

I think you can try something like this list(w.values()) , because continuous_var_matrix returns a dictionary of continuous_var, just convert it to list, and it should work.