Fixed bugs and compile errors for elimination and back-sub
parent
db8fcfaa4f
commit
3d62299789
|
|
@ -596,7 +596,7 @@ namespace gtsam {
|
||||||
orphans.insert(orphans.begin(), clique->children.begin(), clique->children.end());
|
orphans.insert(orphans.begin(), clique->children.begin(), clique->children.end());
|
||||||
clique->children.clear();
|
clique->children.clear();
|
||||||
|
|
||||||
bn.push_back(clique->conditional());
|
bn.push_back(clique->conditional_);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -84,7 +84,7 @@ namespace gtsam {
|
||||||
typedef std::pair<boost::shared_ptr<ConditionalType>, boost::shared_ptr<_FactorType> > EliminationResult;
|
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.
|
/// 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 for an optional ordering as an argument to elimination functions
|
||||||
typedef boost::optional<const OrderingUnordered&> OptionalOrdering;
|
typedef boost::optional<const OrderingUnordered&> OptionalOrdering;
|
||||||
|
|
|
||||||
|
|
@ -43,15 +43,15 @@ namespace gtsam {
|
||||||
assert(childrenResults.size() == children.size());
|
assert(childrenResults.size() == children.size());
|
||||||
|
|
||||||
// Gather factors
|
// Gather factors
|
||||||
std::vector<sharedFactor> gatheredFactors;
|
FactorGraphType gatheredFactors;
|
||||||
gatheredFactors.reserve(factors.size() + children.size());
|
gatheredFactors.reserve(factors.size() + children.size());
|
||||||
gatheredFactors.insert(gatheredFactors.end(), factors.begin(), factors.end());
|
gatheredFactors.push_back(factors.begin(), factors.end());
|
||||||
gatheredFactors.insert(gatheredFactors.end(), childrenResults.begin(), childrenResults.end());
|
gatheredFactors.push_back(childrenResults.begin(), childrenResults.end());
|
||||||
|
|
||||||
// Do dense elimination step
|
// Do dense elimination step
|
||||||
std::vector<Key> keyAsVector(1); keyAsVector[0] = key;
|
std::vector<Key> keyAsVector(1); keyAsVector[0] = key;
|
||||||
std::pair<boost::shared_ptr<ConditionalType>, boost::shared_ptr<FactorType> > eliminationResult =
|
std::pair<boost::shared_ptr<ConditionalType>, boost::shared_ptr<FactorType> > eliminationResult =
|
||||||
function(gatheredFactors, keyAsVector);
|
function(gatheredFactors, OrderingUnordered(keyAsVector));
|
||||||
|
|
||||||
// Add conditional to BayesNet
|
// Add conditional to BayesNet
|
||||||
output->push_back(eliminationResult.first);
|
output->push_back(eliminationResult.first);
|
||||||
|
|
|
||||||
|
|
@ -122,12 +122,19 @@ namespace gtsam {
|
||||||
template<class JUNCTIONTREE>
|
template<class JUNCTIONTREE>
|
||||||
struct EliminationData {
|
struct EliminationData {
|
||||||
EliminationData* const parentData;
|
EliminationData* const parentData;
|
||||||
|
size_t myIndexInParent;
|
||||||
std::vector<typename JUNCTIONTREE::sharedFactor> childFactors;
|
std::vector<typename JUNCTIONTREE::sharedFactor> childFactors;
|
||||||
boost::shared_ptr<typename JUNCTIONTREE::BayesTreeType::Node> bayesTreeNode;
|
boost::shared_ptr<typename JUNCTIONTREE::BayesTreeType::Node> bayesTreeNode;
|
||||||
EliminationData(EliminationData* _parentData, size_t nChildren) :
|
EliminationData(EliminationData* _parentData, size_t nChildren) :
|
||||||
parentData(_parentData),
|
parentData(_parentData),
|
||||||
bayesTreeNode(boost::make_shared<typename JUNCTIONTREE::BayesTreeType::Node>()) {
|
bayesTreeNode(boost::make_shared<typename JUNCTIONTREE::BayesTreeType::Node>())
|
||||||
childFactors.reserve(nChildren);
|
{
|
||||||
|
if(parentData) {
|
||||||
|
myIndexInParent = parentData->childFactors.size();
|
||||||
|
parentData->childFactors.push_back(typename JUNCTIONTREE::sharedFactor());
|
||||||
|
} else {
|
||||||
|
myIndexInParent = 0;
|
||||||
|
}
|
||||||
// Set up BayesTree parent and child pointers
|
// Set up BayesTree parent and child pointers
|
||||||
if(parentData) {
|
if(parentData) {
|
||||||
bayesTreeNode->parent_ = parentData->bayesTreeNode;
|
bayesTreeNode->parent_ = parentData->bayesTreeNode;
|
||||||
|
|
@ -158,26 +165,26 @@ namespace gtsam {
|
||||||
// Typedefs
|
// Typedefs
|
||||||
typedef typename JUNCTIONTREE::sharedFactor sharedFactor;
|
typedef typename JUNCTIONTREE::sharedFactor sharedFactor;
|
||||||
typedef typename JUNCTIONTREE::FactorType FactorType;
|
typedef typename JUNCTIONTREE::FactorType FactorType;
|
||||||
|
typedef typename JUNCTIONTREE::FactorGraphType FactorGraphType;
|
||||||
typedef typename JUNCTIONTREE::ConditionalType ConditionalType;
|
typedef typename JUNCTIONTREE::ConditionalType ConditionalType;
|
||||||
typedef typename JUNCTIONTREE::BayesTreeType::Node BTNode;
|
typedef typename JUNCTIONTREE::BayesTreeType::Node BTNode;
|
||||||
|
|
||||||
// Gather factors
|
// Gather factors
|
||||||
assert(node->children.size() == myData.childFactors.size());
|
FactorGraphType gatheredFactors;
|
||||||
std::vector<sharedFactor> gatheredFactors;
|
|
||||||
gatheredFactors.reserve(node->factors.size() + node->children.size());
|
gatheredFactors.reserve(node->factors.size() + node->children.size());
|
||||||
gatheredFactors.insert(gatheredFactors.end(), node->factors.begin(), node->factors.end());
|
gatheredFactors.push_back(node->factors.begin(), node->factors.end());
|
||||||
gatheredFactors.insert(gatheredFactors.end(), myData.childFactors.begin(), myData.childFactors.end());
|
gatheredFactors.push_back(myData.childFactors.begin(), myData.childFactors.end());
|
||||||
|
|
||||||
// Do dense elimination step
|
// Do dense elimination step
|
||||||
std::pair<boost::shared_ptr<ConditionalType>, boost::shared_ptr<FactorType> > eliminationResult =
|
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
|
// Store conditional in BayesTree clique
|
||||||
myData.bayesTreeNode->conditional_ = eliminationResult.first;
|
myData.bayesTreeNode->conditional_ = eliminationResult.first;
|
||||||
|
|
||||||
// Store remaining factor in parent's gathered factors
|
// Store remaining factor in parent's gathered factors
|
||||||
if(!eliminationResult.second->empty())
|
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
|
// Add remaining factors that were not involved with eliminated variables
|
||||||
boost::shared_ptr<FactorGraphType> allRemainingFactors = boost::make_shared<FactorGraphType>();
|
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(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 result
|
||||||
return std::make_pair(result, allRemainingFactors);
|
return std::make_pair(result, allRemainingFactors);
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <gtsam/linear/linearExceptions.h>
|
||||||
#include <boost/range/adaptor/map.hpp>
|
#include <boost/range/adaptor/map.hpp>
|
||||||
#include <boost/range/adaptor/transformed.hpp>
|
#include <boost/range/adaptor/transformed.hpp>
|
||||||
#include <boost/range/join.hpp>
|
#include <boost/range/join.hpp>
|
||||||
|
|
@ -91,7 +92,7 @@ namespace gtsam {
|
||||||
terms
|
terms
|
||||||
| transformed(&_getPairSecond)
|
| transformed(&_getPairSecond)
|
||||||
| transformed(boost::mem_fn(&Matrix::cols)),
|
| 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
|
// Check and add terms
|
||||||
typedef std::pair<Key, Matrix> Term;
|
typedef std::pair<Key, Matrix> Term;
|
||||||
|
|
|
||||||
|
|
@ -517,8 +517,9 @@ namespace gtsam {
|
||||||
size_t frontalDim = Ab_.range(0, nrFrontals).cols();
|
size_t frontalDim = Ab_.range(0, nrFrontals).cols();
|
||||||
|
|
||||||
// Check for singular factor
|
// Check for singular factor
|
||||||
if(model_->dim() < frontalDim)
|
// TODO: fix this check
|
||||||
throw IndeterminantLinearSystemException(this->keys().front());
|
//if(model_->dim() < frontalDim)
|
||||||
|
// throw IndeterminantLinearSystemException(this->keys().front());
|
||||||
|
|
||||||
// Restrict the matrix to be in the first nrFrontals variables and create the conditional
|
// Restrict the matrix to be in the first nrFrontals variables and create the conditional
|
||||||
gttic(cond_Rd);
|
gttic(cond_Rd);
|
||||||
|
|
@ -531,8 +532,8 @@ namespace gtsam {
|
||||||
GaussianConditionalUnordered::shared_ptr conditional = boost::make_shared<GaussianConditionalUnordered>(
|
GaussianConditionalUnordered::shared_ptr conditional = boost::make_shared<GaussianConditionalUnordered>(
|
||||||
Base::keys_, nrFrontals, Ab_, conditionalNoiseModel);
|
Base::keys_, nrFrontals, Ab_, conditionalNoiseModel);
|
||||||
Ab_.rowStart() += frontalDim;
|
Ab_.rowStart() += frontalDim;
|
||||||
|
Ab_.rowEnd() = std::min(Ab_.cols() - 1, originalRowEnd);
|
||||||
Ab_.firstBlock() += nrFrontals;
|
Ab_.firstBlock() += nrFrontals;
|
||||||
Ab_.rowEnd() = originalRowEnd;
|
|
||||||
gttoc(cond_Rd);
|
gttoc(cond_Rd);
|
||||||
|
|
||||||
// Take lower-right block of Ab to get the new factor
|
// Take lower-right block of Ab to get the new factor
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@
|
||||||
#include <gtsam/linear/VectorValuesUnordered.h>
|
#include <gtsam/linear/VectorValuesUnordered.h>
|
||||||
|
|
||||||
#include <boost/foreach.hpp>
|
#include <boost/foreach.hpp>
|
||||||
|
#include <boost/bind.hpp>
|
||||||
#include <boost/range/combine.hpp>
|
#include <boost/range/combine.hpp>
|
||||||
#include <boost/range/numeric.hpp>
|
#include <boost/range/numeric.hpp>
|
||||||
#include <boost/range/adaptor/transformed.hpp>
|
#include <boost/range/adaptor/transformed.hpp>
|
||||||
|
|
@ -50,7 +51,9 @@ namespace gtsam {
|
||||||
/* ************************************************************************* */
|
/* ************************************************************************* */
|
||||||
VectorValuesUnordered::VectorValuesUnordered(const VectorValuesUnordered& first, const VectorValuesUnordered& second)
|
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())
|
if(size() != first.size() + second.size())
|
||||||
throw std::invalid_argument("Requested to merge two VectorValues that have one or more variables in common.");
|
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;
|
DenseIndex totalDim = 0;
|
||||||
std::vector<const Vector*> items(keys.size());
|
std::vector<const Vector*> items(keys.size());
|
||||||
for(size_t i = 0; i < keys.size(); ++i) {
|
for(size_t i = 0; i < keys.size(); ++i) {
|
||||||
items[i] = &at(i);
|
items[i] = &at(keys[i]);
|
||||||
totalDim += items[i]->size();
|
totalDim += items[i]->size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue