gtsam  4.1.0
gtsam
DiscreteConditional.h
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 
19 #pragma once
20 
24 #include <boost/shared_ptr.hpp>
25 #include <boost/make_shared.hpp>
26 
27 #include <string>
28 
29 namespace gtsam {
30 
35 class GTSAM_EXPORT DiscreteConditional: public DecisionTreeFactor,
36  public Conditional<DecisionTreeFactor, DiscreteConditional> {
37 
38 public:
39  // typedefs needed to play nice with gtsam
41  typedef boost::shared_ptr<This> shared_ptr;
44 
48  typedef boost::shared_ptr<Values> sharedValues;
49 
52 
55  }
56 
58  DiscreteConditional(size_t nFrontals, const DecisionTreeFactor& f);
59 
61  DiscreteConditional(const Signature& signature);
62 
65  const DecisionTreeFactor& marginal);
66 
69  const DecisionTreeFactor& marginal, const Ordering& orderedKeys);
70 
78  template<typename ITERATOR>
79  static shared_ptr Combine(ITERATOR firstConditional,
80  ITERATOR lastConditional);
81 
85 
87  void print(const std::string& s = "Discrete Conditional: ",
88  const KeyFormatter& formatter = DefaultKeyFormatter) const override;
89 
91  bool equals(const DiscreteFactor& other, double tol = 1e-9) const override;
92 
96 
99  const std::string& s = "Discrete Conditional: ",
100  const KeyFormatter& formatter = DefaultKeyFormatter) const {
101  static_cast<const BaseConditional*>(this)->print(s, formatter);
102  }
103 
105  double operator()(const Values& values) const override {
106  return Potentials::operator()(values);
107  }
108 
110  DecisionTreeFactor::shared_ptr toFactor() const {
111  return DecisionTreeFactor::shared_ptr(new DecisionTreeFactor(*this));
112  }
113 
115  ADT choose(const Assignment<Key>& parentsValues) const;
116 
122  size_t solve(const Values& parentsValues) const;
123 
129  size_t sample(const Values& parentsValues) const;
130 
134 
136  void solveInPlace(Values& parentsValues) const;
137 
139  void sampleInPlace(Values& parentsValues) const;
140 
142 
143 };
144 // DiscreteConditional
145 
146 // traits
147 template<> struct traits<DiscreteConditional> : public Testable<DiscreteConditional> {};
148 
149 /* ************************************************************************* */
150 template<typename ITERATOR>
152  ITERATOR firstConditional, ITERATOR lastConditional) {
153  // TODO: check for being a clique
154 
155  // multiply all the potentials of the given conditionals
156  size_t nrFrontals = 0;
157  DecisionTreeFactor product;
158  for (ITERATOR it = firstConditional; it != lastConditional;
159  ++it, ++nrFrontals) {
161  DecisionTreeFactor::shared_ptr factor = c->toFactor();
162  product = (*factor) * product;
163  }
164  // and then create a new multi-frontal conditional
165  return boost::make_shared<DiscreteConditional>(nrFrontals, product);
166 }
167 
168 } // gtsam
169 
gtsam::equals
Template to create a binary predicate.
Definition: Testable.h:110
gtsam::Ordering
Definition: Ordering.h:34
gtsam::DecisionTreeFactor
A discrete probabilistic factor.
Definition: DecisionTreeFactor.h:38
gtsam::DiscreteConditional::This
DiscreteConditional This
Typedef to this class.
Definition: DiscreteConditional.h:40
gtsam::Assignment
An assignment from labels to value index (size_t).
Definition: Assignment.h:34
gtsam::traits
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
gtsam::Conditional
TODO: Update comments.
Definition: Conditional.h:41
gtsam::DiscreteFactor
Base class for discrete probabilistic factors The most general one is the derived DecisionTreeFactor.
Definition: DiscreteFactor.h:34
gtsam::DiscreteConditional::operator()
double operator()(const Values &values) const override
Evaluate, just look up in AlgebraicDecisonTree.
Definition: DiscreteConditional.h:105
gtsam::Signature
Signature for a discrete conditional density, used to construct conditionals.
Definition: Signature.h:52
gtsam::DiscreteConditional::BaseFactor
DecisionTreeFactor BaseFactor
Typedef to our factor base class.
Definition: DiscreteConditional.h:42
gtsam::DiscreteConditional::printSignature
void printSignature(const std::string &s="Discrete Conditional: ", const KeyFormatter &formatter=DefaultKeyFormatter) const
print index signature only
Definition: DiscreteConditional.h:98
gtsam
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
gtsam::DecisionTree< Key, double >::operator()
const double & operator()(const Assignment< Key > &x) const
evaluate
Definition: DecisionTree-inl.h:619
gtsam::print
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition: Matrix.cpp:155
gtsam::DiscreteConditional::DiscreteConditional
DiscreteConditional()
default constructor needed for serialization
Definition: DiscreteConditional.h:54
gtsam::DiscreteConditional
Discrete Conditional Density Derives from DecisionTreeFactor.
Definition: DiscreteConditional.h:36
gtsam::Testable
A helper that implements the traits interface for GTSAM types.
Definition: Testable.h:150
gtsam::DiscreteConditional::Values
Assignment< Key > Values
A map from keys to values.
Definition: DiscreteConditional.h:47
gtsam::DiscreteConditional::Combine
static shared_ptr Combine(ITERATOR firstConditional, ITERATOR lastConditional)
Combine several conditional into a single one.
Definition: DiscreteConditional.h:151
gtsam::KeyFormatter
boost::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
DecisionTreeFactor.h
gtsam::DiscreteConditional::BaseConditional
Conditional< BaseFactor, This > BaseConditional
Typedef to our conditional base class.
Definition: DiscreteConditional.h:43
gtsam::DiscreteConditional::toFactor
DecisionTreeFactor::shared_ptr toFactor() const
Convert to a factor.
Definition: DiscreteConditional.h:110
Signature.h
signatures for conditional densities
gtsam::Conditional< DecisionTreeFactor, DiscreteConditional >::nrFrontals
size_t nrFrontals() const
return the number of frontals
Definition: Conditional.h:85
Conditional.h
Base class for conditional densities.
gtsam::Values
A non-templated config holding any types of Manifold-group elements.
Definition: Values.h:71
gtsam::DiscreteConditional::shared_ptr
boost::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition: DiscreteConditional.h:41