0% found this document useful (0 votes)
13 views

0 Python-Gurobi Interface

Uploaded by

berkyildizdas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

0 Python-Gurobi Interface

Uploaded by

berkyildizdas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

IE 311 – Operations Research I

Python-Gurobi Interface

Summer 2024
Solvers
• A solver is a piece of mathematical software, possibly in the form of a
stand-alone computer program or as a software library, that 'solves' a
mathematical problem.
• A solver takes problem descriptions in some sort of generic form and
calculates their solution.
• Popular solvers:
• CPLEX (LP, MILP, QP)
• GUROBI (LP, MILP, QP)
• MOSEK (LP, MILP, QP)
• BARON (LP, NLP, MINLP)
• CONOPT (Large scale NLP)
• …
IE311 2
Gurobi-Python Interface
• Gurobi Optimizer is a prescriptive analytics platform and a decision-
making technology developed by Gurobi Optimization, LLC.
• The Gurobi Optimizer (often referred to as simply, “Gurobi”) is a
solver, since it uses mathematical optimization to calculate the
answer to a problem.
• Gurobi comes with a Python extension module called “gurobipy” that
offers convenient object-oriented modeling constructs and an API to
all Gurobi features.

IE311 3
A small example

IE311 4
Import functions and classes

• First of all we need to import Gurobi functions and classes:

,Model,quicksum

• This line makes everything in classes GRB, Model and quicksum


available.

IE311 5
Creating a model

• The first step is to create a model. A Gurobi model holds a single


optimization problem.
• It consists of a set of variables, a set of constraints, and the associated
attributes (variable bounds, objective coefficients, variable integrality
types, constraint senses, constraint right-hand side values, etc.).
• We start this example with an empty model object:

IE311 6
Adding variables to the model

• The next step in our example is to add variables to the model.

• Variables are added through the addVar() method on a model object


(or addVars() if you wish to add more than one at a time).

IE311 7
Setting the objective
• The next step in the example is to set the optimization objective:

• The second argument indicates that the sense is maximization.


• Note that while this simple example builds the objective in a single statement
using an explicit list of terms, more complex programs will typically build it
incrementally. For example:

IE311 8
Adding constraints to the model
• The next step in the example is to add the linear constraints.

• As with variables, constraints are always associated with a specific


model. They are created using the addConstr() method on the model
object.
• The second argument to addConstr gives the (optional) constraint
name.

IE311 9
Optimize the model

• Now that the model has been built, the next step is to optimize it.

• This routine performs the optimization and populates several internal


model attributes (including the status of the optimization, the
solution, etc.).

IE311 10
Function Model.addVar()

IE311 11
Function Model.addConstr()

IE311 12
Function Model.setObjective()

IE311 13
Function quicksum()

෍ 𝑎𝑗 𝑥𝑗 ≤ 𝑏
𝑗=1

m.addConstr( (quicksum(a[j]*x[j] for j in range(n))) <= b)

IE311 14

You might also like