0% found this document useful (0 votes)
4 views44 pages

Week 3 Python Pyomo

The document provides a comprehensive guide on using Pyomo for optimization modeling in Python, including installation instructions for Anaconda3 and Pyomo, as well as various optimization packages. It details the core components of Pyomo models, such as variables, objectives, constraints, and expressions, and explains how to define and solve optimization problems using different solvers. Additionally, it outlines the syntax for creating models and the expected statuses upon solving them.

Uploaded by

leotsai2802
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views44 pages

Week 3 Python Pyomo

The document provides a comprehensive guide on using Pyomo for optimization modeling in Python, including installation instructions for Anaconda3 and Pyomo, as well as various optimization packages. It details the core components of Pyomo models, such as variables, objectives, constraints, and expressions, and explains how to define and solve optimization problems using different solvers. Additionally, it outlines the syntax for creating models and the expected statuses upon solving them.

Uploaded by

leotsai2802
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Siamak Naderi

Optimisation Models
Use Anaconda3

• Install Anaconda3
– [Link]
– Windows/Mac OS X/Linux
– Includes several packages for scientific computing already
– Supports easy installation of Pyomo and solvers

• Install Pyomo, solvers, and other packages:


– From a terminal window, type:
1. conda install -c [Link] [Link]
2. conda install glpkipopt_bin-c cachemeorg
Different optimisation packages in Python
• PuLP
– Supports concrete modeling for LP/MILP models
• APLEpy
– Supports concrete modeling for LP/MILP models
• PyMathProg, pyglpk, cplex, gurobi 
– Python interfaces for specific solver tools
• Pyomo 
– Supports concrete/abstract modeling for LP/MILP/NLP models 
– Modeling extensions for stochastic programming, bilevel, MPEC, etc
Pyomo

• It is Pythonic and open source


• Solver agnostic: can use multiple open source or commercial solvers
(GLPK, IPOPT, Gurobi, CPLEX, …)
• High level API
• Extended documentation
• Can solve advanced optimisation problems (LP, NLP, IP, …)
Install with pip in Python

• Install Pyomo
– pip install Pyomo
Install with pip in Python

• Install Pyomo
– pip install Pyomo
Install with pip in Python

• Install Pyomo
– pip install Pyomo
Pyomo
• Pyomo objects exist within the [Link] namespace:
Pyomo
• Pyomo objects exist within the [Link] namespace:
– import [Link]
– model = [Link]()
Pyomo
• Pyomo objects exist within the [Link] namespace:
– import [Link]
– model = [Link]()

• To save time in terms of typing, we will import the core Pyomo classes into the main
namespace:
Pyomo
• Pyomo objects exist within the [Link] namespace:
– import [Link]
– model = [Link]()

• To save time in terms of typing, we will import the core Pyomo classes into the main
namespace:
– from [Link] import *
– model = ConcreteModel()
Pyomo
• Pyomo objects exist within the [Link] namespace:
– import [Link]
– model = [Link]()

• To save time in terms of typing, we will import the core Pyomo classes into the main
namespace:
– from [Link] import *
– model = ConcreteModel()

• To clarify Pyomo-specific syntax, Pyomo symbols are highlighted in green

• Concrete versus Abstract models


Core modelling components in Pyomo
• Var: The Var component is used to represent optimization decision variables. Pyomo supports
continuous and integer variables and includes several pre-defined domains.

13
Core modelling components in Pyomo
• Var: The Var component is used to represent optimization decision variables. Pyomo supports
continuous and integer variables and includes several pre-defined domains.
• Objective: The Objective component defines the function to be optimized by the solver. This
component contains the expression used to define the objective function, and a flag to indicate the
sense (maximise or minimise).

14
Core modelling components in Pyomo
• Var: The Var component is used to represent optimization decision variables. Pyomo supports
continuous and integer variables and includes several pre-defined domains.
• Objective: The Objective component defines the function to be optimized by the solver. This
component contains the expression used to define the objective function, and a flag to indicate the
sense (maximise or minimise).
• Constraint: Constraints are used to define additional restrictions on the decision variables. The
Constraint component contains expressions and the appropriate relational operator. Pyomo
supports equality (==) and general inequality (<= or >=) constraints.

15
Core modelling components in Pyomo
• Var: The Var component is used to represent optimization decision variables. Pyomo supports
continuous and integer variables and includes several pre-defined domains.
• Objective: The Objective component defines the function to be optimized by the solver. This
component contains the expression used to define the objective function, and a flag to indicate the
sense (maximise or minimise).
• Constraint: Constraints are used to define additional restrictions on the decision variables. The
Constraint component contains expressions and the appropriate relational operator. Pyomo
supports equality (==) and general inequality (<= or >=) constraints.
• Expression: The Expression component can be used to create a Pyomo expression that can be
reused in different parts of a Pyomo model. This is useful for representing common sub-expressions
for memory efficiency. Similar to mutable parameters, the underlying expression can be changed
between calls to the solver.

16
Core modelling components in Pyomo
• Var: The Var component is used to represent optimization decision variables. Pyomo supports
continuous and integer variables and includes several pre-defined domains.
• Objective: The Objective component defines the function to be optimized by the solver. This
component contains the expression used to define the objective function, and a flag to indicate the
sense (maximise or minimise).
• Constraint: Constraints are used to define additional restrictions on the decision variables. The
Constraint component contains expressions and the appropriate relational operator. Pyomo
supports equality (==) and general inequality (<= or >=) constraints.
• Expression: The Expression component can be used to create a Pyomo expression that can be
reused in different parts of a Pyomo model. This is useful for representing common sub-expressions
for memory efficiency. Similar to mutable parameters, the underlying expression can be changed
between calls to the solver.
• Set: The Set component represents a collection of data that can include numeric (e.g., integer), or
symbolic (e.g., string) elements. They are most commonly used to define valid indices for other
components. Several common set operations are also supported.
17
Pyomo: the model
All Pyomo model starts with this; it
from [Link] import * tells Python to load the Pyomo
Modeling Environment
Pyomo: the model
All Pyomo model starts with this; it
from [Link] import * tells Python to load the Pyomo
Modeling Environment

model = ConcreteModel()
Pyomo: the model
All Pyomo model starts with this; it
from [Link] import * tells Python to load the Pyomo
Modeling Environment

model = ConcreteModel() Creates an empty sheet as model


Pyomo: the model
All Pyomo model starts with this; it
from [Link] import * tells Python to load the Pyomo
Modeling Environment

model = ConcreteModel() Creates an empty sheet as model

Local variable to hold the model we are about to construct


• While not required, by convention we use “model”
• If you choose to name your model something else, you will
need to tell the Pyomo script the object name through the
command line
Pyomo: the model
All Pyomo model starts with this; it
from [Link] import * tells Python to load the Pyomo
Modeling Environment

model = ConcreteModel() Creates an empty sheet as model

Create an instance of a Concrete model


• Concrete models are immediately constructed
• Data must be present at the time components are defined

Local variable to hold the model we are about to construct


• While not required, by convention we use “model”
• If you choose to name your model something else, you will
need to tell the Pyomo script the object name through the
command line
Pyomo: the variables
model.x_variable = Var(within = NonNegativeReals)
Pyomo: the variables
model.x_variable

A reference
to my blank
model
Pyomo: the variables
model.x_variable

A reference I want to add


to my blank something to
model the model
Pyomo: the variables
model.x_variable

A reference I want to add The thing I am


to my blank something to adding is called
model the model x_variable
Pyomo: the variables
model.x_variable = Var(within = NonNegativeReals)

The name you assign


the object to becomes A Pyomo function
the object’s name, for making
and must be unique in decision variables
any given model.
Pyomo: the variables
model.x_variable = Var(within = NonNegativeReals)

The name you assign “within” is optional


the object to becomes and sets the
the object’s name, variable domain (“
and must be unique in domain” is an alias
any given model. for “within”)
Pyomo: the variables
model.x_variable = Var(within = NonNegativeReals)

The name you assign “within” is optional


the object to becomes and sets the Several pre-defined
the object’s name, variable domain (“ domains, e.g.,
and must be unique in domain” is an alias
any given model. for “within”)

From: Pyomo – Optimization modeling in Python


Pyomo: the variables
model.x_variable = Var(within = NonNegativeReals)

The name you assign “within” is optional


the object to becomes and sets the Several pre-defined
the object’s name, variable domain (“ domains, e.g.,
and must be unique in domain” is an alias
any given model. for “within”)

From: Pyomo – Optimization modeling in Python

• model.x_variable = Var(within or domain, bounds, initialize)


• model.x_variable = Var(bounds = (0, None))
Pyomo: the objective

[Link] = Objective(
expr = This is your objective function, sense = minimize or maximize)

31
Pyomo: the objective

[Link] = Objective(
expr = This is your objective function, sense = minimize or maximize)

model.x = Var(bounds = (0, None))


model.y = Var(within = NonNegativeReals)

32
Pyomo: the objective

[Link] = Objective(
expr = This is your objective function, sense = minimize or maximize)

model.x = Var(bounds = (0, None))


model.y = Var(within = NonNegativeReals)

[Link] = Objective(
expr = 2*model.x – 3*model.y, sense = minimize)

33
Pyomo: the constraints

model.constraint_name = Constraint(
expr = This is your constraint – either equality or inequality)

34
Pyomo: the constraints

model.constraint_name = Constraint(
expr = This is your constraint – either equality or inequality)
“expr” can be an
expression, or any
function like object that
returns an expression

35
Pyomo: the constraints

model.constraint_name = Constraint(
expr = This is your constraint – either equality or inequality)
“expr” can be an
expression, or any
function like object that
returns an expression

model.x = Var(bounds = (0, None))


model.y = Var(within = NonNegativeReals)

36
Pyomo: the constraints

model.constraint_name = Constraint(
expr = This is your constraint – either equality or inequality)
“expr” can be an
expression, or any
function like object that
returns an expression

model.x = Var(bounds = (0, None))


model.y = Var(within = NonNegativeReals)

[Link] = Constraint(expr = -1*model.x + 2*model.y <= 10)


[Link] = Constraint(expr = 2*model.x - model.y == 13)

37
Pyomo: the constraints

[Link] = Constraintlist()

[Link](-1*model.x + 2*model.y <= 10)


[Link](2*model.x - model.y == 13)

38
Pyomo: how to solve the model?
• opt = SolverFactory('glpk')

This is solver. There are


different solvers for Pyomo,
e.g., GUROBI, CPLEX, cbc,
ipopt, etc.
Glpk is one of them that is
open source and can handle
LP/MILP models.

39
Pyomo: how to solve the model?
• opt = SolverFactory('glpk')
• [Link](model)
This is solver. There are
different solvers for Pyomo,
e.g., GUROBI, CPLEX, cbc,
ipopt, etc.
Glpk is one of them that is
open source and can handle
LP/MILP models.

40
Pyomo: how to solve the model?
• opt = SolverFactory('glpk')
• [Link](model)
This is solver. There are
different solvers for Pyomo,
e.g., GUROBI, CPLEX, cbc,
ipopt, etc.
Glpk is one of them that is
This command will solve open source and can handle
the model you have LP/MILP models.
built.

41
Model status

ok Normal termination

warning Termination with unusual condition

error Terminated internally with error

aborted Terminated due to external conditions (e.g., interrupts)

unknown Unknown (an uninitialized value)

42
Termination conditions
maxTimeLimit Exceeded maximum time limit allowed
maxIterations Exceeded maximum number of iterations allowed
minFunctionValue Found solution smaller than specified function value
minStepLength Step length is smaller than specified limit
globallyOptimal Found a globally optimal solution
locallyOptimal Found a locally optimal solution
optimal Found an optimal solution
maxEvaluations Exceeded maximum number of problem evaluations (e.g., branch and bound nodes)

other Other, uncategorized normal termination


unbounded Demonstrated that problem is unbounded
infeasible Demonstrated that problem is infeasible
invalidProblem The problem setup or characteristics are not valid for the solver
solverFailure Solver failed to terminate correctly
internalSolverError Internal solver error
error Other error
userInterrupt Interrupt signal generated by user
resourceInterrupt Interrupt signal in resources used by the solver
licensingProblem Problem accessing solver license 43
Solve this problem using Pyomo

max z = 3*x1 + 2*x2

Subject to:
x1 + 2x2  6
2x1 + x2  8
x2  1
-x1 + x2  1
x1,x2  0

44

You might also like