DiscreteLookupDAG.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 #include <iterator>
24 #include <string>
25 #include <utility>
26 #include <cassert>
27 
28 using std::pair;
29 using std::vector;
30 
31 namespace gtsam {
32 
33 /* ************************************************************************** */
34 // TODO(dellaert): copy/paste from DiscreteConditional.cpp :-(
35 void DiscreteLookupTable::print(const std::string& s,
36  const KeyFormatter& formatter) const {
37  using std::cout;
38  using std::endl;
39 
40  cout << s << " g( ";
41  for (const_iterator it = beginFrontals(); it != endFrontals(); ++it) {
42  cout << formatter(*it) << " ";
43  }
44  if (nrParents()) {
45  cout << "; ";
46  for (const_iterator it = beginParents(); it != endParents(); ++it) {
47  cout << formatter(*it) << " ";
48  }
49  }
50  cout << "):\n";
51  ADT::print("", formatter);
52  cout << endl;
53 }
54 
55 /* ************************************************************************** */
57  ADT pFS = choose(*values, true); // P(F|S=parentsValues)
58 
59  // Initialize
60  DiscreteValues mpe;
61  double maxP = 0;
62 
63  // Get all Possible Configurations
64  const auto allPosbValues = frontalAssignments();
65 
66  // Find the maximum
67  for (const auto& frontalVals : allPosbValues) {
68  double pValueS = pFS(frontalVals); // P(F=value|S=parentsValues)
69  // Update maximum solution if better
70  if (pValueS > maxP) {
71  maxP = pValueS;
72  mpe = frontalVals;
73  }
74  }
75 
76  // set values (inPlace) to maximum
77  for (Key j : frontals()) {
78  (*values)[j] = mpe[j];
79  }
80 }
81 
82 /* ************************************************************************** */
83 size_t DiscreteLookupTable::argmax(const DiscreteValues& parentsValues) const {
84  ADT pFS = choose(parentsValues, true); // P(F|S=parentsValues)
85 
86  // Then, find the max over all remaining
87  // TODO(Duy): only works for one key now, seems horribly slow this way
88  size_t mpe = 0;
89  double maxP = 0;
91  assert(nrFrontals() == 1);
92  Key j = (firstFrontalKey());
93  for (size_t value = 0; value < cardinality(j); value++) {
94  frontals[j] = value;
95  double pValueS = pFS(frontals); // P(F=value|S=parentsValues)
96  // Update MPE solution if better
97  if (pValueS > maxP) {
98  maxP = pValueS;
99  mpe = value;
100  }
101  }
102  return mpe;
103 }
104 
105 /* ************************************************************************** */
107  const DiscreteBayesNet& bayesNet) {
108  DiscreteLookupDAG dag;
109  for (auto&& conditional : bayesNet) {
110  if (auto lookupTable =
111  std::dynamic_pointer_cast<DiscreteLookupTable>(conditional)) {
112  dag.push_back(lookupTable);
113  } else {
114  throw std::runtime_error(
115  "DiscreteFactorGraph::maxProduct: Expected look up table.");
116  }
117  }
118  return dag;
119 }
120 
122  // Argmax each node in turn in topological sort order (parents first).
123  for (auto it = std::make_reverse_iterator(end());
124  it != std::make_reverse_iterator(begin()); ++it) {
125  // dereference to get the sharedFactor to the lookup table
126  (*it)->argmaxInPlace(&result);
127  }
128  return result;
129 }
130 /* ************************************************************************** */
131 
132 } // namespace gtsam
gtsam::DiscreteLookupTable::argmax
size_t argmax(const DiscreteValues &parentsValues) const
return assignment for single frontal variable that maximizes value.
Definition: DiscreteLookupDAG.cpp:83
DiscreteBayesNet.h
gtsam::DiscreteLookupTable::argmaxInPlace
void argmaxInPlace(DiscreteValues *parentsValues) const
Calculate assignment for frontal variables that maximizes value.
Definition: DiscreteLookupDAG.cpp:56
s
RealScalar s
Definition: level1_cplx_impl.h:126
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
result
Values result
Definition: OdometryOptimize.cpp:8
gtsam::AlgebraicDecisionTree< Key >
gtsam::Conditional< DecisionTreeFactor, DiscreteConditional >::endParents
DecisionTreeFactor ::const_iterator endParents() const
Definition: Conditional.h:189
different_sigmas::bayesNet
const HybridBayesNet bayesNet
Definition: testHybridBayesNet.cpp:242
gtsam::DiscreteBayesNet
Definition: DiscreteBayesNet.h:38
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
gtsam::Conditional< DecisionTreeFactor, DiscreteConditional >::endFrontals
DecisionTreeFactor ::const_iterator endFrontals() const
Definition: Conditional.h:183
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::DiscreteConditional::frontalAssignments
std::vector< DiscreteValues > frontalAssignments() const
Return all assignments for frontal variables.
Definition: DiscreteConditional.cpp:329
gtsam::Conditional< DecisionTreeFactor, DiscreteConditional >::beginFrontals
DecisionTreeFactor ::const_iterator beginFrontals() const
Definition: Conditional.h:180
gtsam::Conditional< DecisionTreeFactor, DiscreteConditional >::nrFrontals
size_t nrFrontals() const
Definition: Conditional.h:131
gtsam::Conditional< DecisionTreeFactor, DiscreteConditional >::beginParents
DecisionTreeFactor ::const_iterator beginParents() const
Definition: Conditional.h:186
gtsam::DiscreteFactor::cardinality
size_t cardinality(Key j) const
Definition: DiscreteFactor.h:98
gtsam::Conditional< DecisionTreeFactor, DiscreteConditional >::firstFrontalKey
Key firstFrontalKey() const
Definition: Conditional.h:137
gtsam::Factor::const_iterator
KeyVector::const_iterator const_iterator
Const iterator over keys.
Definition: Factor.h:83
gtsam
traits
Definition: SFMdata.h:40
DiscreteValues.h
make_reverse_iterator
std::reverse_iterator< Iterator > make_reverse_iterator(Iterator i)
Definition: stl_iterators.cpp:16
gtsam::DiscreteValues
Definition: DiscreteValues.h:34
gtsam::FactorGraph::push_back
IsDerived< DERIVEDFACTOR > push_back(std::shared_ptr< DERIVEDFACTOR > factor)
Add a factor directly using a shared_ptr.
Definition: FactorGraph.h:147
gtsam::AlgebraicDecisionTree< Key >::print
void print(const std::string &s="", const typename Base::LabelFormatter &labelFormatter=&DefaultFormatter) const
print method customized to value type double.
Definition: AlgebraicDecisionTree.h:251
gtsam::FactorGraph< DiscreteLookupTable >::end
const_iterator end() const
Definition: FactorGraph.h:342
gtsam::FactorGraph< DiscreteLookupTable >::begin
const_iterator begin() const
Definition: FactorGraph.h:339
gtsam::DiscreteConditional::choose
shared_ptr choose(const DiscreteValues &given) const
< DiscreteValues version
Definition: DiscreteConditional.cpp:184
gtsam::DiscreteLookupDAG::argmax
DiscreteValues argmax(DiscreteValues given=DiscreteValues()) const
argmax by back-substitution, optionally given certain variables.
Definition: DiscreteLookupDAG.cpp:121
gtsam::DiscreteLookupTable::print
void print(const std::string &s="Discrete Lookup Table: ", const KeyFormatter &formatter=DefaultKeyFormatter) const override
GTSAM-style print.
Definition: DiscreteLookupDAG.cpp:35
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
gtsam::DiscreteLookupDAG::FromBayesNet
static DiscreteLookupDAG FromBayesNet(const DiscreteBayesNet &bayesNet)
Create from BayesNet with LookupTables.
Definition: DiscreteLookupDAG.cpp:106
gtsam::Conditional< DecisionTreeFactor, DiscreteConditional >::frontals
Frontals frontals() const
Definition: Conditional.h:145
gtsam::Conditional< DecisionTreeFactor, DiscreteConditional >::nrParents
size_t nrParents() const
Definition: Conditional.h:134
test_callbacks.value
value
Definition: test_callbacks.py:160


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