1.2. Running Pyomo on Google Colab#
Google Colab offers a web-based computational platform for Python and Jupyter notebooks, which can be accessed through a simple web browser. By using this service, users can avoid the burden of setting up and managing a complicated computing environment on their personal computers.
This notebook provides instructions on how to install the pyomo package and open-source solvers on Google Colab. The solvers that will be installed are:
Solver |
Description |
---|---|
LP |
|
MILP |
|
NLP |
|
MINLP |
|
Global MINLP |
Additionally, the notebook tests each solver using a small example problem.
Note: This notebook has been updated to use pyomo and solvers associated with the IDAES project. As of this writing (Feb, 2023), IDAES is undergoing an update to Version 2.0. This notebook will be updated as needed to stay current with IDAES.
1.2.1. Installing Pyomo and Solvers#
An installation of pyomo and solvers needs to be done once at the start of each Colab session. The following cell tests if it is being run on Google Colab and, if so, installs Pyomo and solvers from the IDAES proejct. The cell magic %%capture
captures the lengthy output from the installation scripts. See the documentation for installing the IDAES PSE framework for additional detail.
%%capture
import sys
import os
if 'google.colab' in sys.modules:
!pip install idaes-pse --pre
!idaes get-extensions --to ./bin
os.environ['PATH'] += ':bin'
1.2.2. Test Model#
The installation of pyomo can be verified by entering a simple model. This model is used in subsequent cells to demonstrate the installation and operation of the solvers.
from pyomo.environ import *
# create a model
model = ConcreteModel()
# declare decision variables
model.x = Var(domain=NonNegativeReals)
model.y = Var(domain=NonNegativeReals)
# declare objective
model.profit = Objective(expr = 40*model.x + 30*model.y, sense=maximize)
# declare constraints
model.demand = Constraint(expr = model.x <= 40)
model.laborA = Constraint(expr = model.x + model.y <= 80)
model.laborB = Constraint(expr = 2*model.x + model.y <= 100)
model.pprint()
def display_solution(model):
# display solution
print('\nProfit = ', model.profit())
print('\nDecision Variables')
print('x = ', model.x.value)
print('y = ', model.y())
print('\nConstraints')
print('Demand = ', model.demand())
print('Labor A = ', model.laborA())
print('Labor B = ', model.laborB())
2 Var Declarations
x : Size=1, Index=None
Key : Lower : Value : Upper : Fixed : Stale : Domain
None : 0 : None : None : False : True : NonNegativeReals
y : Size=1, Index=None
Key : Lower : Value : Upper : Fixed : Stale : Domain
None : 0 : None : None : False : True : NonNegativeReals
1 Objective Declarations
profit : Size=1, Index=None, Active=True
Key : Active : Sense : Expression
None : True : maximize : 40*x + 30*y
3 Constraint Declarations
demand : Size=1, Index=None, Active=True
Key : Lower : Body : Upper : Active
None : -Inf : x : 40.0 : True
laborA : Size=1, Index=None, Active=True
Key : Lower : Body : Upper : Active
None : -Inf : x + y : 80.0 : True
laborB : Size=1, Index=None, Active=True
Key : Lower : Body : Upper : Active
None : -Inf : 2*x + y : 100.0 : True
6 Declarations: x y profit demand laborA laborB
1.2.3. COIN-OR Clp#
COIN-OR Clp is a multi-threaded open-source linear programming solver written in C++ and distributed under the Eclipse Public License (EPL). Clp is generally a good choice for linear programs that do not include any binary or integer variables. It is generally a superior alternative to GLPK for linear programming applications.
SolverFactory('clp').solve(model, tee=True).write()
display_solution(model)
Coin LP version 1.17.4, build Nov 3 2022
command line - /content/bin/clp /tmp/tmpwpxnevc3.pyomo.nl -AMPL
CLP 1.17.4: # ==========================================================
# = Solver Results =
# ==========================================================
# ----------------------------------------------------------
# Problem Information
# ----------------------------------------------------------
Problem:
- Lower bound: -inf
Upper bound: inf
Number of objectives: 1
Number of constraints: 3
Number of variables: 2
Sense: unknown
# ----------------------------------------------------------
# Solver Information
# ----------------------------------------------------------
Solver:
- Status: ok
Message: CLP 1.17.4 optimal, objective -2600; 2 iterations
Termination condition: optimal
Id: 0
Error rc: 0
Time: 0.025131702423095703
# ----------------------------------------------------------
# Solution Information
# ----------------------------------------------------------
Solution:
- number of solutions: 0
number of solutions displayed: 0
Profit = 2600.0
Decision Variables
x = 20.0
y = 60.0
Constraints
Demand = 20.0
Labor A = 80.0
Labor B = 100.0
1.2.4. COIN-OR Cbc#
COIN-OR CBC is a multi-threaded open-source Coin-or branch and cut mixed-integer linear programming solver written in C++ under the Eclipse Public License (EPL). CBC is generally a good choice for a general purpose MILP solver for medium to large scale problems. It is generally a superior alternative to GLPK for mixed-integer linear programming applications.
SolverFactory('cbc').solve(model, tee=True).write()
display_solution(model)
Welcome to the CBC MILP Solver
Version: 2.10.4
Build Date: Nov 3 2022
command line - /content/bin/cbc -printingOptions all -import /tmp/tmpvigqpod5.pyomo.lp -stat=1 -solve -solu /tmp/tmpvigqpod5.pyomo.soln (default strategy 1)
Option for printingOptions changed from normal to all
CoinLpIO::readLp(): Maximization problem reformulated as minimization
Coin0009I Switching back to maximization to get correct duals etc
Presolve 2 (-2) rows, 2 (-1) columns and 4 (-2) elements
Statistics for presolved model
Problem has 2 rows, 2 columns (2 with objective) and 4 elements
Column breakdown:
1 of type 0.0->inf, 1 of type 0.0->up, 0 of type lo->inf,
0 of type lo->up, 0 of type free, 0 of type fixed,
0 of type -inf->0.0, 0 of type -inf->up, 0 of type 0.0->1.0
Row breakdown:
0 of type E 0.0, 0 of type E 1.0, 0 of type E -1.0,
0 of type E other, 0 of type G 0.0, 0 of type G 1.0,
0 of type G other, 0 of type L 0.0, 0 of type L 1.0,
2 of type L other, 0 of type Range 0.0->1.0, 0 of type Range other,
0 of type Free
Presolve 2 (-2) rows, 2 (-1) columns and 4 (-2) elements
0 Obj -0 Dual inf 69.999998 (2)
2 Obj 2600
Optimal - objective value 2600
After Postsolve, objective 2600, infeasibilities - dual 0 (0), primal 0 (0)
Optimal objective 2600 - 2 iterations time 0.002, Presolve 0.00
Total time (CPU seconds): 0.00 (Wallclock seconds): 0.00
# ==========================================================
# = Solver Results =
# ==========================================================
# ----------------------------------------------------------
# Problem Information
# ----------------------------------------------------------
Problem:
- Name: unknown
Lower bound: 2600.0
Upper bound: 2600.0
Number of objectives: 1
Number of constraints: 4
Number of variables: 3
Number of nonzeros: 2
Sense: maximize
# ----------------------------------------------------------
# Solver Information
# ----------------------------------------------------------
Solver:
- Status: ok
User time: -1.0
System time: 0.0
Wallclock time: 0.0
Termination condition: optimal
Termination message: Model was solved to optimality (subject to tolerances), and an optimal solution is available.
Statistics:
Branch and bound:
Number of bounded subproblems: None
Number of created subproblems: None
Black box:
Number of iterations: 2
Error rc: 0
Time: 0.046584367752075195
# ----------------------------------------------------------
# Solution Information
# ----------------------------------------------------------
Solution:
- number of solutions: 0
number of solutions displayed: 0
Profit = 2600.0
Decision Variables
x = 20.0
y = 60.0
Constraints
Demand = 20.0
Labor A = 80.0
Labor B = 100.0
1.2.5. COIN-OR Ipopt installation#
COIN-OR Ipopt is an open-source Interior Point Optimizer for large-scale nonlinear optimization available under the Eclipse Public License (EPL). It can solve medium to large scale nonlinear programming problems without integer or binary constraints. Note that performance of Ipopt is dependent on the software library used for the underlying linear algebra computations.
SolverFactory('ipopt').solve(model, tee=True).write()
display_solution(model)
Ipopt 3.13.2:
******************************************************************************
This program contains Ipopt, a library for large-scale nonlinear optimization.
Ipopt is released as open source code under the Eclipse Public License (EPL).
For more information visit http://projects.coin-or.org/Ipopt
This version of Ipopt was compiled from source code available at
https://github.com/IDAES/Ipopt as part of the Institute for the Design of
Advanced Energy Systems Process Systems Engineering Framework (IDAES PSE
Framework) Copyright (c) 2018-2019. See https://github.com/IDAES/idaes-pse.
This version of Ipopt was compiled using HSL, a collection of Fortran codes
for large-scale scientific computation. All technical papers, sales and
publicity material resulting from use of the HSL codes within IPOPT must
contain the following acknowledgement:
HSL, a collection of Fortran codes for large-scale scientific
computation. See http://www.hsl.rl.ac.uk.
******************************************************************************
This is Ipopt version 3.13.2, running with linear solver ma27.
Number of nonzeros in equality constraint Jacobian...: 0
Number of nonzeros in inequality constraint Jacobian.: 5
Number of nonzeros in Lagrangian Hessian.............: 0
Total number of variables............................: 2
variables with only lower bounds: 2
variables with lower and upper bounds: 0
variables with only upper bounds: 0
Total number of equality constraints.................: 0
Total number of inequality constraints...............: 3
inequality constraints with only lower bounds: 0
inequality constraints with lower and upper bounds: 0
inequality constraints with only upper bounds: 3
iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls
0 -2.6000000e+03 0.00e+00 1.17e+01 -1.0 0.00e+00 - 0.00e+00 0.00e+00 0
1 -2.6200900e+03 7.50e-01 6.34e+00 -1.0 1.88e+01 - 7.55e-01 5.27e-02f 1
2 -2.6035835e+03 1.39e-01 1.27e+00 -1.0 1.07e+00 - 9.86e-01 8.49e-01h 1
3 -2.5997998e+03 0.00e+00 1.00e-06 -1.0 8.12e-02 - 1.00e+00 1.00e+00h 1
4 -2.5999944e+03 0.00e+00 2.83e-08 -2.5 9.74e-03 - 1.00e+00 1.00e+00f 1
5 -2.5999997e+03 0.00e+00 1.50e-09 -3.8 2.66e-04 - 1.00e+00 1.00e+00f 1
6 -2.6000000e+03 0.00e+00 1.85e-11 -5.7 1.49e-05 - 1.00e+00 1.00e+00f 1
7 -2.6000000e+03 0.00e+00 2.78e-14 -8.6 1.84e-07 - 1.00e+00 1.00e+00f 1
Number of Iterations....: 7
(scaled) (unscaled)
Objective...............: -2.6000000259949879e+03 -2.6000000259949879e+03
Dual infeasibility......: 2.7778601068099499e-14 2.7778601068099499e-14
Constraint violation....: 0.0000000000000000e+00 0.0000000000000000e+00
Complementarity.........: 2.5059421204891839e-09 2.5059421204891839e-09
Overall NLP error.......: 2.5059421204891839e-09 2.5059421204891839e-09
Number of objective function evaluations = 8
Number of objective gradient evaluations = 8
Number of equality constraint evaluations = 0
Number of inequality constraint evaluations = 8
Number of equality constraint Jacobian evaluations = 0
Number of inequality constraint Jacobian evaluations = 8
Number of Lagrangian Hessian evaluations = 7
Total CPU secs in IPOPT (w/o function evaluations) = 0.005
Total CPU secs in NLP function evaluations = 0.000
EXIT: Optimal Solution Found.
# ==========================================================
# = Solver Results =
# ==========================================================
# ----------------------------------------------------------
# Problem Information
# ----------------------------------------------------------
Problem:
- Lower bound: -inf
Upper bound: inf
Number of objectives: 1
Number of constraints: 3
Number of variables: 2
Sense: unknown
# ----------------------------------------------------------
# Solver Information
# ----------------------------------------------------------
Solver:
- Status: ok
Message: Ipopt 3.13.2\x3a Optimal Solution Found
Termination condition: optimal
Id: 0
Error rc: 0
Time: 0.0486142635345459
# ----------------------------------------------------------
# Solution Information
# ----------------------------------------------------------
Solution:
- number of solutions: 0
number of solutions displayed: 0
Profit = 2600.000025994988
Decision Variables
x = 20.0000001998747
y = 60.0000006
Constraints
Demand = 20.0000001998747
Labor A = 80.0000007998747
Labor B = 100.0000009997494
1.2.6. COIN-OR Bonmin installation#
COIN-OR Bonmin is a basic open-source solver for nonlinear mixed-integer programming problems (MINLP). It utilizes CBC and Ipopt for solving relaxed subproblems.
SolverFactory('bonmin').solve(model, tee=True).write()
display_solution(model)
Bonmin 1.8.8 using Cbc 2.10.4 and Ipopt 3.13.2
bonmin:
Cbc3007W No integer variables - nothing to do
******************************************************************************
This program contains Ipopt, a library for large-scale nonlinear optimization.
Ipopt is released as open source code under the Eclipse Public License (EPL).
For more information visit http://projects.coin-or.org/Ipopt
This version of Ipopt was compiled from source code available at
https://github.com/IDAES/Ipopt as part of the Institute for the Design of
Advanced Energy Systems Process Systems Engineering Framework (IDAES PSE
Framework) Copyright (c) 2018-2019. See https://github.com/IDAES/idaes-pse.
This version of Ipopt was compiled using HSL, a collection of Fortran codes
for large-scale scientific computation. All technical papers, sales and
publicity material resulting from use of the HSL codes within IPOPT must
contain the following acknowledgement:
HSL, a collection of Fortran codes for large-scale scientific
computation. See http://www.hsl.rl.ac.uk.
******************************************************************************
NLP0012I
Num Status Obj It time Location
NLP0014I 1 OPT -2600 6 0.001228
Cbc3007W No integer variables - nothing to do
"Finished"
# ==========================================================
# = Solver Results =
# ==========================================================
# ----------------------------------------------------------
# Problem Information
# ----------------------------------------------------------
Problem:
- Lower bound: -inf
Upper bound: inf
Number of objectives: 1
Number of constraints: 0
Number of variables: 2
Sense: unknown
# ----------------------------------------------------------
# Solver Information
# ----------------------------------------------------------
Solver:
- Status: ok
Message: bonmin\x3a Optimal
Termination condition: optimal
Id: 3
Error rc: 0
Time: 0.07909822463989258
# ----------------------------------------------------------
# Solution Information
# ----------------------------------------------------------
Solution:
- number of solutions: 0
number of solutions displayed: 0
Profit = 2600.0000259999797
Decision Variables
x = 20.000000199999512
y = 60.00000059999998
Constraints
Demand = 20.000000199999512
Labor A = 80.0000007999995
Labor B = 100.000000999999
1.2.7. COIN-OR Couenne installation#
COIN-OR Couenne is attempts to find global optima for mixed-integer nonlinear programming problems (MINLP).
SolverFactory('couenne').solve(model, tee=True).write()
display_solution(model)
Couenne 0.5.8 -- an Open-Source solver for Mixed Integer Nonlinear Optimization
Mailing list: couenne@list.coin-or.org
Instructions: http://www.coin-or.org/Couenne
couenne:
ANALYSIS TEST: Couenne: new cutoff value -2.6000000020e+03 (0.01268 seconds)
NLP0012I
Num Status Obj It time Location
NLP0014I 1 OPT -2600 6 0.003378
Couenne: new cutoff value -2.6000000260e+03 (0.016797 seconds)
Loaded instance "/tmp/tmpo639i8wk.pyomo.nl"
Constraints: 3
Variables: 2 (0 integer)
Auxiliaries: 2 (0 integer)
Coin0506I Presolve 0 (-3) rows, 0 (-4) columns and 0 (-8) elements
Clp0000I Optimal - objective value -2600
Clp0032I Optimal objective -2600 - 0 iterations time 0.002, Presolve 0.00
Cbc3007W No integer variables - nothing to do
Clp0000I Optimal - objective value -2600
"Finished"
Linearization cuts added at root node: 3
Linearization cuts added in total: 3 (separation time: 6.6e-05s)
Total solve time: 0.00059s (0.000588s in branch-and-bound)
Lower bound: -2600
Upper bound: -2600 (gap: 0.00%)
Branch-and-bound nodes: 0
Performance of FBBT: 0.0002s, 3 runs. fix: 0 shrnk: 8.10732 ubd: 0 2ubd: 0 infeas: 0
# ==========================================================
# = Solver Results =
# ==========================================================
# ----------------------------------------------------------
# Problem Information
# ----------------------------------------------------------
Problem:
- Lower bound: -inf
Upper bound: inf
Number of objectives: 1
Number of constraints: 0
Number of variables: 2
Sense: unknown
# ----------------------------------------------------------
# Solver Information
# ----------------------------------------------------------
Solver:
- Status: ok
Message: couenne\x3a Optimal
Termination condition: optimal
Id: 3
Error rc: 0
Time: 0.048601627349853516
# ----------------------------------------------------------
# Solution Information
# ----------------------------------------------------------
Solution:
- number of solutions: 0
number of solutions displayed: 0
Profit = 2600.00002599998
Decision Variables
x = 20.000000199999516
y = 60.000000599999986
Constraints
Demand = 20.000000199999516
Labor A = 80.0000007999995
Labor B = 100.00000099999902