ValueError: Mismatching inner dimensions
Answeredimport numpy as np
import gurobipy as gp
from gurobipy import *
D = 10
F = 2
N = 5
H = np.random.random((N,D))-0.5
R = np.random.random(N) - 0.5
R = np.matmul(R.T,H)
H = np.matmul(H.T,H)
I = np.identity(F)
m = gp.Model()
A = m.addMVar(shape=(D,F),vtype=GRB.CONTINUOUS, name='A')
beta = m.addMVar(shape=F,vtype=GRB.CONTINUOUS,name = 'beta')
v = m.addMVar(shape = D, vtype=GRB.CONTINUOUS)
#obj = (H @ A @ beta - R) @ (H @ A @ beta - R)
obj = v @ H @ v - 2 * R @ v
m.setObjective(obj,GRB.MINIMIZE)
m.addConstr(v == A @ beta)
m.addConstrs(A @ A == I)
m.setParam('NonConvex', 2)
m.optimize()
After running the above code, I got the error message on the following line
m.addConstrs(A @ A == I)
saying that ValueError: Mismatching inner dimensions 2 vs. 10.
Does anyone have any suggestions on how to resolve this issue? Many thanks in advance.
1
-
When you do matrix multiplication, the dimensions need to match. You need to use MVar.transpose().
Respecting the dimension of matrix I, the line should bem.addConstr(A.transpose() @ A == I)
0 -
I see, thank you for the help!
0
Please sign in to leave a comment.
Comments
2 comments