@@ -4,19 +4,20 @@ using Test
4
4
using LinearAlgebra
5
5
using ManifoldsBase
6
6
using Manifolds, Manopt
7
- using Optim
7
+ import Optim
8
8
using FiniteDifferences, ManifoldDiff
9
9
10
10
# #
11
11
12
- @testset " ManifoldDiff, Basic test" begin
13
- # #
14
-
15
12
# finitediff setup
16
13
r_backend = ManifoldDiff. TangentDiffBackend (
17
14
ManifoldDiff. FiniteDifferencesBackend ()
18
15
)
19
16
17
+ # #
18
+ @testset " ManifoldDiff, Basic test" begin
19
+ # #
20
+
20
21
# problem setup
21
22
n = 100
22
23
σ = π / 8
46
47
# #
47
48
end
48
49
49
-
50
- @testset " Optim.jl ManifoldWrapper example from mateuszbaran (copied to catch issues on future changes)" begin
51
- # Example modified from: https://gist.github.com/mateuszbaran/0354c0edfb9cdf25e084a2b915816a09
52
50
# #
53
51
54
52
"""
@@ -70,6 +68,11 @@ function Optim.project_tangent!(M::ManifoldWrapper, g, x)
70
68
return g
71
69
end
72
70
71
+ # #
72
+ @testset " Optim.jl ManifoldWrapper example from mateuszbaran (copied to catch issues on future changes)" begin
73
+ # #
74
+ # Example modified from: https://gist.github.com/mateuszbaran/0354c0edfb9cdf25e084a2b915816a09
75
+
73
76
# example usage of Manifolds.jl manifolds in Optim.jl
74
77
M = Manifolds. Sphere (2 )
75
78
x0 = [1.0 , 0.0 , 0.0 ]
86
89
87
90
# #
88
91
89
- sol = optimize (f, g!, x0, ConjugateGradient (; manifold= ManifoldWrapper (M)))
92
+ sol = Optim . optimize (f, g!, x0, Optim . ConjugateGradient (; manifold= ManifoldWrapper (M)))
90
93
@test isapprox ([0 ,1 ,0. ], sol. minimizer; atol= 1e-8 )
91
94
92
95
93
96
# # finitediff gradient (non-manual)
94
97
95
- r_backend = ManifoldDiff. TangentDiffBackend (
96
- ManifoldDiff. FiniteDifferencesBackend ()
97
- )
98
98
function g_FD! (X,p)
99
99
X .= ManifoldDiff. gradient (M, f, p, r_backend)
100
100
X
@@ -103,17 +103,61 @@ end
103
103
#
104
104
x0 = [1.0 , 0.0 , 0.0 ]
105
105
106
- sol = optimize (f, g_FD!, x0, ConjugateGradient (; manifold= ManifoldWrapper (M)))
106
+ sol = Optim . optimize (f, g_FD!, x0, Optim . ConjugateGradient (; manifold= ManifoldWrapper (M)))
107
107
@test isapprox ([0 ,1 ,0. ], sol. minimizer; atol= 1e-8 )
108
108
109
109
# #
110
110
111
111
# x0 = [1.0, 0.0, 0.0]
112
112
# # internal ForwardDfif doesnt work out the box on Manifolds
113
- # sol = optimize(f, x0, ConjugateGradient(; manifold=ManifoldWrapper(M)); autodiff=:forward )
113
+ # sol = Optim. optimize(f, x0, Optim. ConjugateGradient(; manifold=ManifoldWrapper(M)); autodiff=:forward )
114
114
# @test isapprox([0,1,0.], sol.minimizer; atol=1e-8)
115
115
116
116
# #
117
117
end
118
118
119
- # #
119
+
120
+ @testset " Modified Manifolds.jl ManifoldWrapper <: Optim.Manifold for SpecialEuclidean(2)" begin
121
+ # #
122
+
123
+ M = Manifolds. SpecialEuclidean (2 )
124
+ e0 = ArrayPartition ([0 ,0. ], [1 0 ; 0 1. ])
125
+
126
+ x0 = deepcopy (e0)
127
+ Cq = 9 * ones (3 )
128
+ while 1.5 < abs (Cq[3 ])
129
+ @show Cq .= randn (3 )
130
+ # Cq[3] = 1.5 # breaks ConjugateGradient
131
+ end
132
+ q = exp (M,e0,hat (M,e0,Cq))
133
+
134
+ f (p) = distance (M, p, q)^ 2
135
+
136
+ # # finitediff gradient (non-manual)
137
+ function g_FD! (X,p)
138
+ X .= ManifoldDiff. gradient (M, f, p, r_backend)
139
+ X
140
+ end
141
+
142
+ # # sanity check gradients
143
+
144
+ X = hat (M, e0, zeros (3 ))
145
+ g_FD! (X, q)
146
+ # gradient at the optimal point should be zero
147
+ @test isapprox (0 , sum (abs .(X[:])); atol= 1e-8 )
148
+
149
+ # gradient not the optimal point should be non-zero
150
+ g_FD! (X, e0)
151
+ @test 0.01 < sum (abs .(X[:]))
152
+
153
+ # # do optimization
154
+ x0 = deepcopy (e0)
155
+ sol = Optim. optimize (f, g_FD!, x0, Optim. ConjugateGradient (; manifold= ManifoldWrapper (M)))
156
+ Cq .= randn (3 )
157
+ # Cq[
158
+ @show sol. minimizer
159
+ @test isapprox ( f (sol. minimizer), 0 ; atol= 1e-8 )
160
+ @test isapprox ( 0 , sum (abs .(log (M, e0, compose (M, inv (M,q), sol. minimizer)))); atol= 1e-5 )
161
+
162
+ # #
163
+ end
0 commit comments