11using GalacticOptim, GalacticOptimJL, GalacticFlux, Test
22using ForwardDiff, Zygote, ReverseDiff, FiniteDiff, Tracker
3-
3+ using ModelingToolkit
44x0 = zeros (2 )
5- rosenbrock (x, p= nothing ) = (1 - x[1 ])^ 2 + 100 * (x[2 ] - x[1 ]^ 2 )^ 2
5+ rosenbrock (x, p= nothing ) = (1 - x[1 ])^ 2 + 100 * (x[2 ] - x[1 ]^ 2 )^ 2
66l1 = rosenbrock (x0)
77
88function g! (G, x)
@@ -17,16 +17,48 @@ function h!(H, x)
1717 H[2 , 2 ] = 200.0
1818end
1919
20- G1 = Array {Float64} (undef,2 )
21- G2 = Array {Float64} (undef,2 )
20+ G1 = Array {Float64} (undef, 2 )
21+ G2 = Array {Float64} (undef, 2 )
2222H1 = Array {Float64} (undef, 2 , 2 )
2323H2 = Array {Float64} (undef, 2 , 2 )
2424
2525g! (G1, x0)
2626h! (H1, x0)
2727
28+ cons = (x, p) -> [x[1 ]^ 2 + x[2 ]^ 2 ]
29+ optf = OptimizationFunction (rosenbrock, GalacticOptim. AutoModelingToolkit (), cons= cons)
30+ optprob = GalacticOptim. instantiate_function (optf, x0, GalacticOptim. AutoModelingToolkit (), nothing , 1 )
31+ optprob. grad (G2, x0)
32+ @test G1 == G2
33+ optprob. hess (H2, x0)
34+ @test H1 == H2
35+ @test optprob. cons (x0) == [0.0 ]
36+ J = Array {Float64} (undef, 2 )
37+ optprob. cons_j (J, [5.0 , 3.0 ])
38+ @test J == [10.0 , 6.0 ]
39+ H3 = [Array {Float64} (undef, 2 , 2 )]
40+ optprob. cons_h (H3, x0)
41+ @test H3 == [[2.0 0.0 ; 0.0 2.0 ]]
42+
43+ function con2_c (x, p)
44+ [x[1 ]^ 2 + x[2 ]^ 2 , x[2 ] * sin (x[1 ]) - x[1 ]]
45+ end
46+ optf = OptimizationFunction (rosenbrock, GalacticOptim. AutoModelingToolkit (), cons= con2_c)
47+ optprob = GalacticOptim. instantiate_function (optf, x0, GalacticOptim. AutoModelingToolkit (), nothing , 2 )
48+ optprob. grad (G2, x0)
49+ @test G1 == G2
50+ optprob. hess (H2, x0)
51+ @test H1 == H2
52+ @test optprob. cons (x0) == [0.0 , 0.0 ]
53+ J = Array {Float64} (undef, 2 , 2 )
54+ optprob. cons_j (J, [5.0 , 3.0 ])
55+ @test all (isapprox (J, [10.0 6.0 ; - 0.149013 - 0.958924 ]; rtol= 1e-3 ))
56+ H3 = [Array {Float64} (undef, 2 , 2 ), Array {Float64} (undef, 2 , 2 )]
57+ optprob. cons_h (H3, x0)
58+ @test H3 == [[2.0 0.0 ; 0.0 2.0 ], [- 0.0 1.0 ; 1.0 0.0 ]]
59+
2860optf = OptimizationFunction (rosenbrock, GalacticOptim. AutoForwardDiff ())
29- optprob = GalacticOptim. instantiate_function (optf,x0,GalacticOptim. AutoForwardDiff (),nothing )
61+ optprob = GalacticOptim. instantiate_function (optf, x0, GalacticOptim. AutoForwardDiff (), nothing )
3062optprob. grad (G2, x0)
3163@test G1 == G2
3264optprob. hess (H2, x0)
@@ -35,16 +67,16 @@ optprob.hess(H2, x0)
3567prob = OptimizationProblem (optprob, x0)
3668
3769sol = solve (prob, Optim. BFGS ())
38- @test 10 * sol. minimum < l1
70+ @test 10 * sol. minimum < l1
3971
4072sol = solve (prob, Optim. Newton ())
41- @test 10 * sol. minimum < l1
73+ @test 10 * sol. minimum < l1
4274
4375sol = solve (prob, Optim. KrylovTrustRegion ())
44- @test 10 * sol. minimum < l1
76+ @test 10 * sol. minimum < l1
4577
4678optf = OptimizationFunction (rosenbrock, GalacticOptim. AutoZygote ())
47- optprob = GalacticOptim. instantiate_function (optf,x0,GalacticOptim. AutoZygote (),nothing )
79+ optprob = GalacticOptim. instantiate_function (optf, x0, GalacticOptim. AutoZygote (), nothing )
4880optprob. grad (G2, x0)
4981@test G1 == G2
5082optprob. hess (H2, x0)
@@ -53,33 +85,33 @@ optprob.hess(H2, x0)
5385prob = OptimizationProblem (optprob, x0)
5486
5587sol = solve (prob, Optim. BFGS ())
56- @test 10 * sol. minimum < l1
88+ @test 10 * sol. minimum < l1
5789
5890sol = solve (prob, Optim. Newton ())
59- @test 10 * sol. minimum < l1
91+ @test 10 * sol. minimum < l1
6092
6193sol = solve (prob, Optim. KrylovTrustRegion ())
62- @test 10 * sol. minimum < l1
94+ @test 10 * sol. minimum < l1
6395
6496optf = OptimizationFunction (rosenbrock, GalacticOptim. AutoReverseDiff ())
65- optprob = GalacticOptim. instantiate_function (optf,x0,GalacticOptim. AutoReverseDiff (),nothing )
97+ optprob = GalacticOptim. instantiate_function (optf, x0, GalacticOptim. AutoReverseDiff (), nothing )
6698optprob. grad (G2, x0)
6799@test G1 == G2
68100optprob. hess (H2, x0)
69101@test H1 == H2
70102
71103prob = OptimizationProblem (optprob, x0)
72104sol = solve (prob, Optim. BFGS ())
73- @test 10 * sol. minimum < l1
105+ @test 10 * sol. minimum < l1
74106
75107sol = solve (prob, Optim. Newton ())
76- @test 10 * sol. minimum < l1
108+ @test 10 * sol. minimum < l1
77109
78110sol = solve (prob, Optim. KrylovTrustRegion ())
79- @test 10 * sol. minimum < l1
111+ @test 10 * sol. minimum < l1
80112
81113optf = OptimizationFunction (rosenbrock, GalacticOptim. AutoTracker ())
82- optprob = GalacticOptim. instantiate_function (optf,x0,GalacticOptim. AutoTracker (),nothing )
114+ optprob = GalacticOptim. instantiate_function (optf, x0, GalacticOptim. AutoTracker (), nothing )
83115optprob. grad (G2, x0)
84116@test G1 == G2
85117@test_throws ErrorException optprob. hess (H2, x0)
@@ -88,26 +120,26 @@ optprob.grad(G2, x0)
88120prob = OptimizationProblem (optprob, x0)
89121
90122sol = solve (prob, Optim. BFGS ())
91- @test 10 * sol. minimum < l1
123+ @test 10 * sol. minimum < l1
92124
93125@test_throws ErrorException solve (prob, Newton ())
94126
95127optf = OptimizationFunction (rosenbrock, GalacticOptim. AutoFiniteDiff ())
96- optprob = GalacticOptim. instantiate_function (optf,x0,GalacticOptim. AutoFiniteDiff (),nothing )
128+ optprob = GalacticOptim. instantiate_function (optf, x0, GalacticOptim. AutoFiniteDiff (), nothing )
97129optprob. grad (G2, x0)
98- @test G1 ≈ G2 rtol= 1e-6
130+ @test G1 ≈ G2 rtol = 1e-6
99131optprob. hess (H2, x0)
100- @test H1 ≈ H2 rtol= 1e-6
132+ @test H1 ≈ H2 rtol = 1e-6
101133
102134prob = OptimizationProblem (optprob, x0)
103135sol = solve (prob, Optim. BFGS ())
104- @test 10 * sol. minimum < l1
136+ @test 10 * sol. minimum < l1
105137
106138sol = solve (prob, Optim. Newton ())
107- @test 10 * sol. minimum < l1
139+ @test 10 * sol. minimum < l1
108140
109141sol = solve (prob, Optim. KrylovTrustRegion ())
110142@test sol. minimum < l1 # the loss doesn't go below 5e-1 here
111143
112- sol = solve (prob, Flux. ADAM (0.1 ), maxiters = 1000 )
113- @test 10 * sol. minimum < l1
144+ sol = solve (prob, Flux. ADAM (0.1 ), maxiters= 1000 )
145+ @test 10 * sol. minimum < l1
0 commit comments