# Multiple dimensional sampling with first element string

**URL:** https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863
**Category:** Optimization (Mathematical)
**Tags:** question, jump, flux
**Created:** [April 8, 2021, 5:44pm UTC](https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863 "2021-04-08T17:44:28Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![GraceMabele](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gracemabele/32/21360_2.png) [@GraceMabele](https://discourse.julialang.org/u/GraceMabele)
#### Post date: [April 8, 2021, 5:44pm UTC](https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863/1 "2021-04-08T17:44:28Z")

</div>

Hi,

My equation g below is a function of the variable `d` which is indexed by `s,n`. I would like to sampling points to be of d[s,n]. how do I create the correct lb and ub.

```julia
using DataStructures
using JuMP
using Surrogates

S = ["s1","s2","s3","s4"]
N = [1:10]

price = OrderedDict(
"s1" => 0,
"s2" => 0,
"s3" => 0,
"s4" => 5
)

m = Model()

@variables m begin
  d[s in S, n in N] >= 0
end

g= d -> 

@objective m Max begin
sum((price[s]*d[s,n]) for n in N for s in S)
end

```

```julia
lb = [S[1],1]
ub = [S[4],10]
x_train = sample(10, lb,ub, SobolSample())

y_train = g.(x_train);

```

This is my attempt but it is not working out when inputting into x\_train.

This is the error being returned:

MethodError: no method matching +(::String, ::Int64)  
Closest candidates are:  
+(::Any, ::Any, ::Any, ::Any…) at operators.jl:538  
+(::ChainRulesCore.DoesNotExist, ::Any) at C:\Users\glmab.julia\packages\ChainRulesCore\D0go7\src\differential\_arithmetic.jl:23  
+(::Complex{Bool}, ::Real) at complex.jl:301  
…

Stacktrace:  
[1] add\_sum(::String, ::Int64) at .\reduce.jl:24  
[2] \_mapreduce(::typeof(identity), ::typeof(Base.add\_sum), ::IndexLinear, ::Array{Any,1}) at .\reduce.jl:408  
[3] \_mapreduce\_dim at .\reducedim.jl:318 [inlined]  
[4] #mapreduce#620 at .\reducedim.jl:310 [inlined]  
[5] mapreduce at .\reducedim.jl:310 [inlined]  
[6] \_sum at .\reducedim.jl:727 [inlined]  
[7] \_sum at .\reducedim.jl:726 [inlined]  
[8] #sum#627 at .\reducedim.jl:722 [inlined]  
[9] sum at .\reducedim.jl:722 [inlined]  
[10] Sobol.SobolSeq(::Int64, ::Array{Any,1}, ::Array{Any,1}) at C:\Users\glmab.julia\packages\Sobol\l4iL1\src\Sobol.jl:121  
[11] SobolSeq at C:\Users\glmab.julia\packages\Sobol\l4iL1\src\Sobol.jl:124 [inlined]  
[12] sample(::Int64, ::Array{Any,1}, ::Array{Any,1}, ::SobolSample) at C:\Users\glmab.julia\packages\Surrogates\wJbFN\src\Sampling.jl:77  
[13] top-level scope at In[66]:1  
[14] include\_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [April 8, 2021, 5:58pm UTC](https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863/2 "2021-04-08T17:58:17Z")

</div>

Your example is confusing. Where exactly does it fail? Can you remove all the unnecessary lines from your example?

> [@GraceMabele](#):
>
> This is my attempt but it is not working out when inputting into x\_train.

You are not inputting anything into `x_train` in your example.

---

<div class="post-metadata">

### Author: ![GraceMabele](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gracemabele/32/21360_2.png) [@GraceMabele](https://discourse.julialang.org/u/GraceMabele)
#### Post date: [April 8, 2021, 6:07pm UTC](https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863/3 "2021-04-08T18:07:15Z")

</div>

> [@DNF](#):
>
> Your example is confusing. Where exactly does it fail? Can you remove all the unnecessary lines from your example?

I fixed it. I am inputting lb and ub into x\_train. That is where it is failing. My variable `d` has indices `s,n`.  
I want x\_train to return eg. `d[s1,1]`, `d[s2,3]`

Hope I am making sense now

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [April 8, 2021, 6:38pm UTC](https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863/4 "2021-04-08T18:38:04Z")

</div>

> [@GraceMabele](#):
>
> I am inputting lb and ub into x\_train.

There’s something off here. You’re not inputting anything into `x_train`. You are inputting `lb` and `ub` into `sample`, then you input `x_train` into `g`.

Presumably, you mean that your code fails here?

```julia
x_train = sample(10, lb,ub, SobolSample())

```

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [April 8, 2021, 6:43pm UTC](https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863/5 "2021-04-08T18:43:01Z")

</div>

Where does `SobolSample` come from? It doesn’t seem to belong to either JuMP or DataStructures.

---

<div class="post-metadata">

### Author: ![GraceMabele](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gracemabele/32/21360_2.png) [@GraceMabele](https://discourse.julialang.org/u/GraceMabele)
#### Post date: [April 8, 2021, 6:44pm UTC](https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863/6 "2021-04-08T18:44:37Z")

</div>

Yes.

But I am rather trying to understand how to input the correct type of data to x\_train/y\_train. This is dependent on how I formulate lb and ub (lower and upper bounds respectively).

From the error that y\_train is returning (see below). I believe it is expecting a Tuple{Float64,Float64} but I am giving it (::String, ::Int64).

MethodError: no method matching getindex(::Tuple{Float64,Float64}, ::String, ::Int64)  
Closest candidates are:  
getindex(::Tuple, ::Int64) at tuple.jl:24  
getindex(::Tuple, ::Real) at tuple.jl:25  
getindex(::Tuple, ::UnitRange) at range.jl:293  
…

---

<div class="post-metadata">

### Author: ![GraceMabele](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gracemabele/32/21360_2.png) [@GraceMabele](https://discourse.julialang.org/u/GraceMabele)
#### Post date: [April 8, 2021, 6:45pm UTC](https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863/7 "2021-04-08T18:45:50Z")

</div>

> [@DNF](#):
>
> Where does `SobolSample` come from? It doesn’t seem to belong to either JuMP or DataStructures

Sorry it is part of the package Surrogates. I forgot to include it in the MWE

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [April 8, 2021, 6:48pm UTC](https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863/8 "2021-04-08T18:48:51Z")

</div>

> [@GraceMabele](#):
>
> But I am rather trying to understand how to input the correct type of data to x\_train/y\_train. This is dependent on how I formulate lb and ub (lower and upper bounds respectively).

We are somehow misunderstanding each other. You are not inputting _anything_ into `x_train` _or_ `y_train`. They are _outputs_ from `sample` and `g`, respectively.

Can you confirm on which line your code fails?

---

<div class="post-metadata">

### Author: ![GraceMabele](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gracemabele/32/21360_2.png) [@GraceMabele](https://discourse.julialang.org/u/GraceMabele)
#### Post date: [April 8, 2021, 6:52pm UTC](https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863/9 "2021-04-08T18:52:17Z")

</div>

> [@DNF](#):
>
> We are somehow misunderstanding each other. You are not inputting _anything_ into `x_train` _or_ `y_train` . They are _outputs_ from `sample` and `g` , respectively.

Oh yes. My mistake. My wording was not communicating that.  
My code is failing here.

```julia
y_train = g.(x_train);

```

With this error:

MethodError: no method matching getindex(::Tuple{Float64,Float64}, ::String, ::Int64)  
Closest candidates are:  
getindex(::Tuple, ::Int64) at tuple.jl:24  
getindex(::Tuple, ::Real) at tuple.jl:25  
getindex(::Tuple, ::UnitRange) at range.jl:293  
…

Stacktrace:  
[1] (::var"#195#376"{Int64,Tuple{Float64,Float64},OrderedDict{String,Int64}})(::String) at .\none:0  
[2] MappingRF at .\reduce.jl:93 [inlined]  
[3] \_foldl\_impl(::Base.MappingRF{var"#195#376"{Int64,Tuple{Float64,Float64},OrderedDict{String,Int64}},Base.BottomRF{typeof(Base.add\_sum)}}, ::Base.\_InitialValue, ::Array{String,1}) at .\reduce.jl:58  
[4] FlatteningRF at .\reduce.jl:119 [inlined]  
[5] MappingRF at .\reduce.jl:93 [inlined]  
[6] \_foldl\_impl(::Base.MappingRF{var"#196#375"{Tuple{Float64,Float64},OrderedDict{String,Int64}},Base.FlatteningRF{Base.BottomRF{typeof(Base.add\_sum)}}}, ::Base.\_InitialValue, ::UnitRange{Int64}) at .\reduce.jl:58  
[7] foldl\_impl at .\reduce.jl:48 [inlined]  
[8] mapfoldl\_impl at .\reduce.jl:44 [inlined]  
[9] #mapfoldl#204 at .\reduce.jl:160 [inlined]  
[10] mapfoldl at .\reduce.jl:160 [inlined]  
[11] #mapreduce#208 at .\reduce.jl:287 [inlined]  
[12] mapreduce at .\reduce.jl:287 [inlined]  
[13] sum at .\reduce.jl:494 [inlined]  
[14] sum(::Base.Iterators.Flatten{Base.Generator{UnitRange{Int64},var"#196#375"{Tuple{Float64,Float64},OrderedDict{String,Int64}}}}) at .\reduce.jl:511  
[15] macro expansion at C:\Users\glmab.julia\packages\MutableArithmetics\bPWR4\src\rewrite.jl:276 [inlined]  
[16] macro expansion at C:\Users\glmab.julia\packages\JuMP\y5vgk\src\macros.jl:928 [inlined]  
[17] (::var"#194#374"{OrderedDict{String,Int64}})(::Tuple{Float64,Float64}) at .\In[17]:206  
[18] \_broadcast\_getindex\_evalf at .\broadcast.jl:648 [inlined]  
[19] \_broadcast\_getindex at .\broadcast.jl:621 [inlined]  
[20] getindex at .\broadcast.jl:575 [inlined]  
[21] copy at .\broadcast.jl:876 [inlined]  
[22] materialize(::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Nothing,var"#194#374"{OrderedDict{String,Int64}},Tuple{Array{Tuple{Float64,Float64},1}}}) at .\broadcast.jl:837  
[23] top-level scope at In[51]:1  
[24] include\_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [April 8, 2021, 6:59pm UTC](https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863/10 "2021-04-08T18:59:25Z")

</div>

I’m trying to condense your problem into a minimal working example (MWE), see here [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757) for how to structure questions that are easy to understand.

It is not certain that I can help, but I can help to simplify your question, at least.

So most of your code plays no part here. I reduced your example to this:

```julia
using Surrogates
x_train = sample(10, ["s1", 1], ["s4", 10], SobolSample())

```

This gives an error:

```julia
ERROR: MethodError: no method matching +(::String, ::Int64)

```

But you are saying that this is no longer a problem, and your code now fails at the next line:

> [@GraceMabele](#):
>
> `y_train = g.(x_train);`

instead?

How did you get the `x_train`?

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [April 8, 2021, 7:07pm UTC](https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863/11 "2021-04-08T19:07:54Z")

</div>

> [@DNF](#):
>
> `sample(10, ["s1", 1], ["s4", 10], SobolSample())`

So the error in this line is caused by `Surrogates.sample` trying to call `sum(ub)` when `ub` is `["s4", 10]`, so apparently it makes no sense to use `["s1", 1]`, and `["s4", 10]` as lower and upper bounds.

---

<div class="post-metadata">

### Author: ![GraceMabele](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gracemabele/32/21360_2.png) [@GraceMabele](https://discourse.julialang.org/u/GraceMabele)
#### Post date: [April 8, 2021, 7:12pm UTC](https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863/12 "2021-04-08T19:12:13Z")

</div>

> [@DNF](#):
>
> But you are saying that this is no longer a problem, and your code now fails at the next line:

I am not saying it is no longer a problem but I now want to solve it from y\_train moving up.

x\_train needs to be correct for y\_train to be correct. Because when x\_train is Float64 (or Int64 or the likes). y\_train returns the below error.

MethodError: no method matching getindex(::Tuple{Float64,Float64}, ::String, ::Int64)  
Closest candidates are:  
getindex(::Tuple, ::Int64) at tuple.jl:24  
getindex(::Tuple, ::Real) at tuple.jl:25  
getindex(::Tuple, ::UnitRange) at range.jl:293

---

<div class="post-metadata">

### Author: ![GraceMabele](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gracemabele/32/21360_2.png) [@GraceMabele](https://discourse.julialang.org/u/GraceMabele)
#### Post date: [April 8, 2021, 7:15pm UTC](https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863/13 "2021-04-08T19:15:20Z")

</div>

> [@DNF](#):
>
> How did you get the `x_train` ?

This is my current x\_train

```julia
x_train = sample(10, Float64[1,10] ,Float64[10,100], SobolSample())

```

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [April 8, 2021, 7:20pm UTC](https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863/14 "2021-04-08T19:20:56Z")

</div>

This is taking some time to install, but just an idea: Have you tried using

```julia
N = 1:10

```

instead of

```julia
N = [1:10]

```

They mean very different things, and it looks like it could be a problem.

---

<div class="post-metadata">

### Author: ![GraceMabele](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gracemabele/32/21360_2.png) [@GraceMabele](https://discourse.julialang.org/u/GraceMabele)
#### Post date: [April 8, 2021, 7:27pm UTC](https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863/15 "2021-04-08T19:27:46Z")

</div>

> [@DNF](#):
>
> `N = [1:10]`

Using that remains the error same, Int64 changes to UnitRange{Int64}

MethodError: no method matching getindex(::Tuple{Float64,Float64}, ::String, ::UnitRange{Int64})  
Closest candidates are:  
getindex(::Tuple, ::Int64) at tuple.jl:24  
getindex(::Tuple, ::Real) at tuple.jl:25  
getindex(::Tuple, ::UnitRange) at range.jl:293

---

<div class="post-metadata">

### Author: ![GraceMabele](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gracemabele/32/21360_2.png) [@GraceMabele](https://discourse.julialang.org/u/GraceMabele)
#### Post date: [April 8, 2021, 7:31pm UTC](https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863/16 "2021-04-08T19:31:50Z")

</div>

Do you perhaps know what the getindex error means? While the packages install.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [April 8, 2021, 7:36pm UTC](https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863/17 "2021-04-08T19:36:11Z")

</div>

But anyway, what is your intention with `[1:10]`? Do you intend to create a vector of length 1, containing a single element, which is the range `1:10`?

But looking further at you code now, this function:

```julia
g = d -> @objective m Max begin
    sum((price[s]*d[s,n]) for n in N for s in S)
end

```

Since you broadcast `g` over the elements of `x_train`, the value of `d` is a tuple of two numbers, lets take the first element of `x_train` which equals `(7.1875, 83.125)`. Then you try to index into it like this: `d[s,n]`, but that won’t work, that’s how you index into a two-dimensional array, a tuple only takes a single index.

This fits with the error message:

```julia
julia> t = (7.1875, 83.125);

julia> t["s1", 1:10]
ERROR: MethodError: no method matching getindex(::Tuple{Float64, Float64}, ::String, ::UnitRange{Int64})
Closest candidates are:
  getindex(::Tuple, ::Int64) at tuple.jl:29
  getindex(::Tuple, ::Real) at tuple.jl:30
  getindex(::Tuple, ::Colon) at tuple.jl:33

```

---

<div class="post-metadata">

### Author: ![GraceMabele](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gracemabele/32/21360_2.png) [@GraceMabele](https://discourse.julialang.org/u/GraceMabele)
#### Post date: [April 8, 2021, 7:50pm UTC](https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863/18 "2021-04-08T19:50:47Z")

</div>

> [@DNF](#):
>
> But anyway, what is your intention with `[1:10]` ? Do you intend to create a vector of length 1, containing a single element, which is the range `1:10` ?

My intention with 1:10 is an array of 10 values 1, 2, 3,4… 10.

In the case of g, d is supposed d[s1,1] … d[s1,10] likewise for s2,s3,s4 etc. I assume that is why the getindex error is also saying.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [April 8, 2021, 7:55pm UTC](https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863/19 "2021-04-08T19:55:41Z")

</div>

> [@GraceMabele](#):
>
> My intention with 1:10 is an array of 10 values 1, 2, 3,4… 10.

Then you must write `1:10`, _not_ `[1:10]`.

> [@GraceMabele](#):
>
> In the case of g, d is supposed d[s1,1] … d[s1,10] likewise for s2,s3,s4 etc. I assume that is why the getindex error is also saying.

Then `x_train` has completely the wrong structure.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [April 8, 2021, 7:59pm UTC](https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863/20 "2021-04-08T19:59:06Z")

</div>

Maybe more broadly, could you describe what your code is trying to do?

I find it hard to guess what’s supposed to be happening here, and I have a feeling that this might be due to you maybe being a beginner with the language and struggling to express basic things in an idiomatic way?

[Next page](https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863.md?page=2)
