# ArgumentError: invalid index: "s1" of type String

**URL:** https://discourse.julialang.org/t/argumenterror-invalid-index-s1-of-type-string/58738
**Category:** Optimization (Mathematical)
**Tags:** question, jump, optimization
**Created:** [April 7, 2021, 8:40am UTC](https://discourse.julialang.org/t/argumenterror-invalid-index-s1-of-type-string/58738 "2021-04-07T08:40:33Z")
**Posts on this page:** 12
**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 7, 2021, 8:40am UTC](https://discourse.julialang.org/t/argumenterror-invalid-index-s1-of-type-string/58738/1 "2021-04-07T08:40:33Z")

</div>

Hi,

Here is a MWE of my problem that is returning the same error. I cannot seem to understand why.

```julia
using DataStructures
using JuMP
using TaylorSeries

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)= sum((price[s]*d[s,n]) for n in N for s in keys(price))

∫⬩dn(N::Taylor1) = integrate(p)

function Taylor(f, p0)
    
    p = copy(p0)
    pnew = p0 + ∫⬩dn(f(N))
    
    while (pnew - p) != 0
        p = pnew
        pnew = p0 + ∫⬩dn(f(N))
    end
    
    return p
        
end

degree = 3

p0 = Taylor1([1], degree)

SurrogateApproximation = Taylor(g, p0)

```

When the equation is on its own, it does not return that error but returns it when called into the function `Taylor`

```julia
sum((price[s]*d[s,n]) for n in N for s in keys(price))

```

Here is the full details of the error

ArgumentError: invalid index: “s1” of type String

Stacktrace:  
[1] to\_index(::String) at .\indices.jl:297  
[2] to\_index(::Array{UnitRange{Int64},1}, ::String) at .\indices.jl:274  
[3] to\_indices at .\indices.jl:325 [inlined]  
[4] to\_indices at .\indices.jl:321 [inlined]  
[5] getindex at .\abstractarray.jl:1060 [inlined]  
[6] (::var"#393#396"{UnitRange{Int64},Array{UnitRange{Int64},1}})(::String) at .\none:0  
[7] MappingRF at .\reduce.jl:93 [inlined]  
[8] \_foldl\_impl(::Base.MappingRF{var"#393#396"{UnitRange{Int64},Array{UnitRange{Int64},1}},Base.BottomRF{typeof(Base.add\_sum)}}, ::Base.\_InitialValue, ::Base.KeySet{String,OrderedDict{String,Int64}}) at .\reduce.jl:58  
[9] FlatteningRF at .\reduce.jl:119 [inlined]  
[10] MappingRF at .\reduce.jl:93 [inlined]  
[11] \_foldl\_impl(::Base.MappingRF{var"#394#395"{Array{UnitRange{Int64},1}},Base.FlatteningRF{Base.BottomRF{typeof(Base.add\_sum)}}}, ::Base.\_InitialValue, ::Array{UnitRange{Int64},1}) at .\reduce.jl:58  
[12] foldl\_impl at .\reduce.jl:48 [inlined]  
[13] mapfoldl\_impl at .\reduce.jl:44 [inlined]  
[14] #mapfoldl#204 at .\reduce.jl:160 [inlined]  
[15] mapfoldl at .\reduce.jl:160 [inlined]  
[16] #mapreduce#208 at .\reduce.jl:287 [inlined]  
[17] mapreduce at .\reduce.jl:287 [inlined]  
[18] sum at .\reduce.jl:494 [inlined]  
[19] sum(::Base.Iterators.Flatten{Base.Generator{Array{UnitRange{Int64},1},var"#394#395"{Array{UnitRange{Int64},1}}}}) at .\reduce.jl:511  
[20] h(::Array{UnitRange{Int64},1}) at .\In[29]:1  
[21] Taylor(::typeof(h), ::Taylor1{Int64}) at .\In[21]:4  
[22] top-level scope at In[31]:5  
[23] include\_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091

---

<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 7, 2021, 2:13pm UTC](https://discourse.julialang.org/t/argumenterror-invalid-index-s1-of-type-string/58738/2 "2021-04-07T14:13:05Z")

</div>

Could this be because the variable d has indices `[s,n]`?

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [April 7, 2021, 2:33pm UTC](https://discourse.julialang.org/t/argumenterror-invalid-index-s1-of-type-string/58738/3 "2021-04-07T14:33:15Z")

</div>

Not sure if this is related to your problem, I did not test it, but I am almost sure you want `N = 1:10` and not `N = [1:10]`, can you test with this change?

---

<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 7, 2021, 2:56pm UTC](https://discourse.julialang.org/t/argumenterror-invalid-index-s1-of-type-string/58738/4 "2021-04-07T14:56:50Z")

</div>

It returns the exact same error.

---

<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 7, 2021, 8:14pm UTC](https://discourse.julialang.org/t/argumenterror-invalid-index-s1-of-type-string/58738/5 "2021-04-07T20:14:34Z")

</div>

It’s worth stepping back and asking “what are you trying to do”?

Your `g` function takes an argument `d` indexed over `s` and `n`. But you pass `g` to `Taylor` with a `p0` that is some sort of 3-degree expansion, and isn’t indexed over `s` and `n`.

---

<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 7, 2021, 8:28pm UTC](https://discourse.julialang.org/t/argumenterror-invalid-index-s1-of-type-string/58738/6 "2021-04-07T20:28:00Z")

</div>

Thank you for your note.

I am trying to build a taylor expansion of g. Is there a problem with my formulation considering what I am trying to do?

---

<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 7, 2021, 8:43pm UTC](https://discourse.julialang.org/t/argumenterror-invalid-index-s1-of-type-string/58738/7 "2021-04-07T20:43:40Z")

</div>

> I am trying to build a taylor expansion of g.

To rephrase, why are you trying to do this? What is the bigger goal?

> Is there a problem with my formulation considering what I am trying to do?

Yes. `d` should be a 2-dimensional `DenseAxisArray`, indexed by `String`s and `Int`s with 40 elements. But you are trying to do a univariate Taylor series expansion.

---

<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 7, 2021, 8:57pm UTC](https://discourse.julialang.org/t/argumenterror-invalid-index-s1-of-type-string/58738/8 "2021-04-07T20:57:23Z")

</div>

> [@odow](#):
>
> To rephrase, why are you trying to do this? What is the bigger goal?

The taylor expansion will act as a surrogate/approximation function that I will use in further steps.

---

<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 7, 2021, 9:01pm UTC](https://discourse.julialang.org/t/argumenterror-invalid-index-s1-of-type-string/58738/9 "2021-04-07T21:01:47Z")

</div>

> [@odow](#):
>
> Yes. `d` should be a 2-dimensional `DenseAxisArray` , indexed by `String` s and `Int` s with 40 elements. But you are trying to do a univariate Taylor series expansion.

Hmmm… I think I tried to used TalyorN for multivariate but it returned a similar error or rather my formulation is incorrect.

---

<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 7, 2021, 9:29pm UTC](https://discourse.julialang.org/t/argumenterror-invalid-index-s1-of-type-string/58738/10 "2021-04-07T21:29:50Z")

</div>

> The taylor expansion will act as a surrogate/approximation function that I will use in further steps.

Yes. But why a surrogate, and why Taylor series?

- Your function `g` is linear, you don’t need to approximate. Have you tried just passing the full problem to JuMP?
- Why is JuMP in your MWE? It isn’t used.
- Is there a bug in your `Taylor` function? The `while` loop always exits after the first iteration, because `pnew` never changes.
- TaylorSeries is for low-dimensional problems. Think one, or several. Your `d` matrix has 40 elements. Are you trying to fit a surrogate function to a 40-dimensional surface? You may need to reconsider your approach.

---

<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, 4:39pm UTC](https://discourse.julialang.org/t/argumenterror-invalid-index-s1-of-type-string/58738/11 "2021-04-08T16:39:45Z")

</div>

> [@odow](#):
>
> Your function `g` is linear, you don’t need to approximate. Have you tried just passing the full problem to JuMP?

yes I have and it initially gave me problems. because it was a JuMP @objective which gives a GenericAffExpr. I will try it again this way

---

<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, 4:41pm UTC](https://discourse.julialang.org/t/argumenterror-invalid-index-s1-of-type-string/58738/12 "2021-04-08T16:41:16Z")

</div>

> [@odow](#):
>
> Why is JuMP in your MWE? It isn’t used.

The variable d is a one of the variables in the full model. Hence the use of JuMP
