gtsam 4.2.0
gtsam
Loading...
Searching...
No Matches
Conditional.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
18// \callgraph
19#pragma once
20
21#include <boost/range.hpp>
22
23#include <gtsam/inference/Key.h>
24
25namespace gtsam {
26
27 class HybridValues; // forward declaration.
28
62 template<class FACTOR, class DERIVEDCONDITIONAL>
64 {
65 protected:
68
69 private:
72
73 public:
75 typedef boost::iterator_range<typename FACTOR::const_iterator> Frontals;
76
78 typedef boost::iterator_range<typename FACTOR::const_iterator> Parents;
79
80 protected:
83
86
89
91
92 public:
95
97 void print(const std::string& s = "Conditional", const KeyFormatter& formatter = DefaultKeyFormatter) const;
98
100 bool equals(const This& c, double tol = 1e-9) const;
101
103
106
107 virtual ~Conditional() {}
108
110 size_t nrFrontals() const { return nrFrontals_; }
111
113 size_t nrParents() const { return asFactor().size() - nrFrontals_; }
114
117 if(nrFrontals_ > 0)
118 return asFactor().front();
119 else
120 throw std::invalid_argument("Requested Conditional::firstFrontalKey from a conditional with zero frontal keys");
121 }
122
124 Frontals frontals() const { return boost::make_iterator_range(beginFrontals(), endFrontals()); }
125
127 Parents parents() const { return boost::make_iterator_range(beginParents(), endParents()); }
128
133 virtual double logProbability(const HybridValues& c) const;
134
139 virtual double evaluate(const HybridValues& c) const;
140
142 double operator()(const HybridValues& x) const {
143 return evaluate(x);
144 }
145
150 virtual double logNormalizationConstant() const;
151
153 double normalizationConstant() const;
154
158
160 typename FACTOR::const_iterator beginFrontals() const { return asFactor().begin(); }
161
163 typename FACTOR::const_iterator endFrontals() const { return asFactor().begin() + nrFrontals_; }
164
166 typename FACTOR::const_iterator beginParents() const { return endFrontals(); }
167
169 typename FACTOR::const_iterator endParents() const { return asFactor().end(); }
170
172 size_t& nrFrontals() { return nrFrontals_; }
173
175 typename FACTOR::iterator beginFrontals() { return asFactor().begin(); }
176
178 typename FACTOR::iterator endFrontals() { return asFactor().begin() + nrFrontals_; }
179
181 typename FACTOR::iterator beginParents() { return asFactor().begin() + nrFrontals_; }
182
184 typename FACTOR::iterator endParents() { return asFactor().end(); }
185
199 template <class VALUES>
200 static bool CheckInvariants(const DERIVEDCONDITIONAL& conditional,
201 const VALUES& x);
202
204
205 private:
206
209
210 // Cast to factor type (non-const) (casts down to derived conditional type, then up to factor type)
211 FACTOR& asFactor() { return static_cast<FACTOR&>(static_cast<DERIVEDCONDITIONAL&>(*this)); }
212
213 // Cast to derived type (const) (casts down to derived conditional type, then up to factor type)
214 const FACTOR& asFactor() const { return static_cast<const FACTOR&>(static_cast<const DERIVEDCONDITIONAL&>(*this)); }
215
218 template<class ARCHIVE>
219 void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
220 ar & BOOST_SERIALIZATION_NVP(nrFrontals_);
221 }
222
224
225 };
226
227} // gtsam
Global functions in a separate testing namespace.
Definition chartTesting.h:28
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:100
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
Template to create a binary predicate.
Definition Testable.h:111
HybridValues represents a collection of DiscreteValues and VectorValues.
Definition HybridValues.h:38
Definition Conditional.h:64
static bool CheckInvariants(const DERIVEDCONDITIONAL &conditional, const VALUES &x)
Check invariants of this conditional, given the values x.
Definition Conditional-inst.h:77
size_t nrFrontals_
The first nrFrontal variables are frontal and the rest are parents.
Definition Conditional.h:67
virtual double logNormalizationConstant() const
All conditional types need to implement a log normalization constant to make it such that error>=0.
Definition Conditional-inst.h:62
Key firstFrontalKey() const
Convenience function to get the first frontal key.
Definition Conditional.h:116
FACTOR::const_iterator endFrontals() const
Iterator pointing past the last frontal key.
Definition Conditional.h:163
FACTOR::iterator endParents()
Mutable iterator pointing past the last parent key.
Definition Conditional.h:184
FACTOR::iterator endFrontals()
Mutable iterator pointing past the last frontal key.
Definition Conditional.h:178
virtual double evaluate(const HybridValues &c) const
All conditional types need to implement an evaluate function, that yields a true probability.
Definition Conditional-inst.h:55
Parents parents() const
return a view of the parent keys
Definition Conditional.h:127
double operator()(const HybridValues &x) const
Evaluate probability density, sugar.
Definition Conditional.h:142
boost::iterator_range< typename FACTOR::const_iterator > Parents
View of the separator keys (call parents())
Definition Conditional.h:78
virtual double logProbability(const HybridValues &c) const
All conditional types need to implement a logProbability function, for which exp(logProbability(x)) =...
Definition Conditional-inst.h:48
size_t nrFrontals() const
return the number of frontals
Definition Conditional.h:110
Conditional()
Empty Constructor to make serialization possible.
Definition Conditional.h:85
Conditional(size_t nrFrontals)
Constructor.
Definition Conditional.h:88
FACTOR::iterator beginParents()
Mutable iterator pointing to the first parent key.
Definition Conditional.h:181
size_t & nrFrontals()
Mutable version of nrFrontals.
Definition Conditional.h:172
FACTOR::iterator beginFrontals()
Mutable iterator pointing to first frontal key.
Definition Conditional.h:175
boost::iterator_range< typename FACTOR::const_iterator > Frontals
View of the frontal keys (call frontals())
Definition Conditional.h:75
Frontals frontals() const
return a view of the frontal keys
Definition Conditional.h:124
friend class boost::serialization::access
Serialization function.
Definition Conditional.h:217
FACTOR::const_iterator beginFrontals() const
Iterator pointing to first frontal key.
Definition Conditional.h:160
double normalizationConstant() const
Non-virtual, exponentiate logNormalizationConstant.
Definition Conditional-inst.h:70
size_t nrParents() const
return the number of parents
Definition Conditional.h:113
void print(const std::string &s="Conditional", const KeyFormatter &formatter=DefaultKeyFormatter) const
print with optional formatter
Definition Conditional-inst.h:30
FACTOR::const_iterator endParents() const
Iterator pointing past the last parent key.
Definition Conditional.h:169
FACTOR::const_iterator beginParents() const
Iterator pointing to the first parent key.
Definition Conditional.h:166
the error.