gtsam 4.1.1
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
29namespace gtsam {
30
35class GTSAM_EXPORT DiscreteConditional: public DecisionTreeFactor,
36 public Conditional<DecisionTreeFactor, DiscreteConditional> {
37
38public:
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
147template<> struct traits<DiscreteConditional> : public Testable<DiscreteConditional> {};
148
149/* ************************************************************************* */
150template<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
signatures for conditional densities
Base class for conditional densities.
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition: Matrix.cpp:155
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
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
Template to create a binary predicate.
Definition: Testable.h:111
A helper that implements the traits interface for GTSAM types.
Definition: Testable.h:151
An assignment from labels to value index (size_t).
Definition: Assignment.h:34
const double & operator()(const Assignment< Key > &x) const
evaluate
Definition: DecisionTree-inl.h:619
A discrete probabilistic factor.
Definition: DecisionTreeFactor.h:38
Discrete Conditional Density Derives from DecisionTreeFactor.
Definition: DiscreteConditional.h:36
double operator()(const Values &values) const override
Evaluate, just look up in AlgebraicDecisonTree.
Definition: DiscreteConditional.h:105
boost::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition: DiscreteConditional.h:41
DecisionTreeFactor::shared_ptr toFactor() const
Convert to a factor.
Definition: DiscreteConditional.h:110
Assignment< Key > Values
A map from keys to values.
Definition: DiscreteConditional.h:47
Conditional< BaseFactor, This > BaseConditional
Typedef to our conditional base class.
Definition: DiscreteConditional.h:43
DiscreteConditional This
Typedef to this class.
Definition: DiscreteConditional.h:40
DecisionTreeFactor BaseFactor
Typedef to our factor base class.
Definition: DiscreteConditional.h:42
DiscreteConditional()
default constructor needed for serialization
Definition: DiscreteConditional.h:54
static shared_ptr Combine(ITERATOR firstConditional, ITERATOR lastConditional)
Combine several conditional into a single one.
Definition: DiscreteConditional.h:151
void printSignature(const std::string &s="Discrete Conditional: ", const KeyFormatter &formatter=DefaultKeyFormatter) const
print index signature only
Definition: DiscreteConditional.h:98
Base class for discrete probabilistic factors The most general one is the derived DecisionTreeFactor.
Definition: DiscreteFactor.h:34
Signature for a discrete conditional density, used to construct conditionals.
Definition: Signature.h:52
TODO: Update comments.
Definition: Conditional.h:41
size_t nrFrontals() const
return the number of frontals
Definition: Conditional.h:85
Definition: Ordering.h:34
A non-templated config holding any types of Manifold-group elements.
Definition: Values.h:63