Loading...
Searching...
No Matches
BayesTreeCliqueBase.h
Go to the documentation of this file.
/* ----------------------------------------------------------------------------
* 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
* -------------------------------------------------------------------------- */
#pragma once
#include <gtsam/inference/Key.h>
#include <gtsam/inference/Ordering.h>
#include <gtsam/base/types.h>
#include <gtsam/base/FastVector.h>
#include <boost/optional.hpp>
#include <string>
#include <mutex>
namespace gtsam {
// Forward declarations
template<class CLIQUE> class BayesTree;
template<class GRAPH> struct EliminationTraits;
template<class DERIVED, class FACTORGRAPH>
class BayesTreeCliqueBase
{
private:
typedef BayesTreeCliqueBase<DERIVED, FACTORGRAPH> This;
typedef DERIVED DerivedType;
typedef EliminationTraits<FACTORGRAPH> EliminationTraitsType;
typedef boost::shared_ptr<This> shared_ptr;
typedef boost::weak_ptr<This> weak_ptr;
typedef boost::shared_ptr<DerivedType> derived_ptr;
typedef boost::weak_ptr<DerivedType> derived_weak_ptr;
public:
typedef FACTORGRAPH FactorGraphType;
typedef typename EliminationTraitsType::BayesNetType BayesNetType;
typedef typename BayesNetType::ConditionalType ConditionalType;
typedef boost::shared_ptr<ConditionalType> sharedConditional;
typedef typename FactorGraphType::FactorType FactorType;
typedef typename FactorGraphType::Eliminate Eliminate;
protected:
BayesTreeCliqueBase() : problemSize_(1) {}
BayesTreeCliqueBase(const sharedConditional& conditional)
: conditional_(conditional), problemSize_(1) {}
BayesTreeCliqueBase(const BayesTreeCliqueBase& c)
: conditional_(c.conditional_),
parent_(c.parent_),
children(c.children),
problemSize_(c.problemSize_),
is_root(c.is_root) {}
BayesTreeCliqueBase& operator=(const BayesTreeCliqueBase& c) {
conditional_ = c.conditional_;
parent_ = c.parent_;
children = c.children;
problemSize_ = c.problemSize_;
is_root = c.is_root;
return *this;
}
// Virtual destructor.
virtual ~BayesTreeCliqueBase() {}
mutable boost::optional<FactorGraphType> cachedSeparatorMarginal_;
mutable std::mutex cachedSeparatorMarginalMutex_;
public:
sharedConditional conditional_;
derived_weak_ptr parent_;
FastVector<derived_ptr> children;
int problemSize_;
bool is_root = false;
void setEliminationResult(const typename FactorGraphType::EliminationResult& eliminationResult);
bool equals(const DERIVED& other, double tol = 1e-9) const;
virtual void print(
const std::string& s = "",
const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
const sharedConditional& conditional() const { return conditional_; }
inline bool isRoot() const { return parent_.expired(); }
size_t nrChildren() const { return children.size(); }
const derived_ptr operator[](size_t i) const { return children[i]; }
size_t treeSize() const;
size_t numCachedSeparatorMarginals() const;
derived_ptr parent() const { return parent_.lock(); }
int problemSize() const { return problemSize_; }
BayesNetType shortcut(const derived_ptr& root, Eliminate function = EliminationTraitsType::DefaultEliminate) const;
FactorGraphType separatorMarginal(Eliminate function = EliminationTraitsType::DefaultEliminate) const;
FactorGraphType marginal2(Eliminate function = EliminationTraitsType::DefaultEliminate) const;
void deleteCachedShortcuts();
const boost::optional<FactorGraphType>& cachedSeparatorMarginal() const {
std::lock_guard<std::mutex> marginalLock(cachedSeparatorMarginalMutex_);
return cachedSeparatorMarginal_;
}
friend class BayesTree<DerivedType>;
protected:
KeyVector separator_setminus_B(const derived_ptr& B) const;
KeyVector shortcut_indices(const derived_ptr& B, const FactorGraphType& p_Cp_B) const;
void deleteCachedShortcutsNonRecursive() {
std::lock_guard<std::mutex> marginalLock(cachedSeparatorMarginalMutex_);
cachedSeparatorMarginal_ = boost::none;
}
private:
friend class boost::serialization::access;
template<class ARCHIVE>
void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
if(!parent_.lock()) {
is_root = true;
}
ar & BOOST_SERIALIZATION_NVP(is_root);
ar & BOOST_SERIALIZATION_NVP(conditional_);
if (!is_root) { // TODO(fan): Workaround for boost/serialization #119
ar & BOOST_SERIALIZATION_NVP(parent_);
}
ar & BOOST_SERIALIZATION_NVP(children);
}
};
}
types.h
Typedefs for easier changing of types.
FastVector.h
A thin wrapper around std::vector that uses a custom allocator.
Ordering.h
Variable ordering for the elimination algorithm.
Key.h
gtsam::FastVector
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
FastVector is a type alias to a std::vector with a custom memory allocator.
Definition FastVector.h:33
gtsam
Global functions in a separate testing namespace.
Definition chartTesting.h:28
gtsam::KeyVector
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition Key.h:86
gtsam::KeyFormatter
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition Key.h:35
gtsam::EliminationTraits< DiscreteFactorGraph >::DefaultEliminate
static std::pair< boost::shared_ptr< ConditionalType >, boost::shared_ptr< FactorType > > DefaultEliminate(const FactorGraphType &factors, const Ordering &keys)
The default dense elimination function.
Definition DiscreteFactorGraph.h:82
gtsam::BayesTree
Bayes tree.
Definition BayesTree.h:67
gtsam::EliminationTraits
Traits class for eliminateable factor graphs, specifies the types that result from elimination,...
Definition EliminateableFactorGraph.h:36
gtsam::BayesTreeCliqueBase
This is the base class for BayesTree cliques.
Definition BayesTreeCliqueBase.h:50
gtsam::BayesTreeCliqueBase::operator[]
const derived_ptr operator[](size_t i) const
Return the child at index i.
Definition BayesTreeCliqueBase.h:150
gtsam::BayesTreeCliqueBase::operator=
BayesTreeCliqueBase & operator=(const BayesTreeCliqueBase &c)
Shallow copy assignment constructor.
Definition BayesTreeCliqueBase.h:90
gtsam::BayesTreeCliqueBase::deleteCachedShortcutsNonRecursive
void deleteCachedShortcutsNonRecursive()
Non-recursive delete cached shortcuts and marginals - internal only.
Definition BayesTreeCliqueBase.h:201
gtsam::BayesTreeCliqueBase::nrChildren
size_t nrChildren() const
Return the number of children.
Definition BayesTreeCliqueBase.h:147
gtsam::BayesTreeCliqueBase::problemSize
int problemSize() const
Problem size (used for parallel traversal).
Definition BayesTreeCliqueBase.h:162
gtsam::BayesTreeCliqueBase::BayesTreeCliqueBase
BayesTreeCliqueBase()
Default constructor.
Definition BayesTreeCliqueBase.h:74
gtsam::BayesTreeCliqueBase::parent
derived_ptr parent() const
return a shared_ptr to the parent clique
Definition BayesTreeCliqueBase.h:159
gtsam::BayesTreeCliqueBase::treeSize
size_t treeSize() const
The size of subtree rooted at this clique, i.e., nr of Cliques.
Definition BayesTreeCliqueBase-inst.h:84
gtsam::BayesTreeCliqueBase::BayesTreeCliqueBase
BayesTreeCliqueBase(const sharedConditional &conditional)
Construct from a conditional, leaving parent and child pointers uninitialized.
Definition BayesTreeCliqueBase.h:78
gtsam::BayesTreeCliqueBase< DiscreteBayesTreeClique, DiscreteFactorGraph >::cachedSeparatorMarginalMutex_
std::mutex cachedSeparatorMarginalMutex_
Definition BayesTreeCliqueBase.h:110
gtsam::BayesTreeCliqueBase::isRoot
bool isRoot() const
Return true if this clique is the root of a Bayes tree.
Definition BayesTreeCliqueBase.h:144
gtsam::BayesTreeCliqueBase::BayesTreeCliqueBase
BayesTreeCliqueBase(const BayesTreeCliqueBase &c)
Shallow copy constructor.
Definition BayesTreeCliqueBase.h:82
gtsam::BayesTreeCliqueBase::marginal2
FactorGraphType marginal2(Eliminate function=EliminationTraitsType::DefaultEliminate) const
return the marginal P(C) of the clique, using marginal caching
Definition BayesTreeCliqueBase-inst.h:195
gtsam::BayesTreeCliqueBase::equals
bool equals(const DERIVED &other, double tol=1e-9) const
check equality
Definition BayesTreeCliqueBase-inst.h:35
gtsam::BayesTreeCliqueBase::separatorMarginal
FactorGraphType separatorMarginal(Eliminate function=EliminationTraitsType::DefaultEliminate) const
return the marginal P(S) on the separator
Definition BayesTreeCliqueBase-inst.h:147
gtsam::BayesTreeCliqueBase::shortcut
BayesNetType shortcut(const derived_ptr &root, Eliminate function=EliminationTraitsType::DefaultEliminate) const
return the conditional P(S|Root) on the separator given the root
Definition BayesTreeCliqueBase-inst.h:113
gtsam::BayesTreeCliqueBase::deleteCachedShortcuts
void deleteCachedShortcuts()
This deletes the cached shortcuts of all cliques (subtree) below this clique.
Definition BayesTreeCliqueBase-inst.h:207
gtsam::BayesTreeCliqueBase::shortcut_indices
KeyVector shortcut_indices(const derived_ptr &B, const FactorGraphType &p_Cp_B) const
Determine variable indices to keep in recursive separator shortcut calculation The factor graph p_Cp_...
Definition BayesTreeCliqueBase-inst.h:57
gtsam::BayesTreeCliqueBase< DiscreteBayesTreeClique, DiscreteFactorGraph >::conditional
const sharedConditional & conditional() const
Definition BayesTreeCliqueBase.h:141
gtsam::BayesTreeCliqueBase::separator_setminus_B
KeyVector separator_setminus_B(const derived_ptr &B) const
Calculate set for shortcut calculations.
Definition BayesTreeCliqueBase-inst.h:45
gtsam::BayesTreeCliqueBase::print
virtual void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print this node
Definition BayesTreeCliqueBase-inst.h:76
gtsam::BayesTreeCliqueBase::numCachedSeparatorMarginals
size_t numCachedSeparatorMarginals() const
Collect number of cliques with cached separator marginals.
Definition BayesTreeCliqueBase-inst.h:93
gtsam::BayesTreeCliqueBase< DiscreteBayesTreeClique, DiscreteFactorGraph >::cachedSeparatorMarginal_
boost::optional< FactorGraphType > cachedSeparatorMarginal_
Definition BayesTreeCliqueBase.h:105
gtsam::BayesTreeCliqueBase::setEliminationResult
void setEliminationResult(const typename FactorGraphType::EliminationResult &eliminationResult)
Fill the elimination result produced during elimination.
Definition BayesTreeCliqueBase-inst.h:27
gtsam::EliminateableFactorGraph< DiscreteFactorGraph >::EliminationResult
std::pair< boost::shared_ptr< ConditionalType >, boost::shared_ptr< _FactorType > > EliminationResult
Definition EliminateableFactorGraph.h:86