/** * @file ISAM2-inl.h * @brief Incremental update functionality (ISAM2) for BayesTree, with fluid relinearization. * @author Michael Kaess */ #include #include // for operator += using namespace boost::assign; #include #include "NonlinearFactorGraph-inl.h" #include "GaussianFactor.h" #include "VectorConfig.h" #include "Conditional.h" #include "BayesTree-inl.h" #include "ISAM2.h" namespace gtsam { using namespace std; // from inference-inl.h - need to additionally return the newly created factor for caching boost::shared_ptr _eliminateOne(FactorGraph& graph, CachedFactors& cached, const Symbol& key) { // combine the factors of all nodes connected to the variable to be eliminated // if no factors are connected to key, returns an empty factor boost::shared_ptr joint_factor = removeAndCombineFactors(graph,key); // eliminate that joint factor boost::shared_ptr factor; boost::shared_ptr conditional; boost::tie(conditional, factor) = joint_factor->eliminate(key); // remember the intermediate result to be able to later restart computation in the middle cached[key] = factor; // add new factor on separator back into the graph if (!factor->empty()) graph.push_back(factor); // return the conditional Gaussian return conditional; } // from GaussianFactorGraph.cpp, see _eliminateOne above GaussianBayesNet _eliminate(FactorGraph& graph, CachedFactors& cached, const Ordering& ordering) { GaussianBayesNet chordalBayesNet; // empty BOOST_FOREACH(const Symbol& key, ordering) { GaussianConditional::shared_ptr cg = _eliminateOne(graph, cached, key); chordalBayesNet.push_back(cg); } return chordalBayesNet; } GaussianBayesNet _eliminate_const(const FactorGraph& graph, CachedFactors& cached, const Ordering& ordering) { // make a copy that can be modified locally FactorGraph graph_ignored = graph; return _eliminate(graph_ignored, cached, ordering); } /** Create an empty Bayes Tree */ template ISAM2::ISAM2() : BayesTree() {} /** Create a Bayes Tree from a nonlinear factor graph */ template ISAM2::ISAM2(const NonlinearFactorGraph& nlfg, const Ordering& ordering, const Config& config) : BayesTree(nlfg.linearize(config).eliminate(ordering)), nonlinearFactors_(nlfg), linPoint_(config) { // todo: repeats calculation above, just to set "cached" _eliminate_const(nlfg.linearize(config), cached_, ordering); } /* ************************************************************************* */ // retrieve all factors that ONLY contain the affected variables // (note that the remaining stuff is summarized in the cached factors) template FactorGraph ISAM2::relinearizeAffectedFactors(const list& affectedKeys) { NonlinearFactorGraph nonlinearAffectedFactors; typename FactorGraph >::iterator it; for(it = nonlinearFactors_.begin(); it != nonlinearFactors_.end(); it++) { bool inside = true; BOOST_FOREACH(const Symbol& key, (*it)->keys()) { if (find(affectedKeys.begin(), affectedKeys.end(), key) == affectedKeys.end()) inside = false; } if (inside) nonlinearAffectedFactors.push_back(*it); } return nonlinearAffectedFactors.linearize(linPoint_); } /* ************************************************************************* */ // find intermediate (linearized) factors from cache that are passed into the affected area template FactorGraph ISAM2::getCachedBoundaryFactors(Cliques& orphans) { FactorGraph cachedBoundary; BOOST_FOREACH(sharedClique orphan, orphans) { // find the last variable that is not part of the separator Symbol oneTooFar = orphan->separator_.front(); list keys = orphan->keys(); list::iterator it = find(keys.begin(), keys.end(), oneTooFar); it--; const Symbol& key = *it; // retrieve the cached factor and add to boundary cachedBoundary.push_back(cached_[key]); } return cachedBoundary; } /* ************************************************************************* */ template void ISAM2::update_internal(const NonlinearFactorGraph& newFactors, const Config& config, Cliques& orphans) { // todo: updates all variables... needs to be synced with removeTop!!! linPoint_ = expmap(linPoint_, delta_); // add new variables linPoint_.insert(config); FactorGraph newFactorsLinearized = newFactors.linearize(linPoint_); // Remove the contaminated part of the Bayes tree FactorGraph affectedFactors; // list keysToBeRemoved = newFactorsLinearized.keys(); // todo list keysToBeRemoved = nonlinearFactors_.keys(); this->removeTop(keysToBeRemoved, affectedFactors, orphans); // relinearize the affected factors ... list affectedKeys = affectedFactors.keys(); FactorGraph factors = relinearizeAffectedFactors(affectedKeys); // todo: searches through all factors, potentially expensive // ... add the cached intermediate results from the boundary of the orphans ... FactorGraph cachedBoundary = getCachedBoundaryFactors(orphans); factors.push_back(cachedBoundary); // ... and finally add the new linearized factors themselves factors.push_back(newFactorsLinearized); // create an ordering for the new and contaminated factors Ordering ordering; if (true) { ordering = factors.getOrdering(); } else { list keys = factors.keys(); keys.sort(); // todo: correct sorting order? ordering = keys; } // eliminate into a Bayes net BayesNet bayesNet = _eliminate(factors, cached_, ordering); // remember the new factors for later relinearization nonlinearFactors_.push_back(newFactors); // insert conditionals back in, straight into the topless bayesTree typename BayesNet::const_reverse_iterator rit; for ( rit=bayesNet.rbegin(); rit != bayesNet.rend(); ++rit ) this->insert(*rit); // add orphans to the bottom of the new tree BOOST_FOREACH(sharedClique orphan, orphans) { Symbol key = orphan->separator_.front(); sharedClique parent = (*this)[key]; parent->children_ += orphan; orphan->parent_ = parent; // set new parent! } // update solution - todo: potentially expensive delta_ = optimize2(*this); } template void ISAM2::update(const NonlinearFactorGraph& newFactors, const Config& config) { Cliques orphans; this->update_internal(newFactors, config, orphans); } /* ************************************************************************* */ } /// namespace gtsam