Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ctrl-C exception in REPL #68

Closed
ilicog opened this issue Dec 2, 2016 · 7 comments
Closed

Ctrl-C exception in REPL #68

ilicog opened this issue Dec 2, 2016 · 7 comments

Comments

@ilicog
Copy link

ilicog commented Dec 2, 2016

Is there a snippet of code / example of throwing a CTRL-C exception that would gracefully halt an NLopt optimization in julia REPL?

@stevengj
Copy link
Collaborator

stevengj commented Dec 2, 2016

throw(InterruptException())?

@ilicog
Copy link
Author

ilicog commented Dec 2, 2016

somewhat confused, so I was ideally looking for a MWE (of something that works in REPL/terminal/for multiple processes)

@stevengj
Copy link
Collaborator

stevengj commented Dec 2, 2016

Just throw the exception from your objective function.

@ilicog
Copy link
Author

ilicog commented Dec 3, 2016

In this dummy example (that will just run for 10 sec)

using NLopt

function myfunc(x::Vector, grad::Vector)
    global count
    count::Int += 1
    
    res = sqrt(x[2])

    return res
end

function run_opt()
    opt = Opt(:GN_ISRES, 2)
    upper_bounds!(opt, [100.0, 100.0])
    lower_bounds!(opt, [0.0, 0.0])
    min_objective!(opt, myfunc)
    maxtime!(opt, 10.0)

    (minf,minx,ret) = optimize(opt, [1.234, 5.678])
    return (minf,minx,ret)
end

would throw(InterruptException()) go inside myfunc, with a forced_stop!(opt) in run_opt() and if so how exactly? Tried several things, can't seem to get it right.

@mzaffalon
Copy link
Contributor

using NLopt

function myfunc(x::Vector, grad::Vector)
    throw(InterruptException())
    0.0
end

opt = Opt(:GN_ISRES, 2)
min_objective!(opt, myfunc)
maxtime!(opt, 10.0)

When I run it, I get

julia> optimize(opt, [1.234, 5.678])
in callback catch
ERROR: InterruptException:
Stacktrace:
 [1] myfunc(::Array{Float64,1}, ::Array{Float64,1}) at ./REPL[2]:2
 [2] nlopt_callback_wrapper(::UInt32, ::Ptr{Float64}, ::Ptr{Float64}, ::Ptr{Void}) at /home/michele/.julia/v0.6/NLopt/src/NLopt.jl:427
 [3] optimize!(::NLopt.Opt, ::Array{Float64,1}) at /home/michele/.julia/v0.6/NLopt/src/NLopt.jl:526
 [4] optimize(::NLopt.Opt, ::Array{Float64,1}) at /home/michele/.julia/v0.6/NLopt/src/NLopt.jl:532

@avonmoll
Copy link

I read the README and also found this thread, but I never seem to get exceptions back when I call optimize. The snippet in the previous post just causes the optimizer to return :FORCED_STOP. So, for real code, the optimizer will run for a while and then hit a case where there is an exception and immediately returns :FORCED_STOP and I am left with no information on what went wrong.

@odow
Copy link
Member

odow commented Jan 25, 2023

Closing because this seems to work fine.

julia> count = 0
0

julia> using NLopt

julia> function myfunc(x::Vector, grad::Vector)
           global count
           count::Int += 1
           
           res = sqrt(x[2])

           return res
       end
myfunc (generic function with 1 method)

julia> function run_opt()
           opt = Opt(:GN_ISRES, 2)
           upper_bounds!(opt, [100.0, 100.0])
           lower_bounds!(opt, [0.0, 0.0])
           min_objective!(opt, myfunc)
           maxtime!(opt, 10.0)

           (minf,minx,ret) = optimize(opt, [1.234, 5.678])
           return (minf,minx,ret)
       end
run_opt (generic function with 1 method)

julia> run_opt()
^C(0.0, [6.676949509795536, 0.0], :FORCED_STOP)

The forced stop issue is covered by #156.

@odow odow closed this as completed Jan 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

5 participants