/* ---------------------------------------------------------------------------- * GTSAM Copyright 2010, Georgia Tech Research Corporation, * Atlanta, Georgia 30332-0415 * All Rights Reserved * Authors: Frank Dellaert, et al. (see THANKS for the full author list) * See LICENSE for the license information * -------------------------------------------------------------------------- */ /** * @file NonlinearFactorGraph-inl.h * @brief Factor Graph Consisting of non-linear factors * @author Frank Dellaert * @author Carlos Nieto * @author Christian Potthast */ #pragma once #include #include #include #include #include #define INSTANTIATE_NONLINEAR_FACTOR_GRAPH(C) \ INSTANTIATE_FACTOR_GRAPH(NonlinearFactor); \ template class NonlinearFactorGraph; using namespace std; namespace gtsam { /* ************************************************************************* */ template void NonlinearFactorGraph::print(const std::string& str) const { Base::print(str); } /* ************************************************************************* */ template Vector NonlinearFactorGraph::unwhitenedError(const Values& c) const { list errors; BOOST_FOREACH(const sharedFactor& factor, this->factors_) errors.push_back(factor->unwhitenedError(c)); return concatVectors(errors); } /* ************************************************************************* */ template double NonlinearFactorGraph::error(const Values& c) const { double total_error = 0.; // iterate over all the factors_ to accumulate the log probabilities BOOST_FOREACH(const sharedFactor& factor, this->factors_) total_error += factor->error(c); return total_error; } /* ************************************************************************* */ template Ordering::shared_ptr NonlinearFactorGraph::orderingCOLAMD(const Values& config) const { // Create symbolic graph and initial (iterator) ordering SymbolicFactorGraph::shared_ptr symbolic; Ordering::shared_ptr ordering; boost::tie(symbolic,ordering) = this->symbolic(config); // Compute the VariableIndex (column-wise index) VariableIndex<> variableIndex(*symbolic); // Compute a fill-reducing ordering with COLAMD Permutation::shared_ptr colamdPerm(Inference::PermutationCOLAMD(variableIndex)); // Permute the Ordering and VariableIndex with the COLAMD ordering ordering->permuteWithInverse(*colamdPerm->inverse()); // variableIndex.permute(*colamdPerm); // SL-FIX: fix permutation // Return the Ordering and VariableIndex to be re-used during linearization // and elimination return ordering; } /* ************************************************************************* */ template SymbolicFactorGraph::shared_ptr NonlinearFactorGraph::symbolic( const Values& config, const Ordering& ordering) const { // Generate the symbolic factor graph SymbolicFactorGraph::shared_ptr symbolicfg(new SymbolicFactorGraph); symbolicfg->reserve(this->size()); BOOST_FOREACH(const sharedFactor& factor, this->factors_) { symbolicfg->push_back(factor->symbolic(ordering)); } return symbolicfg; } /* ************************************************************************* */ template pair NonlinearFactorGraph::symbolic(const Values& config) const { // Generate an initial key ordering in iterator order Ordering::shared_ptr ordering(config.orderingArbitrary()); return make_pair(symbolic(config, *ordering), ordering); } /* ************************************************************************* */ template boost::shared_ptr NonlinearFactorGraph::linearize( const Values& config, const Ordering& ordering) const { // create an empty linear FG GaussianFactorGraph::shared_ptr linearFG(new GaussianFactorGraph); linearFG->reserve(this->size()); // linearize all factors BOOST_FOREACH(const sharedFactor& factor, this->factors_) { boost::shared_ptr lf = factor->linearize(config, ordering); if (lf) linearFG->push_back(lf); } return linearFG; } /* ************************************************************************* */ } // namespace gtsam