Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
personal:blog:2014:0904_pyomo_for_gams_users [2014/09/12 16:26] antonello [A Pyomo tutorial for GAMS users] |
personal:blog:2014:0904_pyomo_for_gams_users [2015/01/12 16:17] antonello [Installation] |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== A Pyomo tutorial for GAMS users ====== | ====== A Pyomo tutorial for GAMS users ====== | ||
+ | |||
+ | **Updates: | ||
+ | |||
+ | * **2015.01.12: | ||
+ | |||
+ | |||
[[https:// | [[https:// | ||
Line 17: | Line 23: | ||
- | So let's start. We will see how to code the trasnport.gms problem, the one that ship as default example in GAMS((yes, the default GAMS example | + | So let's start. We will see how to code the trasnport.gms problem, the one that ship as default example in GAMS((yes, the default GAMS example |
GAMS equivalent code is inserted as single-dash comments. The original GAMS code needs slightly different ordering of the commands and it's available at [[http:// | GAMS equivalent code is inserted as single-dash comments. The original GAMS code needs slightly different ordering of the commands and it's available at [[http:// | ||
===== Installation ===== | ===== Installation ===== | ||
- | **Important: | + | **<del>Important: Pyomo requires python 2.x. While python 3.x support is work in progress, at the moment only python 2.x is supported.</ |
+ | |||
+ | //This isn't true any more with Pyomo 4, where support for Python 3.x has been added..// | ||
==== Ubuntu ==== | ==== Ubuntu ==== | ||
Line 30: | Line 38: | ||
* '' | * '' | ||
* **Install pyomo:** | * **Install pyomo:** | ||
- | * '' | + | * '' |
- | * '' | + | * '' |
* **Install solvers:** | * **Install solvers:** | ||
* //linear and MIP solver (glpk)//: '' | * //linear and MIP solver (glpk)//: '' | ||
Line 44: | Line 52: | ||
In pyomo everything is an object. The various components of the model (sets, parameters, variables, constrains, objective..) are all attributes of the main model object while being objects themselves.\\ | In pyomo everything is an object. The various components of the model (sets, parameters, variables, constrains, objective..) are all attributes of the main model object while being objects themselves.\\ | ||
There are two type of models in pyomo: A '' | There are two type of models in pyomo: A '' | ||
- | The first thing to do in the script is hence to load the pyomo library and to create a new ConcreteModel (we have little imagination here, and we call our model " | + | The first thing to do in the script is hence to load the pyomo library and to create a new ConcreteModel (we have little imagination here, and we call our model " |
<code python> | <code python> | ||
# Import of the pyomo module | # Import of the pyomo module | ||
- | from coopr.pyomo import * | + | from pyomo.environ |
| | ||
# Creation of a Concrete Model | # Creation of a Concrete Model | ||
Line 65: | Line 73: | ||
==== Parameters ==== | ==== Parameters ==== | ||
- | Parameter objects are created specifying the sets over which they are defined and are initialised with either a python | + | Parameter objects are created specifying the sets over which they are defined and are initialised with either a python |
<code python> | <code python> | ||
## Define parameters ## | ## Define parameters ## | ||
Line 129: | Line 137: | ||
def demand_rule(model, | def demand_rule(model, | ||
return sum(model.x[i, | return sum(model.x[i, | ||
- | model.demand = Constraint(model.j, | + | model.demand = Constraint(model.j, rule=demand_rule, doc=' |
</ | </ | ||
The above code take advantage of [[https:// | The above code take advantage of [[https:// | ||
Line 180: | Line 188: | ||
If you want advanced features and debugging capabilities you can use a dedicated Python IDE, like e.g. [[https:// | If you want advanced features and debugging capabilities you can use a dedicated Python IDE, like e.g. [[https:// | ||
- | You will normally run the script as '' | + | You will normally run the script as '' |
If you want to run the script as '' | If you want to run the script as '' | ||
<code python> | <code python> | ||
Line 187: | Line 195: | ||
if __name__ == ' | if __name__ == ' | ||
#This replicates what the pyomo command-line tools does | #This replicates what the pyomo command-line tools does | ||
- | from coopr.opt import SolverFactory | + | from pyomo.opt import SolverFactory |
- | import | + | import |
opt = SolverFactory(" | opt = SolverFactory(" | ||
instance = model.create() | instance = model.create() | ||
Line 214: | Line 222: | ||
# | # | ||
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
+ | |||
""" | """ | ||
Basic example of transport model from GAMS model library translated to Pyomo | Basic example of transport model from GAMS model library translated to Pyomo | ||
+ | |||
To run this you need pyomo and a linear solver installed. | To run this you need pyomo and a linear solver installed. | ||
When these dependencies are installed you can solve this example in one of | When these dependencies are installed you can solve this example in one of | ||
this ways (glpk is the default solver): | this ways (glpk is the default solver): | ||
+ | |||
./ | ./ | ||
python transport.py | python transport.py | ||
- | pyomo transport.py | + | pyomo solve transport.py |
- | pyomo --solver=glpk transport.py | + | pyomo solve --solver=glpk transport.py |
+ | |||
To display the results: | To display the results: | ||
+ | |||
cat results.json | cat results.json | ||
cat results.yml (if PyYAML is installed on your system) | cat results.yml (if PyYAML is installed on your system) | ||
+ | |||
GAMS equivalent code is inserted as single-dash comments. The original GAMS code | GAMS equivalent code is inserted as single-dash comments. The original GAMS code | ||
needs slighly different ordering of the commands and it's available at | needs slighly different ordering of the commands and it's available at | ||
http:// | http:// | ||
+ | |||
Original problem formulation: | Original problem formulation: | ||
Dantzig, G B, Chapter 3.3. In Linear Programming and Extensions. | Dantzig, G B, Chapter 3.3. In Linear Programming and Extensions. | ||
Line 244: | Line 252: | ||
Pyomo translation: | Pyomo translation: | ||
Antonello Lobianco | Antonello Lobianco | ||
+ | |||
This file is in the Public Domain | This file is in the Public Domain | ||
""" | """ | ||
+ | |||
# Import | # Import | ||
- | from coopr.pyomo import * | + | from pyomo.environ |
- | + | ||
# Creation of a Concrete Model | # Creation of a Concrete Model | ||
model = ConcreteModel() | model = ConcreteModel() | ||
+ | |||
## Define sets ## | ## Define sets ## | ||
# Sets | # Sets | ||
Line 260: | Line 268: | ||
model.i = Set(initialize=[' | model.i = Set(initialize=[' | ||
model.j = Set(initialize=[' | model.j = Set(initialize=[' | ||
+ | |||
## Define parameters ## | ## Define parameters ## | ||
# | # | ||
Line 292: | Line 300: | ||
return model.f * model.d[i, | return model.f * model.d[i, | ||
model.c = Param(model.i, | model.c = Param(model.i, | ||
+ | |||
## Define variables ## | ## Define variables ## | ||
# Variables | # Variables | ||
Line 299: | Line 307: | ||
# Positive Variable x ; | # Positive Variable x ; | ||
model.x = Var(model.i, | model.x = Var(model.i, | ||
+ | |||
## Define contrains ## | ## Define contrains ## | ||
# supply(i) | # supply(i) | ||
Line 310: | Line 318: | ||
def demand_rule(model, | def demand_rule(model, | ||
return sum(model.x[i, | return sum(model.x[i, | ||
- | model.demand = Constraint(model.j, | + | model.demand = Constraint(model.j, rule=demand_rule, doc=' |
## Define Objective and solve ## | ## Define Objective and solve ## | ||
Line 321: | Line 329: | ||
model.objective = Objective(rule=objective_rule, | model.objective = Objective(rule=objective_rule, | ||
+ | |||
## Display of the output ## | ## Display of the output ## | ||
# Display x.l, x.m ; | # Display x.l, x.m ; | ||
def pyomo_postprocess(options=None, | def pyomo_postprocess(options=None, | ||
model.x.display() | model.x.display() | ||
+ | |||
# This is an optional code path that allows the script to be run outside of | # This is an optional code path that allows the script to be run outside of | ||
# pyomo command-line. | # pyomo command-line. | ||
if __name__ == ' | if __name__ == ' | ||
- | + | ||
#This replicates what the pyomo command-line tools does | #This replicates what the pyomo command-line tools does | ||
- | from coopr.opt import SolverFactory | + | from pyomo.opt import SolverFactory |
- | import | + | import |
opt = SolverFactory(" | opt = SolverFactory(" | ||
instance = model.create() | instance = model.create() | ||
Line 339: | Line 348: | ||
results.write() | results.write() | ||
pyomo_postprocess(None, | pyomo_postprocess(None, | ||
- | + | ||
# Expected result: | # Expected result: | ||
# obj= 153.675 | # obj= 153.675 |