Python Modeling
Python Modeling
Optimization in Python
Ted Ralphs
1 Introduction
2 COIN-OR
3 Modeling Software
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
Outline
1 Introduction
2 COIN-OR
3 Modeling Software
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
Caveats and Motivation
Caveats
I have no idea about the background of the audience.
The talk may be either too basic or too advanced.
Why am I here?
I’m not a Sage developer or user (yet!).
I’m hoping this will be a chance to get more involved in Sage development.
Please ask lots of questions so as to guide me in what to dive into!
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
Mathematical Optimization
x∈X (3)
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
Types of Mathematical Optimization Problems
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
Quick Intro to Modeling and Analysis
These steps generally involve several different pieces of software working in concert.
For optimization problems, the modeling is often done with an algebraic
modeling system.
Data can be obtained from a wide range of sources, including spreadsheets.
Solution of the model is usually relegated to specialized software, depending on
the type of model.
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
Example: Optimal Bond Portfolio
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
Bond Portfolio Model
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
Abstracting the Model
The previous model is not very satisfactory from a practical perspective, since
the basic parameters might change after the model is specified.
An abstract algebraic model is a model that doesn’t have values for the input
data.
Components of an abstract algebraic model are
Data
Sets: Sets that index variables and constraints.
Parameters: Specific values of numerical inputs.
Model
Variables: Values in the model that need to be decided upon.
Objective Function: A function of the variable values to be maximized or
minimized.
Constraints: Functions of the variable values that must lie within given
bounds.
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
Abstract Bond Portfolio Model
Let B be a set of bonds available for purchase and let F be a set of salient
features of these bonds to be limited and/or optimized.
Let xb be the amount of bond b to be purchased.
Let cb be the value of the feature to be optimized for bond b and afb be the value
of feature f for bond b.
Finally, let Lf be the limit on the average value of feature f (assume are all are
upper limits).
X
max cb xb (9)
b∈B
XX
s.t. afb xb ≤ Lb ∀f ∈ F (10)
f ∈F b∈B
xb ≥ 0 ∀b ∈ B (11)
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
Outline
1 Introduction
2 COIN-OR
3 Modeling Software
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
COIN-OR: Open Source Operations Research
We currently have 50+ projects and more are being added all the time.
COIN-OR has solvers for most common optimization problem classes.
Linear programming
Nonlinear programming
Mixed integer linear programming
Mixed integer nonlinear programming (convex and nonconvex)
Stochastic linear programming
Semidefinite programming
Graph problems
Combinatorial problems (VRP, TSP, SPP, etc.)
Graphics and visualization
COIN-OR has various utilities for reading/building/manipulating/preprocessing
optimization models and getting them into solvers.
COIN-OR has overarching frameworks that support implementation of broad
algorithm classes.
Parallel search
Branch and cut (and price)
Decomposition-based algorithms
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
The COIN-OR Optimization Suite
COIN-OR distributes a free and open source suite of software that can handle all
the classes of problems we’ll discuss.
Clp (LP)
Cbc (MILP)
Ipopt (NLP)
SYMPHONY (MILP, BMILP)
DIP (MILP)
Bonmin (Convex MINLP)
Couenne (Non-convex MINLP)
Optimization Services (Interface)
COIN also develops standards and interfaces that allow software components to
interoperate.
Check out the Web site for the project at http://www.coin-or.org
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
Modular Structure of the Suite
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
Basic Building Blocks: CoinUtils
The CoinUtils project contains a wide range of low-level utilities used in almost every
project in suite.
Factorization
File parsing
Sparse matrix and array storage
Presolve
Memory management
Model building
Parameter parsing
Timing
Basic data structures
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
Basic Building Blocks: Open Solver Interface
Uniform API for a variety of solvers:
CBC GLPK
CLP Mosek
CPLEX OSL
DyLP Soplex
FortMP SYMPHONY
XPRESS-MP Volume Algorithm
Read input from MPS or CPLEX LP files or construct instances using COIN-OR
data structures.
Manipulate instances and output to MPS or LP file.
Set solver parameters.
Calls LP solver for LP or MIP LP relaxation.
Manages interaction with dynamic cut and column generators.
Calls MIP solver.
Returns solution and status information.
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
Building Blocks: Cut Generator Library
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
Optimization Suite Dependency Graph
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
Getting the COIN-OR Optimization Suite
Source builds out of the box on Windows, Linux, OSX using the Gnu autotools
(or with Visual Studio project files on Windows).
Source is available from
https://www.coin-or.org
https://github.com/coin-or
https://bintray.com/coin-or/download
For many more details, see Lecture 1 of this tutorial (slightly out of date now):
http://coral.ie.lehigh.edu/ ted/teaching/coin-or
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
Outline
1 Introduction
2 COIN-OR
3 Modeling Software
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
Modeling Software
Most existing modeling software can be used with COIN solvers.
Commercial Systems
GAMS
MPL
AMPL
AIMMS
Python-based Open Source Modeling Languages and Interfaces
Pyomo
PuLP/Dippy
CyLP (provides API-level interface)
yaposib
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
Modeling Software (cont’d)
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
How They Interface
Although not required, it’s useful to know something about how modeling
languages interface with solvers.
In many cases, modeling languages interface with solvers by writing out an
intermediate file that the solver then reads in.
It is also possible to generate these intermediate files directly from a
custom-developed code.
Common file formats
MPS format: The original standard developed by IBM in the days of Fortran, not
easily human-readable and only supports (integer) linear modeling.
LP format: Developed by CPLEX as a human-readable alternative to MPS.
.nl format: AMPL’s intermediate format that also supports non-linear modeling.
OSIL: an open, XML-based format used by the Optimization Services framework of
COIN-OR.
Several projects use Python C Extensions to get the data into the solver through
memory.
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
Outline
1 Introduction
2 COIN-OR
3 Modeling Software
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
Where to Get the Examples
https://github.com/tkralphs/FinancialModels
http://projects.coin-or.org/browser/Dip/trunk/
Dip/src/dippy/examples
https://github.com/Pyomo/PyomoGallery/wiki
https://github.com/coin-or/pulp/tree/master/
examples
https://pythonhosted.org/PuLP/CaseStudies
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
1 Introduction
2 COIN-OR
3 Modeling Software
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
PuLP: Algebraic Modeling in Python
PuLP is a modeling language in COIN-OR that provides data types for Python
that support algebraic modeling.
PuLP only supports development of linear models.
Main classes
LpProblem
LpVariable
Variables can be declared individually or as “dictionaries” (indexed sets).
We do not need an explicit notion of a parameter or set here because Python
provides data structures we can use.
In PuLP, models are technically “concrete,” since the model is always created
with knowledge of the data.
However, it is still possible to maintain a separation between model and data.
Developer: Stuart Mitchell
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
Simple PuLP Model (bonds_simple-PuLP.py)
X1 = LpVariable("X1", 0, None)
X2 = LpVariable("X2", 0, None)
prob.solve()
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
PuLP Model: Bond Portfolio Example (bonds-PuLP.py)
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
PuLP Data: Bond Portfolio Example (bonds_data.py)
’B’ : {’yield’ : 3,
’rating’ : 1,
’maturity’ : 4,},
}
max_cash = 100
max_rating = 1.5
max_maturity = 3.6
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
Notes About the Model
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
Notes About the Data Import
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
Bond Portfolio Example: Solution in PuLP
prob.solve()
epsilon = .001
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
More Complexity: Facility Location Problem
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
PuLP Model: Facility Location Example
prob = LpProblem("Facility_Location")
for j in PRODUCTS:
prob += lpSum(assign_vars[(i, j)] for i in LOCATIONS) == 1
for i in LOCATIONS:
prob += lpSum(assign_vars[(i, j)] * REQUIREMENT[j]
for j in PRODUCTS) <= CAPACITY * use_vars[i]
prob.solve()
for i in LOCATIONS:
if use_vars[i].varValue > 0:
print "Location ", i, " is assigned: ",
print [j for j in PRODUCTS if assign_vars[(i, j)].varValue > 0]
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
PuLP Data: Facility Location Example
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
DIP/DipPy: Decomposition-based Modeling and Solution
prob = dippy.DipProblem("Facility_Location")
for j in PRODUCTS:
prob += lpSum(assign_vars[(i, j)] for i in LOCATIONS) == 1
for i in LOCATIONS:
prob.relaxation[i] += lpSum(assign_vars[(i, j)] * REQUIREMENT[j]
for j in PRODUCTS) <= CAPACITY * use_vars[i]
dippy.Solve(prob, doPriceCut:1)
for i in LOCATIONS:
if use_vars[i].varValue > 0:
print "Location ", i, " is assigned: ",
print [j for j in PRODUCTS if assign_vars[(i, j)].varValue > 0]
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
DipPy Callbacks
2 COIN-OR
3 Modeling Software
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
CyLP: Low-level Modeling and API for Cbc/Clp/Cgl
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
CyLP: Accessing the Tableaux
lp = CyClpSimplex()
x = lp.addVariable(’x’, numVars)
lp += x_u >= x >= 0
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
1 Introduction
2 COIN-OR
3 Modeling Software
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
yaposib: Python Bindings for OSI (C.-M. Duquesne)
Provides Python bindings to any solver with an OSI interface
solver = yaposib.available_solvers()[0]
problem = yaposib.Problem(solver)
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
1 Introduction
2 COIN-OR
3 Modeling Software
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
Pyomo
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017
Thank You!
Questions?
T.K. Ralphs (Lehigh University) Open Source Optimization August 21, 2017