Gurobi Constraints - 'gurobipy.LinExpr' object has no attribute 'shape'
AnsweredHi,
I'm working on an optimization problem in Gurobi where x and pi are the only variables. However, when defining the following constraints, I encountered the error: "'gurobipy.LinExpr' object has no attribute 'shape'".
I attempted to resolve it by applying float() to each constant, but the issue persists. Below is my code for this model.
mQ=gp.Model("Subgradient")
x = mQ.addMVar(shape=(N,H), vtype = GRB.BINARY, name = "x")
pi= mQ.addMVar(shape=(K, Q, N, H), vtype=GRB.CONTINUOUS,lb=0, name="pi")
mQ.addConstrs(pi[k][q][i][h]*float(denominator_square[q][l])<=
gp.quicksum(float(w[q][i][h])*float(all_x_hat[l][i][h])*float(denominator[q][l]) for i in var_i for h in var_h)
+ gp.quicksum((float(w[q][i][h])*(float(denominator[q][l])-float(all_x_hat[l][i][h])*float(w[q][i][h]))
+gp.quicksum(float(w[q][i_tilde][h_tilde])*float(all_x_hat[l][i_tilde][h_tilde])
*float(w[q][i][h]) for i_tilde in var_i if i_tilde != i for h_tilde in var_h if h_tilde!= h))
*(x[i][h] - float(all_x_hat[l][i][h]) ) for i in var_i for h in var_h)
for l in var_hat for k in var_k for q in var_q for i in var_i for h in var_h)
Could you please help me check this issue and suggest a solution? Thank you for your time and assistance!
-
Hi Yutian,
If you're not creating constraints using matrix multiplication, then you're better off not using MVars (because using MVars in this way is very slow). Just use ordinary variables:
x = mQ.addVars(N,H, vtype = GRB.BINARY, name = "x") pi= mQ.addVars(K, Q, N, H, vtype=GRB.CONTINUOUS,lb=0, name="pi")
This may be enough to resolve the issue.
If you still have an issue then try adding just one constraint, for some value of l,k,q,i,h. Then remove pieces of the constraint and try to make it as small as possible while still producing an error. This will make it easier for you to debug.
- Riley
0 -
Hi Riley,
That works! Thank you so much!
Best regards,
Yutian
0
Please sign in to leave a comment.
Comments
2 comments