DiscreteFactorGraph.cpp
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
19 //#define ENABLE_TIMING
27 #include <boost/make_shared.hpp>
28 
29 namespace gtsam {
30 
31  // Instantiate base classes
32  template class FactorGraph<DiscreteFactor>;
33  template class EliminateableFactorGraph<DiscreteFactorGraph>;
34 
35  /* ************************************************************************* */
36  bool DiscreteFactorGraph::equals(const This& fg, double tol) const
37  {
38  return Base::equals(fg, tol);
39  }
40 
41  /* ************************************************************************* */
43  KeySet keys;
44  for(const sharedFactor& factor: *this)
45  if (factor) keys.insert(factor->begin(), factor->end());
46  return keys;
47  }
48 
49  /* ************************************************************************* */
52  for(const sharedFactor& factor: *this)
53  if (factor) result = (*factor) * result;
54  return result;
55  }
56 
57  /* ************************************************************************* */
59  const DiscreteFactor::Values &values) const {
60  double product = 1.0;
61  for( const sharedFactor& factor: factors_ )
62  product *= (*factor)(values);
63  return product;
64  }
65 
66  /* ************************************************************************* */
67  void DiscreteFactorGraph::print(const std::string& s,
68  const KeyFormatter& formatter) const {
69  std::cout << s << std::endl;
70  std::cout << "size: " << size() << std::endl;
71  for (size_t i = 0; i < factors_.size(); i++) {
72  std::stringstream ss;
73  ss << "factor " << i << ": ";
74  if (factors_[i] != nullptr) factors_[i]->print(ss.str(), formatter);
75  }
76  }
77 
78 // /* ************************************************************************* */
79 // void DiscreteFactorGraph::permuteWithInverse(
80 // const Permutation& inversePermutation) {
81 // for(const sharedFactor& factor: factors_) {
82 // if(factor)
83 // factor->permuteWithInverse(inversePermutation);
84 // }
85 // }
86 //
87 // /* ************************************************************************* */
88 // void DiscreteFactorGraph::reduceWithInverse(
89 // const internal::Reduction& inverseReduction) {
90 // for(const sharedFactor& factor: factors_) {
91 // if(factor)
92 // factor->reduceWithInverse(inverseReduction);
93 // }
94 // }
95 
96  /* ************************************************************************* */
98  {
99  gttic(DiscreteFactorGraph_optimize);
100  return BaseEliminateable::eliminateSequential()->optimize();
101  }
102 
103  /* ************************************************************************* */
104  std::pair<DiscreteConditional::shared_ptr, DecisionTreeFactor::shared_ptr> //
106 
107  // PRODUCT: multiply all factors
108  gttic(product);
110  for(const DiscreteFactor::shared_ptr& factor: factors)
111  product = (*factor) * product;
112  gttoc(product);
113 
114  // sum out frontals, this is the factor on the separator
115  gttic(sum);
116  DecisionTreeFactor::shared_ptr sum = product.sum(frontalKeys);
117  gttoc(sum);
118 
119  // Ordering keys for the conditional so that frontalKeys are really in front
120  Ordering orderedKeys;
121  orderedKeys.insert(orderedKeys.end(), frontalKeys.begin(), frontalKeys.end());
122  orderedKeys.insert(orderedKeys.end(), sum->keys().begin(), sum->keys().end());
123 
124  // now divide product/sum to get conditional
125  gttic(divide);
126  DiscreteConditional::shared_ptr cond(new DiscreteConditional(product, *sum, orderedKeys));
127  gttoc(divide);
128 
129  return std::make_pair(cond, sum);
130  }
131 
132 /* ************************************************************************* */
133 } // namespace
134 
friend const mpreal sum(const mpreal tab[], const unsigned long int n, int &status, mp_rnd_t rnd_mode)
Definition: mpreal.h:2381
bool equals(const This &fg, double tol=1e-9) const
DiscreteFactor::sharedValues optimize() const
leaf::MyValues values
GaussianFactorGraph factors(list_of(factor1)(factor2)(factor3))
const KeyFormatter & formatter
bool equals(const This &fg, double tol=1e-9) const
#define gttic(label)
Definition: timing.h:280
Values result
boost::shared_ptr< DiscreteFactor > sharedFactor
Shared pointer to a factor.
Definition: FactorGraph.h:98
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
const mpreal sum(const mpreal tab[], const unsigned long int n, int &status, mp_rnd_t mode=mpreal::get_default_rnd())
Definition: mpreal.h:2381
RealScalar s
Discrete Bayes Tree, the result of eliminating a DiscreteJunctionTree.
std::pair< DiscreteConditional::shared_ptr, DecisionTreeFactor::shared_ptr > EliminateDiscrete(const DiscreteFactorGraph &factors, const Ordering &frontalKeys)
boost::shared_ptr< This > shared_ptr
shared_ptr to this class
boost::shared_ptr< DiscreteFactor > shared_ptr
shared_ptr to this class
static std::stringstream ss
Definition: testBTree.cpp:33
traits
Definition: chartTesting.h:28
#define gttoc(label)
Definition: timing.h:281
boost::shared_ptr< Values > sharedValues
boost::shared_ptr< DecisionTreeFactor > shared_ptr
boost::shared_ptr< BayesNetType > eliminateSequential(OptionalOrderingType orderingType=boost::none, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex=boost::none) const
void print(const std::string &s="DiscreteFactorGraph", const KeyFormatter &formatter=DefaultKeyFormatter) const override
print
const G double tol
Definition: Group.h:83
double operator()(const DiscreteFactor::Values &values) const
FastVector< sharedFactor > factors_
Definition: FactorGraph.h:128
DecisionTreeFactor product() const


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:41:59