1
1
2
2
# dev test script for factor gradients using ForwardDiff and ManifoldDiff
3
3
4
- # using ManifoldDiff
5
- # using Manifolds
4
+ using ManifoldDiff
5
+ using Manifolds
6
6
using IncrementalInference
7
7
using StaticArrays
8
- # using DataStructures: OrderedDict
8
+ using Zygote
9
+ using Test
9
10
10
11
# #
11
12
12
- # M = SpecialEuclidean(2)
13
- # p = ArrayPartition(SA[0.0; 0.0], SMatrix{2,2}(1, 0, 0, 1.))
14
13
15
- # # finitediff setup
16
- # r_backend = ManifoldDiff.TangentDiffBackend(
17
- # ManifoldDiff.FiniteDifferencesBackend()
18
- # )
14
+ @testset " test factorJacobian" begin
15
+ # #
19
16
20
- # e0 = identity_element(M, p)
17
+ # manual EuclidDistance
18
+ z = 10.0
19
+ f_eucliddist (x1,x2; z) = z - norm (x2 - x1)
20
+ x0, x1 = [0.0 ; 0.0 ], [10.0 , 0.0 ]
21
+ J_ = Zygote. jacobian (()-> f_eucliddist (x0, x1; z), Zygote. Params ([x0, x1]))
21
22
22
- # cost(p_) = distance(M, e0, p_)
23
+ Jx0 = J_[x0]
24
+ Jx1 = J_[x1]
23
25
24
- # cost(p)
25
26
26
- # g = ManifoldDiff.jacobian(M, TranslationGroup(3), cost, p, r_backend)
27
+ # #
28
+
29
+ fg = LocalDFG (;
30
+ solverParams = SolverParams (;
31
+ graphinit= false
32
+ )
33
+ )
34
+
35
+ addVariable! .(fg, [:x0 ; :x1 ], Position2)
36
+ f = addFactor! (fg, [:x0 ; :x1 ], EuclidDistance (Normal (z,1.0 )))
37
+
38
+ p1 = [SA[x1[1 ];x1[2 ]] for _ in 1 : 1 ]
39
+
40
+ setVal! (fg, :x1 , p1, solveKey= :parametric )
41
+
42
+ J = IIF. factorJacobian (fg, :x0x1f1 )
43
+
44
+ @test isapprox ( Jx0, J[1 : 1 ,1 : 2 ]; atol= 1e-8 )
45
+ @test isapprox ( Jx1, J[1 : 1 ,3 : 4 ]; atol= 1e-8 )
46
+
27
47
48
+ # #
49
+ end
50
+ # #
28
51
52
+
53
+ # #
54
+ @testset " using RoME; FiniteDiff.jacobian of SpecialEuclidean(2) factor" begin
29
55
# #
30
56
31
57
fg = LocalDFG (;
@@ -34,14 +60,130 @@ fg = LocalDFG(;
34
60
)
35
61
)
36
62
37
- addVariable! .(fg, [:x0 ; :x1 ], Position2 )
38
- f = addFactor! (fg, [:x0 ; :x1 ], EuclidDistance ( Normal ( 10 , 1.0 )))
63
+ addVariable! .(fg, [:x0 ; :x1 ], Pose2 )
64
+ f = addFactor! (fg, [:x0 ; :x1 ], Pose2Pose2 ( MvNormal ([ 10 ; 0 ; pi / 2 ],[ 1 0 0 ; 0 1 0 ; 0 0 1.0 ] )))
39
65
40
- p1 = [SA[ 0.0 ; 0.0 ] for _ in 1 : 1 ]
66
+ p1 = [ArrayPartition ([ 10 ; 0.0 ], [ 0 1 ; - 1 0.0 ]) for _ in 1 : 1 ]
41
67
42
68
setVal! (fg, :x1 , p1, solveKey= :parametric )
43
69
70
+ J = IIF. factorJacobian (fg, :x0x1f1 )
71
+
72
+ @test isapprox ( Jx0, J[1 : 1 ,1 : 2 ]; atol= 1e-8 )
73
+ @test_broken isapprox ( Jx1, J[1 : 1 ,3 : 4 ]; atol= 1e-8 )
74
+
75
+
76
+ # #
77
+ end
78
+ # #
79
+
80
+
81
+ # #
82
+ @testset " ManifoldDiff.jacobian of SpecialEuclidean(2) factor" begin
83
+ # #
84
+
85
+
86
+ M = SpecialEuclidean (2 )
87
+ z = ArrayPartition (SA[10.0 ; 0.0 ], SMatrix {2,2} (0.0 , - 1.0 , 1.0 , 0.0 ))
88
+
89
+ p1 = ArrayPartition (SA[0.0 ; 0.0 ], SMatrix {2,2} (1 , 0 , 0 , 1. ))
90
+ e0 = identity_element (M, p1)
91
+ p2 = exp (M, e0, hat (M, e0, [10 ,0 ,pi / 2 ]))
92
+
93
+
94
+ function resid_SE2 (X, p, q)
95
+ q̂ = Manifolds. compose (M, p, exp (M, identity_element (M, p), X)) # for groups
96
+ return vee (M, q, log (M, q, q̂))
97
+ end
98
+
99
+
100
+ # finitediff setup
101
+ r_backend = ManifoldDiff. TangentDiffBackend (
102
+ ManifoldDiff. FiniteDifferencesBackend ()
103
+ )
104
+
105
+ Me = Euclidean (3 )
106
+
107
+ function _factorJac! (J, z, p1, p2)
108
+ g1 (p_) = resid_SE2 (z, p_, p2)
109
+ g2 (p_) = resid_SE2 (z, p1, p_)
110
+
111
+ J[1 : 3 ,1 : 3 ] = ManifoldDiff. jacobian (M, Me, g1, p1, r_backend)
112
+ J[1 : 3 ,4 : 6 ] = ManifoldDiff. jacobian (M, Me, g2, p2, r_backend)
113
+
114
+ J
115
+ end
116
+ # f_SE2_z(z_) = resid_SE2(z_, e0, p2)
117
+ # f_SE2_x0(p_) = resid_SE2(z, e0, p_)
118
+ # f_SE2_x0(p_) = resid_SE2(z, e0, p_)
119
+
120
+ J = zeros (3 ,6 )
121
+
122
+ J_ = _factorJac! (J, z, p1, p2)
123
+ # @profview _factorJac!(J, z, p1, p2)
124
+
125
+ if false
126
+ z_backend = ManifoldDiff. TangentDiffBackend (
127
+ ManifoldDiff. ZygoteDiffBackend ()
128
+ )
129
+ g = ManifoldDiff. jacobian (M, Euclidean (3 ), f_SE2_x0, p1, z_backend)
130
+ else
131
+ @info " ManifoldDiff.ZygoteDiffBackend usage still under development (23Q3)"
132
+ end
133
+
44
134
45
- J = factorJacobian (fg, :x0x1f1 )
46
135
47
136
# #
137
+ end
138
+
139
+ # cost(p_) = distance(M, e0, p_) # ManifoldDiff.gradient(M, cost, p, r_backend)
140
+ # cost(p)
141
+ # ManifoldDiff.gradient(M, cost, p, r_backend)
142
+
143
+
144
+
145
+
146
+
147
+
148
+
149
+
150
+
151
+
152
+ # ##
153
+ # @testset "CODE STILL UNDER DEV:::Zygote on SpecialEuclidean(2)" begin
154
+ # ##
155
+
156
+ # # manual PosePose
157
+ # M = SpecialEuclidean(2)
158
+ # z = ArrayPartition(SA[10.0; 0.0], SMatrix{2,2}(0.0, -1.0, 1.0, 0.0))
159
+ # e0 = identity_element(M, z)
160
+
161
+ # # modified from IIF/test/testSpecialEuclidean2Mani.jl
162
+ # function f_SE2(X, p, q)
163
+ # q̂ = Manifolds.compose(M, p, exp(M, identity_element(M, p), X)) #for groups
164
+ # Xc = zeros(3)
165
+ # vee!(M, Xc, q, log(M, q, q̂))
166
+ # return Xc
167
+ # # return (Xc'*Xc)[1]
168
+ # end
169
+
170
+ # Xc_0 = [0.0; 0.0; 0.0] # deepcopy(e0)
171
+ # Xc_1 = [10.0; 0.0; pi/2] # deepcopy(z)
172
+
173
+ # J_ = Zygote.jacobian(
174
+ # ()->begin
175
+ # f_SE2(
176
+ # log(M, e0, z),
177
+ # exp(M, e0, hat(M, e0, Xc_0)),
178
+ # exp(M, e0, hat(M, e0, Xc_1))
179
+ # )
180
+ # end,
181
+ # Zygote.Params([Xc_0, Xc_1])
182
+ # )
183
+
184
+ # Jx0 = J_[x0]
185
+ # Jx1 = J_[x1]
186
+
187
+
188
+ # ##
189
+ # end
0 commit comments