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()); it != std::make_reverse_iterator(begin()); ++it) {
123  // dereference to get the sharedFactor to the lookup table
124  (*it)->argmaxInPlace(&result);
125  }
126  return result;
127 }
128 /* ************************************************************************** */
129 
130 } // namespace gtsam
void print(const std::string &s="", const typename Base::LabelFormatter &labelFormatter=&DefaultFormatter) const
print method customized to value type double.
shared_ptr choose(const DiscreteValues &given) const
< DiscreteValues version
DecisionTreeFactor ::const_iterator endFrontals() const
Definition: Conditional.h:182
IsDerived< DERIVEDFACTOR > push_back(std::shared_ptr< DERIVEDFACTOR > factor)
Add a factor directly using a shared_ptr.
Definition: FactorGraph.h:190
leaf::MyValues values
DecisionTreeFactor ::const_iterator beginParents() const
Definition: Conditional.h:185
std::reverse_iterator< Iterator > make_reverse_iterator(Iterator i)
void print(const std::string &s="Discrete Lookup Table: ", const KeyFormatter &formatter=DefaultKeyFormatter) const override
GTSAM-style print.
const KeyFormatter & formatter
DecisionTreeFactor ::const_iterator beginFrontals() const
Definition: Conditional.h:179
const_iterator end() const
Definition: Factor.h:148
Values result
size_t cardinality(Key j) const
DecisionTreeFactor ::const_iterator endParents() const
Definition: Conditional.h:188
RealScalar s
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
traits
Definition: chartTesting.h:28
void argmaxInPlace(DiscreteValues *parentsValues) const
Calculate assignment for frontal variables that maximizes value.
size_t argmax() const
Return assignment that maximizes distribution.
static DiscreteLookupDAG FromBayesNet(const DiscreteBayesNet &bayesNet)
Create from BayesNet with LookupTables.
KeyVector::const_iterator const_iterator
Const iterator over keys.
Definition: Factor.h:82
const_iterator begin() const
Definition: Factor.h:145
DiscreteValues argmax(DiscreteValues given=DiscreteValues()) const
argmax by back-substitution, optionally given certain variables.
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:102
std::ptrdiff_t j
std::vector< DiscreteValues > frontalAssignments() const
Return all assignments for frontal variables.


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:10