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 
28 
29 using std::vector;
30 using std::string;
31 using std::map;
32 
33 namespace gtsam {
34 
35  // Instantiate base classes
36  template class FactorGraph<DiscreteFactor>;
37  template class EliminateableFactorGraph<DiscreteFactorGraph>;
38 
39  /* ************************************************************************ */
40  bool DiscreteFactorGraph::equals(const This& fg, double tol) const {
41  return Base::equals(fg, tol);
42  }
43 
44  /* ************************************************************************ */
46  KeySet keys;
47  for (const sharedFactor& factor : *this) {
48  if (factor) keys.insert(factor->begin(), factor->end());
49  }
50  return keys;
51  }
52 
53  /* ************************************************************************ */
56  for (auto&& factor : *this) {
57  if (auto p = std::dynamic_pointer_cast<DiscreteFactor>(factor)) {
58  DiscreteKeys factor_keys = p->discreteKeys();
59  result.insert(result.end(), factor_keys.begin(), factor_keys.end());
60  }
61  }
62 
63  return result;
64  }
65 
66  /* ************************************************************************ */
69  for (auto it = this->begin(); it != this->end(); ++it) {
70  if (*it) {
71  if (result) {
72  result = result->multiply(*it);
73  } else {
74  // Assign to the first non-null factor
75  result = *it;
76  }
77  }
78  }
79  return result;
80  }
81 
82  /* ************************************************************************ */
84  double product = 1.0;
85  for (const sharedFactor& factor : factors_) {
86  if (factor) product *= (*factor)(values);
87  }
88  return product;
89  }
90 
91  /* ************************************************************************ */
92  void DiscreteFactorGraph::print(const string& s,
93  const KeyFormatter& formatter) const {
94  std::cout << s << std::endl;
95  std::cout << "size: " << size() << std::endl;
96  for (size_t i = 0; i < factors_.size(); i++) {
97  std::stringstream ss;
98  ss << "factor " << i << ": ";
99  if (factors_[i] != nullptr) factors_[i]->print(ss.str(), formatter);
100  }
101  }
102 
103 // /* ************************************************************************* */
104 // void DiscreteFactorGraph::permuteWithInverse(
105 // const Permutation& inversePermutation) {
106 // for(const sharedFactor& factor: factors_) {
107 // if(factor)
108 // factor->permuteWithInverse(inversePermutation);
109 // }
110 // }
111 //
112 // /* ************************************************************************* */
113 // void DiscreteFactorGraph::reduceWithInverse(
114 // const internal::Reduction& inverseReduction) {
115 // for(const sharedFactor& factor: factors_) {
116 // if(factor)
117 // factor->reduceWithInverse(inverseReduction);
118 // }
119 // }
120 
128  const DiscreteFactorGraph& factors) {
129  // PRODUCT: multiply all factors
130  gttic(product);
132  gttoc(product);
133 
134  // Max over all the potentials by pretending all keys are frontal:
135  auto denominator = product->max(product->size());
136 
137  // Normalize the product factor to prevent underflow.
138  product = product->operator/(denominator);
139 
140  return product;
141  }
142 
143  /* ************************************************************************ */
144  // Alternate eliminate function for MPE
145  std::pair<DiscreteConditional::shared_ptr, DiscreteFactor::shared_ptr> //
147  const Ordering& frontalKeys) {
149 
150  // max out frontals, this is the factor on the separator
151  gttic(max);
152  DiscreteFactor::shared_ptr max = product->max(frontalKeys);
153  gttoc(max);
154 
155  // Ordering keys for the conditional so that frontalKeys are really in front
156  DiscreteKeys orderedKeys;
157  for (auto&& key : frontalKeys)
158  orderedKeys.emplace_back(key, product->cardinality(key));
159  for (auto&& key : max->keys())
160  orderedKeys.emplace_back(key, product->cardinality(key));
161 
162  // Make lookup with product
163  gttic(lookup);
164  size_t nrFrontals = frontalKeys.size();
165  auto lookup = std::make_shared<DiscreteLookupTable>(
166  nrFrontals, orderedKeys, product->toDecisionTreeFactor());
167  gttoc(lookup);
168 
169  return {std::dynamic_pointer_cast<DiscreteConditional>(lookup), max};
170  }
171 
172  /* ************************************************************************ */
173  // sumProduct is just an alias for regular eliminateSequential.
175  OptionalOrderingType orderingType) const {
176  gttic(DiscreteFactorGraph_sumProduct);
177  auto bayesNet = eliminateSequential(orderingType);
178  return *bayesNet;
179  }
180 
182  const Ordering& ordering) const {
183  gttic(DiscreteFactorGraph_sumProduct);
185  return *bayesNet;
186  }
187 
188  /* ************************************************************************ */
189  // The max-product solution below is a bit clunky: the elimination machinery
190  // does not allow for differently *typed* versions of elimination, so we
191  // eliminate into a Bayes Net using the special eliminate function above, and
192  // then create the DiscreteLookupDAG after the fact, in linear time.
193 
195  OptionalOrderingType orderingType) const {
196  gttic(DiscreteFactorGraph_maxProduct);
197  auto bayesNet = eliminateSequential(orderingType, EliminateForMPE);
199  }
200 
202  const Ordering& ordering) const {
203  gttic(DiscreteFactorGraph_maxProduct);
206  }
207 
208  /* ************************************************************************ */
210  OptionalOrderingType orderingType) const {
211  gttic(DiscreteFactorGraph_optimize);
212  DiscreteLookupDAG dag = maxProduct(orderingType);
213  return dag.argmax();
214  }
215 
217  gttic(DiscreteFactorGraph_optimize);
219  return dag.argmax();
220  }
221 
222  /* ************************************************************************ */
223  std::pair<DiscreteConditional::shared_ptr, DiscreteFactor::shared_ptr> //
225  const Ordering& frontalKeys) {
227 
228  // sum out frontals, this is the factor on the separator
229  gttic(sum);
230  DiscreteFactor::shared_ptr sum = product->sum(frontalKeys);
231  gttoc(sum);
232 
233  // Ordering keys for the conditional so that frontalKeys are really in front
234  Ordering orderedKeys;
235  orderedKeys.insert(orderedKeys.end(), frontalKeys.begin(),
236  frontalKeys.end());
237  orderedKeys.insert(orderedKeys.end(), sum->keys().begin(),
238  sum->keys().end());
239 
240  // now divide product/sum to get conditional
241  gttic(divide);
242  auto conditional = std::make_shared<DiscreteConditional>(
243  product->toDecisionTreeFactor(), sum->toDecisionTreeFactor(),
244  orderedKeys);
245  gttoc(divide);
246 
247  return {conditional, sum};
248  }
249 
250  /* ************************************************************************ */
252  const KeyFormatter& keyFormatter,
253  const DiscreteFactor::Names& names) const {
254  using std::endl;
255  std::stringstream ss;
256  ss << "`DiscreteFactorGraph` of size " << size() << endl << endl;
257  for (size_t i = 0; i < factors_.size(); i++) {
258  ss << "factor " << i << ":\n";
259  ss << factors_[i]->markdown(keyFormatter, names) << endl;
260  }
261  return ss.str();
262  }
263 
264  /* ************************************************************************ */
265  string DiscreteFactorGraph::html(const KeyFormatter& keyFormatter,
266  const DiscreteFactor::Names& names) const {
267  using std::endl;
268  std::stringstream ss;
269  ss << "<div><p><tt>DiscreteFactorGraph</tt> of size " << size() << "</p>";
270  for (size_t i = 0; i < factors_.size(); i++) {
271  ss << "<p>factor " << i << ":</p>";
272  ss << factors_[i]->html(keyFormatter, names) << endl;
273  }
274  return ss.str();
275  }
276 
277  /* ************************************************************************ */
278  } // namespace gtsam
gtsam::DiscreteFactorGraph::discreteKeys
DiscreteKeys discreteKeys() const
Return the DiscreteKeys in this factor graph.
Definition: DiscreteFactorGraph.cpp:54
gttoc
#define gttoc(label)
Definition: timing.h:296
gtsam::EliminateDiscrete
std::pair< DiscreteConditional::shared_ptr, DiscreteFactor::shared_ptr > EliminateDiscrete(const DiscreteFactorGraph &factors, const Ordering &frontalKeys)
Main elimination function for DiscreteFactorGraph.
Definition: DiscreteFactorGraph.cpp:224
gtsam::EliminateForMPE
std::pair< DiscreteConditional::shared_ptr, DiscreteFactor::shared_ptr > EliminateForMPE(const DiscreteFactorGraph &factors, const Ordering &frontalKeys)
Alternate elimination function for that creates non-normalized lookup tables.
Definition: DiscreteFactorGraph.cpp:146
gtsam::EliminateableFactorGraph< DiscreteFactorGraph >::eliminateSequential
std::shared_ptr< BayesNetType > eliminateSequential(OptionalOrderingType orderingType={}, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex={}) const
Definition: EliminateableFactorGraph-inst.h:29
gtsam::FactorGraph< DiscreteFactor >::factors_
FastVector< sharedFactor > factors_
Definition: FactorGraph.h:92
gtsam::DiscreteFactorGraph
Definition: DiscreteFactorGraph.h:99
gtsam::DiscreteFactorGraph::sumProduct
DiscreteBayesNet sumProduct(OptionalOrderingType orderingType={}) const
Implement the sum-product algorithm.
Definition: DiscreteFactorGraph.cpp:174
s
RealScalar s
Definition: level1_cplx_impl.h:126
DiscreteFactorGraph.h
gtsam::DiscreteFactorGraph::keys
KeySet keys() const
Definition: DiscreteFactorGraph.cpp:45
simple_graph::factors
const GaussianFactorGraph factors
Definition: testJacobianFactor.cpp:213
EliminateableFactorGraph-inst.h
DiscreteLookupDAG.h
formatter
const KeyFormatter & formatter
Definition: treeTraversal-inst.h:204
gtsam::DiscreteLookupDAG
Definition: DiscreteLookupDAG.h:90
different_sigmas::values
HybridValues values
Definition: testHybridBayesNet.cpp:245
gtsam::DiscreteKeys
DiscreteKeys is a set of keys that can be assembled using the & operator.
Definition: DiscreteKey.h:41
gtsam::DiscreteFactorGraph::print
void print(const std::string &s="DiscreteFactorGraph", const KeyFormatter &formatter=DefaultKeyFormatter) const override
print
Definition: DiscreteFactorGraph.cpp:92
gtsam::FastSet< Key >
DiscreteConditional.h
result
Values result
Definition: OdometryOptimize.cpp:8
gtsam::DiscreteFactorGraph::maxProduct
DiscreteLookupDAG maxProduct(OptionalOrderingType orderingType={}) const
Implement the max-product algorithm.
Definition: DiscreteFactorGraph.cpp:194
FactorGraph-inst.h
Factor Graph Base Class.
pruning_fixture::factor
DecisionTreeFactor factor(D &C &B &A, "0.0 0.0 0.0 0.60658897 0.61241912 0.61241969 0.61247685 0.61247742 0.0 " "0.0 0.0 0.99995287 1.0 1.0 1.0 1.0")
ss
static std::stringstream ss
Definition: testBTree.cpp:31
different_sigmas::bayesNet
const HybridBayesNet bayesNet
Definition: testHybridBayesNet.cpp:242
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
DiscreteJunctionTree.h
gtsam::FactorGraph< DiscreteFactor >::equals
bool equals(const This &fg, double tol=1e-9) const
Check equality up to tolerance.
Definition: FactorGraph-inst.h:50
gtsam::DiscreteProduct
static DiscreteFactor::shared_ptr DiscreteProduct(const DiscreteFactorGraph &factors)
Multiply all the factors.
Definition: DiscreteFactorGraph.cpp:127
gtsam::DiscreteFactorGraph::equals
bool equals(const This &fg, double tol=1e-9) const
Definition: DiscreteFactorGraph.cpp:40
gtsam::DiscreteFactorGraph::markdown
std::string markdown(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const DiscreteFactor::Names &names={}) const
Render as markdown tables.
Definition: DiscreteFactorGraph.cpp:251
gtsam::DiscreteFactorGraph::product
DiscreteFactor::shared_ptr product() const
Definition: DiscreteFactorGraph.cpp:67
DiscreteEliminationTree.h
DiscreteBayesTree.h
Discrete Bayes Tree, the result of eliminating a DiscreteJunctionTree.
ordering
static enum @1096 ordering
key
const gtsam::Symbol key('X', 0)
gtsam::FactorGraph< DiscreteFactor >::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
gtsam::DiscreteFactor::shared_ptr
std::shared_ptr< DiscreteFactor > shared_ptr
shared_ptr to this class
Definition: DiscreteFactor.h:45
gtsam::DiscreteFactor::Names
DiscreteValues::Names Names
Translation table from values to strings.
Definition: DiscreteFactor.h:172
gtsam::DiscreteValues
Definition: DiscreteValues.h:34
gtsam::FactorGraph< DiscreteFactor >::end
const_iterator end() const
Definition: FactorGraph.h:342
gtsam::DiscreteValues::insert
std::pair< iterator, bool > insert(const value_type &value)
Definition: DiscreteValues.h:68
p
float * p
Definition: Tutorial_Map_using.cpp:9
gtsam::DiscreteFactorGraph::html
std::string html(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const DiscreteFactor::Names &names={}) const
Render as html tables.
Definition: DiscreteFactorGraph.cpp:265
product
void product(const MatrixType &m)
Definition: product.h:20
gtsam::FactorGraph< DiscreteFactor >::begin
const_iterator begin() const
Definition: FactorGraph.h:339
gtsam::tol
const G double tol
Definition: Group.h:79
gtsam::DiscreteLookupDAG::argmax
DiscreteValues argmax(DiscreteValues given=DiscreteValues()) const
argmax by back-substitution, optionally given certain variables.
Definition: DiscreteLookupDAG.cpp:121
gtsam::DiscreteFactorGraph::optimize
DiscreteValues optimize(OptionalOrderingType orderingType={}) const
Find the maximum probable explanation (MPE) by doing max-product.
Definition: DiscreteFactorGraph.cpp:209
gtsam::FactorGraph< DiscreteFactor >::sharedFactor
std::shared_ptr< DiscreteFactor > sharedFactor
Shared pointer to a factor.
Definition: FactorGraph.h:62
max
#define max(a, b)
Definition: datatypes.h:20
gtsam::DiscreteLookupDAG::FromBayesNet
static DiscreteLookupDAG FromBayesNet(const DiscreteBayesNet &bayesNet)
Create from BayesNet with LookupTables.
Definition: DiscreteLookupDAG.cpp:106
gtsam::Ordering
Definition: inference/Ordering.h:33
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
gtsam::DiscreteFactorGraph::operator()
double operator()(const DiscreteValues &values) const
Definition: DiscreteFactorGraph.cpp:83
gttic
#define gttic(label)
Definition: timing.h:295


gtsam
Author(s):
autogenerated on Tue Jan 7 2025 04:02:11