Skip to content

Commit 006bc96

Browse files
committed
add SpecialEuclidean(2) test with ManifoldDiff
1 parent d7bd96e commit 006bc96

File tree

1 file changed

+58
-14
lines changed

1 file changed

+58
-14
lines changed

test/manifolds/manifolddiff.jl

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@ using Test
44
using LinearAlgebra
55
using ManifoldsBase
66
using Manifolds, Manopt
7-
using Optim
7+
import Optim
88
using FiniteDifferences, ManifoldDiff
99

1010
##
1111

12-
@testset "ManifoldDiff, Basic test" begin
13-
##
14-
1512
# finitediff setup
1613
r_backend = ManifoldDiff.TangentDiffBackend(
1714
ManifoldDiff.FiniteDifferencesBackend()
1815
)
1916

17+
##
18+
@testset "ManifoldDiff, Basic test" begin
19+
##
20+
2021
# problem setup
2122
n = 100
2223
σ = π / 8
@@ -46,9 +47,6 @@ end
4647
##
4748
end
4849

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
5250
##
5351

5452
"""
@@ -70,6 +68,11 @@ function Optim.project_tangent!(M::ManifoldWrapper, g, x)
7068
return g
7169
end
7270

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+
7376
# example usage of Manifolds.jl manifolds in Optim.jl
7477
M = Manifolds.Sphere(2)
7578
x0 = [1.0, 0.0, 0.0]
@@ -86,15 +89,12 @@ end
8689

8790
##
8891

89-
sol = optimize(f, g!, x0, ConjugateGradient(; manifold=ManifoldWrapper(M)))
92+
sol = Optim.optimize(f, g!, x0, Optim.ConjugateGradient(; manifold=ManifoldWrapper(M)))
9093
@test isapprox([0,1,0.], sol.minimizer; atol=1e-8)
9194

9295

9396
## finitediff gradient (non-manual)
9497

95-
r_backend = ManifoldDiff.TangentDiffBackend(
96-
ManifoldDiff.FiniteDifferencesBackend()
97-
)
9898
function g_FD!(X,p)
9999
X .= ManifoldDiff.gradient(M, f, p, r_backend)
100100
X
@@ -103,17 +103,61 @@ end
103103
#
104104
x0 = [1.0, 0.0, 0.0]
105105

106-
sol = optimize(f, g_FD!, x0, ConjugateGradient(; manifold=ManifoldWrapper(M)))
106+
sol = Optim.optimize(f, g_FD!, x0, Optim.ConjugateGradient(; manifold=ManifoldWrapper(M)))
107107
@test isapprox([0,1,0.], sol.minimizer; atol=1e-8)
108108

109109
##
110110

111111
# x0 = [1.0, 0.0, 0.0]
112112
# # 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 )
114114
# @test isapprox([0,1,0.], sol.minimizer; atol=1e-8)
115115

116116
##
117117
end
118118

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

Comments
 (0)