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());
60  it != std::make_reverse_iterator(begin()); ++it) {
61  const DiscreteConditional::shared_ptr& conditional = *it;
62  // Sample the conditional only if value for j not already in result
63  const Key j = conditional->firstFrontalKey();
64  if (result.count(j) == 0) {
65  conditional->sampleInPlace(&result);
66  }
67  }
68  return result;
69 }
70 
71 /* *********************************************************************** */
73  const KeyFormatter& keyFormatter,
74  const DiscreteFactor::Names& names) const {
75  using std::endl;
76  std::stringstream ss;
77  ss << "`DiscreteBayesNet` of size " << size() << endl << endl;
78  for (const DiscreteConditional::shared_ptr& conditional : *this)
79  ss << conditional->markdown(keyFormatter, names) << endl;
80  return ss.str();
81 }
82 
83 /* *********************************************************************** */
84 std::string DiscreteBayesNet::html(const KeyFormatter& keyFormatter,
85  const DiscreteFactor::Names& names) const {
86  using std::endl;
87  std::stringstream ss;
88  ss << "<div><p><tt>DiscreteBayesNet</tt> of size " << size() << "</p>";
89  for (const DiscreteConditional::shared_ptr& conditional : *this)
90  ss << conditional->html(keyFormatter, names) << endl;
91  return ss.str();
92 }
93 
94 /* ************************************************************************* */
95 } // 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
different_sigmas::values
HybridValues values
Definition: testHybridBayesNet.cpp:245
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
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
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:72
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: SFMdata.h:40
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
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:84
gtsam::FactorGraph< DiscreteConditional >::begin
const_iterator begin() const
Definition: FactorGraph.h:339
gtsam::tol
const G double tol
Definition: Group.h:79
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:02:13