HybridConditional.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 #pragma once
19 
25 #include <gtsam/inference/Key.h>
27 
28 #include <memory>
29 #include <stdexcept>
30 #include <string>
31 #include <typeinfo>
32 #include <vector>
33 
34 namespace gtsam {
35 
59 class GTSAM_EXPORT HybridConditional
60  : public HybridFactor,
61  public Conditional<HybridFactor, HybridConditional> {
62  public:
63  // typedefs needed to play nice with gtsam
65  typedef std::shared_ptr<This> shared_ptr;
69 
70  protected:
72  std::shared_ptr<Factor> inner_;
73 
74  public:
77 
79  HybridConditional() = default;
80 
88  HybridConditional(const KeyVector& continuousKeys,
89  const DiscreteKeys& discreteKeys, size_t nFrontals)
90  : BaseFactor(continuousKeys, discreteKeys), BaseConditional(nFrontals) {}
91 
101  HybridConditional(const KeyVector& continuousFrontals,
102  const DiscreteKeys& discreteFrontals,
103  const KeyVector& continuousParents,
104  const DiscreteKeys& discreteParents);
105 
113  const std::shared_ptr<GaussianConditional>& continuousConditional);
114 
122  const std::shared_ptr<DiscreteConditional>& discreteConditional);
123 
130  HybridConditional(const std::shared_ptr<GaussianMixture>& gaussianMixture);
131 
135 
137  void print(
138  const std::string& s = "Hybrid Conditional: ",
139  const KeyFormatter& formatter = DefaultKeyFormatter) const override;
140 
142  bool equals(const HybridFactor& other, double tol = 1e-9) const override;
143 
147 
154  return std::dynamic_pointer_cast<GaussianMixture>(inner_);
155  }
156 
163  return std::dynamic_pointer_cast<GaussianConditional>(inner_);
164  }
165 
172  return std::dynamic_pointer_cast<DiscreteConditional>(inner_);
173  }
174 
176  std::shared_ptr<Factor> inner() const { return inner_; }
177 
179  double error(const HybridValues& values) const override;
180 
182  double logProbability(const HybridValues& values) const override;
183 
189  double logNormalizationConstant() const override;
190 
192  double evaluate(const HybridValues& values) const override;
193 
195  bool frontalsIn(const VectorValues& measurements) const {
196  for (Key key : frontals()) {
197  if (!measurements.exists(key)) {
198  return false;
199  }
200  }
201  return true;
202  }
203 
205 
206  private:
207 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
208 
209  friend class boost::serialization::access;
210  template <class Archive>
211  void serialize(Archive& ar, const unsigned int /*version*/) {
212  ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(BaseFactor);
213  ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(BaseConditional);
214  ar& BOOST_SERIALIZATION_NVP(inner_);
215 
216  // register the various casts based on the type of inner_
217  // https://www.boost.org/doc/libs/1_80_0/libs/serialization/doc/serialization.html#runtimecasting
218  if (isDiscrete()) {
219  boost::serialization::void_cast_register<DiscreteConditional, Factor>(
220  static_cast<DiscreteConditional*>(NULL), static_cast<Factor*>(NULL));
221  } else if (isContinuous()) {
222  boost::serialization::void_cast_register<GaussianConditional, Factor>(
223  static_cast<GaussianConditional*>(NULL), static_cast<Factor*>(NULL));
224  } else {
225  boost::serialization::void_cast_register<GaussianMixture, Factor>(
226  static_cast<GaussianMixture*>(NULL), static_cast<Factor*>(NULL));
227  }
228  }
229 #endif
230 
231 }; // HybridConditional
232 
233 // traits
234 template <>
235 struct traits<HybridConditional> : public Testable<HybridConditional> {};
236 
237 } // namespace gtsam
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:155
const gtsam::Symbol key('X', 0)
A hybrid conditional in the Conditional Linear Gaussian scheme.
std::shared_ptr< This > shared_ptr
shared_ptr to this class
Base class for conditional densities.
std::string serialize(const T &input)
serializes to a string
Conditional< BaseFactor, This > BaseConditional
Typedef to our conditional base class.
HybridFactor BaseFactor
Typedef to our factor base class.
leaf::MyValues values
static const KeyFormatter DefaultKeyFormatter
Definition: Key.h:43
const KeyFormatter & formatter
DiscreteConditional::shared_ptr asDiscrete() const
Return conditional as a DiscreteConditional.
std::shared_ptr< Factor > inner() const
Get the type-erased pointer to the inner type.
bool exists(Key j) const
Definition: VectorValues.h:133
A conditional of gaussian mixtures indexed by discrete variables, as part of a Bayes Network...
bool frontalsIn(const VectorValues &measurements) const
Check if VectorValues measurements contains all frontal keys.
GaussianConditional::shared_ptr asGaussian() const
Return HybridConditional as a GaussianConditional.
Array< double, 1, 3 > e(1./3., 0.5, 2.)
RealScalar s
Conditional Gaussian Base class.
HybridConditional(const KeyVector &continuousKeys, const DiscreteKeys &discreteKeys, size_t nFrontals)
Construct a new Hybrid Conditional object.
#define NULL
Definition: ccolamd.c:609
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
Linearized Hybrid factor graph that uses type erasure.
std::shared_ptr< This > shared_ptr
shared_ptr to this class
traits
Definition: chartTesting.h:28
GaussianMixture::shared_ptr asMixture() const
Return HybridConditional as a GaussianMixture.
HybridConditional This
Typedef to this class.
std::shared_ptr< GaussianMixture > shared_ptr
static double error
Definition: testRot3.cpp:37
const G double tol
Definition: Group.h:86
std::shared_ptr< This > shared_ptr
shared_ptr to this class
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:86
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:102
std::shared_ptr< Factor > inner_
Type-erased pointer to the inner type.
DiscreteKeys is a set of keys that can be assembled using the & operator.
Definition: DiscreteKey.h:41


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:20