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

Can not run test example in Julia #149

Closed
ChrisNabold opened this issue Aug 3, 2020 · 11 comments
Closed

Can not run test example in Julia #149

ChrisNabold opened this issue Aug 3, 2020 · 11 comments

Comments

@ChrisNabold
Copy link

I added NLopt to my Julia 1.5 installation. Pkg.add a well using finishing without error message. However the test examples dont run.
In both myfunc and myconstraint the ::Vector is not acceptedthe lines inequality.constraint are also not accepted by Julia.
My be you can help.
Thank you and best regards
Chris

@mzaffalon
Copy link
Contributor

Can you please post the code you tried? See also https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757

@stevengj
Copy link
Collaborator

stevengj commented Sep 3, 2020

I just tried it, and the tutorial code works fine for me in Julia 1.5 with a fresh install of NLopt.

@LaszloHars
Copy link

I also tried the tutorial code. Only 2 gradient-using algorithms work: LD_MMA, and LD_SLSQP.

The other 7 algorithms of the NLopt library (LD_LBFGS, LD_TNEWTON - all 4 variants, LD_VAR1, LD_VAR2) all give the error at the first call of inequality_constraint!(...) :
LoadError: ArgumentError: invalid NLopt arguments: invalid algorithm for constraints

The documentation does not tell, what the problem is, and how to get around it. Can anyone help?

@mzaffalon
Copy link
Contributor

@mx-the-gray
Copy link

Hello
I installed JUliaPro Version : 1.5.3-1 (https://juliacomputing.com/products/juliapro/#curated-pkgs) in Windows10, I then installed NLopt with the Pkg command. All good. Note that I can use most of the packages I tested so far. I then tried to run the example here below but it gives me the following error:

ERROR: MethodError: objects of type Opt are not callable
Stacktrace:
[1] value!!(::NonDifferentiable{Float64,Array{Float64,1}}, ::Array{Float64,1}) at C:\Users\MAX.julia\packages\NLSolversBase\QPnui\src\interface.jl:9
[2] initial_state(::NelderMead{Optim.AffineSimplexer,Optim.AdaptiveParameters}, ::Optim.Options{Float64,Nothing}, ::NonDifferentiable{Float64,Array{Float64,1}}, ::Array{Float64,1}) at C:\Users\MAX.julia\packages\Optim\auGGa\src\multivariate\solvers\zeroth_order\nelder_mead.jl:158
[3] optimize(::NonDifferentiable{Float64,Array{Float64,1}}, ::Array{Float64,1}, ::NelderMead{Optim.AffineSimplexer,Optim.AdaptiveParameters}, ::Optim.Options{Float64,Nothing}) at C:\Users\MAX.julia\packages\Optim\auGGa\src\multivariate\optimize\optimize.jl:33
[4] optimize(::Opt, ::Array{Float64,1}; inplace::Bool, autodiff::Symbol, kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at C:\Users\MAX.julia\packages\Optim\auGGa\src\multivariate\optimize\interface.jl:90
[5] optimize(::Opt, ::Array{Float64,1}) at C:\Users\MAX.julia\packages\Optim\auGGa\src\multivariate\optimize\interface.jl:84
[6] top-level scope at none:1

using NLopt

function myfunc(x::Vector, grad::Vector)
if length(grad) > 0
grad[1] = 0
grad[2] = 0.5/sqrt(x[2])
end
return sqrt(x[2])
end

function myconstraint(x::Vector, grad::Vector, a, b)
if length(grad) > 0
grad[1] = 3a * (ax[1] + b)^2
grad[2] = -1
end
(a
x[1] + b)^3 - x[2]
end

opt = Opt(:LD_MMA, 2)
opt.lower_bounds = [-Inf, 0.]
opt.xtol_rel = 1e-4

opt.min_objective = myfunc
inequality_constraint!(opt, (x,g) -> myconstraint(x,g,2,0), 1e-8)
inequality_constraint!(opt, (x,g) -> myconstraint(x,g,-1,1), 1e-8)

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

@mzaffalon
Copy link
Contributor

What happens if you execute the code right after starting Julia again? Stacktrace [5] mentions Optim, but NLopt does not depend on this package. Can it be that you you used Optim just before trying this example?

@mx-the-gray
Copy link

I did try a bunch of packages before this example, including Optim. Let me try to restart. Just to be clear. Does this mean that running NLopt is incompatible with using Optim?

@mx-the-gray
Copy link

No error now but it performs only one iteration and exits with
(2.382855429941145, [1.234, 5.678], :FORCED_STOP)

@LaszloHars
Copy link

The variable "ax" does not exist. If you mean multiplication, use "a*x"
It is an unfortunate implementation decision in the NLopt Julia package, that a runtime error in the constraint function causes :FORCED_STOP, instead of giving a decent error message. I always use a dummy call of the objective function and the constraints before the actual optimization, just to catch trivial coding errors.

@mx-the-gray
Copy link

Thanks, I just copied an pasted the example without checking. I am still learning. I started programming in Julia yesterday afternoon but it is really simple. I have already implemented a working DE.

@odow
Copy link
Member

odow commented Mar 3, 2022

Closing as non-reproducible:

julia> using NLopt

julia> function myfunc(x::Vector, grad::Vector)
       if length(grad) > 0
       grad[1] = 0
       grad[2] = 0.5/sqrt(x[2])
       end
       return sqrt(x[2])
       end
myfunc (generic function with 1 method)

julia> function myconstraint(x::Vector, grad::Vector, a, b)
       if length(grad) > 0
       grad[1] = 3a * (a * x[1] + b)^2
       grad[2] = -1
       end
       (a * x[1] + b)^3 - x[2]
       end
myconstraint (generic function with 1 method)

julia> opt = Opt(:LD_MMA, 2)
Opt(LD_MMA, 2)

julia> opt.lower_bounds = [-Inf, 0.]
2-element Vector{Float64}:
 -Inf
   0.0

julia> opt.xtol_rel = 1e-4
0.0001

julia> opt.min_objective = myfunc
myfunc (generic function with 1 method)

julia> inequality_constraint!(opt, (x,g) -> myconstraint(x,g,2,0), 1e-8)

julia> inequality_constraint!(opt, (x,g) -> myconstraint(x,g,-1,1), 1e-8)

julia> (minf,minx,ret) = optimize(opt, [1.234, 5.678])
(0.5443310477213124, [0.3333333342139688, 0.29629628951338166], :XTOL_REACHED)

The optimize(::Opt, ::Array{Float64,1}) at C:\Users\MAX.julia\packages\Optim\auGGa\src\multivariate\optimize\interface.jl:84 suggests the user was calling Optimization.optimize instead of NLopt.optimize.

If anyone has follow-up questions to this thread, please post on the community forum, https://discourse.julialang.org/c/domain/opt/13, and tag me @odow.

@odow odow closed this as completed Mar 3, 2022
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

6 participants