Python I - Intro To Python Modeling
Python I - Intro To Python Modeling
Webinar Overview
Part 1. Introduction
An overview of Python, why you should consider using Python and getting started
• In this webinar we will furthermore use a rich graphical interactive environment instead of the
command line shell: Jupyter Notebooks.
• The Jupyter Notebook is a web application that allows you to create and share documents that
contain live code, equations, visualizations and explanatory text.
http://www.gurobi.com/documentation/current/quickstart_windows/installing_the_anaconda_py.html
• Manual installation:
• Change to GUROBI_HOME directory
• Run python setup.py install
Image: http://optano.net/en/production-planning/
• Goals:
• Make yourself familiar with Jupyter Notebook
• Solve a model file in MPS format
• Steps:
• You can choose any name for the Model object variable:
• Attributes are the primary mechanism for querying and Constraints Attributes
• Primary objects
• Model the model
• Var a variable
• Constr a constraint
• Parameter/attribute examples:
• setParam('TimeLimit', 60) Set global parameter
• model.TimeLimit = 60 Set local parameter
• variable.VarName = 'TotalCosts' Set variable attribute
• constraint.RHS = 0 Set constraint attribute
• For performance reasons we recommend the following structure when building a model instance
with the Gurobi Python API:
• The Python API supports (linear and quadratic) expressions similar to the mathematical notation:
• Simple example:
• You want to decide about three activities
(do or don‘t do) and aim for maximum value
• You need to choose at least activity 1 or 2 (or both)
• The total time limit is 4 hours
• Activity 1 takes 1 hour
• Activity 2 takes 2 hours
• Activity 3 takes 3 hours
• Activity 3 is worth twice as much as 1 and 2
• Ex: IndentationError raised when block spacing is incorrect (must indent before if)
for v in model.getVars():
if v.x != 0:
File "<stdin>", line 2
if v.x != 0:
^
IndentationError: expected an indented block
• Loops iterate over collections of elements • Combining loops with sums is ideal when building
(list, dictionary, …) constraints
• Useful when representing the for-all modeling construct
• Example: Example:
for c in cities:
print c # must indent all loop statements can be built with
• In production planning problems, choices must be made about how many of what
products to produce using what resources (variables) in order to maximize profits or
minimize costs (objective function), while meeting a range of constraints. These
problems are common across a broad range of manufacturing situations.
• We will develop the mathematical model, the Python Implementation and a nice tabular
output of the result all within a single Jupyter Notebook.
http://www.gurobi.com/documentation/current/refman/index.html
http://www.gurobi.com/resources/examples/example-models-overview