templates and typedefs: Factor, CONDITIONAL
parent
b37346dffb
commit
6f2df1e036
|
@ -17,6 +17,8 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <gtsam/inference/BayesNet.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <boost/foreach.hpp>
|
||||
|
@ -28,40 +30,36 @@
|
|||
#include <boost/assign/std/vector.hpp> // for +=
|
||||
using namespace boost::assign;
|
||||
|
||||
#include <gtsam/inference/BayesNet.h>
|
||||
//#include "FactorGraph-inl.h"
|
||||
#include "Conditional.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace gtsam {
|
||||
|
||||
/* ************************************************************************* */
|
||||
template<class Conditional>
|
||||
void BayesNet<Conditional>::print(const string& s) const {
|
||||
template<class CONDITIONAL>
|
||||
void BayesNet<CONDITIONAL>::print(const string& s) const {
|
||||
cout << s << ":\n";
|
||||
BOOST_REVERSE_FOREACH(sharedConditional conditional,conditionals_)
|
||||
conditional->print((boost::format("Node[%1%]") % conditional->key()).str());
|
||||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
template<class Conditional>
|
||||
bool BayesNet<Conditional>::equals(const BayesNet& cbn, double tol) const {
|
||||
template<class CONDITIONAL>
|
||||
bool BayesNet<CONDITIONAL>::equals(const BayesNet& cbn, double tol) const {
|
||||
if(size() != cbn.size()) return false;
|
||||
return equal(conditionals_.begin(),conditionals_.end(),cbn.conditionals_.begin(),equals_star<Conditional>(tol));
|
||||
return equal(conditionals_.begin(),conditionals_.end(),cbn.conditionals_.begin(),equals_star<CONDITIONAL>(tol));
|
||||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
template<class Conditional>
|
||||
void BayesNet<Conditional>::permuteWithInverse(const Permutation& inversePermutation) {
|
||||
template<class CONDITIONAL>
|
||||
void BayesNet<CONDITIONAL>::permuteWithInverse(const Permutation& inversePermutation) {
|
||||
BOOST_FOREACH(sharedConditional conditional, conditionals_) {
|
||||
conditional->permuteWithInverse(inversePermutation);
|
||||
}
|
||||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
template<class Conditional>
|
||||
bool BayesNet<Conditional>::permuteSeparatorWithInverse(const Permutation& inversePermutation) {
|
||||
template<class CONDITIONAL>
|
||||
bool BayesNet<CONDITIONAL>::permuteSeparatorWithInverse(const Permutation& inversePermutation) {
|
||||
bool separatorChanged = false;
|
||||
BOOST_FOREACH(sharedConditional conditional, conditionals_) {
|
||||
if(conditional->permuteSeparatorWithInverse(inversePermutation))
|
||||
|
@ -71,22 +69,22 @@ namespace gtsam {
|
|||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
template<class Conditional>
|
||||
void BayesNet<Conditional>::push_back(const BayesNet<Conditional> bn) {
|
||||
template<class CONDITIONAL>
|
||||
void BayesNet<CONDITIONAL>::push_back(const BayesNet<CONDITIONAL> bn) {
|
||||
BOOST_FOREACH(sharedConditional conditional,bn.conditionals_)
|
||||
push_back(conditional);
|
||||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
template<class Conditional>
|
||||
void BayesNet<Conditional>::push_front(const BayesNet<Conditional> bn) {
|
||||
template<class CONDITIONAL>
|
||||
void BayesNet<CONDITIONAL>::push_front(const BayesNet<CONDITIONAL> bn) {
|
||||
BOOST_FOREACH(sharedConditional conditional,bn.conditionals_)
|
||||
push_front(conditional);
|
||||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
template<class Conditional>
|
||||
list<Index> BayesNet<Conditional>::ordering() const {
|
||||
template<class CONDITIONAL>
|
||||
list<Index> BayesNet<CONDITIONAL>::ordering() const {
|
||||
list<Index> ord;
|
||||
BOOST_FOREACH(sharedConditional conditional,conditionals_)
|
||||
ord.push_back(conditional->key());
|
||||
|
@ -94,8 +92,8 @@ namespace gtsam {
|
|||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
template<class Conditional>
|
||||
void BayesNet<Conditional>::saveGraph(const std::string &s) const {
|
||||
template<class CONDITIONAL>
|
||||
void BayesNet<CONDITIONAL>::saveGraph(const std::string &s) const {
|
||||
ofstream of(s.c_str());
|
||||
of<< "digraph G{\n";
|
||||
BOOST_FOREACH(const_sharedConditional conditional,conditionals_) {
|
||||
|
@ -110,10 +108,10 @@ namespace gtsam {
|
|||
|
||||
/* ************************************************************************* */
|
||||
|
||||
template<class Conditional>
|
||||
typename BayesNet<Conditional>::sharedConditional
|
||||
BayesNet<Conditional>::operator[](Index key) const {
|
||||
const_iterator it = find_if(conditionals_.begin(), conditionals_.end(), boost::lambda::bind(&Conditional::key, *boost::lambda::_1) == key);
|
||||
template<class CONDITIONAL>
|
||||
typename BayesNet<CONDITIONAL>::sharedConditional
|
||||
BayesNet<CONDITIONAL>::operator[](Index key) const {
|
||||
const_iterator it = find_if(conditionals_.begin(), conditionals_.end(), boost::lambda::bind(&CONDITIONAL::key, *boost::lambda::_1) == key);
|
||||
if (it == conditionals_.end()) throw(invalid_argument((boost::format(
|
||||
"BayesNet::operator['%1%']: not found") % key).str()));
|
||||
return *it;
|
||||
|
|
|
@ -19,16 +19,14 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <list>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <boost/serialization/nvp.hpp>
|
||||
//#include <boost/serialization/list.hpp>
|
||||
//#include <boost/serialization/shared_ptr.hpp>
|
||||
|
||||
#include <gtsam/base/types.h>
|
||||
#include <gtsam/base/Testable.h>
|
||||
#include <gtsam/inference/Permutation.h>
|
||||
|
||||
#include <list>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <boost/serialization/nvp.hpp>
|
||||
|
||||
namespace gtsam {
|
||||
|
||||
/**
|
||||
|
@ -37,16 +35,16 @@ namespace gtsam {
|
|||
* corresponding to what is used for the "Conditional" template argument:
|
||||
* a SymbolicConditional, ConditionalProbabilityTable, or a GaussianConditional
|
||||
*/
|
||||
template<class Conditional>
|
||||
class BayesNet: public Testable<BayesNet<Conditional> > {
|
||||
template<class CONDITIONAL>
|
||||
class BayesNet: public Testable<BayesNet<CONDITIONAL> > {
|
||||
|
||||
public:
|
||||
|
||||
typedef typename boost::shared_ptr<BayesNet<Conditional> > shared_ptr;
|
||||
typedef typename boost::shared_ptr<BayesNet<CONDITIONAL> > shared_ptr;
|
||||
|
||||
/** We store shared pointers to Conditional densities */
|
||||
typedef typename boost::shared_ptr<Conditional> sharedConditional;
|
||||
typedef typename boost::shared_ptr<const Conditional> const_sharedConditional;
|
||||
typedef typename boost::shared_ptr<CONDITIONAL> sharedConditional;
|
||||
typedef typename boost::shared_ptr<const CONDITIONAL> const_sharedConditional;
|
||||
typedef typename std::list<sharedConditional> Conditionals;
|
||||
|
||||
typedef typename Conditionals::const_iterator iterator;
|
||||
|
@ -82,10 +80,10 @@ namespace gtsam {
|
|||
}
|
||||
|
||||
// push_back an entire Bayes net */
|
||||
void push_back(const BayesNet<Conditional> bn);
|
||||
void push_back(const BayesNet<CONDITIONAL> bn);
|
||||
|
||||
// push_front an entire Bayes net */
|
||||
void push_front(const BayesNet<Conditional> bn);
|
||||
void push_front(const BayesNet<CONDITIONAL> bn);
|
||||
|
||||
/**
|
||||
* pop_front: remove node at the bottom, used in marginalization
|
||||
|
@ -117,13 +115,13 @@ namespace gtsam {
|
|||
sharedConditional& front() { return conditionals_.front(); }
|
||||
|
||||
/** return last node in ordering */
|
||||
boost::shared_ptr<const Conditional> front() const { return conditionals_.front(); }
|
||||
boost::shared_ptr<const CONDITIONAL> front() const { return conditionals_.front(); }
|
||||
|
||||
/** return last node in ordering */
|
||||
sharedConditional& back() { return conditionals_.back(); }
|
||||
|
||||
/** return last node in ordering */
|
||||
boost::shared_ptr<const Conditional> back() const { return conditionals_.back(); }
|
||||
boost::shared_ptr<const CONDITIONAL> back() const { return conditionals_.back(); }
|
||||
|
||||
/** return iterators. FD: breaks encapsulation? */
|
||||
inline const_iterator const begin() const {return conditionals_.begin();}
|
||||
|
|
|
@ -57,7 +57,7 @@ namespace gtsam {
|
|||
weak_ptr parent_;
|
||||
std::list<shared_ptr> children_;
|
||||
std::list<Index> separator_; /** separator keys */
|
||||
typename Conditional::FactorType::shared_ptr cachedFactor_;
|
||||
typename Conditional::Factor::shared_ptr cachedFactor_;
|
||||
|
||||
friend class BayesTree<Conditional>;
|
||||
|
||||
|
@ -87,7 +87,7 @@ namespace gtsam {
|
|||
const std::list<shared_ptr>& children() const { return children_; }
|
||||
|
||||
/** Reference the cached factor */
|
||||
typename Conditional::FactorType::shared_ptr& cachedFactor() { return cachedFactor_; }
|
||||
typename Conditional::Factor::shared_ptr& cachedFactor() { return cachedFactor_; }
|
||||
|
||||
/** The size of subtree rooted at this clique, i.e., nr of Cliques */
|
||||
size_t treeSize() const;
|
||||
|
|
|
@ -48,7 +48,7 @@ protected:
|
|||
public:
|
||||
|
||||
/** convenience typename for a shared pointer to this class */
|
||||
typedef gtsam::Factor FactorType;
|
||||
typedef gtsam::Factor Factor;
|
||||
typedef boost::shared_ptr<Conditional> shared_ptr;
|
||||
typedef Factor::iterator iterator;
|
||||
typedef Factor::const_iterator const_iterator;
|
||||
|
|
|
@ -33,6 +33,7 @@
|
|||
#include <gtsam/inference/inference.h>
|
||||
#include <gtsam/inference/FactorGraph-inl.h>
|
||||
#include <gtsam/inference/BayesNet-inl.h>
|
||||
#include <gtsam/inference/Conditional.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
|
Loading…
Reference in New Issue