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 (const auto& factor : *this) {
70  if (factor) {
71  result = result ? result->multiply(factor) : factor;
72  }
73  }
74  return result;
75  }
76 
77  /* ************************************************************************ */
79  double product = 1.0;
80  for (const sharedFactor& factor : factors_) {
81  if (factor) product *= (*factor)(values);
82  }
83  return product;
84  }
85 
86  /* ************************************************************************ */
87  void DiscreteFactorGraph::print(const string& s,
88  const KeyFormatter& formatter) const {
89  std::cout << s << std::endl;
90  std::cout << "size: " << size() << std::endl;
91  for (size_t i = 0; i < factors_.size(); i++) {
92  std::stringstream ss;
93  ss << "factor " << i << ": ";
94  if (factors_[i] != nullptr) factors_[i]->print(ss.str(), formatter);
95  }
96  }
97 
98 // /* ************************************************************************* */
99 // void DiscreteFactorGraph::permuteWithInverse(
100 // const Permutation& inversePermutation) {
101 // for(const sharedFactor& factor: factors_) {
102 // if(factor)
103 // factor->permuteWithInverse(inversePermutation);
104 // }
105 // }
106 //
107 // /* ************************************************************************* */
108 // void DiscreteFactorGraph::reduceWithInverse(
109 // const internal::Reduction& inverseReduction) {
110 // for(const sharedFactor& factor: factors_) {
111 // if(factor)
112 // factor->reduceWithInverse(inverseReduction);
113 // }
114 // }
115 
116  /* ************************************************************************ */
118  return product()->scale();
119  }
120 
121  /* ************************************************************************ */
122  // Alternate eliminate function for MPE
123  std::pair<DiscreteConditional::shared_ptr, DiscreteFactor::shared_ptr> //
125  const Ordering& frontalKeys) {
126  DiscreteFactor::shared_ptr product = factors.scaledProduct();
127 
128  // max out frontals, this is the factor on the separator
129  gttic(max);
130  DiscreteFactor::shared_ptr max = product->max(frontalKeys);
131  gttoc(max);
132 
133  // Ordering keys for the conditional so that frontalKeys are really in front
134  DiscreteKeys orderedKeys;
135  for (auto&& key : frontalKeys)
136  orderedKeys.emplace_back(key, product->cardinality(key));
137  for (auto&& key : max->keys())
138  orderedKeys.emplace_back(key, product->cardinality(key));
139 
140  // Make lookup with product
141  gttic(lookup);
142  size_t nrFrontals = frontalKeys.size();
143  auto lookup = std::make_shared<DiscreteLookupTable>(
144  nrFrontals, orderedKeys, product->toDecisionTreeFactor());
145  gttoc(lookup);
146 
147  return {std::dynamic_pointer_cast<DiscreteConditional>(lookup), max};
148  }
149 
150  /* ************************************************************************ */
151  // sumProduct is just an alias for regular eliminateSequential.
153  OptionalOrderingType orderingType) const {
154  gttic(DiscreteFactorGraph_sumProduct);
155  auto bayesNet = eliminateSequential(orderingType);
156  return *bayesNet;
157  }
158 
160  const Ordering& ordering) const {
161  gttic(DiscreteFactorGraph_sumProduct);
163  return *bayesNet;
164  }
165 
166  /* ************************************************************************ */
167  // The max-product solution below is a bit clunky: the elimination machinery
168  // does not allow for differently *typed* versions of elimination, so we
169  // eliminate into a Bayes Net using the special eliminate function above, and
170  // then create the DiscreteLookupDAG after the fact, in linear time.
171 
173  OptionalOrderingType orderingType) const {
174  gttic(DiscreteFactorGraph_maxProduct);
175  auto bayesNet = eliminateSequential(orderingType, EliminateForMPE);
177  }
178 
180  const Ordering& ordering) const {
181  gttic(DiscreteFactorGraph_maxProduct);
184  }
185 
186  /* ************************************************************************ */
188  OptionalOrderingType orderingType) const {
189  gttic(DiscreteFactorGraph_optimize);
190  DiscreteLookupDAG dag = maxProduct(orderingType);
191  return dag.argmax();
192  }
193 
195  gttic(DiscreteFactorGraph_optimize);
197  return dag.argmax();
198  }
199 
200  /* ************************************************************************ */
201  std::pair<DiscreteConditional::shared_ptr, DiscreteFactor::shared_ptr> //
203  const Ordering& frontalKeys) {
204  gttic(product);
205  // `product` is scaled later to prevent underflow.
206  DiscreteFactor::shared_ptr product = factors.scaledProduct();
207  gttoc(product);
208 
209  // sum out frontals, this is the factor on the separator
210  gttic(sum);
211  DiscreteFactor::shared_ptr sum = product->sum(frontalKeys);
212  gttoc(sum);
213 
214  // Ordering keys for the conditional so that frontalKeys are really in front
215  Ordering orderedKeys;
216  orderedKeys.insert(orderedKeys.end(), frontalKeys.begin(),
217  frontalKeys.end());
218  orderedKeys.insert(orderedKeys.end(), sum->keys().begin(),
219  sum->keys().end());
220 
221  // now divide product/sum to get conditional
222  gttic(divide);
223  auto conditional = std::make_shared<DiscreteConditional>(
224  product->toDecisionTreeFactor(), sum->toDecisionTreeFactor(),
225  orderedKeys);
226  gttoc(divide);
227 
228  return {conditional, sum};
229  }
230 
231  /* ************************************************************************ */
233  const KeyFormatter& keyFormatter,
234  const DiscreteFactor::Names& names) const {
235  using std::endl;
236  std::stringstream ss;
237  ss << "`DiscreteFactorGraph` of size " << size() << endl << endl;
238  for (size_t i = 0; i < factors_.size(); i++) {
239  ss << "factor " << i << ":\n";
240  ss << factors_[i]->markdown(keyFormatter, names) << endl;
241  }
242  return ss.str();
243  }
244 
245  /* ************************************************************************ */
246  string DiscreteFactorGraph::html(const KeyFormatter& keyFormatter,
247  const DiscreteFactor::Names& names) const {
248  using std::endl;
249  std::stringstream ss;
250  ss << "<div><p><tt>DiscreteFactorGraph</tt> of size " << size() << "</p>";
251  for (size_t i = 0; i < factors_.size(); i++) {
252  ss << "<p>factor " << i << ":</p>";
253  ss << factors_[i]->html(keyFormatter, names) << endl;
254  }
255  return ss.str();
256  }
257 
258  /* ************************************************************************ */
259  } // 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:327
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:202
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:124
gtsam::EliminateableFactorGraph< DiscreteFactorGraph >::eliminateSequential
std::shared_ptr< BayesNetType > eliminateSequential(OptionalOrderingType orderingType={}, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex={}) const
Definition: EliminateableFactorGraph-inst.h:29
asia::bayesNet
static const DiscreteBayesNet bayesNet
Definition: testDiscreteSearch.cpp:30
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:152
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:247
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:87
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:172
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
gtsam::DiscreteFactorGraph::scaledProduct
DiscreteFactor::shared_ptr scaledProduct() const
Return product of all factors as a single factor, which is scaled by the max value to prevent underfl...
Definition: DiscreteFactorGraph.cpp:117
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::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:232
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:190
gtsam::DiscreteValues
Definition: DiscreteValues.h:34
gtsam::DiscreteValues::insert
std::pair< iterator, bool > insert(const value_type &value)
Definition: DiscreteValues.h:71
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:246
product
void product(const MatrixType &m)
Definition: product.h:20
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:187
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:78
gttic
#define gttic(label)
Definition: timing.h:326


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