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 
23 
24 namespace gtsam {
25 
26 // Instantiate base class
27 template class FactorGraph<DiscreteConditional>;
28 
29 /* ************************************************************************* */
30 bool DiscreteBayesNet::equals(const This& bn, double tol) const {
31  return Base::equals(bn, tol);
32 }
33 
34 /* ************************************************************************* */
36  // evaluate all conditionals and add
37  double result = 0.0;
38  for (const DiscreteConditional::shared_ptr& conditional : *this)
39  result += conditional->logProbability(values);
40  return result;
41 }
42 
43 /* ************************************************************************* */
45  // evaluate all conditionals and multiply
46  double result = 1.0;
47  for (const DiscreteConditional::shared_ptr& conditional : *this)
48  result *= (*conditional)(values);
49  return result;
50 }
51 
52 /* ************************************************************************* */
55  return sample(result);
56 }
57 
59  // sample each node in turn in topological sort order (parents first)
60  for (auto it = std::make_reverse_iterator(end());
61  it != std::make_reverse_iterator(begin()); ++it) {
62  const DiscreteConditional::shared_ptr& conditional = *it;
63  // Sample the conditional only if value for j not already in result
64  const Key j = conditional->firstFrontalKey();
65  if (result.count(j) == 0) {
66  conditional->sampleInPlace(&result);
67  }
68  }
69  return result;
70 }
71 
72 /* ************************************************************************* */
73 // The implementation is: build the entire joint into one factor and then prune.
74 // TODO(Frank): This can be quite expensive *unless* the factors have already
75 // been pruned before. Another, possibly faster approach is branch and bound
76 // search to find the K-best leaves and then create a single pruned conditional.
78  size_t maxNrLeaves, const std::optional<double>& marginalThreshold,
79  DiscreteValues* fixedValues) const {
80  // Multiply into one big conditional. NOTE: possibly quite expensive.
81  DiscreteConditional joint;
82  for (const DiscreteConditional::shared_ptr& conditional : *this)
83  joint = joint * (*conditional);
84 
85  // Prune the joint. NOTE: imperative and, again, possibly quite expensive.
86  DiscreteConditional pruned = joint;
87  pruned.prune(maxNrLeaves);
88 
89  DiscreteValues deadModesValues;
90  // If we have a dead mode threshold and discrete variables left after pruning,
91  // then we run dead mode removal.
92  if (marginalThreshold && pruned.keys().size() > 0) {
94  for (auto dkey : pruned.discreteKeys()) {
95  const Vector probabilities = marginals.marginalProbabilities(dkey);
96 
97  int index = -1;
98  auto threshold = (probabilities.array() > *marginalThreshold);
99  // If atleast 1 value is non-zero, then we can find the index
100  // Else if all are zero, index would be set to 0 which is incorrect
101  if (!threshold.isZero()) {
102  threshold.maxCoeff(&index);
103  }
104 
105  if (index >= 0) {
106  deadModesValues.emplace(dkey.first, index);
107  }
108  }
109 
110  // Remove the modes (imperative)
111  pruned.removeDiscreteModes(deadModesValues);
112 
113  // Set the fixed values if requested.
114  if (fixedValues) {
115  *fixedValues = deadModesValues;
116  }
117  }
118 
119  // Return the resulting DiscreteBayesNet.
121  if (pruned.keys().size() > 0) result.push_back(pruned);
122  return result;
123 }
124 
125 /* *********************************************************************** */
127  const KeyFormatter& keyFormatter,
128  const DiscreteFactor::Names& names) const {
129  using std::endl;
130  std::stringstream ss;
131  ss << "`DiscreteBayesNet` of size " << size() << endl << endl;
132  for (const DiscreteConditional::shared_ptr& conditional : *this)
133  ss << conditional->markdown(keyFormatter, names) << endl;
134  return ss.str();
135 }
136 
137 /* *********************************************************************** */
138 std::string DiscreteBayesNet::html(const KeyFormatter& keyFormatter,
139  const DiscreteFactor::Names& names) const {
140  using std::endl;
141  std::stringstream ss;
142  ss << "<div><p><tt>DiscreteBayesNet</tt> of size " << size() << "</p>";
143  for (const DiscreteConditional::shared_ptr& conditional : *this)
144  ss << conditional->html(keyFormatter, names) << endl;
145  return ss.str();
146 }
147 
148 /* ************************************************************************* */
149 } // namespace gtsam
DiscreteBayesNet.h
gtsam::DiscreteBayesNet::sample
DiscreteValues sample() const
do ancestral sampling
Definition: DiscreteBayesNet.cpp:53
gtsam::DiscreteFactorGraph
Definition: DiscreteFactorGraph.h:99
gtsam::DiscreteBayesNet::equals
bool equals(const This &bn, double tol=1e-9) const
Definition: DiscreteBayesNet.cpp:30
different_sigmas::values
HybridValues values
Definition: testHybridBayesNet.cpp:247
DiscreteConditional.h
gtsam::Vector
Eigen::VectorXd Vector
Definition: Vector.h:39
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:35
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::DiscreteBayesNet::prune
DiscreteBayesNet prune(size_t maxNrLeaves, const std::optional< double > &marginalThreshold={}, DiscreteValues *fixedValues=nullptr) const
Prune the Bayes net.
Definition: DiscreteBayesNet.cpp:77
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:126
gtsam::DiscreteBayesNet::evaluate
double evaluate(const DiscreteValues &values) const
Definition: DiscreteBayesNet.cpp:44
gtsam::DiscreteMarginals
Definition: DiscreteMarginals.h:33
DiscreteMarginals.h
A class for computing marginals in a DiscreteFactorGraph.
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::DiscreteConditional
Definition: DiscreteConditional.h:37
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:190
gtsam::DiscreteValues
Definition: DiscreteValues.h:34
gtsam::Factor::keys
const KeyVector & keys() const
Access the factor's involved variable keys.
Definition: Factor.h:143
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:138
gtsam::FactorGraph< DiscreteConditional >::begin
const_iterator begin() const
Definition: FactorGraph.h:339
gtsam::tol
const G double tol
Definition: Group.h:79
gtsam::DiscreteFactor::discreteKeys
DiscreteKeys discreteKeys() const
Return all the discrete keys associated with this factor.
Definition: DiscreteFactor.cpp:37
marginals
Marginals marginals(graph, result)
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
gtsam::DiscreteConditional::removeDiscreteModes
void removeDiscreteModes(const DiscreteValues &given)
Remove the discrete modes whose assignments are given to us. Only applies to discrete conditionals.
Definition: DiscreteConditional.cpp:503
gtsam::DiscreteConditional::prune
virtual void prune(size_t maxNrAssignments)
Prune the conditional.
Definition: DiscreteConditional.cpp:496


gtsam
Author(s):
autogenerated on Wed Mar 19 2025 03:01:36