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 | ||
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 ===== |
- | < | + | **Step 1:** |
- | #= | + | * Option a: Get an account on [[https:// |
- | Transposition in JuMP of the basic transport model used in the GAMS tutorial | + | * Option b: Install Julia for your platform ([[http:// |
- | This problem finds a least cost shipping schedule that meets | + | **Step 2:** |
- | requirements at markets and supplies at factories. | + | |
- | - Original formulation: | + | Run, only once, the following code to install JuMP language |
- | Princeton University Press, Princeton, New Jersey, 1963. | + | <code julia> |
- | - Gams implementation: | + | Pkg.update() |
- | Rosenthal, R E, Chapter 2: A GAMS Tutorial. In GAMS: A User's Guide. | + | Pkg.add(" |
- | The Scientific Press, Redwood City, California, 1988. | + | Pkg.add(" |
- | - JuMP implementation: | + | Pkg.add(" |
- | =# | + | Pkg.add(" |
+ | </ | ||
- | using JuMP, DataFrames | + | ===== Model components ===== |
- | # Sets | + | ==== Importing the libraries ==== |
- | plants | + | |
- | markets | + | |
- | # Parameters | + | You will need to import as a minima the '' |
- | a = Dict( # capacity of plant i in cases | + | |
- | " | + | |
- | " | + | |
- | ) | + | |
- | b = Dict( # demand at market j in cases | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | ) | + | |
- | # | + | <code julia> |
- | d_table = wsv""" | + | # Import |
- | plants | + | using JuMP, DataFrames |
- | seattle | + | </ |
- | san_diego | + | |
- | """ | + | |
- | 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 | + | ==== Defining the " |
- | c = Dict() # transport cost in thousands | + | JuMP doesn' |
- | [ c[p,m] = f * d[p,m] / 1000 for p in plants, m in markets] | + | While many works with position-based lists, I find more readable using dictionaries instead. So the "sets" |
- | + | One note: it seems that Julia/JuMP don't like much the "-" | |
- | # Model declaration | + | |
- | trmodel = Model() # transport model | + | |
- | + | ||
- | # Variables | + | |
- | @variables trmodel begin | + | |
- | x[p in plants, m in markets] >= 0 # shipment quantities in cases | + | |
- | end | + | |
- | + | ||
- | # Constraints | + | |
- | @constraints | + | |
- | | + | |
- | sum(x[p,m] for m in markets) | + | |
- | demand[m in markets], | + | |
- | sum(x[p,m] for p in plants) | + | |
- | end | + | |
- | + | ||
- | # Objective | + | |
- | @objective trmodel Min begin | + | |
- | sum(c[p, | + | |
- | end | + | |
- | + | ||
- | print(trmodel) | + | |
- | + | ||
- | status = solve(trmodel) | + | |
- | + | ||
- | if status == :Optimal | + | |
- | println("Objective value: | + | |
- | println(" | + | |
- | println(getvalue(x)) | + | |
- | println(" | + | |
- | | + | |
- | println(" | + | |
- | [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' | + | # Sets |
- | # | + | # i |
- | #['seattle',' | + | # |
- | # | + | plants |
- | #[' | + | markets |
- | #[' | + | |
</ | </ | ||
- |