DiscreteBayesNet.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 
22 
23 namespace gtsam {
24 
25 // Instantiate base class
26 template class FactorGraph<DiscreteConditional>;
27 
28 /* ************************************************************************* */
29 bool DiscreteBayesNet::equals(const This& bn, double tol) const {
30  return Base::equals(bn, tol);
31 }
32 
33 /* ************************************************************************* */
35  // evaluate all conditionals and add
36  double result = 0.0;
37  for (const DiscreteConditional::shared_ptr& conditional : *this)
38  result += conditional->logProbability(values);
39  return result;
40 }
41 
42 /* ************************************************************************* */
44  // evaluate all conditionals and multiply
45  double result = 1.0;
46  for (const DiscreteConditional::shared_ptr& conditional : *this)
47  result *= (*conditional)(values);
48  return result;
49 }
50 
51 /* ************************************************************************* */
54  return sample(result);
55 }
56 
58  // sample each node in turn in topological sort order (parents first)
59  for (auto it = std::make_reverse_iterator(end()); it != std::make_reverse_iterator(begin()); ++it) {
60  (*it)->sampleInPlace(&result);
61  }
62  return result;
63 }
64 
65 /* *********************************************************************** */
67  const KeyFormatter& keyFormatter,
68  const DiscreteFactor::Names& names) const {
69  using std::endl;
70  std::stringstream ss;
71  ss << "`DiscreteBayesNet` of size " << size() << endl << endl;
72  for (const DiscreteConditional::shared_ptr& conditional : *this)
73  ss << conditional->markdown(keyFormatter, names) << endl;
74  return ss.str();
75 }
76 
77 /* *********************************************************************** */
78 std::string DiscreteBayesNet::html(const KeyFormatter& keyFormatter,
79  const DiscreteFactor::Names& names) const {
80  using std::endl;
81  std::stringstream ss;
82  ss << "<div><p><tt>DiscreteBayesNet</tt> of size " << size() << "</p>";
83  for (const DiscreteConditional::shared_ptr& conditional : *this)
84  ss << conditional->html(keyFormatter, names) << endl;
85  return ss.str();
86 }
87 
88 /* ************************************************************************* */
89 } // namespace gtsam
DiscreteBayesNet.h
gtsam::DiscreteBayesNet::sample
DiscreteValues sample() const
do ancestral sampling
Definition: DiscreteBayesNet.cpp:52
gtsam::DiscreteBayesNet::equals
bool equals(const This &bn, double tol=1e-9) const
Definition: DiscreteBayesNet.cpp:29
DiscreteConditional.h
result
Values result
Definition: OdometryOptimize.cpp:8
FactorGraph-inst.h
Factor Graph Base Class.
ss
static std::stringstream ss
Definition: testBTree.cpp:31
gtsam::DiscreteBayesNet::logProbability
double logProbability(const DiscreteValues &values) const
Definition: DiscreteBayesNet.cpp:34
gtsam::DiscreteBayesNet
Definition: DiscreteBayesNet.h:38
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::FactorGraph< DiscreteConditional >::equals
bool equals(const This &fg, double tol=1e-9) const
Check equality up to tolerance.
Definition: FactorGraph-inst.h:50
gtsam::DiscreteBayesNet::markdown
std::string markdown(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const DiscreteFactor::Names &names={}) const
Render as markdown tables.
Definition: DiscreteBayesNet.cpp:66
gtsam::DiscreteBayesNet::evaluate
double evaluate(const DiscreteValues &values) const
Definition: DiscreteBayesNet.cpp:43
gtsam::DiscreteConditional::shared_ptr
std::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition: DiscreteConditional.h:43
gtsam::FactorGraph< DiscreteConditional >::size
size_t size() const
Definition: FactorGraph.h:297
process_shonan_timing_results.names
dictionary names
Definition: process_shonan_timing_results.py:175
gtsam
traits
Definition: chartTesting.h:28
make_reverse_iterator
std::reverse_iterator< Iterator > make_reverse_iterator(Iterator i)
Definition: stl_iterators.cpp:16
gtsam::DiscreteFactor::Names
DiscreteValues::Names Names
Translation table from values to strings.
Definition: DiscreteFactor.h:121
gtsam::DiscreteValues
Definition: DiscreteValues.h:34
leaf::values
leaf::MyValues values
gtsam::FactorGraph< DiscreteConditional >::end
const_iterator end() const
Definition: FactorGraph.h:342
gtsam::DiscreteBayesNet::html
std::string html(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const DiscreteFactor::Names &names={}) const
Render as html tables.
Definition: DiscreteBayesNet.cpp:78
gtsam::FactorGraph< DiscreteConditional >::begin
const_iterator begin() const
Definition: FactorGraph.h:339
gtsam::tol
const G double tol
Definition: Group.h:79


gtsam
Author(s):
autogenerated on Tue Jun 25 2024 03:00:48