# 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:** 14
**Page:** 2

<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, 8:03pm UTC](https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863/21 "2021-04-08T20:03:00Z")

</div>

Yes I agree.

I am trying to see how to get the correct structure of x\_train, which should be (string, int64) if I’m correct? It should not be a Tuple{String,Int64} if I am correct.

---

<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, 8:04pm UTC](https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863/22 "2021-04-08T20:04:17Z")

</div>

x\_train, y\_train will pass through into the following:

```julia
sgt = NeuralSurrogate(x_train, y_train, bounds..., model=model, loss=loss, opt=optimizer, n_echos=n_epochs)

```

---

<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, 8:36pm UTC](https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863/23 "2021-04-08T20:36:10Z")

</div>

> [@GraceMabele](#):
>
> the correct structure of x\_train, which should be (string, int64)

I’m not sure how to achieve that, but I can say that even if `d` is a `Tuple{String, Int64}`, you cannot index into it with `d[s,n]`, only a single index can be used, and that index must be either `1` or `2`, since they are the only valid indices in a tuple of length 2.

**Edit:** Well, to be honest, you can also index with a vector. For example, `d[1:2]`, or `d[[2,1]]` will work.

---

<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, 8:44pm UTC](https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863/24 "2021-04-08T20:44:52Z")

</div>

Okay it appears you are roughly trying to follow the [Neural network tutorial](http://surrogates.sciml.ai/stable/neural/)?

I don’t know much about JuMP or Surrogates, but in that tutorial it seems to me like they have an objective function `schaffer` which takes an input `x` and produces an output `y`, which you don’t seem to have. Is `g` supposed to be your objective function? It’s not defined in a way such that `g(x)` produces on objective value, so `g.(x_train)` won’t work pretty much irrespective of how you define `x_train`.

---

<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, 8:53pm UTC](https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863/25 "2021-04-08T20:53:06Z")

</div>

Thank you for this comment. I did not use that tutorial, it is my 1st time seeing it.

But yes g is the objective function. I previously attempted to try and do a taylor expansion of g so that I use that as a replacement. I was not successful with that. Is there a another method you could propose that will give me a different form of g?

---

<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, 8:54pm UTC](https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863/26 "2021-04-08T20:54:48Z")

</div>

> [@DNF](#):
>
> **Edit:** Well, to be honest, you can also index with a vector. For example, `d[1:2]` , or `d[[2,1]]` will work.

I don’t think I quite understand what you are proposing?

---

<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, 8:56pm UTC](https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863/27 "2021-04-08T20:56:42Z")

</div>

> [@DNF](#):
>
> I’m not sure how to achieve that, but I can say that even if `d` is a `Tuple{String, Int64}` , you cannot index into it with `d[s,n]` , only a single index can be used, and that index must be either `1` or `2` , since they are the only valid indices in a tuple of length 2.

I don’t think it will be achievable because the sampling methods still return tuples from what I just read. It still will not work in y\_train no matter what x\_train is.

However, thank you so much for your assistance and commitment to assisting me.

---

<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, 9:07pm UTC](https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863/28 "2021-04-08T21:07:54Z")

</div>

> [@GraceMabele](#):
>
> I don’t think I quite understand what you are proposing?

It’s not a proposal, I was just correcting my claim that you can only index with a single number. It is still not possible to index with a two-dimensional index.

Your problem appears to be that this function

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

```

is simply not correctly defined.

---

<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, 9:14pm UTC](https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863/29 "2021-04-08T21:14:13Z")

</div>

Here is the original function. Did I define it correctly?

![eqn](https://global.discourse-cdn.com/julialang/original/3X/2/4/24918b255f80bf8b434367308bbb868e84d4977f.jpeg)

---

<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, 9:17pm UTC](https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863/30 "2021-04-08T21:17:36Z")

</div>

But to call this function, the input to `g` must be a matrix, not a tuple. And also, you seem to try to index with a string, which won’t work, anyway. Indexing with a string can work for dicts, but they are not 2-dimensional.

So what is `d` supposed to be here?

---

<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, 9:27pm UTC](https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863/31 "2021-04-08T21:27:33Z")

</div>

d is a variable that is described as the amount of material (s) at a point in time (n).

---

<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: [April 9, 2021, 4:52am UTC](https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863/32 "2021-04-09T04:52:22Z")

</div>

x-ref: [ArgumentError: invalid index: "s1" of type String](https://discourse.julialang.org/t/argumenterror-invalid-index-s1-of-type-string/58738)

@GraceMabele you need to step back and re-consider what you are trying to do.

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

```

is a JuMP objective. it doesn’t make sense to try and fit a surrogate model to this, regardless of which method you chose.

It seems you really want something like

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

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

function objective(d::Matrix{<:Real})
    return sum(price[s] * d[i, n] for n in N for (i, s) in enumerate(S))
end

objective(rand(4, 10)) # Objective given a random demand

```

and now your take is to approximate `objective` given a 4x10 input matrix.

Here’s something to get you started

```julia
function objective(d::NTuple)
    return objective(reshape(collect(d), length(S), length(N)))
end

lb = fill(0.0, 40)
ub = fill(1.0, 40)
x_train = sample(10, lb, ub, SobolSample())
y_train = objective.(x_train)

```

However, I would again caution you. If you can write down the JuMP objective, why are you trying to fit a surrogate?

---

<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 9, 2021, 5:29am UTC](https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863/33 "2021-04-09T05:29:40Z")

</div>

I suspected this but wasn’t sure whether JuMP objectives would work with surrogates or not.

While I think Oscar’s suggestion is spot on (basically decide whether you just want to solve a simple maximisation problem in JuMP or fit a neural surrogate to a non JuMP objective), I would also note that your current problem seems at a minimum to be lacking constraints, if I’m reading it correctly the solution will always be the highest possible value for d.

Also, again if I’m reading correctly, your profit is price times demand (presumably quantity demanded?), and seems to not consider costs? It is also a bit unusual to use demand as the choice variable here, when we would usually think of a firm facing a demand schedule and choosing prices to maximise profits. Now in general the firm’s optimisation problem can of course be formulated as an optimal quantity (supply) choice,but I would expect some link between that quantity choice and the equilibrium price?

---

<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 9, 2021, 5:44am UTC](https://discourse.julialang.org/t/multiple-dimensional-sampling-with-first-element-string/58863/34 "2021-04-09T05:44:41Z")

</div>

@odow

I see the assumption that this is the whole model.

The complete model is a MILP(of a batch chemical process). I have omitted the rest of the model. The model I am using is not formulated in that way.  
The values of d are dependent on the prementioned constraints.

Hence I cannot enumerate S in this case.

> [@nilshg](#):
>
> fit a neural surrogate to a non JuMP objective

This is more of what I am trying to do.

Unfortunately cannot provide full details of my work here since it a public platform.

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