Optim: MethodError: no method matching zero

When I do optimization calculations with the Optim package, an error occurred. The type of A1,A2,A3,A4 is ::Array{Complex{Float64},2}. I don’t know how to solve it.
Is there a way to make optimize() support this type?Or what data type can replace this type and serve as optimize()?

Please don’t post screenshots, and include an MWE.

If anyone else finds this, I encountered this issue when u0 was (erroneously) set to Vector{Vector}. If someone finds the time, it would be great to implement a more descriptive error message, if possible.

Can you be a little more specific?

MWE:

using Optimization, OptimizationOptimJL

my_func(x, p) = x[1]^2 + x[2]^2
my_grad!(G, x, p) = begin
    G[1] = 2*x[1]
    G[2] = 2*x[2]
end
of = OptimizationFunction(my_func; grad=my_grad!)
op = OptimizationProblem(of, [[1.], [1.]], ())
solve(op, LBFGS())

yields

ERROR: MethodError: no method matching zero(::Type{Vector{Float64}})

Closest candidates are:
  zero(::Type{Union{}}, Any...)
   @ Base number.jl:310
  zero(::Type{Dates.Time})
   @ Dates /nix/store/psyzpzlbdyv09mbww56c4i1cd7yccygi-julia-bin-1.10.2/share/julia/stdlib/v1.10/Dates/src/types.jl:440
  zero(::Type{LibGit2.GitHash})
   @ LibGit2 /nix/store/psyzpzlbdyv09mbww56c4i1cd7yccygi-julia-bin-1.10.2/share/julia/stdlib/v1.10/LibGit2/src/oid.jl:221
  ...

Stacktrace:
 [1] __solve(cache::OptimizationCache{…})
   @ OptimizationOptimJL ~/.julia/packages/OptimizationOptimJL/fdBis/src/OptimizationOptimJL.jl:196
 [2] solve!
   @ ~/.julia/packages/SciMLBase/QEvkv/src/solve.jl:188 [inlined]
 [3] #solve#627
   @ ~/.julia/packages/SciMLBase/QEvkv/src/solve.jl:96 [inlined]
 [4] solve(::OptimizationProblem{…}, ::LBFGS{…})
   @ SciMLBase ~/.julia/packages/SciMLBase/QEvkv/src/solve.jl:93
 [5] top-level scope
   @ REPL[13]:1
Some type information was truncated. Use `show(err)` to see complete types.

The fix is

op = OptimizationProblem(of, [1., 1.], ())

because the given u0 was of wrong type.

1 Like