1
1
using GalacticOptim, GalacticOptimJL, GalacticFlux, Test
2
2
using ForwardDiff, Zygote, ReverseDiff, FiniteDiff, Tracker
3
-
3
+ using ModelingToolkit
4
4
x0 = 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
6
6
l1 = rosenbrock (x0)
7
7
8
8
function g! (G, x)
@@ -17,16 +17,48 @@ function h!(H, x)
17
17
H[2 , 2 ] = 200.0
18
18
end
19
19
20
- G1 = Array {Float64} (undef,2 )
21
- G2 = Array {Float64} (undef,2 )
20
+ G1 = Array {Float64} (undef, 2 )
21
+ G2 = Array {Float64} (undef, 2 )
22
22
H1 = Array {Float64} (undef, 2 , 2 )
23
23
H2 = Array {Float64} (undef, 2 , 2 )
24
24
25
25
g! (G1, x0)
26
26
h! (H1, x0)
27
27
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
+
28
60
optf = OptimizationFunction (rosenbrock, GalacticOptim. AutoForwardDiff ())
29
- optprob = GalacticOptim. instantiate_function (optf,x0,GalacticOptim. AutoForwardDiff (),nothing )
61
+ optprob = GalacticOptim. instantiate_function (optf, x0, GalacticOptim. AutoForwardDiff (), nothing )
30
62
optprob. grad (G2, x0)
31
63
@test G1 == G2
32
64
optprob. hess (H2, x0)
@@ -35,16 +67,16 @@ optprob.hess(H2, x0)
35
67
prob = OptimizationProblem (optprob, x0)
36
68
37
69
sol = solve (prob, Optim. BFGS ())
38
- @test 10 * sol. minimum < l1
70
+ @test 10 * sol. minimum < l1
39
71
40
72
sol = solve (prob, Optim. Newton ())
41
- @test 10 * sol. minimum < l1
73
+ @test 10 * sol. minimum < l1
42
74
43
75
sol = solve (prob, Optim. KrylovTrustRegion ())
44
- @test 10 * sol. minimum < l1
76
+ @test 10 * sol. minimum < l1
45
77
46
78
optf = OptimizationFunction (rosenbrock, GalacticOptim. AutoZygote ())
47
- optprob = GalacticOptim. instantiate_function (optf,x0,GalacticOptim. AutoZygote (),nothing )
79
+ optprob = GalacticOptim. instantiate_function (optf, x0, GalacticOptim. AutoZygote (), nothing )
48
80
optprob. grad (G2, x0)
49
81
@test G1 == G2
50
82
optprob. hess (H2, x0)
@@ -53,33 +85,33 @@ optprob.hess(H2, x0)
53
85
prob = OptimizationProblem (optprob, x0)
54
86
55
87
sol = solve (prob, Optim. BFGS ())
56
- @test 10 * sol. minimum < l1
88
+ @test 10 * sol. minimum < l1
57
89
58
90
sol = solve (prob, Optim. Newton ())
59
- @test 10 * sol. minimum < l1
91
+ @test 10 * sol. minimum < l1
60
92
61
93
sol = solve (prob, Optim. KrylovTrustRegion ())
62
- @test 10 * sol. minimum < l1
94
+ @test 10 * sol. minimum < l1
63
95
64
96
optf = OptimizationFunction (rosenbrock, GalacticOptim. AutoReverseDiff ())
65
- optprob = GalacticOptim. instantiate_function (optf,x0,GalacticOptim. AutoReverseDiff (),nothing )
97
+ optprob = GalacticOptim. instantiate_function (optf, x0, GalacticOptim. AutoReverseDiff (), nothing )
66
98
optprob. grad (G2, x0)
67
99
@test G1 == G2
68
100
optprob. hess (H2, x0)
69
101
@test H1 == H2
70
102
71
103
prob = OptimizationProblem (optprob, x0)
72
104
sol = solve (prob, Optim. BFGS ())
73
- @test 10 * sol. minimum < l1
105
+ @test 10 * sol. minimum < l1
74
106
75
107
sol = solve (prob, Optim. Newton ())
76
- @test 10 * sol. minimum < l1
108
+ @test 10 * sol. minimum < l1
77
109
78
110
sol = solve (prob, Optim. KrylovTrustRegion ())
79
- @test 10 * sol. minimum < l1
111
+ @test 10 * sol. minimum < l1
80
112
81
113
optf = OptimizationFunction (rosenbrock, GalacticOptim. AutoTracker ())
82
- optprob = GalacticOptim. instantiate_function (optf,x0,GalacticOptim. AutoTracker (),nothing )
114
+ optprob = GalacticOptim. instantiate_function (optf, x0, GalacticOptim. AutoTracker (), nothing )
83
115
optprob. grad (G2, x0)
84
116
@test G1 == G2
85
117
@test_throws ErrorException optprob. hess (H2, x0)
@@ -88,26 +120,26 @@ optprob.grad(G2, x0)
88
120
prob = OptimizationProblem (optprob, x0)
89
121
90
122
sol = solve (prob, Optim. BFGS ())
91
- @test 10 * sol. minimum < l1
123
+ @test 10 * sol. minimum < l1
92
124
93
125
@test_throws ErrorException solve (prob, Newton ())
94
126
95
127
optf = OptimizationFunction (rosenbrock, GalacticOptim. AutoFiniteDiff ())
96
- optprob = GalacticOptim. instantiate_function (optf,x0,GalacticOptim. AutoFiniteDiff (),nothing )
128
+ optprob = GalacticOptim. instantiate_function (optf, x0, GalacticOptim. AutoFiniteDiff (), nothing )
97
129
optprob. grad (G2, x0)
98
- @test G1 ≈ G2 rtol= 1e-6
130
+ @test G1 ≈ G2 rtol = 1e-6
99
131
optprob. hess (H2, x0)
100
- @test H1 ≈ H2 rtol= 1e-6
132
+ @test H1 ≈ H2 rtol = 1e-6
101
133
102
134
prob = OptimizationProblem (optprob, x0)
103
135
sol = solve (prob, Optim. BFGS ())
104
- @test 10 * sol. minimum < l1
136
+ @test 10 * sol. minimum < l1
105
137
106
138
sol = solve (prob, Optim. Newton ())
107
- @test 10 * sol. minimum < l1
139
+ @test 10 * sol. minimum < l1
108
140
109
141
sol = solve (prob, Optim. KrylovTrustRegion ())
110
142
@test sol. minimum < l1 # the loss doesn't go below 5e-1 here
111
143
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