You are on page 1of 19

NumPy & Gurobi

IEDA 3010 Tutorial 02


Chen Yang
NumPy
NumPy is the fundamental package for scientific
computing in Python.
• multidimensional array object (ndarray);
• assortment of routines for fast operations on
arrays.

Official site: https://numpy.org/doc/stable/index.html


Basics for beginners:
https://numpy.org/doc/stable/user/absolute_beginne
rs.html
NumPy
• Import numpy in python

• Array in numpy (ndarray)


▪ Core data type in numpy.
▪ A list-like object but more fast and compact.
▪ Elements in ndarray should be homogeneous.
▪ Multi-dimension. “ndarray” is shorthand for “N-dimension array”.
▪ Manipulations and broadcasting.
Array • Shape, size and data type

• Create an array

empty() is the fastest but its values are


mess and needed to be reassigned.
Array • Slice and stack of 2d

• Index and slice

Conditions as index

Array and list as index


• Sum, max, min
Array
• Basic operations
• Axis of high dimension

Efficient element-wise computation


Array
We want to construct a square
matrix whose element is defined as
𝑎𝑖𝑗 = 𝑖 − 𝑗
NumPy • Input and output

• The @ operator
Gurobi
• “The Fastest Solver”
https://www.gurobi.com/

• How to acquire the solver and the license?


▪ Search “Anaconda and Gurobi” on the official website.
▪ Instruction document on Canvas

• Some useful tutorials for Gurobi


▪ Resources on the official website
▪ Quick start:
https://www.gurobi.com/documentation/9.5/quickstart_windows/cs_python.ht
ml#section:Python
Gurobi
• Test whether you install gurobi successfully

• Core object: Model


▪ Model represents the model of the problem you want to solve.
▪ All the information is input into the model object, such as the objective,
variables and constraints.
• Var: Gurobi variable object.
• LinExpr: Gurobi linear expression object.
• Constr: Gurobi constraint object.
• Sense: =, >=, <=, max, min
Advanced documents
• Full API Overview of Gurobi
▪ https://www.gurobi.com/documentation/9.5/refman/py_python_api_overvie
w.html

• Introduction to the object model:


▪ https://www.gurobi.com/documentation/9.5/refman/py_model.html

• Examples with Python:


▪ https://www.gurobi.com/resource/modeling-examples-using-the-gurobi-
python-api-in-jupyter-notebook/
▪ https://www.gurobi.com/documentation/9.5/examples/index.html
Simple Example
min 𝑐 𝑇 𝑥 ------Objective
𝑠. 𝑡. 𝐴𝑥 ≤ 𝑏 ------Constraints
𝑙 ≤ 𝑥 ≤ 𝑢 ------Boundary
Simple Example
• Create a model:

• Create variables:

▪ lb, ub : lower bound & upper bound


▪ vtype : {GRB.CONTINUOUS, GRB.BINARY, GRB.INTEGER, GRB.SEMICONT, or GRB.SEMIINT}
Simple Example
• Set Objective:

▪ Expr : expression object. A mathematical formula of variables and numbers.


▪ Sense : GRB.MINIMIZE, GRB.MAXIMIZE

• Add constraint:

▪ constr: constraint object consisting of expressions. (expr sense expr)


Simple Example
• Optimize model:

• Get output:

Output
Simple Example

Output:
Matrix Example

MVar: matrix variable object

▪ shape: the shape of the MVar to create. Same as NumPy


Matrix Example

MLinExpr: object of the linear matrix expression.

Alternative way to add matrix constraint, but AddConstr is the most


convenient and straightforward one.
Matrix Example Output

You might also like