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:33] 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:// | ||
+ | * Option b: Install Julia for your platform ([[http:// | ||
+ | **Step 2:** | ||
- | Transposition in JuMP of the basic transport model used in the GAMS tutorial | + | Run, only once, the following code to install |
+ | <code julia> | ||
+ | Pkg.update() | ||
+ | Pkg.add(" | ||
+ | Pkg.add(" | ||
+ | Pkg.add(" | ||
+ | Pkg.add(" | ||
+ | </ | ||
- | This problem finds a least cost shipping schedule that meets | + | ===== Model components ===== |
- | requirements at markets and supplies at factories. | + | |
- | - Original formulation: | + | ==== Importing the libraries ==== |
- | 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. | + | |
- | - JuMP implementation: | + | |
+ | 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 | ||
using JuMP, DataFrames | using JuMP, DataFrames | ||
+ | </ | ||
- | # Sets | + | ==== Defining the " |
+ | |||
+ | JuMP doesn' | ||
+ | While many works with position-based lists, I find more readable using dictionaries instead. So the " | ||
+ | One note: it seems that Julia/JuMP don't like much the " | ||
+ | |||
+ | <code julia> | ||
+ | ## Define sets ## | ||
+ | # | ||
+ | # | ||
+ | # | ||
plants | plants | ||
markets = [" | markets = [" | ||
- | |||
- | # Parameters | ||
- | a = Dict( # capacity of plant i in cases | ||
- | " | ||
- | " | ||
- | ) | ||
- | b = Dict( # demand at market j in cases | ||
- | " | ||
- | " | ||
- | " | ||
- | ) | ||
- | |||
- | # distance in thousands of miles | ||
- | d_table = wsv""" | ||
- | plants | ||
- | seattle | ||
- | san_diego | ||
- | """ | ||
- | d = Dict( (r[: | ||
- | |||
- | f = 90 # freight in dollars per case per thousand miles | ||
- | |||
- | 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] | ||
- | |||
- | # 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 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 | ||
- | |||
- | # Objective | ||
- | @objective trmodel Min begin | ||
- | sum(c[p, | ||
- | end | ||
- | |||
- | print(trmodel) | ||
- | |||
- | status = solve(trmodel) | ||
- | |||
- | if status == :Optimal | ||
- | println(" | ||
- | println(" | ||
- | println(getvalue(x)) | ||
- | println(" | ||
- | [println(" | ||
- | println(" | ||
- | [println(" | ||
- | |||
- | else | ||
- | println(" | ||
- | println(status) | ||
- | end | ||
- | |||
- | # Expected result: | ||
- | # obj= 153.675 | ||
- | # | ||
- | # | ||
- | # | ||
- | # | ||
- | # | ||
- | # | ||
</ | </ | ||
- |