gtsam  4.0.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 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 // traits
134 template<> struct traits<DiscreteConditional> : public Testable<DiscreteConditional> {};
135 
136 /* ************************************************************************* */
137 template<typename ITERATOR>
139  ITERATOR firstConditional, ITERATOR lastConditional) {
140  // TODO: check for being a clique
141 
142  // multiply all the potentials of the given conditionals
143  size_t nrFrontals = 0;
144  DecisionTreeFactor product;
145  for (ITERATOR it = firstConditional; it != lastConditional;
146  ++it, ++nrFrontals) {
148  DecisionTreeFactor::shared_ptr factor = c->toFactor();
149  product = (*factor) * product;
150  }
151  // and then create a new multi-frontal conditional
152  return boost::make_shared<DiscreteConditional>(nrFrontals, product);
153 }
154 
155 } // gtsam
156 
const double & operator()(const Assignment< Key > &x) const
evaluate
Definition: DecisionTree-inl.h:619
A non-templated config holding any types of Manifold-group elements.
Definition: Values.h:70
DecisionTreeFactor::shared_ptr toFactor() const
Convert to a factor.
Definition: DiscreteConditional.h:97
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition: Matrix.cpp:141
static shared_ptr Combine(ITERATOR firstConditional, ITERATOR lastConditional)
Combine several conditional into a single one.
Definition: DiscreteConditional.h:138
Discrete Conditional Density Derives from DecisionTreeFactor.
Definition: DiscreteConditional.h:33
Template to create a binary predicate.
Definition: Testable.h:110
Base class for discrete probabilistic factors The most general one is the derived DecisionTreeFactor.
Definition: DiscreteFactor.h:34
A helper that implements the traits interface for GTSAM types.
Definition: Testable.h:150
DiscreteConditional()
default constructor needed for serialization
Definition: DiscreteConditional.h:52
Assignment< Key > Values
A map from keys to values.
Definition: DiscreteConditional.h:45
signatures for conditional densities
An assignment from labels to value index (size_t).
Definition: Assignment.h:34
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:33
TODO: Update comments.
Definition: Conditional.h:40
DecisionTreeFactor BaseFactor
Typedef to our factor base class.
Definition: DiscreteConditional.h:40
A discrete probabilistic factor.
Definition: DecisionTreeFactor.h:38
Signature for a discrete conditional density, used to construct conditionals.
Definition: Signature.h:52
Conditional< BaseFactor, This > BaseConditional
Typedef to our conditional base class.
Definition: DiscreteConditional.h:41
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
size_t nrFrontals() const
return the number of frontals
Definition: Conditional.h:84
DiscreteConditional This
Typedef to this class.
Definition: DiscreteConditional.h:38
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
boost::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition: DiscreteConditional.h:39
virtual double operator()(const Values &values) const
Evaluate, just look up in AlgebraicDecisonTree.
Definition: DiscreteConditional.h:92
Base class for conditional densities.