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

Hi,

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

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

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

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

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?

It returns the exact same error.

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

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

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.

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

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

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.
1 Like

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

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