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:// | ||
+ | * Option b: Install Julia for your platform ([[http:// | ||
- | using JuMP, DataFrames | + | **Step 2:** |
- | # Sets | + | Run, only once, the following code to install JuMP language and a couple of open source solvers: |
- | plants | + | <code julia> |
- | markets = ["new_york","chicago"," | + | Pkg.update() |
+ | Pkg.add("JuMP") # The mathematical optimisation library | ||
+ | Pkg.add("GLPKMathProgInterface") | ||
+ | Pkg.add("Ipopt") # A non-linear solver | ||
+ | Pkg.add("DataFrames") # A library to deal with dataframes (R-like tabular data) | ||
+ | </ | ||
- | # Parameters | + | ===== Model components ===== |
- | a = Dict( # capacity of plant i in cases | + | |
- | " | + | |
- | " | + | |
- | ) | + | |
- | b = Dict( # demand at market j in cases | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | ) | + | |
- | # distance in thousands of miles | + | ==== Importing the libraries ==== |
- | d_table | + | |
- | plants | + | |
- | seattle | + | |
- | san_diego | + | |
- | """ | + | |
- | d = Dict( (r[: | + | |
- | f = 90 # freight in dollars per case per thousand miles | + | You will need to import as a minima the '' |
- | c = Dict() | + | <code julia> |
- | [ c[p,m] = f * d[p,m] / 1000 for p in plants, m in markets] | + | # Import |
+ | using JuMP, DataFrames | ||
+ | </ | ||
- | # Model declaration | + | ==== Defining the " |
- | trmodel | + | |
- | # Variables | + | JuMP doesn' |
- | @variables trmodel begin | + | While many works with position-based lists, I find more readable using dictionaries instead. So the "sets" |
- | x[p in plants, m in markets] >= 0 # shipment quantities in cases | + | One note: it seems that Julia/JuMP don't like much the "-" |
- | 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 |
- | #[' | + | |
</ | </ | ||
- |