Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous 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:39] (current)
antonello [Further help]
Line 15: Line 15:
 You have plenty of development environment to choose from (e.g. Jupiter, Juno), a clear modern language, the possibility to interface your model with third party libraries.. all of this basically for free.\\ You have plenty of development environment to choose from (e.g. Jupiter, Juno), a clear modern language, the possibility to interface your model with third party libraries.. all of this basically for free.\\
 It is also, at least for my user case, much faster than GAMS. Aside the preparation of the model to pass to the solver, where it is roughly equivalent, in the solver execution I can benefit of having on my system a version of IPOPT compiled with the much more performing ma27 linear solver, while for GAMS I would have to rely on the embedded version that is compiled with the MUMPS linear solver. That's part of the flexibility you gain in using JuMP in place of GAMS. It is also, at least for my user case, much faster than GAMS. Aside the preparation of the model to pass to the solver, where it is roughly equivalent, in the solver execution I can benefit of having on my system a version of IPOPT compiled with the much more performing ma27 linear solver, while for GAMS I would have to rely on the embedded version that is compiled with the MUMPS linear solver. That's part of the flexibility you gain in using JuMP in place of GAMS.
-That's said, for people that don't need such flexibility, the package automatically install a local pre-compiled version of the solver, so just adding the package relative to the solver is enough to start writing the model. Even more, for people that doesn't care too much about performances, there is a service on [[https://juliabox.com|JuliaBox.com]] that allows to run Julia/JuMP scripts for free in the browser, without anything to install on the local computer.   +That's said, for people that don't need such flexibility, the package automatically install a local pre-compiled version of the solver, so just adding the package relative to the solver is enough to start writing the model.   
  
  
Line 31: Line 31:
 Run, only once, the following code to install JuMP language and a couple of open source solvers: Run, only once, the following code to install JuMP language and a couple of open source solvers:
 <code julia> <code julia>
-Pkg.update()                        # To refresh the list of newest packages +using Pkg               # Load the package manager 
-Pkg.add("JuMP"                    The mathematical optimisation library +Pkg.update()            # To refresh the list of newest packages 
-Pkg.add("GLPKMathProgInterface"   # A lineaqr and MIP solver +Pkg.add("CSV"         library to work with Comma Separated Values 
-Pkg.add("Ipopt"                   # A non-linear solver +Pkg.add("DataFrames"  # A library to deal with dataframes (R like tabular data) 
-Pkg.add("DataFrames"              # A library to deal with dataframes (R-like tabular data)+Pkg.add("JuMP"        # The mathematical optimisation library 
 +Pkg.add("GLPK"        # A linear and MIP solver 
 +Pkg.add("Ipopt"       # A non-linear solver (not needed in this example)
 </code> </code>
  
Line 42: Line 44:
 ==== Importing the libraries ==== ==== Importing the libraries ====
  
-You will need to import as a minima the ''JuMP'' module. If you wish to specify a solver engine rather than letting JuMP select a suitable one, you will need to import also the module relative to the solver, e.g. ''Ipopt'' or  ''GLPKMathProgInterface''+You will need to import as a minima the ''JuMP'' module and a suitable solver. In this case the problem is linear, so we can use ''GLPK'' (''HiGHS'' is another popular alternative). If the problem would have been non-linear, you could have used the ''Ipopt'' solver/package
  
-<code julia> +<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, GLPK, CSV and DataFrames modules (the latter twos just to import the data from a header based table, as in the original trasnport example in GAMS  
-using JuMP, DataFrames +using CSV, DataFrames, GLPK, JuMP 
-<code>+</code>
  
 ==== Defining the "sets" ==== ==== Defining the "sets" ====
 +
 JuMP doesn't really have a concept of sets, but it uses the native containers available in the core Julia language\\Variables, parameters and constraints can be indexed using these containers.\\ JuMP doesn't really have a concept of sets, but it uses the native containers available in the core Julia language\\Variables, parameters and constraints can be indexed using these containers.\\
 While many works with position-based lists, I find more readable using dictionaries instead. So the "sets" are represented as lists, but then everything else is a dictionary with the elements of the list as keys.\\ While many works with position-based lists, I find more readable using dictionaries instead. So the "sets" are represented as lists, but then everything else is a dictionary with the elements of the list as keys.\\
Line 55: Line 58:
    
 <code julia> <code julia>
-# Sets+Define sets # 
 +#  Sets 
 +#         canning plants   / seattle, san-diego / 
 +#         markets          / new-york, chicago, topeka / ;
 plants  = ["seattle","san_diego"         # canning plants plants  = ["seattle","san_diego"         # canning plants
 markets = ["new_york","chicago","topeka" # markets markets = ["new_york","chicago","topeka" # markets
Line 61: Line 67:
  
  
-==== Defining the "parameters" ==== +==== Definition of the "parameters" ==== 
-Capacity of plants and demand of markets are directly defined as dictionaries, while the distance is first read from a white-space separated table and then it is converted in a "(plant, market) => value" dictionary.+ 
 +Capacity of plants and demand of markets are directly defined as dictionaries, while the distance is first read as a DataFrame from a white-space separated table and then it is converted in a "(plant, market) => value" dictionary.
  
 <code julia> <code julia>
-# Parameters+Define parameters # 
 +#   Parameters 
 +#       a(i)  capacity of plant i in cases 
 +#         /    seattle     350 
 +#              san-diego   600  /
 a = Dict(              # capacity of plant i in cases a = Dict(              # capacity of plant i in cases
   "seattle"   => 350,   "seattle"   => 350,
   "san_diego" => 600,   "san_diego" => 600,
 ) )
 +
 +#       b(j)  demand at market j in cases
 +#         /    new-york    325
 +#              chicago     300
 +#              topeka      275  / ;
 b = Dict(              # demand at market j in cases b = Dict(              # demand at market j in cases
   "new_york"  => 325,   "new_york"  => 325,
Line 76: Line 92:
 ) )
  
-#  distance in thousands of miles +Table d(i,j)  distance in thousands of miles 
-d_table = wsv"""+#                    new-york       chicago      topeka 
 +#      seattle          2.5           1.7          1.8 
 +#      san-diego        2.5           1.8          1.4  ; 
 +d_table = CSV.read(IOBuffer("""
 plants     new_york  chicago  topeka plants     new_york  chicago  topeka
 seattle    2.5       1.7      1.8 seattle    2.5       1.7      1.8
 san_diego  2.5       1.8      1.4 san_diego  2.5       1.8      1.4
-"""+"""), DataFrame, delim=" ", ignorerepeated=true,copycols=true)
 d = Dict( (r[:plants],m) => r[Symbol(m)] for r in eachrow(d_table), m in markets) d = Dict( (r[:plants],m) => r[Symbol(m)] for r in eachrow(d_table), m in markets)
 +# Here we are converting the table in a "(plant, market) => distance" dictionary
 # r[:plants]:   the first key, row field using a fixed header # r[:plants]:   the first key, row field using a fixed header
 # m:            the second key # m:            the second key
 # r[Symbol(m)]: the value, the row field with a dynamic header # r[Symbol(m)]: the value, the row field with a dynamic header
  
 +# 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,j)  transport cost in thousands of dollars per case ;
 +#            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] 
 </code> </code>
 +The above code take advantage of [[http://docs.julialang.org/en/stable/manual/arrays/#comprehensions|List Comprehensions]], a powerful feature of the Julia language that provides a concise way to loop over a list.
 +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[:plants],m) => r[Symbol(m)]
 +  end
 +end
 +</code>
 +Using List Comprehension is however quicker to code and more readable.
  
  
-# Model declaration +==== Declaration of the model ====
-trmodel Model() # transport model+
  
-# 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.\\ 
 +The solver engine to use is given as argument of the ''Model()'' call.\\ 
 +We could pass solver-specific options with the ''set_optimizer_attribute'' function, e.g.: 
 +''set_optimizer_attribute(trmodel, "msg_lev", GLPK.GLP_MSG_ON)'' 
 + 
 +<code julia> 
 +Model declaration (transport model) 
 +trmodel = Model(GLPK.Optimizer)  
 +</code> 
 + 
 +==== Declaration of the model variables ==== 
 + 
 +Variables can have multiple-dimensions - that is, being indexed under several indexes -, and bounds are given at the same time as their declaration.\\ 
 +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 
 +#       x(i,j)  shipment quantities in cases 
 +#             total transportation costs in thousands of dollars ; 
 +#  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
 +</code>
  
-Constraints+==== Declaration of the model constraints ==== 
 + 
 +As in GAMS, each constraint can actually be a "family" of constraints: 
 + 
 +<code julia> 
 +## Define contrains ## 
 +# supply(i)   observe supply limit at plant i 
 +# supply(i) .. sum (j, x(i,j)) =l= a(i) 
 +# demand(j)   satisfy demand at market j ;   
 +# demand(j) .. sum(i, x(i,j)) =g= b(j);
 @constraints trmodel begin @constraints trmodel begin
     supply[p in plants],   # observe supply limit at plant p     supply[p in plants],   # observe supply limit at plant p
Line 110: Line 173:
         sum(x[p,m] for p in plants)  >=  b[m]         sum(x[p,m] for p in plants)  >=  b[m]
 end end
 +</code>
  
 +==== 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,m]*x[p,m] for p in plants, m in markets)     sum(c[p,m]*x[p,m] for p in plants, m in markets)
 end end
 +</code>
  
-print(trmodel)+==== Human-readable visualisation of the model (optional====
  
-status = solve(trmodel)+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
  
-if status == :Optimal +<code julia> 
-    println("Objective value: ", getobjectivevalue(trmodel)+print(trmodel)
-    println(getvalue(x)) +
-else +
-    println("Model didn't solved"+
-    println(status) +
-end+
 </code> </code>
  
-==== Set definition ==== +==== Resolution of the model ====
-Sets are created as attributes object of the main model objects and all the information is given as parameter in the constructor function. Specifically, we are passing to the constructor the initial elements of the set and a documentation string to keep track on what our set represents: +
-<code python> +
-## Define sets ## +
-#  Sets +
-#         canning plants   / seattle, san-diego / +
-#         markets          / new-york, chicago, topeka / ; +
-model.i Set(initialize=['seattle','san-diego'], doc='Canning plans'+
-model.j Set(initialize=['new-york','chicago', 'topeka'], doc='Markets'+
-</code>+
  
-==== Parameters ==== +It is at this point that the solver is called and the model is passed to the solver engine for its solutionThe return value is the status of the optimisation (''MOI.OPTIMAL'' if all went fine)
-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 ## +
-#   Parameters +
-#       a(i)  capacity of plant i in cases +
-#         /    seattle     350 +
-#              san-diego   600 +
-#       b(j)  demand at market j in cases +
-#         /    new-york    325 +
-#              chicago     300 +
-#              topeka      275  / ; +
-model.a = Param(model.i, initialize={'seattle':350,'san-diego':600}, doc='Capacity of plant i in cases'+
-model.b = Param(model.j, initialize={'new-york':325,'chicago':300,'topeka':275}, doc='Demand at market j in cases'+
-#  Table d(i,j)  distance in thousands of miles +
-#                    new-york       chicago      topeka +
-#      seattle          2.5           1.7          1.8 +
-#      san-diego        2.5           1.8          1.4  ; +
-dtab = { +
-    ('seattle',  'new-york') : 2.5, +
-    ('seattle',  'chicago' : 1.7, +
-    ('seattle',  'topeka'  : 1.8, +
-    ('san-diego','new-york') : 2.5, +
-    ('san-diego','chicago' : 1.8, +
-    ('san-diego','topeka'  : 1.4, +
-    } +
-model.d = Param(model.i, model.j, initialize=dtab, doc='Distance in thousands of miles'+
-#  Scalar f  freight in dollars per case per thousand miles  /90/ ; +
-model.f = Param(initialize=90, doc='Freight in dollars per case per thousand miles'+
-</code> +
-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,j)  transport cost in thousands of dollars per case ; +
-#            c(i,j) = f * d(i,j) / 1000 ; +
-def c_init(model, i, j): +
-  return model.f * model.d[i,j] / 1000 +
-model.c = Param(model.i, model.j, initialize=c_init, doc='Transport cost in thousands of dollar per case') +
-</code>+
  
-==== Variables ==== +<code julia
-Similar to parameters, variables are created specifying their domain(s). For variables we can also specify the upper/lower bounds in the constructor.\\ +optimize!(trmodel
-Differently from GAMS, we don't need to define the variable that is on the left hand side of the objective function. +status termination_status(trmodel)
-<code python+
-## Define variables ## +
-#  Variables +
-#       x(i,j shipment quantities in cases +
-#             total transportation costs in thousands of dollars ; +
-#  Positive Variable x ; +
-model.x Var(model.i, model.j, bounds=(0.0,None), doc='Shipment quantities in case')+
 </code> </code>
  
-==== Constrains ==== +==== Visualisation of the results ==== 
-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.   +While you can do any fancy output you may wish after you retrieve the optimal value of the variables with ''getvalue(var_name)''you can just ''println(getvalue(x))'' to get basic output.\\ 
-<code python> +Notice that you can also easily retrieve the dual value associated to the constraint with ''getdual(constraint_name)''.
-## Define contrains ## +
-# supply(i  observe supply limit at plant i +
-# supply(i) .. sum (jx(i,j)) =l= a(i) +
-def supply_rule(model, i): +
-  return sum(model.x[i,j] for j in model.j) <= model.a[i] +
-model.supply = Constraint(model.i, rule=supply_rule, doc='Observe supply limit at plant i'+
-# demand(j)   satisfy demand at market j ;   +
-# demand(j) .. sum(i, x(i,j)) =g= b(j); +
-def demand_rule(model, j): +
-  return sum(model.x[i,j] for i in model.i) >= model.b[j]   +
-model.demand = Constraint(model.j, rule=demand_rule, doc='Satisfy demand at market j'+
-</code> +
-The above code take advantage of [[https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions|List Comprehensions]], a powerful feature of the python language that provides a concise way to loop over list+
-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, i)+
-  supply = 0.0 +
-  for j in model.j: +
-    supply += model.x[i,j] +
-  return supply <= model.a[i] +
-</code> +
-Using List Comprehension is however quicker to code and more readable.+
  
-==== Objective & solving ==== +<code julia
-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. +if status == MOI.OPTIMAL 
-<code python+    println("Objective value: "objective_value(trmodel)
-## Define Objective and solve ## +    println("Shipped quantities: ") 
-#  cost        define objective function +    println(value.(x)) 
-#  cost ..        z  =e sum((i,j), c(i,j)*x(i,j)) ; +    println("Shadow prices of supply:") 
-#  Model transport /all/ ; +    [println("$p = $(dual(supply[p]))"for in plants] 
-#  Solve transport using lp minimizing z ; +    println("Shadow prices of demand:"
-def objective_rule(model)+    [println("$m $(dual(demand[m]))") for m in markets] 
-  return sum(model.c[i,j]*model.x[i,j] for in model.i for j in model.j+  
-model.objective Objective(rule=objective_rule, sense=minimize, doc='Define objective function'+else 
-</code> +    println("Model didn't solved"
-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: +    println(status) 
-<code python> +end
-def objective_rule(model)+
-  obj = 0.0   +
-  for ki in model.i: +
-    for kj in model.j: +
-      obj += model.c[ki,kj]*model.x[ki,kj] +
-  return obj+
 </code> </code>
  
-==== 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://matplotlib.org|matplotlib]] or to save it in a csv file) we use the ''pyomo_postprocess()'' function.\\ 
-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, instance=None, results=None): 
-  model.x.display() 
-</code> 
-We can print model structure information with ''model.pprint()'' ("pprint" stand for "pretty print").\\ 
-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 Python IDE, like e.g. [[https://code.google.com/p/spyderlib/|Spyder]].+If you want advanced features and debugging capabilities you can use a dedicated Julia IDE, like the [[https://www.julia-vscode.org/|Julia extension for VSCode]].
  
-You will normally run the script as ''pyomo solve --solver=glpk transport.py''. You can output solver specific output adding the option ''--stream-output''.\\ +If you are using instead the Julia terminal,  you can run the script as ''julia transport.jl''.
-If you want to run the script as ''python transport.py'' add the following lines at the end:\\ +
-<code python> +
-# This is an optional code path that allows the script to be run outside of +
-# pyomo command-line For example:  python transport.py +
-if __name__ == '__main__': +
-    #This replicates what the pyomo command-line tools does +
-    from pyomo.opt import SolverFactory +
-    import pyomo.environ +
-    opt = SolverFactory("glpk"+
-    instance = model.create() +
-    results = opt.solve(instance) +
-    #sends results to stdout +
-    results.write() +
-    pyomo_postprocess(None, instance, results) +
-</code> +
- +
-Finally, if you are very lazy and want to run the script with just ./transport.py (and you are in Linux) add the following lines at the top: +
-<code python> +
-#!/usr/bin/env python +
-# -*- coding: utf-8 -*- +
-</code>+
  
 ===== Further help ===== ===== Further help =====
-Documentation of pyomo is available from [[https://software.sandia.gov/trac/coopr/wiki/Documentation|this page]]. However if you want to do serious things with pyomoit is most likely that you will have to either look at the source code or consult the [[https://groups.google.com/forum/#!forum/coopr-forum|mailing list]].+Documentation of JuMP is available from [[https://jump.dev/|this page]], and community-based support is available on [[https://discourse.julialang.org/c/domain/opt|the Discourse forum]]. 
 + 
 +Happy modelling with JuMP ;-)
  
-Happy modelling with pyomo ;-) 
 ===== Complete script ===== ===== Complete script =====
  
 Here is the complete script:  Here is the complete script: 
  
-<code python+<code Julia
-#!/usr/bin/env python +Transport example 
-# -*codingutf-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: Dantzig, G B, Chapter 3.3. In Linear Programming and Extensions. 
 +# Princeton University Press, Princeton, New Jersey, 1963. 
 +Gams implementationThis 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: Antonello Lobianco
    
-""" +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  = ["seattle","san_diego"         # canning plants 
-this ways (glpk is the default solver):+markets = ["new_york","chicago","topeka" # markets
    
-    ./transport.py (Linux only+# Parameters 
-    python transport.py +a = Dict             # capacity of plant i in cases 
-    pyomo solve transport.py +  "seattle"   => 350, 
-    pyomo solve --solver=glpk transport.py+  "san_diego" => 600, 
 +
 +b = Dict(              # demand at market j in cases 
 +  "new_york"  => 325, 
 +  "chicago"   => 300, 
 +  "topeka"    => 275, 
 +)
    
-To display the results:+#  distance in thousands of miles 
 +d_table = CSV.read(IOBuffer(""" 
 +plants     new_york  chicago  topeka 
 +seattle    2.5       1.7      1.8 
 +san_diego  2.5       1.8      1.4 
 +"""), DataFrame, delim=" ", ignorerepeated=true,copycols=true) 
 +d = Dict( (r[:plants],m) => r[Symbol(m)] for r in eachrow(d_table), m in markets)
    
-    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 of dollars per case ; 
-needs slighly different ordering of the commands and it's available at +[ c[p,m] = f * d[p,m] 1000 for p in plants, m in markets]
-http://www.gams.com/mccarl/trnsport.gms+
    
-Original problem formulation: +# Model declaration 
-    Dantzig, G B, Chapter 3.3. In Linear Programming and Extensions. +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 in cases 
 +end
    
-Import +Constraints 
-from pyomo.environ import *+@constraints trmodel begin 
 +    supply[p in plants],   # observe supply limit at plant p 
 +        sum(x[p,m] for m in markets)  <=  a[p] 
 +    demand[m in markets],  # satisfy demand at market m 
 +        sum(x[p,m] for p in plants)  >=  b[m] 
 +end
    
-Creation of a Concrete Model +Objective 
-model = ConcreteModel()+@objective trmodel Min begin 
 +    sum(c[p,m]*x[p,m] for p in plants, m in markets) 
 +end
    
-## Define sets ## +print(trmodel)
-#  Sets +
-#         canning plants   / seattle, san-diego / +
-#         markets          / new-york, chicago, topeka / ; +
-model.i = Set(initialize=['seattle','san-diego'], doc='Canning plans'+
-model.j = Set(initialize=['new-york','chicago', 'topeka'], doc='Markets')+
    
-## Define parameters ## +optimize!(trmodel
-#   Parameters +status termination_status(trmodel)
-#       a(i capacity of plant i in cases +
-#         /    seattle     350 +
-#              san-diego   600 +
-#       b(j)  demand at market j in cases +
-#         /    new-york    325 +
-#              chicago     300 +
-#              topeka      275  / ; +
-model.a Param(model.i, initialize={'seattle':350,'san-diego':600}, doc='Capacity of plant i in cases'+
-model.b = Param(model.j, initialize={'new-york':325,'chicago':300,'topeka':275}, doc='Demand at market j in cases'+
-#  Table d(i,j)  distance in thousands of miles +
-#                    new-york       chicago      topeka +
-#      seattle          2.5           1.7          1.8 +
-#      san-diego        2.5           1.8          1.4  ; +
-dtab = { +
-    ('seattle',  'new-york') : 2.5, +
-    ('seattle',  'chicago' : 1.7, +
-    ('seattle',  'topeka'  : 1.8, +
-    ('san-diego','new-york') : 2.5, +
-    ('san-diego','chicago' : 1.8, +
-    ('san-diego','topeka'  : 1.4, +
-    } +
-model.d = Param(model.i, model.j, initialize=dtab, doc='Distance in thousands of miles'+
-#  Scalar f  freight in dollars per case per thousand miles  /90/ ; +
-model.f = Param(initialize=90, doc='Freight in dollars per case per thousand miles'+
-#  Parameter c(i,j)  transport cost in thousands of dollars per case ; +
-#            c(i,j) = f * d(i,j) / 1000 ; +
-def c_init(model, i, j): +
-  return model.f * model.d[i,j] / 1000 +
-model.c = Param(model.i, model.j, initialize=c_init, doc='Transport cost in thousands of dollar per case'+
-  +
-## Define variables ## +
-#  Variables +
-#       x(i,j)  shipment quantities in cases +
-#             total transportation costs in thousands of dollars ; +
-#  Positive Variable x ; +
-model.x = Var(model.i, model.j, bounds=(0.0,None), doc='Shipment quantities in case'+
-  +
-## Define contrains ## +
-# supply(i)   observe supply limit at plant i +
-# supply(i) .. sum (j, x(i,j)) =l= a(i) +
-def supply_rule(model, i): +
-  return sum(model.x[i,j] for j in model.j) <= model.a[i] +
-model.supply = Constraint(model.i, rule=supply_rule, doc='Observe supply limit at plant i') +
-# demand(j)   satisfy demand at market j ;   +
-# demand(j) .. sum(i, x(i,j)) =g= b(j); +
-def demand_rule(model, j): +
-  return sum(model.x[i,j] for i in model.i) >= model.b[j]   +
-model.demand = Constraint(model.j, rule=demand_rule, doc='Satisfy demand at market j')+
  
-## Define Objective and solve ## +if status == MOI.OPTIMAL 
-#  cost        define objective function +    println("Objective value: "objective_value(trmodel)
-#  cost ..        z  =e sum((i,j), c(i,j)*x(i,j)) ; +    println("Shipped quantities: ") 
-#  Model transport /all/ ; +    println(value.(x)) 
-#  Solve transport using lp minimizing z ; +    println("Shadow prices of supply:"
-def objective_rule(model): +    [println("$p = $(dual(supply[p]))"for in plants] 
-  return sum(model.c[i,j]*model.x[i,j] for in model.i for j in model.j+    println("Shadow prices of demand:"
-model.objective = Objective(rule=objective_rule, sense=minimize, doc='Define objective function')+    [println("$m $(dual(demand[m]))") for m in markets] 
 +  
 +else 
 +    println("Model didn't solved") 
 +    println(status) 
 +end
  
-  
-## Display of the output ## 
-# Display x.l, x.m ; 
-def pyomo_postprocess(options=None, instance=None, results=None): 
-  model.x.display() 
-  
-# This is an optional code path that allows the script to be run outside of 
-# pyomo command-line.  For example:  python transport.py 
-if __name__ == '__main__': 
-  
-    #This replicates what the pyomo command-line tools does 
-    from pyomo.opt import SolverFactory 
-    import pyomo.environ 
-    opt = SolverFactory("glpk") 
-    instance = model.create() 
-    results = opt.solve(instance) 
-    #sends results to stdout 
-    results.write() 
-    pyomo_postprocess(None, instance, results) 
-  
 # Expected result: # Expected result:
 # obj= 153.675 # obj= 153.675
Line 426: Line 333:
 #['san-diego','topeka'  = 275 #['san-diego','topeka'  = 275
 </code> </code>
- 
- 
- 
-~~DISCUSSION~~ 
- 
- 
- 
  
 ~~DISCUSSION~~ ~~DISCUSSION~~
  
personal/blog/2017/0203_jump_for_gams_users.1486128594.txt.gz · Last modified: 2018/06/18 15:10 (external edit)
CC Attribution-Noncommercial-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0