pythongurobielementwise-operations

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

  • Edit: Starting with Gurobi 10.0, the matrix API was extended. We can now perform point-wise multiplication involving combinations of matrix-friendly objects like MVars and np.arrays. Consequently, the above example works as expected:

    In [8]: y * b
    Out[8]:
    <MLinExpr (3,)  >
    array([ 6.289812872427028 <gurobi.Var *Awaiting Model Update*>,
            4.5850437115732765 <gurobi.Var *Awaiting Model Update*>,
            0.5140722391131802 <gurobi.Var *Awaiting Model Update*>])
    

    More details can be found in the detailed release notes for Gurobi 10.0.

    Previous answer (Gurobi <= 9.x.x):

    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])