11using OptimizationNLopt, Optimization, Zygote
2- using Test
2+ using Test, Random
33
44@testset " OptimizationNLopt.jl" begin
55 rosenbrock (x, p) = (p[1 ] - x[1 ])^ 2 + p[2 ] * (x[2 ] - x[1 ]^ 2 )^ 2
@@ -16,7 +16,7 @@ using Test
1616 optprob = OptimizationFunction (rosenbrock, Optimization. AutoZygote ())
1717 prob = OptimizationProblem (optprob, x0, _p)
1818
19- sol = solve (prob, NLopt. Opt (:LN_BOBYQA , 2 ))
19+ sol = solve (prob, NLopt. Opt (:LD_LBFGS , 2 ))
2020 @test sol. retcode == ReturnCode. Success
2121 @test 10 * sol. objective < l1
2222
@@ -26,10 +26,6 @@ using Test
2626 @test sol. retcode == ReturnCode. Success
2727 @test 10 * sol. objective < l1
2828
29- sol = solve (prob, NLopt. Opt (:LD_LBFGS , 2 ))
30- @test sol. retcode == ReturnCode. Success
31- @test 10 * sol. objective < l1
32-
3329 sol = solve (prob, NLopt. Opt (:G_MLSL_LDS , 2 ), local_method = NLopt. Opt (:LD_LBFGS , 2 ),
3430 maxiters = 10000 )
3531 @test sol. retcode == ReturnCode. MaxIters
@@ -82,4 +78,46 @@ using Test
8278 # nlopt gives the last best not the one where callback stops
8379 @test sol. objective < 0.8
8480 end
81+
82+ @testset " constrained" begin
83+ cons = (res, x, p) -> res .= [x[1 ]^ 2 + x[2 ]^ 2 - 1.0 ]
84+ x0 = zeros (2 )
85+ optprob = OptimizationFunction (rosenbrock, Optimization. AutoZygote ();
86+ cons = cons)
87+ prob = OptimizationProblem (optprob, x0, _p, lcons = [0.0 ], ucons = [0.0 ])
88+ sol = solve (prob, NLopt. LN_COBYLA ())
89+ @test sol. retcode == ReturnCode. Success
90+ @test 10 * sol. objective < l1
91+
92+ Random. seed! (1 )
93+ prob = OptimizationProblem (optprob, rand (2 ), _p,
94+ lcons = [0.0 ], ucons = [0.0 ])
95+
96+ sol = solve (prob, NLopt. LD_SLSQP ())
97+ @test sol. retcode == ReturnCode. Success
98+ @test 10 * sol. objective < l1
99+
100+ Random. seed! (1 )
101+ prob = OptimizationProblem (optprob, rand (2 ), _p,
102+ lcons = [0.0 ], ucons = [0.0 ])
103+ sol = solve (prob, NLopt. AUGLAG (), local_method = NLopt. LD_LBFGS ())
104+ @test sol. retcode == ReturnCode. Success
105+ @test 10 * sol. objective < l1
106+
107+ function con2_c (res, x, p)
108+ res .= [x[1 ]^ 2 + x[2 ]^ 2 - 1.0 , x[2 ] * sin (x[1 ]) - x[1 ] - 2.0 ]
109+ end
110+
111+ optprob = OptimizationFunction (rosenbrock, Optimization. AutoForwardDiff ();cons = con2_c)
112+ Random. seed! (1 )
113+ prob = OptimizationProblem (optprob, rand (2 ), _p, lcons = [0.0 , - Inf ], ucons = [0.0 , 0.0 ])
114+ sol = solve (prob, NLopt. LD_AUGLAG (), local_method = NLopt. LD_LBFGS ())
115+ @test sol. retcode == ReturnCode. Success
116+ @test 10 * sol. objective < l1
117+
118+ prob = OptimizationProblem (optprob, rand (2 ), _p, lcons = [- Inf , - Inf ], ucons = [0.0 , 0.0 ], lb = [- 1.0 , - 1.0 ], ub = [1.0 , 1.0 ])
119+ sol = solve (prob, NLopt. GN_ISRES (), maxiters = 1000 )
120+ @test sol. retcode == ReturnCode. MaxIters
121+ @test 10 * sol. objective < l1
122+ end
85123end
0 commit comments