Skip to content

Experiment with factor gradients in solve #1825

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,17 @@ function CalcFactorResidualAP(
return ArrayPartition{CalcFactorResidual, typeof(parts_tuple)}(parts_tuple)
end

function (cfm::CalcFactorResidual)(p)
function (cfm::CalcFactorResidual)(p::Vector)
meas = cfm.meas
points = map(idx->p[idx], cfm.varOrderIdxs)
return cfm.sqrt_iΣ * cfm(meas, points...)
end

function (cfm::CalcFactorResidual)(p::ArrayPartition)
points = map(idx->p.x[idx], cfm.varOrderIdxs)
return cfm.sqrt_iΣ * cfm(cfm.meas, points...)
end

# cost function f: M->ℝᵈ for Riemannian Levenberg-Marquardt
struct CostFres_cond!{PT, CFT}
points::PT
Expand Down
29 changes: 29 additions & 0 deletions IncrementalInference/src/services/FactorGradients.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,36 @@ function factorJacobian(
return ManifoldDiff.jacobian(M_dom, M_codom, costf, p0, backend)
end

function factorGradient(
cf::CalcFactorResidual,
M,
p,
backend = ManifoldDiff.TangentDiffBackend(ManifoldDiff.FiniteDiffBackend()),
)
ManifoldDiff.gradient(M, (x) -> 1//2 * norm(cf(x))^2, p, backend)
end

function factorJacobian(
cf::CalcFactorResidual,
M_dom,
p,
backend = ManifoldDiff.TangentDiffBackend(ManifoldDiff.FiniteDiffBackend()),
)
# M_dom = ProductManifold(getManifold.(fg, varlabels)...)
M_codom = Euclidean(manifold_dimension(getManifold(cf)))

return ManifoldDiff.jacobian(M_dom, M_codom, cf, p, backend)
end

#
function factorGradient(
cf::CalcFactorNormSq,
M,
p,
backend = ManifoldDiff.TangentDiffBackend(ManifoldDiff.FiniteDiffBackend()),
)
ManifoldDiff.gradient(M, cf, p, backend)
end

export getCoordSizes
export checkGradientsToleranceMask, calcPerturbationFromVariable
Expand Down
18 changes: 15 additions & 3 deletions IncrementalInference/src/services/NumericalCalculations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,16 @@ function _solveLambdaNumeric(
return r.minimizer
end

# struct OptimCalcConv end
struct OptimCalcConv end
# CalcFactorNormSq cost function for an input in coordinates as used by Optim.jl
function (hypoCalcFactor::CalcFactorNormSq)(M::AbstractManifold, Xc::AbstractVector)
function (hypoCalcFactor::CalcFactorNormSq)(::Type{OptimCalcConv}, M::AbstractManifold, Xc::AbstractVector)
# hypoCalcFactor.manifold is the factor's manifold, not the variable's manifold that is needed here
ϵ = getPointIdentity(M)
X = get_vector(M, ϵ, SVector(Xc), DefaultOrthogonalBasis())
p = exp(M, ϵ, X)
return hypoCalcFactor(CalcConv, p)
end
(hypoCalcFactor::CalcFactorNormSq)(M::AbstractManifold, p) = hypoCalcFactor(OptimCalcConv, M, p)

struct ManoptCalcConv end

Expand Down Expand Up @@ -117,10 +118,19 @@ function _solveLambdaNumeric(
retraction_method = ExponentialRetraction()
)
return r
elseif false
r = gradient_descent(
M,
(M,x)->hypoCalcFactor(x),
(M, x)-> factorGradient(hypoCalcFactor, M, x),
u0;
stepsize=ConstantStepsize(0.1),
)
return r
end

r = Optim.optimize(
x->hypoCalcFactor(M, x),
x->hypoCalcFactor(OptimCalcConv, M, x),
X0c,
alg
)
Expand Down Expand Up @@ -394,6 +404,8 @@ function (cf::CalcFactorNormSq)(::Type{CalcConv}, x)
res = isnothing(cf.slack) ? res : res .- cf.slack
return sum(x->x^2, res)
end
#default to conv
(cf::CalcFactorNormSq)(x) = cf(CalcConv, x)

function _buildHypoCalcFactor(ccwl::CommonConvWrapper, smpid::Integer, _slack=nothing)
# build a view to the decision variable memory
Expand Down
Loading