diff --git a/src/NLopt.jl b/src/NLopt.jl index 2ccfa81..47f3a9f 100644 --- a/src/NLopt.jl +++ b/src/NLopt.jl @@ -8,56 +8,117 @@ module NLopt using CEnum: @cenum using NLopt_jll: libnlopt +# NLopt@2.9.0 removed the LD_LBFGS_NOCEDAL enum. +# See https://github.com/stevengj/nlopt/issues/584 for details. +function _is_version_newer_than_2_9() + major, minor, bugfix = Ref{Cint}(), Ref{Cint}(), Ref{Cint}() + @ccall libnlopt.nlopt_version( + major::Ptr{Cint}, + minor::Ptr{Cint}, + bugfix::Ptr{Cint}, + )::Cvoid + return (major[] > 2) || (major[] == 2 && minor[] >= 9) +end + include("libnlopt.jl") ############################################################################ # Mirrors of NLopt's C enum constants: -@enum Algorithm::Cint begin - GN_DIRECT = 0 - GN_DIRECT_L = 1 - GN_DIRECT_L_RAND = 2 - GN_DIRECT_NOSCAL = 3 - GN_DIRECT_L_NOSCAL = 4 - GN_DIRECT_L_RAND_NOSCAL = 5 - GN_ORIG_DIRECT = 6 - GN_ORIG_DIRECT_L = 7 - GD_STOGO = 8 - GD_STOGO_RAND = 9 - LD_LBFGS_NOCEDAL = 10 - LD_LBFGS = 11 - LN_PRAXIS = 12 - LD_VAR1 = 13 - LD_VAR2 = 14 - LD_TNEWTON = 15 - LD_TNEWTON_RESTART = 16 - LD_TNEWTON_PRECOND = 17 - LD_TNEWTON_PRECOND_RESTART = 18 - GN_CRS2_LM = 19 - GN_MLSL = 20 - GD_MLSL = 21 - GN_MLSL_LDS = 22 - GD_MLSL_LDS = 23 - LD_MMA = 24 - LN_COBYLA = 25 - LN_NEWUOA = 26 - LN_NEWUOA_BOUND = 27 - LN_NELDERMEAD = 28 - LN_SBPLX = 29 - LN_AUGLAG = 30 - LD_AUGLAG = 31 - LN_AUGLAG_EQ = 32 - LD_AUGLAG_EQ = 33 - LN_BOBYQA = 34 - GN_ISRES = 35 - AUGLAG = 36 - AUGLAG_EQ = 37 - G_MLSL = 38 - G_MLSL_LDS = 39 - LD_SLSQP = 40 - LD_CCSAQ = 41 - GN_ESCH = 42 - GN_AGS = 43 +@static if _is_version_newer_than_2_9() + @enum Algorithm::Cint begin + GN_DIRECT = 0 + GN_DIRECT_L + GN_DIRECT_L_RAND + GN_DIRECT_NOSCAL + GN_DIRECT_L_NOSCAL + GN_DIRECT_L_RAND_NOSCAL + GN_ORIG_DIRECT + GN_ORIG_DIRECT_L + GD_STOGO + GD_STOGO_RAND + # LD_LBFGS_NOCEDAL + LD_LBFGS + LN_PRAXIS + LD_VAR1 + LD_VAR2 + LD_TNEWTON + LD_TNEWTON_RESTART + LD_TNEWTON_PRECOND + LD_TNEWTON_PRECOND_RESTART + GN_CRS2_LM + GN_MLSL + GD_MLSL + GN_MLSL_LDS + GD_MLSL_LDS + LD_MMA + LN_COBYLA + LN_NEWUOA + LN_NEWUOA_BOUND + LN_NELDERMEAD + LN_SBPLX + LN_AUGLAG + LD_AUGLAG + LN_AUGLAG_EQ + LD_AUGLAG_EQ + LN_BOBYQA + GN_ISRES + AUGLAG + AUGLAG_EQ + G_MLSL + G_MLSL_LDS + LD_SLSQP + LD_CCSAQ + GN_ESCH + GN_AGS + end +else + @enum Algorithm::Cint begin + GN_DIRECT = 0 + GN_DIRECT_L + GN_DIRECT_L_RAND + GN_DIRECT_NOSCAL + GN_DIRECT_L_NOSCAL + GN_DIRECT_L_RAND_NOSCAL + GN_ORIG_DIRECT + GN_ORIG_DIRECT_L + GD_STOGO + GD_STOGO_RAND + LD_LBFGS_NOCEDAL + LD_LBFGS + LN_PRAXIS + LD_VAR1 + LD_VAR2 + LD_TNEWTON + LD_TNEWTON_RESTART + LD_TNEWTON_PRECOND + LD_TNEWTON_PRECOND_RESTART + GN_CRS2_LM + GN_MLSL + GD_MLSL + GN_MLSL_LDS + GD_MLSL_LDS + LD_MMA + LN_COBYLA + LN_NEWUOA + LN_NEWUOA_BOUND + LN_NELDERMEAD + LN_SBPLX + LN_AUGLAG + LD_AUGLAG + LN_AUGLAG_EQ + LD_AUGLAG_EQ + LN_BOBYQA + GN_ISRES + AUGLAG + AUGLAG_EQ + G_MLSL + G_MLSL_LDS + LD_SLSQP + LD_CCSAQ + GN_ESCH + GN_AGS + end end Base.convert(::Type{nlopt_algorithm}, a::Algorithm) = nlopt_algorithm(Int(a)) diff --git a/src/libnlopt.jl b/src/libnlopt.jl index 5a52da7..4cb9609 100644 --- a/src/libnlopt.jl +++ b/src/libnlopt.jl @@ -16,52 +16,102 @@ const nlopt_mfunc = Ptr{Cvoid} # typedef void ( * nlopt_precond ) ( unsigned n , const double * x , const double * v , double * vpre , void * data ) const nlopt_precond = Ptr{Cvoid} -@cenum nlopt_algorithm::UInt32 begin - NLOPT_GN_DIRECT = 0 - NLOPT_GN_DIRECT_L = 1 - NLOPT_GN_DIRECT_L_RAND = 2 - NLOPT_GN_DIRECT_NOSCAL = 3 - NLOPT_GN_DIRECT_L_NOSCAL = 4 - NLOPT_GN_DIRECT_L_RAND_NOSCAL = 5 - NLOPT_GN_ORIG_DIRECT = 6 - NLOPT_GN_ORIG_DIRECT_L = 7 - NLOPT_GD_STOGO = 8 - NLOPT_GD_STOGO_RAND = 9 - NLOPT_LD_LBFGS_NOCEDAL = 10 - NLOPT_LD_LBFGS = 11 - NLOPT_LN_PRAXIS = 12 - NLOPT_LD_VAR1 = 13 - NLOPT_LD_VAR2 = 14 - NLOPT_LD_TNEWTON = 15 - NLOPT_LD_TNEWTON_RESTART = 16 - NLOPT_LD_TNEWTON_PRECOND = 17 - NLOPT_LD_TNEWTON_PRECOND_RESTART = 18 - NLOPT_GN_CRS2_LM = 19 - NLOPT_GN_MLSL = 20 - NLOPT_GD_MLSL = 21 - NLOPT_GN_MLSL_LDS = 22 - NLOPT_GD_MLSL_LDS = 23 - NLOPT_LD_MMA = 24 - NLOPT_LN_COBYLA = 25 - NLOPT_LN_NEWUOA = 26 - NLOPT_LN_NEWUOA_BOUND = 27 - NLOPT_LN_NELDERMEAD = 28 - NLOPT_LN_SBPLX = 29 - NLOPT_LN_AUGLAG = 30 - NLOPT_LD_AUGLAG = 31 - NLOPT_LN_AUGLAG_EQ = 32 - NLOPT_LD_AUGLAG_EQ = 33 - NLOPT_LN_BOBYQA = 34 - NLOPT_GN_ISRES = 35 - NLOPT_AUGLAG = 36 - NLOPT_AUGLAG_EQ = 37 - NLOPT_G_MLSL = 38 - NLOPT_G_MLSL_LDS = 39 - NLOPT_LD_SLSQP = 40 - NLOPT_LD_CCSAQ = 41 - NLOPT_GN_ESCH = 42 - NLOPT_GN_AGS = 43 - NLOPT_NUM_ALGORITHMS = 44 +@static if _is_version_newer_than_2_9() + @cenum nlopt_algorithm::UInt32 begin + NLOPT_GN_DIRECT = 0 + NLOPT_GN_DIRECT_L + NLOPT_GN_DIRECT_L_RAND + NLOPT_GN_DIRECT_NOSCAL + NLOPT_GN_DIRECT_L_NOSCAL + NLOPT_GN_DIRECT_L_RAND_NOSCAL + NLOPT_GN_ORIG_DIRECT + NLOPT_GN_ORIG_DIRECT_L + NLOPT_GD_STOGO + NLOPT_GD_STOGO_RAND + # NLOPT_LD_LBFGS_NOCEDAL = 10 + NLOPT_LD_LBFGS + NLOPT_LN_PRAXIS + NLOPT_LD_VAR1 + NLOPT_LD_VAR2 + NLOPT_LD_TNEWTON + NLOPT_LD_TNEWTON_RESTART + NLOPT_LD_TNEWTON_PRECOND + NLOPT_LD_TNEWTON_PRECOND_RESTART + NLOPT_GN_CRS2_LM + NLOPT_GN_MLSL + NLOPT_GD_MLSL + NLOPT_GN_MLSL_LDS + NLOPT_GD_MLSL_LDS + NLOPT_LD_MMA + NLOPT_LN_COBYLA + NLOPT_LN_NEWUOA + NLOPT_LN_NEWUOA_BOUND + NLOPT_LN_NELDERMEAD + NLOPT_LN_SBPLX + NLOPT_LN_AUGLAG + NLOPT_LD_AUGLAG + NLOPT_LN_AUGLAG_EQ + NLOPT_LD_AUGLAG_EQ + NLOPT_LN_BOBYQA + NLOPT_GN_ISRES + NLOPT_AUGLAG + NLOPT_AUGLAG_EQ + NLOPT_G_MLSL + NLOPT_G_MLSL_LDS + NLOPT_LD_SLSQP + NLOPT_LD_CCSAQ + NLOPT_GN_ESCH + NLOPT_GN_AGS + NLOPT_NUM_ALGORITHMS + end +else + @cenum nlopt_algorithm::UInt32 begin + NLOPT_GN_DIRECT = 0 + NLOPT_GN_DIRECT_L + NLOPT_GN_DIRECT_L_RAND + NLOPT_GN_DIRECT_NOSCAL + NLOPT_GN_DIRECT_L_NOSCAL + NLOPT_GN_DIRECT_L_RAND_NOSCAL + NLOPT_GN_ORIG_DIRECT + NLOPT_GN_ORIG_DIRECT_L + NLOPT_GD_STOGO + NLOPT_GD_STOGO_RAND + NLOPT_LD_LBFGS_NOCEDAL # This one exists + NLOPT_LD_LBFGS + NLOPT_LN_PRAXIS + NLOPT_LD_VAR1 + NLOPT_LD_VAR2 + NLOPT_LD_TNEWTON + NLOPT_LD_TNEWTON_RESTART + NLOPT_LD_TNEWTON_PRECOND + NLOPT_LD_TNEWTON_PRECOND_RESTART + NLOPT_GN_CRS2_LM + NLOPT_GN_MLSL + NLOPT_GD_MLSL + NLOPT_GN_MLSL_LDS + NLOPT_GD_MLSL_LDS + NLOPT_LD_MMA + NLOPT_LN_COBYLA + NLOPT_LN_NEWUOA + NLOPT_LN_NEWUOA_BOUND + NLOPT_LN_NELDERMEAD + NLOPT_LN_SBPLX + NLOPT_LN_AUGLAG + NLOPT_LD_AUGLAG + NLOPT_LN_AUGLAG_EQ + NLOPT_LD_AUGLAG_EQ + NLOPT_LN_BOBYQA + NLOPT_GN_ISRES + NLOPT_AUGLAG + NLOPT_AUGLAG_EQ + NLOPT_G_MLSL + NLOPT_G_MLSL_LDS + NLOPT_LD_SLSQP + NLOPT_LD_CCSAQ + NLOPT_GN_ESCH + NLOPT_GN_AGS + NLOPT_NUM_ALGORITHMS + end end function nlopt_algorithm_name(a) diff --git a/test/C_API.jl b/test/C_API.jl index 89cbd11..aeae14c 100644 --- a/test/C_API.jl +++ b/test/C_API.jl @@ -567,7 +567,11 @@ function test_algorithm_name() sol = "Limited-memory BFGS (L-BFGS) (local, derivative-based)" @test algorithm_name(algorithm) == sol @test algorithm_name(:LD_LBFGS) == sol - @test algorithm_name(11) == sol + if NLopt._is_version_newer_than_2_9() + @test algorithm_name(10) == sol + else + @test algorithm_name(11) == sol + end opt = Opt(:LD_LBFGS, 2) @test algorithm_name(opt) == sol sprint(show, algorithm_name(:LD_LBFGS))