-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathParametricManopt.jl
More file actions
903 lines (750 loc) · 26.2 KB
/
Copy pathParametricManopt.jl
File metadata and controls
903 lines (750 loc) · 26.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
using Manopt
using Manopt.Printf
using FiniteDiff
using SparseDiffTools
using SparseArrays
using ForwardDiff
# using Zygote
##
function getVarIntLabelMap(
vartypeslist::OrderedDict{DataType, Vector{Symbol}}
)
varlist_tuple = (values(vartypeslist)...,)
varlabelsAP = ArrayPartition{Symbol, typeof(varlist_tuple)}(varlist_tuple)
varIntLabel = OrderedDict(zip(varlabelsAP, collect(1:length(varlabelsAP))))
return varIntLabel, varlabelsAP
end
function CalcFactorResidual(
fg,
fct::FactorCompute,
varIntLabel
)
fac_func = getObservation(fct)
varOrder = collect(getVariableOrder(fct))
varOrderIdxs = getindex.(Ref(varIntLabel), varOrder)
M = getManifold(getObservation(fct))
dims = manifold_dimension(M)
meas, iΣ = getFactorMeasurementParametric(fct)
sqrt_iΣ = convert(SMatrix{dims, dims}, sqrt(iΣ))
cache = preambleCache(fg, getVariable.(fg, varOrder), getObservation(fct))
return CalcFactorResidual(
fct.label,
fac_func,
tuple(varOrder...),
tuple(varOrderIdxs...),
meas,
sqrt_iΣ,
cache,
)
end
"""
CalcFactorResidualAP
Create an `ArrayPartition` of `CalcFactorResidual`s.
"""
function CalcFactorResidualAP(
fg::GraphsDFG,
factorLabels::Vector{Symbol},
varIntLabel::OrderedDict{Symbol, Int64}
)
factypes, typedict, alltypes = getFactorTypesCount(getFactor.(fg, factorLabels))
# skip non-numeric prior (MetaPrior)
#TODO test... remove MetaPrior{T} something like this
metaPriorKeys = filter(k->contains(string(k), "MetaPrior"), collect(keys(alltypes)))
delete!.(Ref(alltypes), metaPriorKeys)
parts = map(values(alltypes)) do labels
map(getFactor.(fg, labels)) do fct
CalcFactorResidual(fg, fct, varIntLabel)
end
end
parts_tuple = (parts...,)
return ArrayPartition{CalcFactorResidual, typeof(parts_tuple)}(parts_tuple)
end
function (cfm::CalcFactorResidual)(p)
meas = cfm.meas
points = map(idx->p[idx], cfm.varOrderIdxs)
return cfm.sqrt_iΣ * cfm(meas, points...)
end
# cost function f: M->ℝᵈ for Riemannian Levenberg-Marquardt
struct CostFres_cond!{PT, CFT}
points::PT
costfuns::ArrayPartition{CalcFactorResidual, CFT}
varLabels::Vector{Symbol}
end
function (costf::CostFres_cond!)(M::AbstractManifold, x::Vector, p::AbstractVector)
costf.points[1:length(p)] .= p
st = 1
for cfm_part in costf.costfuns.x
st = calcFactorResVec!(x, cfm_part, costf.points, st)
end
return x
end
struct CostFres!{CFT}
# points::PT #TODO RENAME - don't update this in functor, seperator static points only!
costfuns::ArrayPartition{CalcFactorResidual, CFT}
varLabels::Vector{Symbol} # vector for performance above ArrayPartition{Symbol}?
# varPoints::VPT
# sepLabels::Vector{Symbol}
# sepPoints::SPT
# facLabels::Vector{Symbol}
# add return_ranges to allow MultiThreaded
end
function calcFactorResVec!(
x::Vector{T},
cfm_part::Vector{<:CalcFactorResidual{FT, N, D}},
p::AbstractArray,
st::Int
) where {T, FT, N, D}
for cfm in cfm_part
x[st:st + D - 1] = cfm(p) #NOTE looks like do not broadcast here
st += D
end
return st
end
function calcFactorResVec_threaded!(x::Vector{T}, cfm_part::Vector{<:CalcFactorResidual}, p::AbstractArray, st::Int) where T
l = getDimension(cfm_part[1]) # all should be the same
N = length(cfm_part)
chunkies = Iterators.partition(1:N, N ÷ Threads.nthreads())
Threads.@threads for chunki in collect(chunkies)
for i in chunki
r = range(st + l*(i - 1); length = l)
cfm = cfm_part[i]
x[r] = cfm(p) #NOTE looks like do not broadcast here
end
end
return st + l*N
end
function (costf::CostFres!{CFT})(M::AbstractManifold, x::Vector{T}, p::AbstractVector) where {CFT,T}
st = 1
for cfm_part in costf.costfuns.x
# if length(cfm_part) > Threads.nthreads() * 10
# st = calcFactorResVec_threaded!(x, cfm_part, p, st)
# else
st = calcFactorResVec!(x, cfm_part, p, st)
# end
end
return x
end
## --------------------------------------------------------------------------------------------------------------
## jacobian of function for Riemannian Levenberg-Marquardt
## --------------------------------------------------------------------------------------------------------------
struct JacF_RLM!{CF, TX, TQ, JC}
costF!::CF
X0::Vector{Float64}
X::TX
q::TQ
res::Vector{Float64}
Jcache::JC
end
# function JacF_RLM!(M, costF!; basis_domain::AbstractBasis = DefaultOrthonormalBasis())
function JacF_RLM!(M, costF!, p, fg=nothing;
all_points=p,
basis_domain::AbstractBasis = LieGroups.DefaultLieAlgebraOrthogonalBasis(),
is_sparse=!isnothing(fg)
)
res = reduce(vcat, map(f -> f(all_points), Vector(costF!.costfuns)))
X0 = zeros(manifold_dimension(M))
X = get_vector(M, p, X0, basis_domain)
# X = vee(LieAlgebra(M), X0)
q = exp(M, p, X)
if is_sparse
factLabels = collect(getproperty.(costF!.costfuns, :faclbl))
sparsity = eltype(res).(getSparsityPattern(fg, costF!.varLabels, factLabels))
colorvec = matrix_colors(sparsity)
else
sparsity = nothing
colorvec = 1:length(X0)
end
cache = FiniteDiff.JacobianCache(X0, res; colorvec, sparsity)
return JacF_RLM!(costF!, X0, X, q, res, cache)
end
# TODO addd M to JacF_RLM! and test this ipo closure
# function (jacF!::JacF_RLM!)(res, Xc)
# X = jacF!.X
# q = jacF!.q
# get_vector!(M, X, p, Xc, basis_domain)
# exp!(M, q, p, X)
# return jacF!.costF!(M, res, q)
# end
function (jacF!::JacF_RLM!)(
M::AbstractManifold,
J,
p::T;
# basis_domain::AbstractBasis = DefaultOrthonormalBasis(),
# basis_domain::AbstractBasis = DefaultOrthogonalBasis(),
basis_domain::AbstractBasis = LieGroups.DefaultLieAlgebraOrthogonalBasis(),
) where T
X0 = jacF!.X0
X = jacF!.X
q = jacF!.q
cache = jacF!.Jcache
fill!(X0, 0)
# TODO make sure closure performs (let, ::, or (jacF!::JacF_RLM!)(res, Xc))
function costf!(res, Xc)
get_vector!(M, X, p, Xc, basis_domain)
exp!(M, q, p, X)
jacF!.costF!(M, res, q)
end
FiniteDiff.finite_difference_jacobian!(
J,
costf!,
X0,
cache;
)
return J
end
# ϵ = getPointIdentity(M)
# function jaccost(res, Xc)
# exp!(M, q, ϵ, get_vector!(M, X, p, Xc, basis_domain))
# compose!(M, q, p, q)
# jacF!.costF!(M, res, q)
# end
# ManifoldDiff._jacobian!(
# J,
# (Xc)->jacF!.costF!(M, jacF!.res, exp!(M, q, p, get_vector!(M, X, p, Xc, basis_domain))),
# X0,
# ManifoldDiff.default_differential_backend()
# )
## --------------------------------------------------------------------------------------------------------------
## ForwardDiff jacobian for Riemannian Levenberg-Marquardt
## --------------------------------------------------------------------------------------------------------------
struct JacF_RLM_ForwardDiff!{CF, JC}
costF!::CF
X0::Vector{Float64}
res::Vector{Float64}
sparsity::Union{SparseMatrixCSC, Nothing}
jac_cache::JC # ForwardColorJacCache or nothing
end
function JacF_RLM_ForwardDiff!(M, costF!, p, fg=nothing;
all_points=p,
basis_domain::AbstractBasis = LieGroups.DefaultLieAlgebraOrthogonalBasis(),
is_sparse=!isnothing(fg),
)
res = reduce(vcat, map(f -> f(all_points), Vector(costF!.costfuns)))
X0 = zeros(manifold_dimension(M))
if is_sparse && !isnothing(fg)
factLabels = collect(getproperty.(costF!.costfuns, :faclbl))
sparsity = eltype(res).(getSparsityPattern(fg, costF!.varLabels, factLabels))
colorvec = matrix_colors(sparsity)
# build the in-place wrapper that ForwardColorJacCache expects: f(out, x)
function _inplace_costf!(out, Xc)
_X = get_vector(M, p, Xc, basis_domain)
_q = exp(M, p, _X)
costF!(M, out, _q)
end
jac_cache = ForwardColorJacCache(_inplace_costf!, X0; dx=similar(res), colorvec, sparsity)
else
sparsity = nothing
jac_cache = nothing
end
return JacF_RLM_ForwardDiff!(costF!, X0, res, sparsity, jac_cache)
end
function (jacF!::JacF_RLM_ForwardDiff!)(
M::AbstractManifold,
J,
p;
basis_domain::AbstractBasis = DefaultOrthogonalBasis(),
# basis_domain::AbstractBasis = LieGroups.DefaultLieAlgebraOrthogonalBasis(),
)
X0 = jacF!.X0
fill!(X0, 0)
if !isnothing(jacF!.jac_cache)
# sparse path: use coloring-aware ForwardDiff via SparseDiffTools
function _inplace_costf_sparse!(out, Xc)
X = get_vector(M, p, Xc, basis_domain)
# X = hat(LieAlgebra(M), Xc)
q = exp(M, p, X)
jacF!.costF!(M, out, q)
end
forwarddiff_color_jacobian!(J, _inplace_costf_sparse!, X0, jacF!.jac_cache)
else
# dense path: standard ForwardDiff
nres = length(jacF!.res)
function costf(Xc)
X = get_vector(M, p, Xc, basis_domain)
q = exp(M, p, X)
_res = zeros(eltype(Xc), nres)
jacF!.costF!(M, _res, q)
return _res
end
ForwardDiff.jacobian!(J, costf, X0)
end
return J
end
struct FactorGradient{A <: AbstractMatrix}
manifold::AbstractManifold
JacF!::JacF_RLM!
J::A
end
# TODO this function is not the sparsity pattern yet, it just fills in all entries from the biadjacency matrix
# TODO allow getting sparcity pattern for a subfg
# OLD 0.424040 seconds (940.11 k allocations: 45.512 MiB)
# NEW 0.001552 seconds (2.04 k allocations: 1.816 MiB)
function getSparsityPattern(fg, varLabels, factLabels)
biadj = getBiadjacencyMatrix(fg; varLabels, factLabels)
vdims = getDimension.(getVariable.(fg, biadj.varLabels))
fdims = getDimension.(getFactor.(fg, biadj.facLabels))
c_end = cumsum(vdims)
r_end = cumsum(fdims)
C_range = range.(c_end - vdims .+1, c_end)
R_range = range.(r_end - fdims .+1, r_end)
ROWS, COLS, _ = findnz(biadj.B)
iter = reduce(vcat, map(zip(ROWS, COLS)) do (R,C)
vec(CartesianIndices((R_range[R], C_range[C])))
end)
# vec(CartesianIndices((R_range[2], C_range[1])))
return sparse(getindex.(iter,1), getindex.(iter,2), ones(Bool, length(iter)))
end
function precisionFiniteDiff(M, jacF!::JacF_RLM!, p0)
# Jcache
X0 = fill!(deepcopy(jacF!.X0), 0)
function costf(Xc)
let res = jacF!.res, X = jacF!.X, q = jacF!.q, p0=p0
get_vector!(M, X, p0, Xc, DefaultOrthogonalBasis())
# get_vector!(M, X, p0, Xc, LieGroups.DefaultLieAlgebraOrthogonalBasis())
exp!(M, q, p0, X)
1/2*norm(jacF!.costF!(M, res, q))^2
end
end
FiniteDiff.finite_difference_hessian(costf, X0)
end
function precisionFiniteDiff(M, jacF!::JacF_RLM_ForwardDiff!, p0)
X0 = fill!(copy(jacF!.X0), 0)
nres = length(jacF!.res)
function costf(Xc)
# X = get_vector(M, p0, Xc, DefaultOrthogonalBasis())
X = get_vector(M, p0, Xc, LieGroups.DefaultLieAlgebraOrthogonalBasis())
q = exp(M, p0, X)
_res = zeros(nres)
jacF!.costF!(M, _res, q)
return 1/2*norm(_res)^2
end
FiniteDiff.finite_difference_hessian(costf, X0)
end
function qr_linear_subsolver!(sk, JJ, grad_f_c)
sk .= qr(JJ) \ grad_f_c
return sk
end
function pinv_subsolver!(sk, JJ, grad_f_c)
sk .= pinv(JJ) * grad_f_c
return sk
end
"""
DebugTension(dof; io=stdout)
Manopt `DebugAction` that prints the tension (reduced chi-squared) at each iteration.
Tension = 2*cost / dof, where `dof = N - M` (residual dimension minus state dimension).
Usage in `solve_RLM`:
```julia
dof = num_components - manifold_dimension(M)
solve_RLM(fg; debug = [:Iteration, " | ", DebugTension(dof), "\n", 1])
```
"""
mutable struct DebugTension <: Manopt.DebugAction
dof::Int
io::IO
format::String
end
DebugTension(dof::Int; io::IO=stdout, format="tension: %.2f") = DebugTension(dof, io, format)
function (d::DebugTension)(p::Manopt.AbstractManoptProblem, st::Manopt.AbstractManoptSolverState, k::Int)
if d.dof <= 0
s = NaN
else
cost = Manopt.get_cost(p, Manopt.get_iterate(st))
s = sqrt(2 * cost / d.dof)
end
Printf.format(d.io, Printf.Format(d.format), s)
return nothing
end
# replace :tension symbol with DebugTension(dof) in a debug vector
_inject_tension(debug, dof) = map(x -> x === :tension ? DebugTension(dof) : x, debug)
function solve_RLM(
fg,
varlabels = ls(fg),
faclabels = lsf(fg);
is_sparse = true,
finiteDiffCovariance = false,
jacobian_method::Symbol = :finitediff,
solveKey::Symbol = :parametric,
# linear_subsolver! = Manopt.default_lm_lin_solve!,
linear_subsolver! = qr_linear_subsolver!,
kwargs...
)
# get the manifold and variable types
vars = getVariable.(fg, varlabels)
M, varTypes, vartypeslist = buildGraphSolveManifold(vars)
varIntLabel, varlabelsAP = getVarIntLabelMap(vartypeslist)
#Can use varIntLabel (because its an OrderedDict), but varLabelsAP makes the ArrayPartition.
p0 = map(varlabelsAP) do label
mean(getBelief(getState(fg, label, solveKey)))
end
# create an ArrayPartition{CalcFactorResidual} for faclabels
calcfacs = CalcFactorResidualAP(fg, faclabels, varIntLabel)
#cost and jacobian functions
# cost function f: M->ℝᵈ for Riemannian Levenberg-Marquardt
costF! = CostFres!(calcfacs, collect(varlabelsAP))
# jacobian of function for Riemannian Levenberg-Marquardt
if jacobian_method == :forwarddiff
jacF! = JacF_RLM_ForwardDiff!(M, costF!, p0, fg; is_sparse)
else
jacF! = JacF_RLM!(M, costF!, p0, fg; is_sparse)
end
num_components = length(jacF!.res)
initial_residual_values = zeros(num_components)
# initial_jacobian_f not type stable, but function barrier so should be ok.
initial_jacobian_f = if jacF! isa JacF_RLM! && is_sparse
jacF!.Jcache.sparsity
elseif jacF! isa JacF_RLM_ForwardDiff! && !isnothing(jacF!.sparsity)
jacF!.sparsity
else
zeros(num_components, manifold_dimension(M))
end
# inject DebugTension for :tension symbol in debug kwarg
dof = num_components - manifold_dimension(M)
if haskey(kwargs, :debug)
kwargs = (; kwargs..., debug = _inject_tension(kwargs[:debug], dof))
end
lm_r = Manopt.LevenbergMarquardt!(
M,
costF!,
jacF!,
p0,
num_components;
evaluation=InplaceEvaluation(),
jacobian_tangent_basis = LieGroups.DefaultLieAlgebraOrthogonalBasis(),
# jacobian_tangent_basis = DefaultOrthogonalBasis(),
initial_residual_values,
initial_jacobian_f,
linear_subsolver!,
kwargs...
)
if finiteDiffCovariance
Λ = precisionFiniteDiff(M, jacF!, lm_r)
else
J = initial_jacobian_f
jacF!(M, J, lm_r) # recompute J at solution point
Λ = Symmetric(J'J) # approx Hessian = precision matrix
end
# tension (reduced chi-squared): ||r||^2 / (N-M) = 2*cost / (N-M)
dof = num_components - manifold_dimension(M)
final_res = zeros(num_components)
costF!(M, final_res, lm_r)
tension = dof > 0 ? sum(abs2, final_res) / dof : NaN
return M, varlabelsAP, lm_r, Λ, tension
end
# nlso = NonlinearLeastSquaresObjective(
# costF!,
# jacF!,
# num_components;
# evaluation = InplaceEvaluation(),
# jacobian_tangent_basis = DefaultOrthogonalBasis(),
# )
# @debug "starting solver"
# lm_r = LevenbergMarquardt!(
# M, nlso, p0;
# evaluation = InplaceEvaluation(),
# jacobian_tangent_basis = DefaultOrthogonalBasis(),
# initial_residual_values,
# initial_jacobian_f,
# kwargs...
# )
function build_costF_jacF(
fg,
varlabels = ls(fg),
faclabels = lsf(fg);
is_sparse = false,
)
# get the manifold and variable types
vars = getVariable.(fg, varlabels)
M, varTypes, vartypeslist = buildGraphSolveManifold(vars)
varIntLabel, varlabelsAP = getVarIntLabelMap(vartypeslist)
#Can use varIntLabel (because its an OrderedDict), but varLabelsAP makes the ArrayPartition.
p0 = map(varlabelsAP) do label
getVal(fg, label, solveKey = :parametric)[1]
end
# create an ArrayPartition{CalcFactorResidual} for faclabels
calcfacs = CalcFactorResidualAP(fg, faclabels, varIntLabel)
#cost and jacobian functions
# cost function f: M->ℝᵈ for Riemannian Levenberg-Marquardt
costF! = CostFres!(calcfacs, collect(varlabelsAP))
# jacobian of function for Riemannian Levenberg-Marquardt
jacF! = JacF_RLM!(M, costF!, p0, fg; is_sparse)
return M, costF!, jacF!, p0
end
function solve_RLM_conditional(
fg,
frontals::Vector{Symbol} = ls(fg),
separators::Vector{Symbol} = setdiff(ls(fg), frontals);
is_sparse=false,
finiteDiffCovariance=true,
jacobian_method::Symbol = :finitediff,
solveKey::Symbol = :parametric,
linear_subsolver! = qr_linear_subsolver!,
kwargs...
)
is_sparse && error("Sparse solve_RLM_conditional not supported yet")
# get the subgraph formed by all frontals, separators and fully connected factors
varlabels = union(frontals, separators)
_, faclabels = listNeighborhood(fg, varlabels, 1)
filter!(faclabels) do fl
return issubset(getVariableOrder(fg, fl), varlabels)
end
@assert !isempty(faclabels) "Empty factor set for graph with variables $(ls(fg))"
frontal_vars = getVariable.(fg, frontals)
separator_vars = getVariable.(fg, separators)
# so the subgraph consists of varlabels(frontals + separators) and faclabels
_, _, frontal_vartypeslist = getVariableTypesCount(getVariable.(fg,frontals))
frontal_varIntLabel, frontal_varlabelsAP = getVarIntLabelMap(frontal_vartypeslist)
if isempty(separators)
separator_vartypeslist = OrderedDict{DataType, Vector{Symbol}}()
separator_varlabelsAP = ArrayPartition{Symbol,Tuple}(())
else
_, _, separator_vartypeslist = getVariableTypesCount(getVariable.(fg,separators))
separator_varIntLabel, separator_varlabelsAP = getVarIntLabelMap(separator_vartypeslist)
end
all_varlabelsAP = ArrayPartition((frontal_varlabelsAP.x..., separator_varlabelsAP.x...))
all_points = map(all_varlabelsAP) do label
mean(getBelief(getState(fg, label, solveKey)))
end
p0 = ArrayPartition(all_points.x[1:length(frontal_varlabelsAP.x)])
all_varIntLabel = OrderedDict{Symbol,Int}(
map(enumerate(all_varlabelsAP)) do (i,l)
l=>i
end
)
# varIntLabel_frontals = filter(p->first(p) in frontals, varIntLabel)
# varIntLabel_separators = filter(p->first(p) in separators, varIntLabel)
calcfacs = CalcFactorResidualAP(fg, faclabels, all_varIntLabel)
# get the manifold and variable types
M, varTypes, vartypeslist = buildGraphSolveManifold(frontal_vars)
#cost and jacobian functions
# cost function f: M->ℝᵈ for Riemannian Levenberg-Marquardt
costF! = CostFres_cond!(all_points, calcfacs, Vector{Symbol}(collect(all_varlabelsAP)))
# jacobian of function for Riemannian Levenberg-Marquardt
if jacobian_method == :forwarddiff
jacF! = JacF_RLM_ForwardDiff!(M, costF!, p0, fg; all_points, is_sparse)
else
jacF! = JacF_RLM!(M, costF!, p0, fg; all_points, is_sparse)
end
num_components = length(jacF!.res)
initial_residual_values = zeros(num_components)
initial_jacobian_f = if jacF! isa JacF_RLM! && is_sparse
jacF!.Jcache.sparsity
elseif jacF! isa JacF_RLM_ForwardDiff! && !isnothing(jacF!.sparsity)
jacF!.sparsity
else
zeros(num_components, manifold_dimension(M))
end
# inject DebugTension for :tension symbol in debug kwarg
dof = num_components - manifold_dimension(M)
if haskey(kwargs, :debug)
kwargs = (; kwargs..., debug = _inject_tension(kwargs[:debug], dof))
end
lm_r = LevenbergMarquardt(
M,
costF!,
jacF!,
p0,
num_components;
evaluation=InplaceEvaluation(),
initial_residual_values,
initial_jacobian_f,
linear_subsolver!,
kwargs...
)
if finiteDiffCovariance
Λ = precisionFiniteDiff(M, jacF!, lm_r)
else
jacF!(M, initial_jacobian_f, lm_r)
Λ = Symmetric(initial_jacobian_f' * initial_jacobian_f)
end
# tension (reduced chi-squared): ||r||^2 / (N-M) = 2*cost / (N-M)
final_res = zeros(num_components)
costF!(M, final_res, lm_r)
tension = sum(abs2, final_res) / dof
return M, frontal_varlabelsAP, lm_r, Λ, tension
end
function extractMarginalsAP(M, labelsAP::ArrayPartition{Symbol}, Σ::AbstractArray{<:Real})
st = 1
Σvec = map(eachindex(labelsAP.x)) do i
l = getDimension(M.manifolds[i].manifold)
map(eachindex(labelsAP.x[i])) do j
r = st:st + l - 1
st += l
SMatrix{l,l,Float64}(Σ[r,r])
end
end
ArrayPartition(Σvec...)
end
#HEX solve
# sparse J 0.025235 seconds (133.65 k allocations: 9.964 MiB
# new1 0.013486 seconds (36.16 k allocations: 2.593 MiB)
# new2 0.010764 seconds (34.61 k allocations: 3.111 MiB)
# dense J 0.022079 seconds (283.54 k allocations: 18.146 MiB)
function autoinitParametric!(
fg,
clique_order = getInitOrderParametric(fg);
reinit = false,
kwargs...
)
init_labels = @showprogress map(clique_order) do cliq
autoinitParametric!(fg, cliq.frontals, cliq.separators; reinit, kwargs...)
end
filter!(!isnothing, init_labels)
return init_labels
end
function autoinitParametric!(dfg::AbstractDFG, initme::Symbol; kwargs...)
return autoinitParametric!(dfg, getVariable(dfg, initme); kwargs...)
end
function autoinitParametric!(dfg::AbstractDFG, xi::VariableCompute; solveKey = :parametric, kwargs...)
initme = getLabel(xi)
prepareState!(xi, NLLSSolver(), solveKey)
separators = ls2(dfg, initme)
filter!(separators) do vl
return hasState(dfg, vl, solveKey) && isInitialized(dfg, vl, solveKey)
end
return autoinitParametric!(dfg, [initme], separators; solveKey, kwargs...)
end
function autoinitParametric!(
dfg::AbstractDFG,
frontals::Vector{Symbol},
separators::Vector{Symbol} = Symbol[];
solveKey = :parametric,
reinit::Bool = false,
linear_subsolver! = pinv_subsolver!,
kwargs...,
)
#TODO prepare only the relevant states.
prepareStates!(dfg, NLLSSolver(), solveKey)
# Filter to only uninitialized variables (unless reinit)
to_init = if reinit
frontals
else
filter(v -> !isInitialized(dfg, v, solveKey), frontals)
end
isempty(to_init) && return false
# Filter separators to only those already initialized
active_separators = filter(separators) do vl
hasState(dfg, vl, solveKey) && isInitialized(dfg, vl, solveKey)
end
# Nothing to initialize if no separators and no priors on any frontal
if isempty(active_separators)
has_any_prior = any(to_init) do v
any(isPrior.(dfg, listNeighbors(dfg, v)))
end
has_any_prior || return false
end
# Check that we have usable factors
varlabels = union(to_init, active_separators)
_, faclabels = listNeighborhood(dfg, varlabels, 1)
filter!(fl -> issubset(getVariableOrder(dfg, fl), varlabels), faclabels)
isempty(faclabels) && return false
# Seed each frontal from an initialized separator of the same type
for v in to_init
xi = getVariable(dfg, v)
vnd = getState(xi, solveKey)
has_prior = any(isPrior.(dfg, listNeighbors(dfg, v)))
if !has_prior && !isempty(active_separators)
my_kind = getStateKind(xi)
same_kind = filter(active_separators) do vl
getStateKind(getVariable(dfg, vl)) === my_kind
end
if !isempty(same_kind)
mn = mean(getBelief(getState(dfg, same_kind[1], solveKey)))
_bw = cov(getBelief(vnd))
# BW = getBW(getBelief(vnd))
# _bw = (0<length(BW)) && isassigned(BW,1) ? BW[1] : nothing
_hode = HomotopyDensity_legacy(getStateKind(vnd),[mn,]; bw=_bw, newbw=false)
setBelief!(vnd, _hode)
end
end
end
# Solve
M, varlabelsAP, lm_r, Λ, _ = solve_RLM_conditional(dfg, to_init, active_separators; solveKey, linear_subsolver!, kwargs...)
_Σ = (I)(size(Λ, 1))
_Σ_ = sparse(1.0*I, size(Λ, 1), size(Λ, 1)) # create a sparse identity matrix
# _Σ_ = (1.0*I)(size(Λ, 1)) # legacy was Bool, weird refactor forced premature Float64
offset = 0
invertonce = true
# Update each frontal variable with result
for (i, v) in enumerate(varlabelsAP)
vrb = getVariable(dfg, v)
state = getState(vrb, solveKey)
# Update covariances from joint precision if positive definite
if invertonce && !isnothing(Λ)
F = cholesky!(Λ; check = false)
if issuccess(F)
invertonce = false
_Σ_ .= (F \ _Σ)
end
end
dim = getDimension(vrb)
r = (offset + 1):(offset + dim)
offset += dim
bw = ApproxManifoldProducts._forcestatic(_Σ_[r,r])
# @info "WHAT" string(r) string(lm_r[i]) string(bw)
hode = HomotopyDensity_legacy(getStateKind(state),[lm_r[i],]; bw, newbw=false)
setBelief!(state, hode, true)
end
return true
end
"""
$SIGNATURES
Batch parametric graph solve using Riemannian Levenberg Marquardt.
"""
solveGraphParametric(args...; kwargs...) = solve_RLM(args...; kwargs...)
function DFG.solveGraphParametric!(
fg::AbstractDFG,
args...;
init::Bool = true,
solveKey::Symbol = :parametric,
is_sparse = true,
# debug, stopping_criterion, damping_term_min=1e-2,
# expect_zero_residual=true,
kwargs...
)
# make sure variables has solverData, see #1637
prepare!(fg, NLLSSolver(), solveKey)
init && autoinitParametric!(fg; solveKey)
M, v, r, Λ, tension = solve_RLM(fg, args...; is_sparse, kwargs...)
updateParametricSolution!(fg, M, v, r, Λ)
return M, v, r, Λ
end
## Check when time and delete if it can't be improved, curretnly ArrayPartition works best
#=
using FunctionWrappers: FunctionWrapper
# call with
calcfacs = CalcFactorResidualWrapper(fg, faclabels, varIntLabel, all_points)
costF! = CostF_RLM_WRAP2!(all_points, calcfacs, map(cfm->size(cfm.obj.x.iΣ,1), calcfacs))
function CalcFactorResidualWrapper(fg, factorLabels::Vector{Symbol}, varIntLabel::OrderedDict{Symbol, Int64}, points::ArrayPartition)
factypes, typedict, alltypes = getFactorTypesCount(getFactor.(fg, factorLabels))
# skip non-numeric prior (MetaPrior)
#TODO test... remove MetaPrior{T} something like this
metaPriorKeys = filter(k->contains(string(k), "MetaPrior"), collect(keys(alltypes)))
delete!.(Ref(alltypes), metaPriorKeys)
calcfacs = map(factorLabels) do labels
fct = getFactor(fg, labels)
# moet ek 'n view in p0 in maak wat jy net p0 update en CFM view automaties, toets dit...
cfm = IIF.CalcFactorResidual(fg, fct, varIntLabel, points)
# return FunctionWrapper{Vector{Float64}, Tuple{typeof(points)}}(cfm)
return FunctionWrapper{Vector{Float64}, Tuple{}}(cfm)
end
return calcfacs
end
struct CostF_RLM_WRAP2!{PT, CFW}
points::PT
costfuns::Vector{CFW}
retdims::Vector{Int}
end
function (cost::CostF_RLM_WRAP2!)(M::AbstractManifold, x::Vector{T}, p::AbstractVector{T}) where T
# x .= reduce(vcat, map(f -> f(p), cost.costfuns))
# x .= reduce(vcat, map(f -> f(), cost.costfuns))
st = 1
for (d, f) in zip(cost.retdims, cost.costfuns)
x[st:st + d - 1] .= f(p)
# x[st:st + d - 1] .= f()
# fx = f.obj.x
# x[st:st + d - 1] = fx.sqrt_iΣ * fx(fx.meas, fx.points...)
st += d
end
return x
end
=#