Parameter setting

Dear all,
I need to set a parameter with the structure equivalent to this AMPL declaration:
param wg{c in C, cont in CONT[c]};
sets C and CONT[c] are as follows:
set C := C1 C2;
set CONT[C1] := 1 2 3 4 5;
set CONT[C2] := 6 7 8 9 10;
param wg in AMPL looks like:
wg :=
C1 1 100
C1 2 200
C1 3 300
C1 4 400
C1 5 400
C2 6 130
C2 7 150
C2 8 200
C2 9 200
C2 10 400
;
Thanks on your support.
Best,
mamo

To clarify, you want to do this in JuMP?

JuMP doesn’t have explicit parameters like AMPL. You can use any data structure you like. For example:

C = ["C1", "C2"]
CONT = Dict(
    "C1" => [1, 2, 3, 4, 5],
    "C2" => [6, 7, 8, 9, 10],
)
wg = Dict(
    ("C1", 1) => 100,
    ("C1", 2) => 200,
    ("C1", 3) => 300,
    ("C1", 4) => 400,
    ("C1", 5) => 400,
    ("C2", 6) => 130,
    ("C2", 7) => 150,
    ("C2", 8) => 200,
    ("C2", 9) => 200,
    ("C2", 10) => 400,
)

You might want to review the “Getting started” tutorials in JuMP:

1 Like

Perfect, thanks Oscar.
Best,
M

1 Like

Dear Oscar and community,
I would need additional help with declaring this s parameter in Julia =>
param s:=
[W1, ,]: C1 C2 :=
P11 0 1
P12 2 0

[W2,,]: C1 C2 :=
P21 3 0
P22 1 1

[W3,,]: C1 C2 :=
P31 4 0
P32 2 1
P33 0 2
P34 2 1;

W and C are arrays =>
W=[“W1” “W2” “W3”]
C=[“C1” “C2”]
P is a dict =>
P = Dict(“W1” => [“P11”, “P12”], “W2” => [“P21”, “P22”], “W3” => [“P31”, “P32”, “P33”, “P34”])
Entries can be declared as int array:
D= [0 1 2 0 3 0 1 1 4 0 2 1 0 2 2 1]

I suppose I have to use tuples, such as =>
for w in W
for p in values(P)
for c in C
s[w,p,c]=Array{Tuple{(:w,:p,:c,:d), Tuple{String, String, String, Int64}}}
end
end
end

Thank you!

It might be best to start a new thread for a new question.

I wouldn’t use strings for indexing if the indexing is really just labelled integers. So, for example,

# Indexing sets
W = [1, 2, 3]
C = [1, 2]
# Base data
D = [0 1 2 0 3 0 1 1 4 0 2 1 0 2 2 1]
# New data structure
s = Dict{Tuple{Int64, Int64, Int64}, Number}()
# Read loop:
for w in W
    for p in P
1 Like

Here is a full example:

# Indexing sets
W = [1, 2, 3]
C = [1, 2]

P = Dict( 1 => [1,2], 2 => [1, 2], 3 => [1, 2, 3, 4])

# Base data
D = [0 1 2 0 3 0 1 1 4 0 2 1 0 2 2 1]

# New data structure
s = Dict{Int, Matrix{Int}}()

# Read loop:
LW = length(W);
LC = length(C); # We're assuming this remains constant!
block_lengths = [LC*length(P[w]) for w in W]

start_ptr = 1
for w in W
    end_ptr = start_ptr + block_lengths[w] - 1

    B = D[start_ptr:(end_ptr)] # get the right block of data
    println(B)

    s[w] = transpose(reshape(B, LC, length(P[w])))

    start_ptr = end_ptr+1
end

And you can access each block of data using the s dictionary:

julia> s[1]
2×2 Matrix{Int64}:
 0  1
 2  0

julia> s[2]
2×2 Matrix{Int64}:
 3  0
 1  1

julia> s[3]
4×2 Matrix{Int64}:
 4  0
 2  1
 0  2
 2  1

Edit: get the transposition right.

2 Likes

Thank you jd-foster. I have to use strings for W and C, but this idea will be very usefull.
Best,
M

Dear all,
since I have to use strings instead of int dicts for W and C, share with me please the idea how to modify Jason’s code. if i use eachindex(W) in above loop I come to a problem at “s[w]” and viceversa, with W as a string the problem is with block_lengths.
Thanks,
M

Another option is:

julia> using JuMP

julia> C = ["C1", "C2"]
2-element Vector{String}:
 "C1"
 "C2"

julia> s = Dict(
           "W1" => Containers.DenseAxisArray([0 1; 2 0], ["P11", "P12"], C),
           "W2" => Containers.DenseAxisArray([3 0; 1 1], ["P21", "P22"], C),
           "W3" => Containers.DenseAxisArray([4 0; 2 1; 0 2; 2 1], ["P31", "P32", "P33", "P34"], C),
       )
Dict{String, JuMP.Containers.DenseAxisArray{Int64, 2, Tuple{Vector{String}, Vector{String}}, Tuple{JuMP.Containers._AxisLookup{Dict{String, Int64}}, JuMP.Containers._AxisLookup{Dict{String, Int64}}}}} with 3 entries:
  "W2" => 2-dimensional DenseAxisArray{Int64,2,...} with index sets:…
  "W3" => 2-dimensional DenseAxisArray{Int64,2,...} with index sets:…
  "W1" => 2-dimensional DenseAxisArray{Int64,2,...} with index sets:…

julia> s["W1"]
2-dimensional DenseAxisArray{Int64,2,...} with index sets:
    Dimension 1, ["P11", "P12"]
    Dimension 2, ["C1", "C2"]
And data, a 2×2 Matrix{Int64}:
 0  1
 2  0

julia> s["W1"]["P11", "C1"]
0

julia> s["W1"]["P12", "C1"]
2

julia> s["W3"]["P34", "C1"]
2
3 Likes

Excellent, thank you Oscar. In also tried with:
s=Dict{Array{String,1}, Int}(

["W1", "P11", "C1"]=>0,
["W1","P11", "C2"]=>1,
["W1", "P12", "C1"]=>2,
["W2", "P21", "C1"]=>3,
["W2", "P21", "C2"]=>0,
...)

Thanks!
Best,
M

1 Like

Yes, the key here is to get away from thinking like AMPL, to realizing that you can construct your data in whatever form fits best. There isn’t one right answer.

1 Like