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
17
18// \callgraph
19#pragma once
20
21#include <gtsam/inference/Key.h>
22
23namespace gtsam {
24
25 class HybridValues; // forward declaration.
26
62 template<class FACTOR, class DERIVEDCONDITIONAL>
64 {
65 protected:
68
69 private:
72
73
74 public:
77 typedef std::pair<typename FACTOR::const_iterator, typename FACTOR::const_iterator> ConstFactorRange;
78 struct ConstFactorRangeIterator {
79 ConstFactorRange range_;
80 // Delete default constructor
81 ConstFactorRangeIterator() = delete;
82 ConstFactorRangeIterator(ConstFactorRange const& x) : range_(x) {}
83 // Implement begin and end for iteration
84 typename FACTOR::const_iterator begin() const { return range_.first; }
85 typename FACTOR::const_iterator end() const { return range_.second; }
86 size_t size() const { return std::distance(range_.first, range_.second); }
87 const auto& front() const { return *begin(); }
88 // == operator overload for comparison with another iterator
89 template <class OTHER>
90 bool operator==(const OTHER& rhs) const {
91 if (this->size() != rhs.size()) return false;
92 if (this->size() == 0) return true;
93 return std::equal(begin(), end(), rhs.begin(), rhs.end());
94 }
95 };
96
98 typedef ConstFactorRangeIterator Frontals;
99
101 typedef ConstFactorRangeIterator Parents;
102
103 protected:
106
109
112
114
115 public:
118
120 void print(const std::string& s = "Conditional", const KeyFormatter& formatter = DefaultKeyFormatter) const;
121
123 bool equals(const This& c, double tol = 1e-9) const;
124
126
129
130 virtual ~Conditional() {}
131
133 size_t nrFrontals() const { return nrFrontals_; }
134
136 size_t nrParents() const { return asFactor().size() - nrFrontals_; }
137
140 if(nrFrontals_ > 0)
141 return asFactor().front();
142 else
143 throw std::invalid_argument("Requested Conditional::firstFrontalKey from a conditional with zero frontal keys");
144 }
145
147 Frontals frontals() const { return ConstFactorRangeIterator({beginFrontals(), endFrontals()});}
148
150 Parents parents() const { return ConstFactorRangeIterator({beginParents(), endParents()}); }
151
156 virtual double logProbability(const HybridValues& c) const;
157
162 virtual double evaluate(const HybridValues& c) const;
163
165 double operator()(const HybridValues& x) const {
166 return evaluate(x);
167 }
168
175 virtual double negLogConstant() const;
176
180
182 typename FACTOR::const_iterator beginFrontals() const { return asFactor().begin(); }
183
185 typename FACTOR::const_iterator endFrontals() const { return asFactor().begin() + nrFrontals_; }
186
188 typename FACTOR::const_iterator beginParents() const { return endFrontals(); }
189
191 typename FACTOR::const_iterator endParents() const { return asFactor().end(); }
192
194 size_t& nrFrontals() { return nrFrontals_; }
195
197 typename FACTOR::iterator beginFrontals() { return asFactor().begin(); }
198
200 typename FACTOR::iterator endFrontals() { return asFactor().begin() + nrFrontals_; }
201
203 typename FACTOR::iterator beginParents() { return asFactor().begin() + nrFrontals_; }
204
206 typename FACTOR::iterator endParents() { return asFactor().end(); }
207
221 template <class VALUES>
222 static bool CheckInvariants(const DERIVEDCONDITIONAL& conditional,
223 const VALUES& x);
224
226
227 private:
228
231
232 // Cast to factor type (non-const) (casts down to derived conditional type, then up to factor type)
233 FACTOR& asFactor() { return static_cast<FACTOR&>(static_cast<DERIVEDCONDITIONAL&>(*this)); }
234
235 // Cast to derived type (const) (casts down to derived conditional type, then up to factor type)
236 const FACTOR& asFactor() const { return static_cast<const FACTOR&>(static_cast<const DERIVEDCONDITIONAL&>(*this)); }
237
238#if GTSAM_ENABLE_BOOST_SERIALIZATION
240 friend class boost::serialization::access;
241 template<class ARCHIVE>
242 void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
243 ar & BOOST_SERIALIZATION_NVP(nrFrontals_);
244 }
245#endif
246
248
249 };
250
251} // gtsam
Global functions in a separate testing namespace.
Definition chartTesting.h:28
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition Key.cpp:30
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
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:43
HybridValues represents a collection of DiscreteValues and VectorValues.
Definition HybridValues.h:37
static bool CheckInvariants(const DERIVEDCONDITIONAL &conditional, const VALUES &x)
Check invariants of this conditional, given the values x.
Definition Conditional-inst.h:69
size_t nrFrontals_
Definition Conditional.h:67
Key firstFrontalKey() const
Convenience function to get the first frontal key.
Definition Conditional.h:139
FACTOR::const_iterator endFrontals() const
Iterator pointing past the last frontal key.
Definition Conditional.h:185
FACTOR::iterator endParents()
Mutable iterator pointing past the last parent key.
Definition Conditional.h:206
FACTOR::iterator endFrontals()
Mutable iterator pointing past the last frontal key.
Definition Conditional.h:200
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:150
bool equals(const This &c, double tol=1e-9) const
check equality
Definition Conditional-inst.h:41
virtual double negLogConstant() const
All conditional types need to implement this as the negative log of the normalization constant to mak...
Definition Conditional-inst.h:62
std::pair< typename BaseFactor::const_iterator, typename BaseFactor::const_iterator > ConstFactorRange
Definition Conditional.h:77
double operator()(const HybridValues &x) const
Evaluate probability density, sugar.
Definition Conditional.h:165
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
Definition Conditional.h:133
Conditional()
Empty Constructor to make serialization possible.
Definition Conditional.h:108
Conditional(size_t nrFrontals)
Constructor.
Definition Conditional.h:111
FACTOR::iterator beginParents()
Mutable iterator pointing to the first parent key.
Definition Conditional.h:203
size_t & nrFrontals()
Mutable version of nrFrontals.
Definition Conditional.h:194
FACTOR::iterator beginFrontals()
Mutable iterator pointing to first frontal key.
Definition Conditional.h:197
Frontals frontals() const
return a view of the frontal keys
Definition Conditional.h:147
FACTOR::const_iterator beginFrontals() const
Iterator pointing to first frontal key.
Definition Conditional.h:182
ConstFactorRangeIterator Parents
Definition Conditional.h:101
ConstFactorRangeIterator Frontals
Definition Conditional.h:98
size_t nrParents() const
return the number of parents
Definition Conditional.h:136
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:191
FACTOR::const_iterator beginParents() const
Iterator pointing to the first parent key.
Definition Conditional.h:188