'per-element vector multiplication with gurobipy

The last line of this code fails:

A = np.random.rand(d, d)*5.0
b = np.random.rand(d)*10.0
m = gp.Model()
x = m.addMVar(d, name='x')
y = m.addMVar(d, name='y', vtype=gp.GRB.BINARY)
m.addConstr(A@x <= b)
m.addConstr(A@x >= y*b) // error occurs here on y*b

It gives this error for gurobipy version 9.5.1:

File "src/gurobipy/mvar.pxi", line 108, in gurobipy.MVar.__mul__
File "src/gurobipy/mvar.pxi", line 184, in gurobipy.MVar._scalar_mult
TypeError: only size-1 arrays can be converted to Python scalars

I was expecting per-element multiplication with the asterisk. How do I declare the per-element multiplication in a way that is compatible with the gurobipy API?



Solution 1:[1]

AFAIK, this is currently not supported by Gurobi's matrix interface. Maybe it's worth making a feature request here. In the meantime, you can express the element-wise product y*b as a regular matrix-vector product:

m.addConstr(A @ x >= np.diag(b) @ y)

Alternatively, you can write it similar to the term-based interface:

from gurobipy import quicksum as qsum

for i in range(d):
    m.addConstr(qsum(A[i, j] * x[j] for j in range(d)) >= y[i] * b[i])

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 joni