Sie sind auf Seite 1von 23

Building Applications with

Python & Gurobi


Kostja Siefen
Building optimization models with Gurobi
} Option 1 – use a modeling language
◦ Very easy to build models
– Optimization modeling constructs built into language
◦ Attractive choice for non-programmers
◦ Alternatives: AMPL, GAMS, Pyomo, …

} Option 2 – use a full programming language


◦ Much more powerful and flexible development environment
– Complete access to solver functionality
– Richer set of language features
◦ Natural choice when deploying models and/or integrating them into
applications for others to use
◦ Alternatives: C, C++, C#, Java, MATLAB, Python, R, VB

} But what if users didn't have to choose between easy and


powerful?

2 © 2015 Gurobi Optimization


Gurobi Python Environment
} High-level optimization modeling constructs embedded in
Python programming language
◦ Combines expressiveness of a modeling language with the power and
flexibility of a programming language

} Design goals:
◦ Require minimal programming skills to get started
◦ Bring "feel" of a modeling language to the Python interface
◦ Allow for code that is easy to write and maintain
◦ Maintain unified design across all of our interfaces
◦ Remain lightweight and efficient (memory & CPU) when compared with
solver alone
◦ Support all solver and programming needs

} Consistently earns high praise from our users

3 © 2015 Gurobi Optimization


Why Python?
} Fast, powerful and easy-to-use
} Extendible with vast library of prewritten modules (over 50,000)
◦ Statistical analysis, visualization, GUIs, web development, web services,
data connections, file compression, data encryption, …

} One of top-10 most popular programming languages, according to


TIOBE Programming Community Index
◦ http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html
◦ Large and rapidly growing Python community with great training resources

} Interactive shell is useful for prototyping


} Can be deployed to create full-featured optimization applications
◦ GUI, server, web, …

} Free and open source with license that's business-friendly

4 © 2015 Gurobi Optimization


Getting started with Python
} Don't need to be an expert to start modeling in Python

} Nothing extra to buy or install


◦ Gurobi distribution includes Python interpreter and basic modules

} Gurobi Interactive Shell is Python shell with Gurobi extensions


◦ Powerful environment for interacting with existing models

} To launch the shell:


◦ Windows
– Double-click the Gurobi icon on the desktop
– Or run gurobi from a Command Prompt window
◦ Linux
– Run the gurobi.sh command from a terminal
◦ Mac
– Click on the Gurobi icon in the Launchpad
– Or run the gurobi.sh command from a terminal

5 © 2015 Gurobi Optimization


Programming in Python
} Python is a cross-platform, high-level programming language
◦ Object-oriented
◦ Interpreted
◦ Dynamically typed
◦ Automatic memory management
◦ Includes a large standard library
◦ Documentation tightly integrated with the code

} Python programs are typically stored in text files with .py extension

} Most functionality in Python is provided by modules


◦ To use a module in Python programs, include an import statement

} To use the Gurobi interface, import the gurobipy module:


from gurobipy import *

6 © 2015 Gurobi Optimization


Editing and running Python programs
} Any text editor can be used to work with .py files
◦ Most programming editors have Python mode with syntax highlighting,
etc.

} To run a program:
◦ Windows
– At a Command Prompt window, enter
gurobi [program_name]
◦ Linux/Mac
– At a terminal, enter
gurobi.sh [program_name]

} Many IDEs available


◦ https://wiki.python.org/moin/IntegratedDevelopmentEnvironments
◦ Easy to install Gurobi in other Python distributions

7 © 2015 Gurobi Optimization


Gurobi recommends Anaconda
} Windows, Mac, Linux; 32/64-bit
} Python 2.x, 3.x — your choice
} ~150 packages in the default installation
} conda package and environment management
◦ ~350 packages available with a simple conda install
◦ Create isolated “silos” for multiple projects

} Gurobi provides/maintains Anaconda packages

8 © 2015 Gurobi Optimization


Essential Gurobi modeling constructs
} Model a model
} Var a decision variable
} Constr a constraint

} Overloaded operators
◦ Basic arithmetic (+, -, ×, ÷)
◦ Constraint (≤, =, ≥)

} Aggregate sum operator (quicksum)

} Python provides the rest!

9 © 2015 Gurobi Optimization


Typical steps for building a model
} Create empty Model object with Model() constructor

} Add variables with Model.addVar()

} Call Model.update() once to process pending model updates


◦ Gurobi provides efficient "lazy updates"
◦ Only call Model.update() when necessary to reference new elements

} Call Model.setObjective() to set objective function

} Add constraints with Model.addConstr()

} Solve the model with Model.optimize()


◦ Will process any pending model updates

10 © 2015 Gurobi Optimization


Example: simple model

# Create empty Model


m = Model()

# Add variables
x = m.addVar(vtype=GRB.BINARY)
y = m.addVar(vtype=GRB.BINARY)
z = m.addVar(vtype=GRB.BINARY)

# Process pending updates


m.update()

11 © 2015 Gurobi Optimization


Example: simple model

# Set objective function


m.setObjective(x + y + 2*z, GRB.MAXIMIZE)

# Add constraints
m.addConstr(x + 2*y + 3*z <= 4)
m.addConstr(x + y >= 1)

# Solve model
m.optimize()

12 © 2015 Gurobi Optimization


Indices and subscripts in Python
} List is a mutable sequence of elements
◦ Useful when representing index sets
◦ Elements of any type (integers, strings, …) indexed by integers (0 to n-1)
◦ Ex: cities=['CHI', 'NYC', 'ATL']; cities.append('MIA');
cities[2]='BOS'; cities.remove('NYC'); cities.sort()

} Tuple is an immutable, compound grouping of elements


◦ Ideal for representing multidimensional subscripts
◦ Ex: arc=('CHI','NYC'); j=arc[1]

} Dictionary is a mutable mapping from keys to values


◦ Useful when representing coefficients or variables indexed by subscripts
◦ Keys must be immutable (integers, strings, tuples, …)
◦ Ex: cost={('CHI','NYC'):100, ('ATL','MIA'):150};
cost[('NYC','MIA')]=200; c=cost['ATL','MIA']

13 © 2015 Gurobi Optimization


Iterating in Python and aggregate sums
} Loops iterate over collections of elements (list, dictionary, …)
◦ Useful when representing the for-all modeling construct
◦ Ex:
for c in cities:
print c # must indent all statements in loop

} List comprehension efficiently builds lists via set notation


◦ Ex: penaltyarcs=[a for a in arcs if cost[a]>1000]

} quicksum() is direct replacement for Python's sum() function


◦ Gurobi feature that is more efficient when working with Var objects
◦ Ex: obj=quicksum(cost[a]*x[a] for a in arcs)

} Combining loops with sums is ideal when building constraints

Ex: ∑ j∈J
xij ≤ 5, ∀i ∈ I can be built with

for i in I:
m.addConstr(quicksum(x[i,j] for j in J) <= 5)

14 © 2015 Gurobi Optimization


Building a Gurobi Model: Six Steps

Create a model m = Model()

Add variables x = m.addVar(...)

Commit changes m.update()

Set objective m.setObjective(...)

Add constraints m.addConstr(...)

Optimize m.optimize()

15 © 2015 Gurobi Optimization


Spyder
} Scientific Python Development EnviRonment
} Familiar integrated development environment (IDE) for users of
MATLAB, Visual Studio, etc.
} Built-in editor, file explorer, documentation browser
} Graphical interface for debugging (ipdb)
} IPython console for interactive code execution

http://pythonhosted.org/spyder

16 © 2015 Gurobi Optimization


Latest Python-related enhancements in v6.5
} Interactive examples for commonly faced business problems
◦ http://www.gurobi.com/resources/examples/example-models-overview

} Expression building is more than 4X faster

} Lazy update mode


◦ Set new UpdateMode parameter to 1
◦ Refer to new variables and constraints without calling Model.update()

} More control over when Gurobi environments are created/released


◦ Default environment not created until first used
◦ Released with new disposeDefaultEnv() method

} New Conda package simplifies installation for the Anaconda Python distribution
$ conda config --add channels http://conda.anaconda.org/gurobi
$ conda install gurobi

17 © 2015 Gurobi Optimization


Interactive Examples

18 © 2015 Gurobi Optimization


Jupyter (formerly IPython)
} A web application for interactive data analysis
} Create notebooks containing live code, text, equations, and
visualizations
} A great way to prototype new algorithms, document experiments,
share reproducible results
} Originally for Python, now there are kernels for over 50 different
languages

http://www.jupyter.org

19 © 2015 Gurobi Optimization


Pandas
} Fast and efficient DataFrame and Series objects
} Read/write CSV, HDF5, Excel, SQL, plain text
} Missing data handling
} Slicing, fancy indexing, subsetting
} Merges, joins
} Split-apply-combine operations (groupby)

http://pandas.pydata.org

20 © 2015 Gurobi Optimization


Bokeh
} Interactive visualization for web browsers
◦ High performance with streaming data and big data
◦ In-browser interactivity on otherwise static pages
◦ Bindings for Python, Scala, Julia, R

http://bokeh.pydata.org

21 © 2015 Gurobi Optimization


Jupyter Demo

22 © 2015 Gurobi Optimization


References
} Visit http://www.gurobi.com/documentation/ for more information on
the Gurobi Python Interface
◦ Quick Start Guide
◦ Reference Manual

} Explore our functional code examples to learn more about advanced


features
◦ Working with sparse models: netflow, workforce
◦ Accessing data sources (databases, spreadsheets, …): diet

} Visit https://docs.python.org/ learn more about Python


◦ Official Python website
◦ Tutorials, documentation, examples, …

} Visit https://www.continuum.io to get started with the Anaconda Python


distribution, which includes iPython and Jupyter

23 © 2015 Gurobi Optimization

Das könnte Ihnen auch gefallen