Starting with Gurobi 10, you can concisely pointwise multiply two MVar objects following numpy's conventions. For example, the expression
$$\begin{align} \begin{bmatrix} u_1 \\ u_2 \\ u_3 \end{bmatrix} = \begin{bmatrix} x_1 y_1 \\ x_2 y_2 \\ x_3 y_3 \end{bmatrix} \end{align}$$
can be constructed as follows:
import gurobipy as gp
import numpy as np
m = gp.Model()
x = m.addMVar(3)
y = m.addMVar(3)
u = m.addMVar(3)
m.addConstr(u == x * y)
In Gurobi 9, this operation was not supported. Executing the above code with Gurobi 9 results in TypeError: float() argument must be a string or a number, not 'MVar'. Instead, these constraints had to be added row-by-row. For example:
import gurobipy as gp
import numpy as np
m = gp.Model()
x = m.addMVar(3)
y = m.addMVar(3)
u = m.addMVar(3)
m.addConstrs(u[i] == x[i] @ y[i] for i in range(3))
Note: Gurobi 9.0 introduced the first version of the Python matrix API. The developers are continually working to improve the usability of the matrix API.
Further information
- How do I multiply two MLinExpr objects together with the matrix-friendly Python API?
- How do I pointwise multiply a numpy vector with a (1,) MVar with the matrix-friendly Python API?
- How do I multiply an array and a 2-D MVar object using the matrix-friendly Python API?
- How do I pointwise multiply an array and an MVar with the matrix-friendly Python API?
Comments
0 comments
Article is closed for comments.