# Parameter setting

**URL:** <https://discourse.julialang.org/t/parameter-setting/76836>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [February 21, 2022, 11:35am UTC](https://discourse.julialang.org/t/parameter-setting/76836 "2022-02-21T11:35:43Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![mamo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mamo/32/25591_2.png) [@mamo](https://discourse.julialang.org/u/mamo)\
**Post date:** [February 21, 2022, 11:35am UTC](https://discourse.julialang.org/t/parameter-setting/76836/1 "2022-02-21T11:35:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [February 21, 2022, 9:03pm UTC](https://discourse.julialang.org/t/parameter-setting/76836/2 "2022-02-21T21:03:18Z")

</div>

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:

```julia
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:

- [Getting started with sets and indexing · JuMP](https://jump.dev/JuMP.jl/stable/tutorials/getting_started/getting_started_with_sets_and_indexing/)
- [Getting started with data and plotting · JuMP](https://jump.dev/JuMP.jl/stable/tutorials/getting_started/getting_started_with_data_and_plotting/)

---

<div class="post-metadata">

**Author:** ![mamo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mamo/32/25591_2.png) [@mamo](https://discourse.julialang.org/u/mamo)\
**Post date:** [February 22, 2022, 8:13pm UTC](https://discourse.julialang.org/t/parameter-setting/76836/3 "2022-02-22T20:13:58Z")

</div>

Perfect, thanks Oscar.  
Best,  
M

---

<div class="post-metadata">

**Author:** ![mamo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mamo/32/25591_2.png) [@mamo](https://discourse.julialang.org/u/mamo)\
**Post date:** [February 23, 2022, 11:25am UTC](https://discourse.julialang.org/t/parameter-setting/76836/4 "2022-02-23T11:25:25Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [February 23, 2022, 11:21pm UTC](https://discourse.julialang.org/t/parameter-setting/76836/5 "2022-02-23T23:21:32Z")

</div>

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,

```julia
# 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
```

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [February 24, 2022, 12:16am UTC](https://discourse.julialang.org/t/parameter-setting/76836/6 "2022-02-24T00:16:43Z")

</div>

Here is a full example:

```julia
# 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
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.

---

<div class="post-metadata">

**Author:** ![mamo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mamo/32/25591_2.png) [@mamo](https://discourse.julialang.org/u/mamo)\
**Post date:** [February 24, 2022, 9:33am UTC](https://discourse.julialang.org/t/parameter-setting/76836/7 "2022-02-24T09:33:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mamo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mamo/32/25591_2.png) [@mamo](https://discourse.julialang.org/u/mamo)\
**Post date:** [February 27, 2022, 2:03pm UTC](https://discourse.julialang.org/t/parameter-setting/76836/8 "2022-02-27T14:03:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [February 27, 2022, 7:31pm UTC](https://discourse.julialang.org/t/parameter-setting/76836/9 "2022-02-27T19:31:55Z")

</div>

Another option is:

```nohighlight
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

```

---

<div class="post-metadata">

**Author:** ![mamo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mamo/32/25591_2.png) [@mamo](https://discourse.julialang.org/u/mamo)\
**Post date:** [February 27, 2022, 7:39pm UTC](https://discourse.julialang.org/t/parameter-setting/76836/10 "2022-02-27T19:39:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [February 27, 2022, 9:40pm UTC](https://discourse.julialang.org/t/parameter-setting/76836/11 "2022-02-27T21:40:48Z")

</div>

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.
