matmul: Input operand 0 does not have enough dimensions error
AnsweredHi,
I am trying to convert my code from matlab to python. My matlab code is:
#matlab
R=eye(4);
u=sdpvar(4,1);
optimize(F,u'*R*u,sdpsettings('verbose',0));
and the same logic I coded in python as:
#python
m=gp.Model("matrix1")
R=np.eye(4)
u=m.addVars(4)
m.setObjective(u@R@u, GRB.MINIMIZE)
PS: I have R matrix as 4x4 identity matrix and u matrix as decision variable of 4x1. I wanted to optimize u. However, I am getting the dimensions error.
could anybody help me to get solve this problem? thank you in advance.
-
Official comment
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?. -
Model.addVars() returns a tupledict of Var objects. The \( \texttt{@} \) operator must be used with MVar objects, which are created using Model.addMVar(). For example:
m = gp.Model('matrix1')
R = np.eye(4)
u = m.addMVar(shape=4, name='u')
m.setObjective(u @ R @ u, GRB.MINIMIZE)You might find the matrix1.py and matrix2.py examples helpful.
1 -
I got it. Thank you!
0
Post is closed for comments.
Comments
3 comments