diff --git a/cpp/ISAM2-inl.h b/cpp/ISAM2-inl.h index 8622fe976..added3f52 100644 --- a/cpp/ISAM2-inl.h +++ b/cpp/ISAM2-inl.h @@ -23,7 +23,7 @@ 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 string& key) { + 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 @@ -47,7 +47,7 @@ namespace gtsam { // from GaussianFactorGraph.cpp, see _eliminateOne above GaussianBayesNet _eliminate(FactorGraph& graph, cachedFactors& cached, const Ordering& ordering) { GaussianBayesNet chordalBayesNet; // empty - BOOST_FOREACH(string key, ordering) { + BOOST_FOREACH(const Symbol& key, ordering) { GaussianConditional::shared_ptr cg = _eliminateOne(graph, cached, key); chordalBayesNet.push_back(cg); } @@ -67,7 +67,7 @@ namespace gtsam { /** 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), config_(config) { + : BayesTree(nlfg.linearize(config).eliminate(ordering)), nonlinearFactors_(nlfg), linPoint_(config), estimate_(config) { // todo: repeats calculation above, just to set "cached" _eliminate_const(nlfg.linearize(config), cached, ordering); } @@ -76,36 +76,41 @@ namespace gtsam { // 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) { + 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(string key, (*it)->keys()) { + 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(config_); + + return nonlinearAffectedFactors.linearize(linPoint_); } /* ************************************************************************* */ + // find intermediate (linearized) factors from cache that are passed into the affected area template FactorGraph ISAM2::getCachedBoundaryFactors(Cliques& orphans) { - // add intermediate (linearized) factors from cache that are passed into the affected area FactorGraph cachedBoundary; + BOOST_FOREACH(sharedClique orphan, orphans) { // find the last variable that is not part of the separator string oneTooFar = orphan->separator_.front(); - list keys = orphan->keys(); - list::iterator it = find(keys.begin(), keys.end(), oneTooFar); + list keys = orphan->keys(); + list::iterator it = find(keys.begin(), keys.end(), oneTooFar); it--; - string key = *it; + const Symbol& key = *it; // retrieve the cached factor and add to boundary cachedBoundary.push_back(cached[key]); } + return cachedBoundary; } @@ -116,20 +121,21 @@ namespace gtsam { // copy variables into config_, but don't overwrite existing entries (current linearization point!) for (typename Config::const_iterator it = config.begin(); it!=config.end(); it++) { - if (!config_.contains(it->first)) { - config_.insert(it->first, it->second); + if (!linPoint_.contains(it->first)) { + linPoint_.insert(it->first, it->second); + estimate_.insert(it->first, it->second); } } - FactorGraph newFactorsLinearized = newFactors.linearize(config_); + FactorGraph newFactorsLinearized = newFactors.linearize(linPoint_); // Remove the contaminated part of the Bayes tree FactorGraph affectedFactors; boost::tie(affectedFactors, orphans) = this->removeTop(newFactorsLinearized); // relinearize the affected factors ... - list affectedKeys = affectedFactors.keys(); - FactorGraph factors = relinearizeAffectedFactors(affectedKeys); + 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); @@ -143,7 +149,7 @@ namespace gtsam { if (true) { ordering = factors.getOrdering(); } else { - list keys = factors.keys(); + list keys = factors.keys(); keys.sort(); // todo: correct sorting order? ordering = keys; } @@ -162,16 +168,17 @@ namespace gtsam { // add orphans to the bottom of the new tree BOOST_FOREACH(sharedClique orphan, orphans) { - string key = orphan->separator_.front(); + Symbol key = orphan->separator_.front(); sharedClique parent = (*this)[key]; parent->children_ += orphan; orphan->parent_ = parent; // set new parent! } - // update solution - VectorConfig solution = optimize2(*this); - solution.print(); + // update solution - todo: potentially expensive + VectorConfig delta = optimize2(*this); +// delta.print(); + estimate_ += delta; }