|
56 | 56 | @deprecate initManual!(w...;kw...) initVariable!(w...;kw...) |
57 | 57 |
|
58 | 58 |
|
| 59 | +##============================================================================== |
| 60 | +## Old parametric kept for comparason until code stabilize |
| 61 | +##============================================================================== |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | +""" |
| 66 | + $SIGNATURES |
| 67 | +
|
| 68 | +Batch solve a Gaussian factor graph using Optim.jl. Parameters can be passed directly to optim. |
| 69 | +Notes: |
| 70 | + - Only :Euclid and :Circular manifolds are currently supported, own manifold are supported with `algorithmkwargs` (code may need updating though) |
| 71 | +""" |
| 72 | +function solveGraphParametric2(fg::AbstractDFG; |
| 73 | + computeCovariance::Bool = true, |
| 74 | + solvekey::Symbol=:parametric, |
| 75 | + autodiff = :forward, |
| 76 | + algorithm=Optim.BFGS, |
| 77 | + algorithmkwargs=(), # add manifold to overwrite computed one |
| 78 | + options = Optim.Options(allow_f_increases=true, |
| 79 | + time_limit = 100, |
| 80 | + # show_trace = true, |
| 81 | + # show_every = 1, |
| 82 | + )) |
| 83 | + |
| 84 | + #Other options |
| 85 | + # options = Optim.Options(time_limit = 100, |
| 86 | + # iterations = 1000, |
| 87 | + # show_trace = true, |
| 88 | + # show_every = 1, |
| 89 | + # allow_f_increases=true, |
| 90 | + # g_tol = 1e-6, |
| 91 | + # ) |
| 92 | + # Example for useing Optim's manifold functions |
| 93 | + # mc_mani = IIF.MixedCircular(fg, varIds) |
| 94 | + # alg = algorithm(;manifold=mc_mani, algorithmkwargs...) |
| 95 | + |
| 96 | + |
| 97 | + varIds = listVariables(fg) |
| 98 | + |
| 99 | + flatvar = FlatVariables(fg, varIds) |
| 100 | + |
| 101 | + for vId in varIds |
| 102 | + p = getVariableSolverData(fg, vId, solvekey).val[1] |
| 103 | + flatvar[vId] = getCoordinates(getVariableType(fg,vId), p) |
| 104 | + end |
| 105 | + |
| 106 | + initValues = flatvar.X |
| 107 | + # initValues .+= randn(length(initValues))*0.0001 |
| 108 | + |
| 109 | + alg = algorithm(; algorithmkwargs...) |
| 110 | + |
| 111 | + cfd = calcFactorMahalanobisDict(fg) |
| 112 | + tdtotalCost = Optim.TwiceDifferentiable((x)->_totalCost(fg, cfd, flatvar, x), initValues, autodiff = autodiff) |
| 113 | + |
| 114 | + result = Optim.optimize(tdtotalCost, initValues, alg, options) |
| 115 | + rv = Optim.minimizer(result) |
| 116 | + |
| 117 | + Σ = if computeCovariance |
| 118 | + H = Optim.hessian!(tdtotalCost, rv) |
| 119 | + pinv(H) |
| 120 | + else |
| 121 | + N = length(initValues) |
| 122 | + zeros(N,N) |
| 123 | + end |
| 124 | + |
| 125 | + d = Dict{Symbol,NamedTuple{(:val, :cov),Tuple{Vector{Float64},Matrix{Float64}}}}() |
| 126 | + |
| 127 | + for key in varIds |
| 128 | + r = flatvar.idx[key] |
| 129 | + push!(d,key=>(val=rv[r],cov=Σ[r,r])) |
| 130 | + end |
| 131 | + |
| 132 | + return d, result, flatvar.idx, Σ |
| 133 | +end |
| 134 | + |
| 135 | +## ================================================================================================ |
| 136 | +## Manifolds.jl Consolidation |
| 137 | +## TODO: Still to be completed and tested. |
| 138 | +## ================================================================================================ |
| 139 | +# struct ManifoldsVector <: Optim.Manifold |
| 140 | +# manis::Vector{Manifold} |
| 141 | +# end |
| 142 | + |
| 143 | +# Base.getindex(mv::ManifoldsVector, inds...) = getindex(mv.mani, inds...) |
| 144 | +# Base.setindex!(mv, X, inds...) = setindex!(mv.mani, X, inds...) |
| 145 | + |
| 146 | +# function ManifoldsVector(fg::AbstractDFG, varIds::Vector{Symbol}) |
| 147 | +# manis = Bool[] |
| 148 | +# for k = varIds |
| 149 | +# push!(manis, getVariableType(fg, k) |> getManifold) |
| 150 | +# end |
| 151 | +# ManifoldsVector(manis) |
| 152 | +# end |
| 153 | + |
| 154 | +# function Optim.retract!(manis::ManifoldsVector, x) |
| 155 | +# for (i,M) = enumerate(manis) |
| 156 | +# x[i] = project(M, x[i]) |
| 157 | +# end |
| 158 | +# return x |
| 159 | +# end |
| 160 | +# function Optim.project_tangent!(manis::ManifoldsVector, G, x) |
| 161 | +# for (i, M) = enumerate(manis) |
| 162 | +# G[i] = project(M, x[i], G) |
| 163 | +# end |
| 164 | +# return G |
| 165 | +# end |
0 commit comments