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
bool equals(const This &fg, double tol=1e-9) const
Check equality up to tolerance.
double evaluate(const DiscreteValues &values) const
leaf::MyValues values
std::string markdown(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const DiscreteFactor::Names &names={}) const
Render as markdown tables.
std::reverse_iterator< Iterator > make_reverse_iterator(Iterator i)
Values result
double logProbability(const DiscreteValues &values) const
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
static std::stringstream ss
Definition: testBTree.cpp:31
DiscreteValues::Names Names
Translation table from values to strings.
traits
Definition: chartTesting.h:28
std::string html(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const DiscreteFactor::Names &names={}) const
Render as html tables.
const G double tol
Definition: Group.h:86
std::shared_ptr< This > shared_ptr
shared_ptr to this class
Factor Graph Base Class.
DiscreteValues sample() const
do ancestral sampling
bool equals(const This &bn, double tol=1e-9) const


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:10