NLopt: Finding errors in objective function

You can do something like

function _f(x,y)
    try f(x,y)
    catch e
    	bt = catch_backtrace()
    	showerror(stdout, e, bt)
    	rethrow(e)
    end
end
julia> fVal, soln, exitFlag = NLopt.optimize(optS, [1.0, 1.0])
Input  [1.0, 1.0]
Returning  2.0
Input  [2.0, 1.0]
Returning  5.0
Input  [1.0, 2.0]
Returning  5.0
Input  [2.0, 0.0]
Returning  4.0
Input  [1.0, 0.0]
Returning  1.0
Input  [0.5, -0.5]
Error branch
This is not reported
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] f(::Array{Float64,1}, ::Array{Float64,1}) at ./REPL[3]:9
 [3] _f(::Array{Float64,1}, ::Array{Float64,1}) at ./REPL[8]:2
 [4] nlopt_callback_wrapper(::UInt32, ::Ptr{Float64}, ::Ptr{Float64}, ::Ptr{Nothing}) at /home/kc/.julia/packages/NLopt/eqN9a/src/NLopt.jl:413
 [5] optimize! at /home/kc/.julia/packages/NLopt/eqN9a/src/NLopt.jl:604 [inlined]
 [6] optimize(::Opt, ::Array{Float64,1}) at /home/kc/.julia/packages/NLopt/eqN9a/src/NLopt.jl:611
 [7] top-level scope at REPL[19]:1
 [8] eval(::Module, ::Any) at ./boot.jl:331
 [9] eval_user_input(::Any, ::REPL.REPLBackend) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.4/REPL/src/REPL.jl:86
 [10] macro expansion at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.4/REPL/src/REPL.jl:118 [inlined]
 [11] (::REPL.var"#26#27"{REPL.REPLBackend})() at ./task.jl:358(1.0, [1.0, 0.0], :FORCED_STOP)
3 Likes