diff --git a/gtsam/inference/BayesTreeUnordered-inst.h b/gtsam/inference/BayesTreeUnordered-inst.h index 696b0aafa..6343c43ad 100644 --- a/gtsam/inference/BayesTreeUnordered-inst.h +++ b/gtsam/inference/BayesTreeUnordered-inst.h @@ -596,7 +596,7 @@ namespace gtsam { orphans.insert(orphans.begin(), clique->children.begin(), clique->children.end()); clique->children.clear(); - bn.push_back(clique->conditional()); + bn.push_back(clique->conditional_); } } diff --git a/gtsam/inference/EliminateableFactorGraph.h b/gtsam/inference/EliminateableFactorGraph.h index 1ed2d2c3d..f594280ba 100644 --- a/gtsam/inference/EliminateableFactorGraph.h +++ b/gtsam/inference/EliminateableFactorGraph.h @@ -84,7 +84,7 @@ namespace gtsam { typedef std::pair, boost::shared_ptr<_FactorType> > EliminationResult; /// The function type that does a single dense elimination step on a subgraph. - typedef boost::function >, std::vector)> Eliminate; + typedef boost::function Eliminate; /// Typedef for an optional ordering as an argument to elimination functions typedef boost::optional OptionalOrdering; diff --git a/gtsam/inference/EliminationTreeUnordered-inst.h b/gtsam/inference/EliminationTreeUnordered-inst.h index babf39dd4..baa05f2f8 100644 --- a/gtsam/inference/EliminationTreeUnordered-inst.h +++ b/gtsam/inference/EliminationTreeUnordered-inst.h @@ -43,15 +43,15 @@ namespace gtsam { assert(childrenResults.size() == children.size()); // Gather factors - std::vector gatheredFactors; + FactorGraphType gatheredFactors; gatheredFactors.reserve(factors.size() + children.size()); - gatheredFactors.insert(gatheredFactors.end(), factors.begin(), factors.end()); - gatheredFactors.insert(gatheredFactors.end(), childrenResults.begin(), childrenResults.end()); + gatheredFactors.push_back(factors.begin(), factors.end()); + gatheredFactors.push_back(childrenResults.begin(), childrenResults.end()); // Do dense elimination step std::vector keyAsVector(1); keyAsVector[0] = key; std::pair, boost::shared_ptr > eliminationResult = - function(gatheredFactors, keyAsVector); + function(gatheredFactors, OrderingUnordered(keyAsVector)); // Add conditional to BayesNet output->push_back(eliminationResult.first); diff --git a/gtsam/inference/JunctionTreeUnordered-inst.h b/gtsam/inference/JunctionTreeUnordered-inst.h index de61a3792..1afb6c91e 100644 --- a/gtsam/inference/JunctionTreeUnordered-inst.h +++ b/gtsam/inference/JunctionTreeUnordered-inst.h @@ -122,17 +122,24 @@ namespace gtsam { template struct EliminationData { EliminationData* const parentData; + size_t myIndexInParent; std::vector childFactors; boost::shared_ptr bayesTreeNode; EliminationData(EliminationData* _parentData, size_t nChildren) : parentData(_parentData), - bayesTreeNode(boost::make_shared()) { - childFactors.reserve(nChildren); - // Set up BayesTree parent and child pointers - if(parentData) { - bayesTreeNode->parent_ = parentData->bayesTreeNode; - parentData->bayesTreeNode->children.push_back(bayesTreeNode); - } + bayesTreeNode(boost::make_shared()) + { + if(parentData) { + myIndexInParent = parentData->childFactors.size(); + parentData->childFactors.push_back(typename JUNCTIONTREE::sharedFactor()); + } else { + myIndexInParent = 0; + } + // Set up BayesTree parent and child pointers + if(parentData) { + bayesTreeNode->parent_ = parentData->bayesTreeNode; + parentData->bayesTreeNode->children.push_back(bayesTreeNode); + } } }; @@ -158,26 +165,26 @@ namespace gtsam { // Typedefs typedef typename JUNCTIONTREE::sharedFactor sharedFactor; typedef typename JUNCTIONTREE::FactorType FactorType; + typedef typename JUNCTIONTREE::FactorGraphType FactorGraphType; typedef typename JUNCTIONTREE::ConditionalType ConditionalType; typedef typename JUNCTIONTREE::BayesTreeType::Node BTNode; // Gather factors - assert(node->children.size() == myData.childFactors.size()); - std::vector gatheredFactors; + FactorGraphType gatheredFactors; gatheredFactors.reserve(node->factors.size() + node->children.size()); - gatheredFactors.insert(gatheredFactors.end(), node->factors.begin(), node->factors.end()); - gatheredFactors.insert(gatheredFactors.end(), myData.childFactors.begin(), myData.childFactors.end()); + gatheredFactors.push_back(node->factors.begin(), node->factors.end()); + gatheredFactors.push_back(myData.childFactors.begin(), myData.childFactors.end()); // Do dense elimination step std::pair, boost::shared_ptr > eliminationResult = - eliminationFunction(gatheredFactors, node->keys); + eliminationFunction(gatheredFactors, OrderingUnordered(node->keys)); // Store conditional in BayesTree clique myData.bayesTreeNode->conditional_ = eliminationResult.first; // Store remaining factor in parent's gathered factors if(!eliminationResult.second->empty()) - myData.parentData->childFactors.push_back(eliminationResult.second); + myData.parentData->childFactors[myData.myIndexInParent] = eliminationResult.second; } } @@ -244,8 +251,11 @@ namespace gtsam { // Add remaining factors that were not involved with eliminated variables boost::shared_ptr allRemainingFactors = boost::make_shared(); + allRemainingFactors->reserve(remainingFactors_.size() + rootsContainer.childFactors.size()); allRemainingFactors->push_back(remainingFactors_.begin(), remainingFactors_.end()); - allRemainingFactors->push_back(rootsContainer.childFactors.begin(), rootsContainer.childFactors.end()); + BOOST_FOREACH(const sharedFactor& factor, rootsContainer.childFactors) + if(factor) + allRemainingFactors->push_back(factor); // Return result return std::make_pair(result, allRemainingFactors); diff --git a/gtsam/linear/JacobianFactorUnordered-inl.h b/gtsam/linear/JacobianFactorUnordered-inl.h index 5c1f8df64..c552f19ad 100644 --- a/gtsam/linear/JacobianFactorUnordered-inl.h +++ b/gtsam/linear/JacobianFactorUnordered-inl.h @@ -18,6 +18,7 @@ */ #pragma once +#include #include #include #include @@ -91,7 +92,7 @@ namespace gtsam { terms | transformed(&_getPairSecond) | transformed(boost::mem_fn(&Matrix::cols)), - cref_list_of<1>((DenseIndex)1)), b.size()); + boost::assign::cref_list_of<1>((DenseIndex)1)), b.size()); // Check and add terms typedef std::pair Term; diff --git a/gtsam/linear/JacobianFactorUnordered.cpp b/gtsam/linear/JacobianFactorUnordered.cpp index 1059f97be..43b12dd0c 100644 --- a/gtsam/linear/JacobianFactorUnordered.cpp +++ b/gtsam/linear/JacobianFactorUnordered.cpp @@ -517,8 +517,9 @@ namespace gtsam { size_t frontalDim = Ab_.range(0, nrFrontals).cols(); // Check for singular factor - if(model_->dim() < frontalDim) - throw IndeterminantLinearSystemException(this->keys().front()); + // TODO: fix this check + //if(model_->dim() < frontalDim) + // throw IndeterminantLinearSystemException(this->keys().front()); // Restrict the matrix to be in the first nrFrontals variables and create the conditional gttic(cond_Rd); @@ -531,8 +532,8 @@ namespace gtsam { GaussianConditionalUnordered::shared_ptr conditional = boost::make_shared( Base::keys_, nrFrontals, Ab_, conditionalNoiseModel); Ab_.rowStart() += frontalDim; + Ab_.rowEnd() = std::min(Ab_.cols() - 1, originalRowEnd); Ab_.firstBlock() += nrFrontals; - Ab_.rowEnd() = originalRowEnd; gttoc(cond_Rd); // Take lower-right block of Ab to get the new factor diff --git a/gtsam/linear/VectorValuesUnordered.cpp b/gtsam/linear/VectorValuesUnordered.cpp index 9c2a3182b..002c4dacd 100644 --- a/gtsam/linear/VectorValuesUnordered.cpp +++ b/gtsam/linear/VectorValuesUnordered.cpp @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -50,7 +51,9 @@ namespace gtsam { /* ************************************************************************* */ VectorValuesUnordered::VectorValuesUnordered(const VectorValuesUnordered& first, const VectorValuesUnordered& second) { - std::merge(first.begin(), first.end(), second.begin(), second.end(), std::inserter(values_, values_.end())); + // Merge using predicate for comparing first of pair + std::merge(first.begin(), first.end(), second.begin(), second.end(), std::inserter(values_, values_.end()), + boost::bind(&std::less::operator(), std::less(), boost::bind(&KeyValuePair::first, _1), boost::bind(&KeyValuePair::first, _2))); if(size() != first.size() + second.size()) throw std::invalid_argument("Requested to merge two VectorValues that have one or more variables in common."); } @@ -120,7 +123,7 @@ namespace gtsam { DenseIndex totalDim = 0; std::vector items(keys.size()); for(size_t i = 0; i < keys.size(); ++i) { - items[i] = &at(i); + items[i] = &at(keys[i]); totalDim += items[i]->size(); }