Unsupported type (<class 'gurobipy.MLinExpr'>) for LinExpr addition argumentAnswered
in my model I get the error
File src\gurobipy\var.pxi:265, in gurobipy.Var.__sub__()
File src\gurobipy\linexpr.pxi:485, in gurobipy.LinExpr.__isub__()
File src\gurobipy\linexpr.pxi:183, in gurobipy.LinExpr.add()
GurobiError: Unsupported type (<class 'gurobipy.MLinExpr'>) for LinExpr addition argument
when adding the following constraints:
v[t, a] == v[t-1, a] - x @ o[t-1, a]
+ x @ d[t-1, a]
for t in range(1,T+1)
for a in range(S)
where o[t-1, a] and d[t-1, a] are numpy arrays with shape (1615,) and the variables are initialized as follows:
x = self.model.addMVar(shape=1615, vtype=GRB.BINARY, name="x")
v = self.model.addMVar(shape=(T+1, S), vtype=GRB.INTEGER, name="v",
I already stumbled upon this thread and I guess the problem is that v[t-1, a] is evaluated as a gurobipy.Var instead of a gurobipy.MVar.
But I'm still not sure how to solve it.
(The objective is
self.f @ x - gp.quicksum(c[a,b] * y[a,b]
for a in range(S)
for b in range(S)),
if it matters)
after a brief look into the documentation, I believe that instead of adding variables per model.addMVar, you should add them per model.addVar or model.addVars and store them in a tupledict.
According to the documentation, model.addMVar works best if you are able to supply a complete matrix of coefficients. You seem to be building the constraints one by one - I believe you would be better off when dealing with variables "one by one" as well.
Perhaps someone from the Gurobi's support team can confirm this?
Jonasz is correct.
Currently, the Python Matrix API works best with a complete matrix of coefficients. It is possible to make it work with vectors, cf. Python Matrix API in our Knowledge Base. However, it is currently requires some extra work and might not seem intuitive.
In your particular case, using Vars over MVars should not result in any significant performance loss/gain.
We are trying to improve the Python Matrix API with each release. Thus, this particular behavior may improve significantly in an upcoming release.
Please sign in to leave a comment.