gtsam  3.2.1
gtsam
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 namespace gtsam {
28 
33 class GTSAM_EXPORT DiscreteConditional: public DecisionTreeFactor,
34  public Conditional<DecisionTreeFactor, DiscreteConditional> {
35 
36 public:
37  // typedefs needed to play nice with gtsam
39  typedef boost::shared_ptr<This> shared_ptr;
42 
46  typedef boost::shared_ptr<Values> sharedValues;
47 
50 
53  }
54 
56  DiscreteConditional(size_t nFrontals, const DecisionTreeFactor& f);
57 
59  DiscreteConditional(const Signature& signature);
60 
63  const DecisionTreeFactor& marginal, const boost::optional<Ordering>& orderedKeys = boost::none);
64 
72  template<typename ITERATOR>
73  static shared_ptr Combine(ITERATOR firstConditional,
74  ITERATOR lastConditional);
75 
79 
81  void print(const std::string& s = "Discrete Conditional: ",
82  const KeyFormatter& formatter = DefaultKeyFormatter) const;
83 
85  bool equals(const DiscreteFactor& other, double tol = 1e-9) const;
86 
90 
92  virtual double operator()(const Values& values) const {
93  return Potentials::operator()(values);
94  }
95 
97  DecisionTreeFactor::shared_ptr toFactor() const {
98  return DecisionTreeFactor::shared_ptr(new DecisionTreeFactor(*this));
99  }
100 
102  ADT choose(const Assignment<Key>& parentsValues) const;
103 
109  size_t solve(const Values& parentsValues) const;
110 
116  size_t sample(const Values& parentsValues) const;
117 
121 
123  void solveInPlace(Values& parentsValues) const;
124 
126  void sampleInPlace(Values& parentsValues) const;
127 
129 
130 };
131 // DiscreteConditional
132 
133 /* ************************************************************************* */
134 template<typename ITERATOR>
136  ITERATOR firstConditional, ITERATOR lastConditional) {
137  // TODO: check for being a clique
138 
139  // multiply all the potentials of the given conditionals
140  size_t nrFrontals = 0;
141  DecisionTreeFactor product;
142  for (ITERATOR it = firstConditional; it != lastConditional;
143  ++it, ++nrFrontals) {
145  DecisionTreeFactor::shared_ptr factor = c->toFactor();
146  product = (*factor) * product;
147  }
148  // and then create a new multi-frontal conditional
149  return boost::make_shared<DiscreteConditional>(nrFrontals, product);
150 }
151 
152 } // gtsam
153 
DecisionTreeFactor BaseFactor
Typedef to our factor base class.
Definition: DiscreteConditional.h:40
TODO: Update comments.
Definition: Conditional.h:40
static shared_ptr Combine(ITERATOR firstConditional, ITERATOR lastConditional)
Combine several conditional into a single one.
Definition: DiscreteConditional.h:135
Signature for a discrete conditional density, used to construct conditionals.
Definition: Signature.h:52
boost::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition: DiscreteConditional.h:39
signatures for conditional densities
Base class for discrete probabilistic factors The most general one is the derived DecisionTreeFactor...
Definition: DiscreteFactor.h:33
void solve(Matrix &A, Matrix &B)
solve AX=B via in-place Lu factorization and backsubstitution After calling, A contains LU...
Definition: Matrix.cpp:283
A discrete probabilistic factor.
Definition: DecisionTreeFactor.h:39
Assignment< Key > Values
A map from keys to values.
Definition: DiscreteConditional.h:45
Base class for conditional densities.
A non-templated config holding any types of Manifold-group elements.
Definition: Values.h:75
void print(const Matrix &A, const string &s, ostream &stream)
print a matrix
Definition: Matrix.cpp:183
const double & operator()(const Assignment< Key > &x) const
evaluate
DiscreteConditional This
Typedef to this class.
Definition: DiscreteConditional.h:38
An assignment from labels to value index (size_t).
Definition: Assignment.h:35
Template to create a binary predicate.
Definition: Testable.h:102
virtual double operator()(const Values &values) const
Evaluate, just look up in AlgebraicDecisonTree.
Definition: DiscreteConditional.h:92
Conditional< BaseFactor, This > BaseConditional
Typedef to our conditional base class.
Definition: DiscreteConditional.h:41
DecisionTreeFactor::shared_ptr toFactor() const
Convert to a factor.
Definition: DiscreteConditional.h:97
Discrete Conditional Density Derives from DecisionTreeFactor.
Definition: DiscreteConditional.h:33
DiscreteConditional()
default constructor needed for serialization
Definition: DiscreteConditional.h:52
size_t nrFrontals() const
return the number of frontals
Definition: Conditional.h:84
boost::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: types.h:62