trouble using indicator constraints - 'MLinExpr' object has no attribute 'size'
AnsweredDear All,
I have a program implementing indicator constraints using bigM constraints. I wanted to replace the bigM constraints with the gurobi indicator ones because apparently those lead to different branching procedures. These indicator constraints lead to the error "'MLinExpr' object has no attribute 'size'". Perhaps someone can spot the error? I guess the issues I am using an MVar in my indicator constraints, but is there a simple way to over come this? These MVar constraints are a result of other matrix operations so changing the Mvar to Var would require changing several other things.
import numpy as np
from scipy.linalg import circulant
import gurobipy as gp
from gurobipy import GRB
m = gp.Model("matrix1")
CL = 20
Spectra = [100, 125,200]
# First Create Node Values
x = m.addMVar(shape=CL, vtype=GRB.BINARY, name="x")
data1 = [[1.0, 2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0,15.0,16.0,17.0,18.0,19.0,20.0]]
data2 = [[2.0, 4.0, 6.0, 8.0, 10.0,12.0,14.0,16.0,18.0,20.0,22.0,24.0,26.0,28.0,30.0,32.0,34.0,36.0,38.0,40.0]]
NodeVals = m.addMVar(CL)
m.addConstrs(NodeVals[i] == x[i] * data1[0][i] + (1-x[i])*data2[0][i] for i in range(CL))
Q = m.addMVar((CL-1,CL))
for k in range(1,CL):
vec = np.zeros((CL))
vec[0:k] =1
MAT = circulant(vec)
m.addConstr(Q[k-1,:] == MAT @ NodeVals)
#set constraints that lead to objective
y = m.addMVar(shape=(CL-1,CL,len(Spectra)), vtype=GRB.BINARY, name="x")
for t in range(0,len(Spectra)):
#m.addConstrs(Q[i,j]-Spectra[t] <= (1-y[i,j,t]) * 1000 for i in range(CL-1) for j in range(CL))
#m.addConstrs(-(1-y[i,j,t]) * 1000 <= Q[i,j]-Spectra[t] for i in range(CL-1) for j in range(CL))
m.addConstrs((y[i,j,t] == 1) >> (Q[i,j] == Spectra[t]) for i in range(CL-1) for j in range(CL))
m.setObjective(y.sum(), GRB.MAXIMIZE)
m.optimize()
print(x.X)
print('Obj: %g' % m.objVal)
Best,
Mihir
-
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?. -
Dear Mihir,
Please excuse us for not responding earlier.
With the upcoming Gurobi version 9.1, you can use \(\texttt{tolist()}\) to cast \(\texttt{MVars}\) to \(\texttt{Vars}\):m.addConstrs((y[i,j,t].tolist()[0] == 1) >> (Q[i,j].tolist()[0] == Spectra[t]) for i in range(CL-1) for j in range(CL))
Unfortunately, this is not yet possible in Gurobi version 9.0.
Cheers,
Matthias0
Post is closed for comments.
Comments
2 comments