maintaining versus rebuilding a ceres Problem

972 views
Skip to first unread message

phreak...@gmail.com

unread,
Nov 27, 2017, 10:34:03 AM11/27/17
to Ceres Solver
Hi,
for my SLAM-like application, I currently create a ceres Problem and add all residuals and parameter blocks every time I need to perform minimization. I saner way would be to reuse the ceres Problem and only add new parameters and residuals while removing old ones (falling out of the optimization window). While I see RemoveResidualBlock and RemoveParameterBlock functions, the documentation for these says that after using them, the ordering will be broken. I don't understand that if this means that I can't rely on the state of the Problem afterwards or it would be ok to use it as mentioned. 
What would be the recommended approach for this case?

Best,
Matias

Sameer Agarwal

unread,
Nov 27, 2017, 2:13:34 PM11/27/17
to ceres-...@googlegroups.com
You should have no problems adding and removing parameter or residual blocks.
The ordering refers to the order in which these parameter and residual blocks are stored internally. Unless you are depending on that somehow, you should be fine. 
Sameer


--
You received this message because you are subscribed to the Google Groups "Ceres Solver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ceres-solver...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ceres-solver/0feea175-ed50-45df-b0d9-6dfcd0e02219%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

phreak...@gmail.com

unread,
Nov 28, 2017, 4:38:46 AM11/28/17
to Ceres Solver
Good to know.
While we are on the subject, in case I want to perform marginalization as it is mostly done with SLAM methods (applying Schur complement) I woudl have to do this myself, right? Or does ceres help with this somehow? I noticed that OKVIS uses Ceres and seems to have coded its own marginalization by creating an extra cost function which sums up the marginalized restrictions (I think, at least).

Best,
Matías

Sameer Agarwal

unread,
Nov 28, 2017, 8:03:37 AM11/28/17
to ceres-...@googlegroups.com

Yep, marginalization is just a cost function you create.


phreak...@gmail.com

unread,
Nov 29, 2017, 3:53:29 PM11/29/17
to Ceres Solver
Great. Do you have any references on the subject? I mean, how could I implement this in Ceres as a residual? I can only find OKVIS using Ceres for this purpose and the code is impossible to understand. Moreover, I can't find a clear reference on the corresponding math.

Also, would this be possible as part of Ceres or does computing this marginalizacion residual require knowledge of the problem?

Best,
Matías

Sameer Agarwal

unread,
Nov 29, 2017, 7:07:48 PM11/29/17
to ceres-...@googlegroups.com
Matias,
I am sorry but I am not a SLAM/marginalization expert. 
You will have to do the implementation yourself, there is nothing in ceres which does this for you.
Sameer


--
You received this message because you are subscribed to the Google Groups "Ceres Solver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ceres-solver...@googlegroups.com.

phreak...@gmail.com

unread,
Nov 30, 2017, 4:47:21 AM11/30/17
to Ceres Solver
No problem, thank you!

Jing Wang

unread,
Dec 10, 2017, 10:31:26 AM12/10/17
to Ceres Solver
VINS-MONO is another open-source SLMA(Visual Inertial) program that use ceres-solver as optimization tool. But it doesn't maintain a ceres problem.
https://github.com/hkUST-Aerial-Robotics/VINS-Mono

在 2017年11月30日星期四 UTC+8下午5:47:21,phreak...@gmail.com写道:

raf...@dotproduct3d.com

unread,
Feb 7, 2025, 10:40:52 AMFeb 7
to Ceres Solver

Yep, marginalization is just a cost function you create.


Old thread, but how would this work in practice?

The result of a marginalization step is a symmetric square matrix G and corresponding vector q, both of size of the reduced system. I.e. two quantities that are subtracted from the reduced system's JTJ and JTr respectively before solving.

On the other hand, when implementing a cost function, I have to implement Evaluate() which produces residuals and jacobians.
I don't see a direct path of how to get a Ceres cost function to subtract G from JTJ and q from JTr using residuals and jacobians as outputs. Am I missing something obvious?

I see that VINS Mono is doing some funky Eigendecomposition (?) stuff but that looks like a hackaround and it'd take me more time that I'm willing to invest to figure out what exactly they are doing there.

Any pointers, clarifications or ideas?

Thanks!

Tim Pfeifer

unread,
Mar 16, 2025, 5:05:07 PMMar 16
to Ceres Solver
You are right, ceres has no inbuilt functionality for marginalization.
There is a PR laying around for years now, but that way manifolds are handled in ceres prevents it completion:
https://ceres-solver-review.googlesource.com/c/ceres-solver/+/17540

I faced the same demand and had to implemented one for myself, very similar to (and actually based on) the VINS-Mono code:
https://github.com/TUC-ProAut/libRSF/blob/d54801a232af02c0cda6012dabd3dea6c5fd77c5/src/FactorGraph.cpp#L258

To implement such a marginalization, you need at least three basic steps:
  1. Find all states and cost functions that are connected to the states you want to marginalize out and evaluate their cost and Jacobian.
    I had to create my own helper structure here, but the PR above solves it much more elegant inside the ceres problem.
    https://github.com/TUC-ProAut/libRSF/blob/d54801a232af02c0cda6012dabd3dea6c5fd77c5/src/FactorGraphStructure.cpp#L30

  2. Build a linear equation system and reduce it to the remaining states.
    (There comes the fancy matrix decomposition. ;-) )
    https://github.com/TUC-ProAut/libRSF/blob/d54801a232af02c0cda6012dabd3dea6c5fd77c5/src/Marginalization.cpp

  3. Use the reduced linear system to build a new cost function that replaces the marginalized ones.
    This is easy for euclidean stetes, but manifolds make this step hard. I went for some handcrafted solution, because the manifold/local-parameterization of ceres does not support the required operations.
    The lack of this operations caused introduction of the new manifold class to ceres as a result of the PR above, but did not solve the problem fully. :-/
    https://github.com/TUC-ProAut/libRSF/blob/d54801a232af02c0cda6012dabd3dea6c5fd77c5/src/factors/MarginalPrior.cpp

So you have to implement it yourself if you really need it - which is not a hack, just a missing feature that can be implemented outside of ceres.
The downside is that the likelihood of an error in logic or math is quite high - so many people would be super happy if some PhD student with too much time would implement a generic solution for the ceres solver :-)

raf...@dotproduct3d.com

unread,
Apr 15, 2025, 10:27:48 AMApr 15
to Ceres Solver
Thanks a lot Tim! This is helpful.
I'll check it out.

Parker Lusk

unread,
Apr 15, 2025, 10:48:50 AMApr 15
to Ceres Solver
Evan Levine also has some good resources for marginalization with ceres
Reply all
Reply to author
Forward
0 new messages