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 
27 using std::pair;
28 using std::vector;
29 
30 namespace gtsam {
31 
32 /* ************************************************************************** */
33 // TODO(dellaert): copy/paste from DiscreteConditional.cpp :-(
34 void DiscreteLookupTable::print(const std::string& s,
35  const KeyFormatter& formatter) const {
36  using std::cout;
37  using std::endl;
38 
39  cout << s << " g( ";
40  for (const_iterator it = beginFrontals(); it != endFrontals(); ++it) {
41  cout << formatter(*it) << " ";
42  }
43  if (nrParents()) {
44  cout << "; ";
45  for (const_iterator it = beginParents(); it != endParents(); ++it) {
46  cout << formatter(*it) << " ";
47  }
48  }
49  cout << "):\n";
50  ADT::print("", formatter);
51  cout << endl;
52 }
53 
54 /* ************************************************************************** */
56  ADT pFS = choose(*values, true); // P(F|S=parentsValues)
57 
58  // Initialize
59  DiscreteValues mpe;
60  double maxP = 0;
61 
62  // Get all Possible Configurations
63  const auto allPosbValues = frontalAssignments();
64 
65  // Find the maximum
66  for (const auto& frontalVals : allPosbValues) {
67  double pValueS = pFS(frontalVals); // P(F=value|S=parentsValues)
68  // Update maximum solution if better
69  if (pValueS > maxP) {
70  maxP = pValueS;
71  mpe = frontalVals;
72  }
73  }
74 
75  // set values (inPlace) to maximum
76  for (Key j : frontals()) {
77  (*values)[j] = mpe[j];
78  }
79 }
80 
81 /* ************************************************************************** */
82 size_t DiscreteLookupTable::argmax(const DiscreteValues& parentsValues) const {
83  ADT pFS = choose(parentsValues, true); // P(F|S=parentsValues)
84 
85  // Then, find the max over all remaining
86  // TODO(Duy): only works for one key now, seems horribly slow this way
87  size_t mpe = 0;
88  double maxP = 0;
90  assert(nrFrontals() == 1);
91  Key j = (firstFrontalKey());
92  for (size_t value = 0; value < cardinality(j); value++) {
93  frontals[j] = value;
94  double pValueS = pFS(frontals); // P(F=value|S=parentsValues)
95  // Update MPE solution if better
96  if (pValueS > maxP) {
97  maxP = pValueS;
98  mpe = value;
99  }
100  }
101  return mpe;
102 }
103 
104 /* ************************************************************************** */
106  const DiscreteBayesNet& bayesNet) {
107  DiscreteLookupDAG dag;
108  for (auto&& conditional : bayesNet) {
109  if (auto lookupTable =
110  std::dynamic_pointer_cast<DiscreteLookupTable>(conditional)) {
111  dag.push_back(lookupTable);
112  } else {
113  throw std::runtime_error(
114  "DiscreteFactorGraph::maxProduct: Expected look up table.");
115  }
116  }
117  return dag;
118 }
119 
121  // Argmax each node in turn in topological sort order (parents first).
122  for (auto it = std::make_reverse_iterator(end());
123  it != std::make_reverse_iterator(begin()); ++it) {
124  // dereference to get the sharedFactor to the lookup table
125  (*it)->argmaxInPlace(&result);
126  }
127  return result;
128 }
129 /* ************************************************************************** */
130 
131 } // namespace gtsam
gtsam::DiscreteLookupTable::argmax
size_t argmax(const DiscreteValues &parentsValues) const
return assignment for single frontal variable that maximizes value.
Definition: DiscreteLookupDAG.cpp:82
DiscreteBayesNet.h
gtsam::DiscreteLookupTable::argmaxInPlace
void argmaxInPlace(DiscreteValues *parentsValues) const
Calculate assignment for frontal variables that maximizes value.
Definition: DiscreteLookupDAG.cpp:55
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:327
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:93
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:271
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:182
gtsam::DiscreteLookupDAG::argmax
DiscreteValues argmax(DiscreteValues given=DiscreteValues()) const
argmax by back-substitution, optionally given certain variables.
Definition: DiscreteLookupDAG.cpp:120
gtsam::DiscreteLookupTable::print
void print(const std::string &s="Discrete Lookup Table: ", const KeyFormatter &formatter=DefaultKeyFormatter) const override
GTSAM-style print.
Definition: DiscreteLookupDAG.cpp:34
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:105
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 Sat Nov 16 2024 04:02:13