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
home_test_julia [2017/02/07 09:28]
antonello
home_test_julia [2017/02/07 10:15]
antonello
Line 1: Line 1:
-not in code+===== Installation =====
  
-<code julia+**Step 1:**  
-#= +  * Option a: Get an account on [[https://juliabox.com|JuliaBox.com]] to run julia/JuMP script without installing anything on the local computer 
-Transposition in JuMP of the basic transport model used in the GAMS tutorial+  * Option b: Install Julia for your platform ([[http://julialang.org/downloads/|http://julialang.org/downloads/]])
  
-This problem finds a least cost shipping schedule that meets +**Step 2:**
-requirements at markets and supplies at factories.+
  
-- Original formulation: DantzigG BChapter 3.3. In Linear Programming and Extensions+Runonly oncethe following code to install JuMP language and a couple of open source solvers: 
-Princeton University Press, Princeton, New Jersey, 1963+<code julia> 
-- Gams implementation: This formulation is described in detail in: +Pkg.update()                        # To refresh the list of newest packages 
-Rosenthal, R E, Chapter 2: A GAMS TutorialIn GAMS: User's Guide. +Pkg.add("JuMP"                    # The mathematical optimisation library 
-The Scientific Press, Redwood City, California, 1988. +Pkg.add("GLPKMathProgInterface"   # A lineaqr and MIP solver 
-JuMP implementation: Antonello Lobianco +Pkg.add("Ipopt"                   # non-linear solver 
-=#+Pkg.add("DataFrames"              # A library to deal with dataframes (R-like tabular data) 
 +</code>
  
-using JuMP, DataFrames+===== Model components =====
  
-# Sets +==== Importing the libraries ====
-plants  ["seattle","san_diego"         # canning plants +
-markets ["new_york","chicago","topeka" # markets+
  
-# Parameters +You will need to import as minima the ''JuMP'' module. If you wish to specify a solver engine rather than letting JuMP select a suitable oneyou will need to import also the module relative to the solvere.g. ''Ipopt'' or  ''GLPKMathProgInterface''
-= Dict(              # capacity of plant i in cases +
-  "seattle"   => 350, +
-  "san_diego" => 600, +
-+
-b = Dict(              # demand at market j in cases +
-  "new_york"  => 325, +
-  "chicago"   => 300, +
-  "topeka"    => 275, +
-)+
  
- distance in thousands of miles +<code julia> 
-d_table = wsv""" +Import of the JuMP and DataFrames modules (the latter one just to import the data from a header-based tableas in the original trasnport example in GAMS  
-plants     new_york  chicago  topeka +using JuMPDataFrames 
-seattle    2.5       1.7      1.8 +</code>
-san_diego  2.5       1.8      1.4 +
-""" +
-d = Dict(r[:plants],m) => r[Symbol(m)] for r in eachrow(d_table)m in markets)+
  
-90 # freight in dollars per case per thousand miles+==== Defining the "sets" ====
  
-c = Dict() # transport cost in thousands of dollars per case ; +JuMP doesn't really have a concept of setsbut it uses the native containers available in the core Julia language\\Variables, parameters and constraints can be indexed using these containers.\\ 
-[ c[p,m] = f * d[p,m] / 1000 for p in plants, m in markets] +While many works with position-based listsI find more readable using dictionaries instead. So the "setsare represented as listsbut then everything else is a dictionary with the elements of the list as keys.\\ 
- +One noteit seems that Julia/JuMP don't like much the "-symbol, so I replaced it to "_".\\
-# Model declaration +
-trmodel = Model() # transport model +
- +
-Variables +
-@variables trmodel begin +
-    x[p in plantsm in markets] >= 0 # shipment quantities in cases +
-end +
- +
-# Constraints +
-@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 +
- +
-# Objective +
-@objective trmodel Min begin +
-    sum(c[p,m]*x[p,m] for p in plants, m in markets) +
-end +
- +
-print(trmodel) +
- +
-status = solve(trmodel) +
- +
-if status == :Optimal +
-    println("Objective value: ", getobjectivevalue(trmodel)) +
-    println("Shipped quantities: ") +
-    println(getvalue(x)) +
-    println("Shadow prices of supply:") +
-    [println("$p = $(getdual(supply[p]))") for p in plants] +
-    println("Shadow prices of demand:"+
-    [println("$m = $(getdual(demand[m]))") for m in markets] +
- +
-else +
-    println("Model didn't solved"+
-    println(status) +
-end+
    
-Expected result: +<code julia> 
-obj= 153.675 +## Define sets #
-#['seattle','new-york'  = 50 + Sets 
-#['seattle','chicago'   300 +      i   canning plants   / seattle, san-diego / 
-#['seattle','topeka'    = 0 +#         markets          / new-york, chicago, topeka / ; 
-#['san-diego','new-york'275 +plants  = ["seattle","san_diego"         # canning plants 
-#['san-diego','chicago']  = 0 +markets = ["new_york","chicago","topeka"]  # markets
-#['san-diego','topeka'  = 275+
 </code> </code>
- 
home_test_julia.txt · Last modified: 2018/06/18 15:11 (external edit)
CC Attribution-Noncommercial-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0