Gurobipy Matrixfriendly Webinar-Slides
Gurobipy Matrixfriendly Webinar-Slides
import gurobipy as gp
m = gp.Model()
x = m.addVar()
m.addConstr(x >= 42)
sha1_base64="fagbKbpwBG3a33aVReDcG4aVsDY=">AAACN3icbVBNSwMxFMz67fpV9eglWCyell0V9Fj14kkstCp0a8mmr20wyS5JVrYs/Vde/Bve9OJBEa/+A9Pag20dCAzzZnh5EyWcaeP7L87M7Nz8wuLSsruyura+UdjcutZxqijUaMxjdRsRDZxJqBlmONwmCoiIONxE9+eD+c0DKM1iWTW9BBqCdCRrM0qMlZqFy1Aw2cxw2NUJoZAHIPolnN1VcQVnYeiGIoqzXHvG6495cAmf2lQHcGRdNjHkfrNQ9D1/CDxNghEpohGumoXnsBXTVIA0lBOt64GfmEZOlGGUQ98NUw126T3pQN1SSQToRj68u4/3rNLC7VjZJw0eqn8TORFa90RknYKYrp6cDcT/ZvXUtE8aOZNJakDS30XtlGMT40GJuMUUUMN7lhCqmP0rpl2iCDW2ateWEEyePE2uD7zg0DuoHBXLZ6M6ltAO2kX7KEDHqIwu0BWqIYoe0St6Rx/Ok/PmfDpfv9YZZ5TZRmNwvn8AKNapkQ==</latexit>
<latexit
• We would need to traverse nonzeros of Q and A.
• We would need to construct explicit modeling objects for all the expressions.
• Somewhat superfluous since these expressions are already defined through the matrix-
vector relations between Q, A, x and b on a higher syntactic level.
New in 9.0: You can build optimization models directly in terms of matrices and vectors.
>>> x[:, 2].obj = -1.0 # Set Gurobi attribute on slice via broadcast
>>> model.update(); x.obj # Did it work? (Yes.)
array([[ 1., 1., -1.],
[ 1., 1., -1.]])
• Use Python3 matrix multiplication operator @ to build linear expressions and constraints.
• Typical usage pattern: model.addConstr(A @ x == b)
• A is a Numpy ndarray, or a Scipy.sparse matrix.
• b is a Numpy ndarray.
• (the senses <= and >= can be used just as well).
• Example: Random sparse linear system
model = gp.Model()
x = model.addMVar(3)
model.addConstr(A @ x == 1) # RHS is broadcast!
y = model.addMVar(5)
model.addConstr(y == A @ x) # y = Ax
model.addConstr(y.sum() <= 0.5) # Mvar.sum() is for convenience
z = model.addMVar(10)
model.addConstr(A @ x + D @ z == 0) # Column wise composition
• Altough MVar objects can be N-D, linear constraints involving MVars are restricted to one
dimension.
• Example:
A = np.random.rand(4,4)
B = np.random.rand(4,4)
model = gp.Model()
X = model.addMVar((4,4))
# Not (yet) supported: Add 16 linear constraints AX = B
model.addConstr(A@X == B) # Error!
model = gp.Model()
x = model.addMVar(3)
model.setObjective(x @ x) # x[0]^2 + x[1]^2 + x[2]^2
s = model.addMVar(100)
z = model.addMVar(100)
model.addConstr(s @ z == 0) # Complementarity constraint, nonconvex!
• Also new in Gurobi 9.0: A lower level interface for modeling with matrix data.
• No modeling objects involved at all, almost no overhead
• Add linear constraints
• Model.addMConstrs(A, x, sense, b)
• Add quadratic constraints
• Model.addMQConstr(Q, c, sense, rhs, xQ_L=None, xQ_R=None, xc=None)
• Set quadratic and linear objective functions
• Model.setMObjective(Q, c, constant, xQ_L=None, xQ_R=None, xc=None, sense=None)
• These API functions just take the raw data, and possibly a subset of variables.
• Useful in certain specialized situations, or if one is tied to Python 2.
• If your optimization data is naturally expressed in matrices and vectors, gurobipy 9.0 gives you
tools to work more idiomatically with that data.
• This is our first step towards making guropbipy matrix-friendly – let us know what you would
like to see next!
Thanks!
• A recording of this webinar, including the slides, will be available in roughly one week.