0

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.

Greg Glockner
  • 5,433
  • 2
  • 20
  • 23
Null_Space
  • 449
  • 1
  • 4
  • 9

1 Answers1

2

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)
Greg Glockner
  • 5,433
  • 2
  • 20
  • 23