Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
personal:blog:2017:0203_jump_for_gams_users [2017/02/03 14:29] antonello |
personal:blog:2017:0203_jump_for_gams_users [2023/12/22 11:12] antonello [Complete script] |
||
---|---|---|---|
Line 35: | Line 35: | ||
Pkg.add(" | Pkg.add(" | ||
Pkg.add(" | Pkg.add(" | ||
- | Pkg.add(" | + | Pkg.add(" |
</ | </ | ||
Line 44: | Line 44: | ||
You will need to import as a minima the '' | You will need to import as a minima the '' | ||
- | <code julia> | + | < |
- | # Import of the JuMP and DataFrames modules (the latter one just to import the data from a header-based table, as in the original trasnport example in GAMS | + | # Import of the JuMP and DataFrames modules (the latter one just to import the data from a header based table, as in the original trasnport example in GAMS |
using JuMP, DataFrames | using JuMP, DataFrames | ||
- | < | + | </code> |
==== Defining the " | ==== Defining the " | ||
+ | |||
JuMP doesn' | JuMP doesn' | ||
While many works with position-based lists, I find more readable using dictionaries instead. So the " | While many works with position-based lists, I find more readable using dictionaries instead. So the " | ||
Line 55: | Line 56: | ||
<code julia> | <code julia> | ||
- | # Sets | + | # Define sets # |
+ | # | ||
+ | # | ||
+ | # | ||
plants | plants | ||
markets = [" | markets = [" | ||
Line 61: | Line 65: | ||
- | ==== Defining | + | ==== Definition of the " |
- | Capacity of plants and demand of markets are directly defined as dictionaries, | + | |
+ | Capacity of plants and demand of markets are directly defined as dictionaries, | ||
<code julia> | <code julia> | ||
- | # Parameters | + | # Define parameters # |
+ | # Parameters | ||
+ | # | ||
+ | # / | ||
+ | # san-diego | ||
a = Dict( # capacity of plant i in cases | a = Dict( # capacity of plant i in cases | ||
" | " | ||
" | " | ||
) | ) | ||
+ | |||
+ | # | ||
+ | # / | ||
+ | # chicago | ||
+ | # topeka | ||
b = Dict( # demand at market j in cases | b = Dict( # demand at market j in cases | ||
" | " | ||
Line 76: | Line 90: | ||
) | ) | ||
- | # distance in thousands of miles | + | # Table d(i, |
+ | # new-york | ||
+ | # seattle | ||
+ | # san-diego | ||
d_table = wsv""" | d_table = wsv""" | ||
plants | plants | ||
Line 83: | Line 100: | ||
""" | """ | ||
d = Dict( (r[: | d = Dict( (r[: | ||
+ | # Here we are converting the table in a " | ||
# r[: | # r[: | ||
# m: the second key | # m: the second key | ||
# r[Symbol(m)]: | # r[Symbol(m)]: | ||
+ | # Scalar f freight in dollars per case per thousand miles /90/ ; | ||
f = 90 # freight in dollars per case per thousand miles | f = 90 # freight in dollars per case per thousand miles | ||
+ | # Parameter c(i, | ||
+ | # c(i,j) = f * d(i,j) / 1000 ; | ||
# We first declare an empty dictionary and then we fill it with the values | # We first declare an empty dictionary and then we fill it with the values | ||
c = Dict() # transport cost in thousands of dollars per case ; | c = Dict() # transport cost in thousands of dollars per case ; | ||
[ c[p,m] = f * d[p,m] / 1000 for p in plants, m in markets] | [ c[p,m] = f * d[p,m] / 1000 for p in plants, m in markets] | ||
</ | </ | ||
+ | The above code take advantage of [[http:// | ||
+ | If we take the creation of the d dictionary as example, without List Comprehensions we would have had to write a nested for loop like: | ||
+ | <code julia> | ||
+ | d = Dict() | ||
+ | for r in eachrow(d_table) | ||
+ | for m in markets | ||
+ | d = (r[: | ||
+ | end | ||
+ | end | ||
+ | </ | ||
+ | Using List Comprehension is however quicker to code and more readable. | ||
- | # Model declaration | + | ==== Declaration of the model ==== |
- | trmodel | + | |
- | # Variables | + | Here we declare a JuML optimisation model and we give it a name. This name will be then passed as first argument to all the subsequent operations, like creation of variables, constraints and objective function.\\ |
+ | We can, if we wish, works with several models at the same time.\\ | ||
+ | If we do not specify a solver, we let JuML use a suitable solver for the type of problem. Aside to specify the solver, we can also pass it solver-level options, e.g.: | ||
+ | '' | ||
+ | |||
+ | <code julia> | ||
+ | # Model declaration (transport model) | ||
+ | trmodel = Model() | ||
+ | |||
+ | </ | ||
+ | |||
+ | ==== Declaration of the model variables ==== | ||
+ | |||
+ | Variables | ||
+ | Differently from GAMS, we don't need to define the variable that is on the left hand side of the objective function. | ||
+ | |||
+ | <code julia> | ||
+ | ## Define variables ## | ||
+ | # Variables | ||
+ | # | ||
+ | # | ||
+ | # Positive Variable x ; | ||
@variables trmodel begin | @variables trmodel begin | ||
x[p in plants, m in markets] >= 0 # shipment quantities in cases | x[p in plants, m in markets] >= 0 # shipment quantities in cases | ||
end | end | ||
+ | </ | ||
- | # Constraints | + | ==== Declaration of the model constraints ==== |
+ | |||
+ | As in GAMS, each constraint can actually be a " | ||
+ | |||
+ | <code julia> | ||
+ | ## Define contrains ## | ||
+ | # supply(i) | ||
+ | # supply(i) .. sum (j, x(i,j)) =l= a(i) | ||
+ | # demand(j) | ||
+ | # demand(j) .. sum(i, x(i,j)) =g= b(j); | ||
@constraints trmodel begin | @constraints trmodel begin | ||
supply[p in plants], | supply[p in plants], | ||
Line 110: | Line 172: | ||
sum(x[p,m] for p in plants) | sum(x[p,m] for p in plants) | ||
end | end | ||
+ | </ | ||
+ | ==== Declaration of the model objective ==== | ||
+ | |||
+ | Contrary to constraints and variables, the objective is always a unique function. Note that it is at this point that we specify the direction of the optimisation. | ||
+ | |||
+ | <code julia> | ||
# Objective | # Objective | ||
@objective trmodel Min begin | @objective trmodel Min begin | ||
sum(c[p, | sum(c[p, | ||
end | end | ||
+ | </ | ||
+ | ==== Human-readable visualisation of the model (optional) ==== | ||
+ | |||
+ | If we wish we can get the optimisation model printed in a human-readable fashion, so we can expect all is like it should be | ||
+ | |||
+ | <code julia> | ||
print(trmodel) | print(trmodel) | ||
+ | </ | ||
+ | ==== Resolution of the model ==== | ||
+ | |||
+ | It is at this point that the solver is called and the model is passed to the solver engine for its solution. The return value is the status of the optimisation (": | ||
+ | |||
+ | <code julia> | ||
status = solve(trmodel) | status = solve(trmodel) | ||
+ | </ | ||
+ | ==== Visualisation of the results ==== | ||
+ | While you can do any fancy output you may wish after you retrieve the optimal value of the variables with '' | ||
+ | Notice that you can also easily retrieve the dual value associated to the constraint with '' | ||
+ | |||
+ | <code julia> | ||
if status == :Optimal | if status == :Optimal | ||
println(" | println(" | ||
println(getvalue(x)) | println(getvalue(x)) | ||
+ | println(" | ||
+ | [println(" | ||
+ | println(" | ||
+ | [println(" | ||
else | else | ||
println(" | println(" | ||
Line 129: | Line 219: | ||
</ | </ | ||
- | ==== Set definition ==== | ||
- | Sets are created as attributes object of the main model objects and all the information is given as parameter in the constructor function. Specifically, | ||
- | <code python> | ||
- | ## Define sets ## | ||
- | # Sets | ||
- | # | ||
- | # | ||
- | model.i = Set(initialize=[' | ||
- | model.j = Set(initialize=[' | ||
- | </ | ||
- | |||
- | ==== Parameters ==== | ||
- | Parameter objects are created specifying the sets over which they are defined and are initialised with either a python dictionary or a scalar: | ||
- | <code python> | ||
- | ## Define parameters ## | ||
- | # | ||
- | # | ||
- | # / | ||
- | # san-diego | ||
- | # | ||
- | # / | ||
- | # chicago | ||
- | # topeka | ||
- | model.a = Param(model.i, | ||
- | model.b = Param(model.j, | ||
- | # Table d(i, | ||
- | # new-york | ||
- | # seattle | ||
- | # san-diego | ||
- | dtab = { | ||
- | (' | ||
- | (' | ||
- | (' | ||
- | (' | ||
- | (' | ||
- | (' | ||
- | } | ||
- | model.d = Param(model.i, | ||
- | # Scalar f freight in dollars per case per thousand miles /90/ ; | ||
- | model.f = Param(initialize=90, | ||
- | </ | ||
- | A third, powerful way to initialize a parameter is using a user-defined function.\\ | ||
- | This function will be automatically called by pyomo with any possible (i,j) set. In this case pyomo will actually call c_init() six times in order to initialize the model.c parameter. | ||
- | <code python> | ||
- | # Parameter c(i, | ||
- | # c(i,j) = f * d(i,j) / 1000 ; | ||
- | def c_init(model, | ||
- | return model.f * model.d[i, | ||
- | model.c = Param(model.i, | ||
- | </ | ||
- | |||
- | ==== Variables ==== | ||
- | Similar to parameters, variables are created specifying their domain(s). For variables we can also specify the upper/lower bounds in the constructor.\\ | ||
- | Differently from GAMS, we don't need to define the variable that is on the left hand side of the objective function. | ||
- | <code python> | ||
- | ## Define variables ## | ||
- | # Variables | ||
- | # | ||
- | # | ||
- | # Positive Variable x ; | ||
- | model.x = Var(model.i, | ||
- | </ | ||
- | |||
- | ==== Constrains ==== | ||
- | At this point, it should not be a surprise that constrains are again defined as model objects with the required information passed as parameter in the constructor function. | ||
- | <code python> | ||
- | ## Define contrains ## | ||
- | # supply(i) | ||
- | # supply(i) .. sum (j, x(i,j)) =l= a(i) | ||
- | def supply_rule(model, | ||
- | return sum(model.x[i, | ||
- | model.supply = Constraint(model.i, | ||
- | # demand(j) | ||
- | # demand(j) .. sum(i, x(i,j)) =g= b(j); | ||
- | def demand_rule(model, | ||
- | return sum(model.x[i, | ||
- | model.demand = Constraint(model.j, | ||
- | </ | ||
- | The above code take advantage of [[https:// | ||
- | If we take the supply_rule as example, this is actually called two times by pyomo (once for each of the elements of i). Without List Comprehensions we would have had to write our function using a for loop, like: | ||
- | <code python> | ||
- | def supply_rule(model, | ||
- | supply = 0.0 | ||
- | for j in model.j: | ||
- | supply += model.x[i, | ||
- | return supply <= model.a[i] | ||
- | </ | ||
- | Using List Comprehension is however quicker to code and more readable. | ||
- | |||
- | ==== Objective & solving ==== | ||
- | The definition of the objective is similar to those of the constrains, except that most solvers require a scalar objective function, hence a unique function, and we can specify the sense (direction) of the optimisation. | ||
- | <code python> | ||
- | ## Define Objective and solve ## | ||
- | # cost define objective function | ||
- | # cost .. z =e= sum((i,j), c(i, | ||
- | # Model transport /all/ ; | ||
- | # Solve transport using lp minimizing z ; | ||
- | def objective_rule(model): | ||
- | return sum(model.c[i, | ||
- | model.objective = Objective(rule=objective_rule, | ||
- | </ | ||
- | As we are here looping over two distinct sets, we can see how List Comprehension really simplifies the code. The objective function could have being written without List Comprehension as: | ||
- | <code python> | ||
- | def objective_rule(model): | ||
- | obj = 0.0 | ||
- | for ki in model.i: | ||
- | for kj in model.j: | ||
- | obj += model.c[ki, | ||
- | return obj | ||
- | </ | ||
- | |||
- | ==== Retrieving the output ==== | ||
- | To retrieve the output and do something with it (either to just display it -like we do here-, to plot a graph with [[http:// | ||
- | This function is called by pyomo after the solver has finished. | ||
- | <code python> | ||
- | ## Display of the output ## | ||
- | # Display x.l, x.m ; | ||
- | def pyomo_postprocess(options=None, | ||
- | model.x.display() | ||
- | </ | ||
- | We can print model structure information with '' | ||
- | Results are also by default saved in a results.json file or, if PyYAML is installed in the system, in results.yml. | ||
==== Editing and running the script ==== | ==== Editing and running the script ==== | ||
- | Differently from GAMS you can use whatever editor environment you wish to code a pyomo script. If you don't need debugging features, a simple text editor like Notepad++ (in windows), gedit or kate (in Linux) will suffice. They already have syntax highlight for python.\\ | + | Differently from GAMS you can use whatever editor environment you wish to code a JuMP script. If you don't need debugging features, a simple text editor like Notepad++ (in windows), gedit or kate (in Linux) will suffice. They already have syntax highlight for Julia.\\ |
- | If you want advanced features and debugging capabilities you can use a dedicated | + | If you want advanced features and debugging capabilities you can use a dedicated |
- | You will normally run the script as '' | + | If you are using instead |
- | If you want to run the script as '' | + | |
- | <code python> | + | |
- | # This is an optional code path that allows the script to be run outside of | + | |
- | # pyomo command-line. For example: | + | |
- | if __name__ == ' | + | |
- | #This replicates what the pyomo command-line tools does | + | |
- | from pyomo.opt import SolverFactory | + | |
- | import pyomo.environ | + | |
- | opt = SolverFactory(" | + | |
- | instance = model.create() | + | |
- | results = opt.solve(instance) | + | |
- | #sends results to stdout | + | |
- | results.write() | + | |
- | pyomo_postprocess(None, | + | |
- | </ | + | |
- | + | ||
- | Finally, if you are very lazy and want to run the script with just ./ | + | |
- | <code python> | + | |
- | # | + | |
- | # -*- coding: utf-8 -*- | + | |
- | </ | + | |
===== Further help ===== | ===== Further help ===== | ||
- | Documentation of pyomo is available from [[https://software.sandia.gov/trac/coopr/wiki/ | + | Documentation of JuMP is available from [[https://jump.readthedocs.io/en/latest/|this page]]. However if you want to do serious things with juMP, it is most likely that you will have to either look at the source code or consult the [[https://discourse.julialang.org/c/domain/opt|discussion |
+ | |||
+ | Happy modelling with JuMP ;-) | ||
- | Happy modelling with pyomo ;-) | ||
===== Complete script ===== | ===== Complete script ===== | ||
Here is the complete script: | Here is the complete script: | ||
- | < | + | < |
- | #!/ | + | # Transport example |
- | # -*- coding: utf-8 -*- | + | |
+ | # Transposition in JuMP of the basic transport model used in the GAMS tutorial | ||
+ | # | ||
+ | # This problem finds a least cost shipping schedule that meets | ||
+ | # requirements at markets and supplies at factories. | ||
+ | # | ||
+ | # - Original formulation: | ||
+ | # Princeton University Press, Princeton, New Jersey, 1963. | ||
+ | # - Gams implementation: This formulation is described in detail in: | ||
+ | # Rosenthal, R E, Chapter 2: A GAMS Tutorial. In GAMS: A User's Guide. | ||
+ | # The Scientific Press, Redwood City, California, 1988. | ||
+ | # - JuMP implementation: | ||
- | """ | + | using CSV, DataFrames, GLPK, JuMP |
- | Basic example of transport model from GAMS model library translated to Pyomo | + | |
- | To run this you need pyomo and a linear solver installed. | + | # Sets |
- | When these dependencies are installed you can solve this example in one of | + | plants |
- | this ways (glpk is the default solver): | + | markets = [" |
- | ./ | + | # Parameters |
- | | + | a = Dict( # capacity of plant i in cases |
- | pyomo solve transport.py | + | " |
- | | + | " |
+ | ) | ||
+ | b = Dict( # demand at market j in cases | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | ) | ||
- | To display the results: | + | # distance in thousands of miles |
+ | d_table = CSV.read(IOBuffer(""" | ||
+ | plants | ||
+ | seattle | ||
+ | san_diego | ||
+ | """ | ||
+ | d = Dict( (r[:plants],m) => r[Symbol(m)] for r in eachrow(d_table), | ||
- | cat results.json | + | f = 90 # freight in dollars per case per thousand miles |
- | cat results.yml (if PyYAML is installed on your system) | + | |
- | GAMS equivalent code is inserted as single-dash comments. The original GAMS code | + | c = Dict() # transport cost in thousands |
- | needs slighly different ordering | + | [ c[p,m] = f * d[p, |
- | http:// | + | |
- | Original problem formulation: | + | # Model declaration |
- | | + | trmodel = Model(GLPK.Optimizer) # transport model |
- | Princeton University Press, Princeton, New Jersey, 1963. | + | |
- | GAMS implementation: | + | |
- | Rosenthal, R E, Chapter 2: A GAMS Tutorial. In GAMS: A User's Guide. | + | |
- | The Scientific Press, Redwood City, California, 1988. | + | |
- | Pyomo translation: | + | |
- | Antonello Lobianco | + | |
- | This file is in the Public Domain | + | # Variables |
- | """ | + | @variables trmodel begin |
+ | x[p in plants, m in markets] >= 0 # shipment quantities | ||
+ | end | ||
- | # Import | + | # Constraints |
- | from pyomo.environ import * | + | @constraints trmodel begin |
+ | supply[p in plants], | ||
+ | sum(x[p,m] for m in markets) | ||
+ | demand[m in markets], | ||
+ | sum(x[p,m] for p in plants) | ||
+ | end | ||
- | # Creation of a Concrete Model | + | # Objective |
- | model = ConcreteModel() | + | @objective trmodel Min begin |
+ | sum(c[p, | ||
+ | end | ||
- | ## Define sets ## | + | print(trmodel) |
- | # Sets | + | |
- | # | + | |
- | # | + | |
- | model.i = Set(initialize=[' | + | |
- | model.j = Set(initialize=[' | + | |
- | ## Define parameters ## | + | optimize!(trmodel) |
- | # | + | status |
- | # a(i) | + | |
- | # / | + | |
- | # san-diego | + | |
- | # | + | |
- | # / | + | |
- | # chicago | + | |
- | # topeka | + | |
- | model.a | + | |
- | model.b = Param(model.j, | + | |
- | # Table d(i, | + | |
- | # new-york | + | |
- | # seattle | + | |
- | # san-diego | + | |
- | dtab = { | + | |
- | (' | + | |
- | (' | + | |
- | (' | + | |
- | (' | + | |
- | (' | + | |
- | (' | + | |
- | } | + | |
- | model.d = Param(model.i, | + | |
- | # Scalar f freight in dollars per case per thousand miles /90/ ; | + | |
- | model.f = Param(initialize=90, | + | |
- | # Parameter c(i, | + | |
- | # c(i,j) = f * d(i,j) / 1000 ; | + | |
- | def c_init(model, | + | |
- | return model.f * model.d[i, | + | |
- | model.c = Param(model.i, | + | |
- | + | ||
- | ## Define variables ## | + | |
- | # Variables | + | |
- | # | + | |
- | # | + | |
- | # Positive Variable x ; | + | |
- | model.x = Var(model.i, | + | |
- | + | ||
- | ## Define contrains ## | + | |
- | # supply(i) | + | |
- | # supply(i) .. sum (j, x(i,j)) =l= a(i) | + | |
- | def supply_rule(model, | + | |
- | return sum(model.x[i, | + | |
- | model.supply = Constraint(model.i, | + | |
- | # demand(j) | + | |
- | # demand(j) .. sum(i, x(i,j)) =g= b(j); | + | |
- | def demand_rule(model, | + | |
- | return sum(model.x[i, | + | |
- | model.demand = Constraint(model.j, | + | |
- | ## Define Objective and solve ## | + | if status |
- | # cost define objective function | + | println(" |
- | # cost .. z | + | println(" |
- | # Model transport /all/ ; | + | println(value.(x)) |
- | # Solve transport using lp minimizing z ; | + | |
- | def objective_rule(model): | + | |
- | | + | println(" |
- | model.objective = Objective(rule=objective_rule, | + | |
+ | |||
+ | else | ||
+ | println(" | ||
+ | println(status) | ||
+ | end | ||
- | |||
- | ## Display of the output ## | ||
- | # Display x.l, x.m ; | ||
- | def pyomo_postprocess(options=None, | ||
- | model.x.display() | ||
- | |||
- | # This is an optional code path that allows the script to be run outside of | ||
- | # pyomo command-line. | ||
- | if __name__ == ' | ||
- | |||
- | #This replicates what the pyomo command-line tools does | ||
- | from pyomo.opt import SolverFactory | ||
- | import pyomo.environ | ||
- | opt = SolverFactory(" | ||
- | instance = model.create() | ||
- | results = opt.solve(instance) | ||
- | #sends results to stdout | ||
- | results.write() | ||
- | pyomo_postprocess(None, | ||
- | |||
# Expected result: | # Expected result: | ||
# obj= 153.675 | # obj= 153.675 | ||
Line 426: | Line 329: | ||
# | # | ||
</ | </ | ||
- | |||
- | |||
- | |||
- | ~~DISCUSSION~~ | ||
- | |||
- | |||
- | |||
~~DISCUSSION~~ | ~~DISCUSSION~~ | ||