Error while using Optimization.jl

function errorfunction(data, p1)
** ddprob, ddprob_response = data**
** error = 0.0**
** for i in 1:length(ddprob)**
** error += sum(abs2, ddprob_response[i][:] - ddprob[i][:])**
** end**
** return error**
end

Above function is the error function from the systems in order to acheive synchronization. While using prob = OptimizationProblem(errorfunction, z0, p1) the output is “prob = OptimizationProblem. In-place: true
u0: 2-element Vector{Float64}:
0.0
0.0”

When I wanted to use solve(prob, OptimizationOptimJL.NelderMead()), I am gettin message as: MethodError: no method matching getindex(::Float64, ::Colon)

Closest candidates are:

getindex(::Number, !Matched::Integer)

@ Base number.jl:96

getindex(::Number)

@ Base number.jl:95

getindex(::Number, !Matched::Integer…)

@ Base number.jl:101

and in stack trace

  1. errorfunction(data::Vector{Float64}, p1::Vector{Float64}) @ [Other cell: line 6]
[
error = 0.0
for i in 1:length(ddprob)
    error += sum(abs2, ddprob_response[i][:] - ddprob[i][:])
end
return error
]

Few suggestions/ideas would be helpful to me. Thank you in advance

It’s hard to figure things out at the moment, can you post a complete example and try to format your code a little better?

Off the top of my head I would bet that ddprob is a matrix, and that you should be indexing it as ddprob[i, :] instead of ddprob[i][:] (which would work in Python). In Julia, whatever the shape of the array a, a[i] for integer i is the i-th element in the flattened array, hence a scalar. That’s why you cannot apply getindex(..., :) (“colon”) on it.

1 Like

Here, ddprob is a matrix and size(ddprob) = (3, 100001). In the program, I wanted to find the error between ddprob_response and ddprob, where both matrices are the same size. So here length would be 100001.

So the function I have defined is:
function errorfunction(ddprob, ddprob_response, p1)
error = 0.0
for i in 1:length(ddprob)
error += sum(abs2, ddprob_response[i][:] - ddprob[i][:])
end
return error
end

To the above function, I wanted to optimize, so the code used is:

prob = OptimizationProblem(errorfunction, z0, p1)

here z0 is the initial guess and p1 is the parameters. And the output to the above code is:
true
u0: 2-element Vector{Float64}:
0.0
0.0

After this I use to evaluate the code, which is:
solve(prob, OptimizationOptimJL.NelderMead())

but this time, error message is shown as follow:
MethodError: no method matching getindex(::Float64, ::Colon)

I hope, I explained it correctly, if not please suggest me. Thank you so much.

It is as I assumed: you are indeed trying to manipulate a matrix by first selecting the row and then performing operations. But the syntax to do that in Julia might be counter-intuitive if you come from Python.
Does the following example help you understand what is going on?

julia> M = rand(2, 3)
2×3 Matrix{Float64}:
 0.954341  0.924978  0.168994
 0.22158   0.888139  0.999073

julia> size(M)
(2, 3)

julia> length(M)
6

julia> M[1, 1] == M[1]
true

julia> M[2, 1] == M[2]
true

julia> M[1, 2] == M[3]
true

julia> M[1, :]
3-element Vector{Float64}:
 0.9543413417562148
 0.9249777531861214
 0.16899390662598546

julia> M[1]
0.9543413417562148

julia> M[6]
0.9990731572586894

julia> M[1][:]
ERROR: MethodError: no method matching getindex(::Float64, ::Colon)

Closest candidates are:
  getindex(::Number, ::Integer)
   @ Base number.jl:96
  getindex(::Number)
   @ Base number.jl:95
  getindex(::Number, ::Integer...)
   @ Base number.jl:101
  ...

Side note: you can format your code with triple backticks to make it easier to read for others, as mentioned in the post I linked.

2 Likes

The output of the code

ddprob = DataDrivenProblem(sol, use_interpolation = true)

is

Continuous DataDrivenProblem{Float64} ##DDProblem#1130 in 3 dimensions and 1000001 samples

similarly for ddprob_response

Okay, what is your question here?

1 Like

solve(prob, OptimizationOptimJL.NelderMead())

When I am trying to run the above code,

MethodError: no method matching errorfunction(::Vector{Float64})

Closest candidates are:

errorfunction(::Any, !Matched::Any)

In the Stack trace

I am getting the above message.

prob = OptimizationProblem(errorfunction, z0, p1, lb = [-1.0, -1.0], ub = [1.0, 1.0])

output:

prob
OptimizationProblem. In-place: true
u0: 2-element Vector{Float64}:
 0.0
 0.0

Thank you.

Can you please post one complete code file, surrounded by backticks, with all the necessary imports and definitions, so that we can reproduce the bug? And it seems you still haven’t corrected your array indexing problem

1 Like