Reputation: 3751
I need to define some constant parameter in Julia+JuMP, similarly to what you do in AMPL when you define
set A := a0 a1 a2;
param p :=
a0 1
a1 5
a2 10 ;
How do I define something like A
and p
in Julia?
Upvotes: 2
Views: 1143
Reputation: 6431
I coudn't get the original answer of @mlubin working. Also, many examples around the web uses position-based indexing, that I don't feel so natural, so I rewritten the trnsport.gms example of the GAMS tutorial using dictionaries instead.. that feel much more close to gams/ampl "sets"..
#=
Transposition in JuMP of the basic transport model used in the GAMS tutorial
This problem finds a least cost shipping schedule that meets requirements at markets and supplies at factories.
- Original formulation: Dantzig, G B, Chapter 3.3. In Linear Programming and Extensions.
Princeton University Press, Princeton, New Jersey, 1963.
- Gams implementation: This formulation is described in detail in:
Rosenthal, R E, Chapter 2: A GAMS Tutorial. In GAMS: A User's Guide.
The Scientific Press, Redwood City, California, 1988.
- JuMP implementation: Antonello Lobianco
=#
using JuMP, DataFrames
# Sets
plants = ["seattle","san_diego"] # canning plants
markets = ["new_york","chicago","topeka"] # markets
# Parameters
a = 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
d_table = wsv"""
plants new_york chicago topeka
seattle 2.5 1.7 1.8
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)
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], # 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:
# obj= 153.675
#['seattle','new-york'] = 50
#['seattle','chicago'] = 300
#['seattle','topeka'] = 0
#['san-diego','new-york'] = 275
#['san-diego','chicago'] = 0
#['san-diego','topeka'] = 275
A much more commented version is available on my related blog post.
Upvotes: 0
Reputation: 953
JuMP itself doesn't define a special syntax for index sets beyond what's available in Julia. So, for example, you can define
A = [:a0, :a1, :a2]
where :a0
defines a symbol.
If you want to index a variable over this set, then the syntax is:
m = Model()
@variable(m, x[A])
JuMP also doesn't make a distinction between the data and the model in the same way as AMPL, so there's no real concept of a parameter. Instead, you just provide the data at the time when it's used. If I understand your question correctly, you could do something like
p = Dict(:a0 => 1, :a1 => 5, :a2 => 10)
@constraint(m, sum(p[i]*x[i] for i in A) <= 20)
This will add the constraint
x[a0] + 5 x[a1] + 10 x[a2] <= 20
Where we define p
as a Julia dictionary. There's nothing specific to JuMP here, and really any julia expression can be provided as coefficient. One could just as easily say
@constraint(m, sum(foo(i)*x[i] for i in A) <= 20)
Where foo
is an arbitrary Julia function that could perform a database lookup, compute digits of pi, etc.
Upvotes: 4