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
Next revision Both sides next revision
personal:blog:2017:0203_jump_for_gams_users [2017/02/03 15:32]
antonello
personal:blog:2017:0203_jump_for_gams_users [2023/12/22 11:12]
antonello [Complete script]
Line 35: Line 35:
 Pkg.add("GLPKMathProgInterface"   # A lineaqr and MIP solver Pkg.add("GLPKMathProgInterface"   # A lineaqr and MIP solver
 Pkg.add("Ipopt"                   # A non-linear solver 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)
 </code> </code>
  
Line 44: Line 44:
 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. 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''
  
-<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 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> </code>
Line 56: Line 56:
    
 <code julia> <code julia>
-## Define sets ##+# Define sets #
 #  Sets #  Sets
 #         canning plants   / seattle, san-diego / #         canning plants   / seattle, san-diego /
Line 70: Line 70:
  
 <code julia> <code julia>
-## Define parameters ##+# Define parameters #
 #   Parameters #   Parameters
 #       a(i)  capacity of plant i in cases #       a(i)  capacity of plant i in cases
Line 135: Line 135:
  
 <code julia> <code julia>
-# Model declaration +# Model declaration (transport model) 
-trmodel = Model() # transport model+trmodel = Model()  
 </code> </code>
  
Line 190: Line 191:
 <code julia> <code julia>
 print(trmodel) print(trmodel)
-</code+</code>
  
 ==== Resolution of the model ==== ==== Resolution of the model ====
Line 234: Line 235:
 Here is the complete script:  Here is the complete script: 
  
-<code julia+<code Julia
-#+Transport example
-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 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: Antonello Lobianco +
-=# +
- +
-using JuMP, DataFrames+
  
 +# 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 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: Antonello Lobianco
 + 
 +using CSV, DataFrames, GLPK, JuMP
 + 
 # Sets # Sets
 plants  = ["seattle","san_diego"         # canning plants plants  = ["seattle","san_diego"         # canning plants
 markets = ["new_york","chicago","topeka" # markets markets = ["new_york","chicago","topeka" # markets
 + 
 # Parameters # Parameters
 a = Dict(              # capacity of plant i in cases a = Dict(              # capacity of plant i in cases
Line 265: Line 266:
   "topeka"    => 275,   "topeka"    => 275,
 ) )
 + 
 #  distance in thousands of miles #  distance in thousands of miles
-d_table = wsv"""+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)
 + 
 f = 90 # freight in dollars per case per thousand miles f = 90 # freight in dollars per case per thousand miles
 + 
 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]
 + 
 # Model declaration # Model declaration
-trmodel = Model() # transport model +trmodel = Model(GLPK.Optimizer) # transport model 
 + 
 # Variables # Variables
 @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 # Constraints
 @constraints trmodel begin @constraints trmodel begin
Line 294: Line 295:
         sum(x[p,m] for p in plants)  >=  b[m]         sum(x[p,m] for p in plants)  >=  b[m]
 end end
 + 
 # 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
 + 
 print(trmodel) print(trmodel)
 + 
 +optimize!(trmodel)
 +status = termination_status(trmodel)
  
-status = solve(trmodel) +if status == MOI.OPTIMAL 
- +    println("Objective value: ", objective_value(trmodel))
-if status == :Optimal +
-    println("Objective value: ", getobjectivevalue(trmodel))+
     println("Shipped quantities: ")     println("Shipped quantities: ")
-    println(getvalue(x))+    println(value.(x))
     println("Shadow prices of supply:")     println("Shadow prices of supply:")
-    [println("$p = $(getdual(supply[p]))") for p in plants]+    [println("$p = $(dual(supply[p]))") for p in plants]
     println("Shadow prices of demand:")     println("Shadow prices of demand:")
-    [println("$m = $(getdual(demand[m]))") for m in markets] +    [println("$m = $(dual(demand[m]))") for m in markets] 
 + 
 else else
     println("Model didn't solved")     println("Model didn't solved")
     println(status)     println(status)
 end end
- +
 # Expected result: # Expected result:
 # obj= 153.675 # obj= 153.675
personal/blog/2017/0203_jump_for_gams_users.txt · Last modified: 2023/12/22 11:39 by antonello
CC Attribution-Noncommercial-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0