@@ -173,6 +173,40 @@ void WindowedOptimizer::optimizationLoop()
173173 preprocessMarginalization (*new_transaction);
174174 // Combine the new transactions with any marginal transaction from the end of the last cycle
175175 new_transaction->merge (marginal_transaction_);
176+
177+ // check for hanging constraints (constraints added to variables that are about to be marginalized)
178+ auto new_marginal_transaction = fuse_core::Transaction::make_shared ();
179+ std::vector<fuse_core::UUID> variables_to_remarginalize;
180+ for (auto &uuid : marginal_transaction_->removedVariables ()) {
181+ // get constraints that are to be removed in marginal transaction
182+ auto to_remove_constraints = marginal_transaction_->getRemovedConstraints ();
183+ // get connected constraint to this variable
184+ auto all_constraints = graph_->getConnectedConstraints (uuid);
185+ // check if there is a constraint on this variable that isnt in the
186+ // marginal transaction
187+ for (auto &constraint : all_constraints) {
188+ fuse_core::UUID constraint_uuid = constraint->uuid ();
189+ bool is_contained = false ;
190+ for (auto &to_remove : to_remove_constraints) {
191+ if (constraint_uuid == to_remove) {
192+ is_contained = true ;
193+ break ;
194+ }
195+ }
196+ if (!is_contained) {
197+ variables_to_remarginalize.push_back (uuid);
198+ }
199+ }
200+ }
201+
202+ // if some variables have hanging constraints then remarginalize them
203+ if (variables_to_remarginalize.size () > 0 ) {
204+ new_marginal_transaction = fuse_constraints::marginalizeVariables (
205+ ros::this_node::getName (), variables_to_remarginalize, *graph_);
206+ postprocessMarginalization (*new_marginal_transaction);
207+ new_transaction->merge (new_marginal_transaction);
208+ }
209+
176210 // Update the graph
177211 try
178212 {
0 commit comments