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 | ||
personal:blog:2017:0203_jump_for_gams_users [2017/02/07 10:20] antonello |
personal:blog:2017:0203_jump_for_gams_users [2023/12/22 11:20] antonello [Definition of the "parameters"] |
||
---|---|---|---|
Line 31: | Line 31: | ||
Run, only once, the following code to install JuMP language and a couple of open source solvers: | Run, only once, the following code to install JuMP language and a couple of open source solvers: | ||
<code julia> | <code julia> | ||
- | Pkg.update() | + | using Pkg # Load the package manager |
- | Pkg.add(" | + | Pkg.update() |
- | Pkg.add(" | + | Pkg.add(" |
- | Pkg.add(" | + | Pkg.add(" |
- | Pkg.add(" | + | Pkg.add(" |
+ | Pkg.add(" | ||
+ | Pkg.add(" | ||
</ | </ | ||
Line 42: | Line 44: | ||
==== Importing the libraries ==== | ==== Importing the libraries ==== | ||
- | You will need to import as a minima the '' | + | You will need to import as a minima the '' |
< | < | ||
- | # Import of the JuMP and DataFrames modules (the latter | + | # Import of the JuMP, GLPK, CSV and DataFrames modules (the latter |
- | using JuMP, DataFrames | + | using CSV, DataFrames, GLPK, JuMP |
</ | </ | ||
Line 56: | Line 58: | ||
<code julia> | <code julia> | ||
- | ## Define sets ## | + | # Define sets # |
# Sets | # Sets | ||
# | # | ||
Line 70: | Line 72: | ||
<code julia> | <code julia> | ||
- | ## Define parameters | + | # Define parameters # |
# | # | ||
# | # | ||
Line 94: | Line 96: | ||
# seattle | # seattle | ||
# san-diego | # san-diego | ||
- | d_table = wsv""" | + | d_table = CSV.read(IOBuffer(""" |
plants | plants | ||
seattle | seattle | ||
san_diego | san_diego | ||
- | """ | + | """ |
d = Dict( (r[: | d = Dict( (r[: | ||
# Here we are converting the table in a " | # Here we are converting the table in a " | ||
Line 135: | Line 137: | ||
<code julia> | <code julia> | ||
- | # Model declaration | + | # Model declaration |
- | trmodel = Model() | + | trmodel = Model() |
</ | </ | ||
Line 235: | Line 238: | ||
<code Julia> | <code Julia> | ||
+ | # Transport example | ||
+ | |||
# Transposition in JuMP of the basic transport model used in the GAMS tutorial | # Transposition in JuMP of the basic transport model used in the GAMS tutorial | ||
# | # | ||
Line 246: | Line 251: | ||
# The Scientific Press, Redwood City, California, 1988. | # The Scientific Press, Redwood City, California, 1988. | ||
# - JuMP implementation: | # - JuMP implementation: | ||
- | + | ||
- | using JuMP, DataFrames | + | using CSV, DataFrames, GLPK, JuMP |
+ | |||
# Sets | # Sets | ||
plants | plants | ||
markets = [" | markets = [" | ||
+ | |||
# Parameters | # Parameters | ||
a = Dict( # capacity of plant i in cases | a = Dict( # capacity of plant i in cases | ||
Line 263: | Line 268: | ||
" | " | ||
) | ) | ||
+ | |||
# distance in thousands of miles | # distance in thousands of miles | ||
- | d_table = wsv""" | + | d_table = CSV.read(IOBuffer(""" |
plants | plants | ||
seattle | seattle | ||
san_diego | san_diego | ||
- | """ | + | """ |
d = Dict( (r[: | d = Dict( (r[: | ||
+ | |||
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 292: | Line 297: | ||
sum(x[p,m] for p in plants) | sum(x[p,m] for p in plants) | ||
end | end | ||
+ | |||
# Objective | # Objective | ||
@objective trmodel Min begin | @objective trmodel Min begin | ||
sum(c[p, | sum(c[p, | ||
end | end | ||
+ | |||
print(trmodel) | print(trmodel) | ||
+ | |||
+ | optimize!(trmodel) | ||
+ | status = termination_status(trmodel) | ||
- | status = solve(trmodel) | + | if status == MOI.OPTIMAL |
- | + | println(" | |
- | if status == :Optimal | + | |
- | println(" | + | |
println(" | println(" | ||
- | println(getvalue(x)) | + | println(value.(x)) |
println(" | println(" | ||
- | [println(" | + | [println(" |
println(" | println(" | ||
- | [println(" | + | [println(" |
+ | |||
else | else | ||
println(" | println(" | ||
println(status) | println(status) | ||
end | end | ||
- | + | ||
# Expected result: | # Expected result: | ||
# obj= 153.675 | # obj= 153.675 |