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?
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.