Fixed bugs and compile errors for elimination and back-sub

release/4.3a0
Richard Roberts 2013-07-09 17:50:51 +00:00
parent db8fcfaa4f
commit 3d62299789
7 changed files with 41 additions and 26 deletions

View File

@ -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_);
}
}

View File

@ -84,7 +84,7 @@ namespace gtsam {
typedef std::pair<boost::shared_ptr<ConditionalType>, boost::shared_ptr<_FactorType> > EliminationResult;
/// The function type that does a single dense elimination step on a subgraph.
typedef boost::function<EliminationResult(std::vector<boost::shared_ptr<_FactorType> >, std::vector<Key>)> Eliminate;
typedef boost::function<EliminationResult(const FactorGraphType&, const OrderingUnordered&)> Eliminate;
/// Typedef for an optional ordering as an argument to elimination functions
typedef boost::optional<const OrderingUnordered&> OptionalOrdering;

View File

@ -43,15 +43,15 @@ namespace gtsam {
assert(childrenResults.size() == children.size());
// Gather factors
std::vector<sharedFactor> 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<Key> keyAsVector(1); keyAsVector[0] = key;
std::pair<boost::shared_ptr<ConditionalType>, boost::shared_ptr<FactorType> > eliminationResult =
function(gatheredFactors, keyAsVector);
function(gatheredFactors, OrderingUnordered(keyAsVector));
// Add conditional to BayesNet
output->push_back(eliminationResult.first);

View File

@ -122,17 +122,24 @@ namespace gtsam {
template<class JUNCTIONTREE>
struct EliminationData {
EliminationData* const parentData;
size_t myIndexInParent;
std::vector<typename JUNCTIONTREE::sharedFactor> childFactors;
boost::shared_ptr<typename JUNCTIONTREE::BayesTreeType::Node> bayesTreeNode;
EliminationData(EliminationData* _parentData, size_t nChildren) :
parentData(_parentData),
bayesTreeNode(boost::make_shared<typename JUNCTIONTREE::BayesTreeType::Node>()) {
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<typename JUNCTIONTREE::BayesTreeType::Node>())
{
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<sharedFactor> 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<ConditionalType>, boost::shared_ptr<FactorType> > 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<FactorGraphType> allRemainingFactors = boost::make_shared<FactorGraphType>();
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);

View File

@ -18,6 +18,7 @@
*/
#pragma once
#include <gtsam/linear/linearExceptions.h>
#include <boost/range/adaptor/map.hpp>
#include <boost/range/adaptor/transformed.hpp>
#include <boost/range/join.hpp>
@ -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<Key, Matrix> Term;

View File

@ -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<GaussianConditionalUnordered>(
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

View File

@ -19,6 +19,7 @@
#include <gtsam/linear/VectorValuesUnordered.h>
#include <boost/foreach.hpp>
#include <boost/bind.hpp>
#include <boost/range/combine.hpp>
#include <boost/range/numeric.hpp>
#include <boost/range/adaptor/transformed.hpp>
@ -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<Key>::operator(), std::less<Key>(), 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<const 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();
}