Null_Space
Null_Space

Reputation: 469

Gurobipy: matrix constraints

Let x be the vector of n variables defined with: x = M.addMVar(shape = n, vtype = GRB.BINARY, name = "x"). Let A be an n by n matrix. Let v be an n by 1 constant vector that consists of positive integers. The constraint that I am interested in:

np.multiply(v, x) <= A @ x

However, when I add this constraint in gurobi:

M.addConstr(np.multiply(v, x) <= A @ x, name = "c1")

It gives errors:

File "src/gurobipy/model.pxi", line 3325, in gurobipy.Model.addConstr

File "src/gurobipy/model.pxi", line 3586, in gurobipy.Model.addMConstr

TypeError: must be real number, not MLinExpr

Any idea why this happening? I have been working on a solution for hours. My current guess is that Gurobi is not happy with variables occurring on both sides of the inequality. However, I haven't figured out a workaround.

Upvotes: 0

Views: 1036

Answers (1)

Greg Glockner
Greg Glockner

Reputation: 5653

This will not work because the matrix representation in the Gurobi Python interface requires the canonical form Ax = b. So you need to incorporate v into the A matrix, and your code becomes something like:

A2 = A-v*np.eye(n)
M.addConstr(A2 @ x >= 0)

Upvotes: 2

Related Questions