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 sharedFactor& factor : *this) {
70  if (factor) result = (*factor) * result;
71  }
72  return result;
73  }
74 
75  /* ************************************************************************ */
77  double product = 1.0;
78  for (const sharedFactor& factor : factors_) {
79  if (factor) product *= (*factor)(values);
80  }
81  return product;
82  }
83 
84  /* ************************************************************************ */
85  void DiscreteFactorGraph::print(const string& s,
86  const KeyFormatter& formatter) const {
87  std::cout << s << std::endl;
88  std::cout << "size: " << size() << std::endl;
89  for (size_t i = 0; i < factors_.size(); i++) {
90  std::stringstream ss;
91  ss << "factor " << i << ": ";
92  if (factors_[i] != nullptr) factors_[i]->print(ss.str(), formatter);
93  }
94  }
95 
96 // /* ************************************************************************* */
97 // void DiscreteFactorGraph::permuteWithInverse(
98 // const Permutation& inversePermutation) {
99 // for(const sharedFactor& factor: factors_) {
100 // if(factor)
101 // factor->permuteWithInverse(inversePermutation);
102 // }
103 // }
104 //
105 // /* ************************************************************************* */
106 // void DiscreteFactorGraph::reduceWithInverse(
107 // const internal::Reduction& inverseReduction) {
108 // for(const sharedFactor& factor: factors_) {
109 // if(factor)
110 // factor->reduceWithInverse(inverseReduction);
111 // }
112 // }
113 
122  const DiscreteFactorGraph& factors) {
123  // PRODUCT: multiply all factors
124  gttic(product);
125  DecisionTreeFactor product = factors.product();
126  gttoc(product);
127 
128  // Max over all the potentials by pretending all keys are frontal:
129  auto normalization = product.max(product.size());
130 
131  // Normalize the product factor to prevent underflow.
132  product = product / (*normalization);
133 
134  return product;
135  }
136 
137  /* ************************************************************************ */
138  // Alternate eliminate function for MPE
139  std::pair<DiscreteConditional::shared_ptr, DiscreteFactor::shared_ptr> //
141  const Ordering& frontalKeys) {
143 
144  // max out frontals, this is the factor on the separator
145  gttic(max);
146  DecisionTreeFactor::shared_ptr max = product.max(frontalKeys);
147  gttoc(max);
148 
149  // Ordering keys for the conditional so that frontalKeys are really in front
150  DiscreteKeys orderedKeys;
151  for (auto&& key : frontalKeys)
152  orderedKeys.emplace_back(key, product.cardinality(key));
153  for (auto&& key : max->keys())
154  orderedKeys.emplace_back(key, product.cardinality(key));
155 
156  // Make lookup with product
157  gttic(lookup);
158  size_t nrFrontals = frontalKeys.size();
159  auto lookup =
160  std::make_shared<DiscreteLookupTable>(nrFrontals, orderedKeys, product);
161  gttoc(lookup);
162 
163  return {std::dynamic_pointer_cast<DiscreteConditional>(lookup), max};
164  }
165 
166  /* ************************************************************************ */
167  // sumProduct is just an alias for regular eliminateSequential.
169  OptionalOrderingType orderingType) const {
170  gttic(DiscreteFactorGraph_sumProduct);
171  auto bayesNet = eliminateSequential(orderingType);
172  return *bayesNet;
173  }
174 
176  const Ordering& ordering) const {
177  gttic(DiscreteFactorGraph_sumProduct);
179  return *bayesNet;
180  }
181 
182  /* ************************************************************************ */
183  // The max-product solution below is a bit clunky: the elimination machinery
184  // does not allow for differently *typed* versions of elimination, so we
185  // eliminate into a Bayes Net using the special eliminate function above, and
186  // then create the DiscreteLookupDAG after the fact, in linear time.
187 
189  OptionalOrderingType orderingType) const {
190  gttic(DiscreteFactorGraph_maxProduct);
191  auto bayesNet = eliminateSequential(orderingType, EliminateForMPE);
193  }
194 
196  const Ordering& ordering) const {
197  gttic(DiscreteFactorGraph_maxProduct);
200  }
201 
202  /* ************************************************************************ */
204  OptionalOrderingType orderingType) const {
205  gttic(DiscreteFactorGraph_optimize);
206  DiscreteLookupDAG dag = maxProduct(orderingType);
207  return dag.argmax();
208  }
209 
211  const Ordering& ordering) const {
212  gttic(DiscreteFactorGraph_optimize);
214  return dag.argmax();
215  }
216 
217  /* ************************************************************************ */
218  std::pair<DiscreteConditional::shared_ptr, DiscreteFactor::shared_ptr> //
220  const Ordering& frontalKeys) {
222 
223  // sum out frontals, this is the factor on the separator
224  gttic(sum);
225  DecisionTreeFactor::shared_ptr sum = product.sum(frontalKeys);
226  gttoc(sum);
227 
228  // Ordering keys for the conditional so that frontalKeys are really in front
229  Ordering orderedKeys;
230  orderedKeys.insert(orderedKeys.end(), frontalKeys.begin(),
231  frontalKeys.end());
232  orderedKeys.insert(orderedKeys.end(), sum->keys().begin(),
233  sum->keys().end());
234 
235  // now divide product/sum to get conditional
236  gttic(divide);
237  auto conditional =
238  std::make_shared<DiscreteConditional>(product, *sum, orderedKeys);
239  gttoc(divide);
240 
241  return {conditional, sum};
242  }
243 
244  /* ************************************************************************ */
246  const KeyFormatter& keyFormatter,
247  const DiscreteFactor::Names& names) const {
248  using std::endl;
249  std::stringstream ss;
250  ss << "`DiscreteFactorGraph` of size " << size() << endl << endl;
251  for (size_t i = 0; i < factors_.size(); i++) {
252  ss << "factor " << i << ":\n";
253  ss << factors_[i]->markdown(keyFormatter, names) << endl;
254  }
255  return ss.str();
256  }
257 
258  /* ************************************************************************ */
259  string DiscreteFactorGraph::html(const KeyFormatter& keyFormatter,
260  const DiscreteFactor::Names& names) const {
261  using std::endl;
262  std::stringstream ss;
263  ss << "<div><p><tt>DiscreteFactorGraph</tt> of size " << size() << "</p>";
264  for (size_t i = 0; i < factors_.size(); i++) {
265  ss << "<p>factor " << i << ":</p>";
266  ss << factors_[i]->html(keyFormatter, names) << endl;
267  }
268  return ss.str();
269  }
270 
271  /* ************************************************************************ */
272  } // 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:219
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:140
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::DecisionTreeFactor
Definition: DecisionTreeFactor.h:45
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:168
s
RealScalar s
Definition: level1_cplx_impl.h:126
DiscreteFactorGraph.h
gtsam::DiscreteFactorGraph::keys
KeySet keys() const
Definition: DiscreteFactorGraph.cpp:45
gtsam::ProductAndNormalize
static DecisionTreeFactor ProductAndNormalize(const DiscreteFactorGraph &factors)
Multiply all the factors and normalize the product to prevent underflow.
Definition: DiscreteFactorGraph.cpp:121
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:77
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:85
gtsam::FastSet< Key >
DiscreteConditional.h
gtsam::DecisionTreeFactor::shared_ptr
std::shared_ptr< DecisionTreeFactor > shared_ptr
Definition: DecisionTreeFactor.h:51
result
Values result
Definition: OdometryOptimize.cpp:8
gtsam::DiscreteFactorGraph::maxProduct
DiscreteLookupDAG maxProduct(OptionalOrderingType orderingType={}) const
Implement the max-product algorithm.
Definition: DiscreteFactorGraph.cpp:188
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::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:245
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::DiscreteFactorGraph::product
DecisionTreeFactor product() const
Definition: DiscreteFactorGraph.cpp:67
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::Names
DiscreteValues::Names Names
Translation table from values to strings.
Definition: DiscreteFactor.h:139
gtsam::DiscreteValues
Definition: DiscreteValues.h:34
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:259
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:203
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:76
gttic
#define gttic(label)
Definition: timing.h:295


gtsam
Author(s):
autogenerated on Sun Dec 22 2024 04:11:28