dcsam
Factored inference for discrete-continuous smoothing and mapping
DCMixtureFactor.h
Go to the documentation of this file.
1 
9 #pragma once
10 
11 #include <gtsam/nonlinear/NonlinearFactor.h>
12 #include <gtsam/nonlinear/Symbol.h>
13 #include <math.h>
14 
15 #include <algorithm>
16 #include <limits>
17 #include <vector>
18 
19 #include "DCFactor.h"
20 
21 namespace dcsam {
22 
29 template <class NonlinearFactorType>
30 class DCMixtureFactor : public DCFactor {
31  private:
32  gtsam::DiscreteKey dk_;
33  std::vector<NonlinearFactorType> factors_;
34  bool normalized_;
35 
36  public:
37  using Base = DCFactor;
38 
39  DCMixtureFactor() = default;
40 
41  DCMixtureFactor(const gtsam::KeyVector& keys, const gtsam::DiscreteKey& dk,
42  const std::vector<NonlinearFactorType>& factors,
43  bool normalized = false)
44  : dk_(dk), factors_(factors), normalized_(normalized) {
45  // Compiler doesn't like `keys_` in the initializer list.
46  keys_ = keys;
47 
48  // Add `dk` to `dkeys` list.
49  discreteKeys_.push_back(dk);
50  }
51 
53  Base::operator=(rhs);
54  this->dk_ = rhs.dk_;
55  this->factors_ = rhs.factors_;
56  }
57 
58  ~DCMixtureFactor() = default;
59 
60  double error(const gtsam::Values& continuousVals,
61  const DiscreteValues& discreteVals) const override {
62  // Retrieve the assignment to our discrete key.
63  const size_t assignment = discreteVals.at(dk_.first);
64 
65  // `assignment` indexes the nonlinear factors we have stored to compute the
66  // error.
67  const double factorError = factors_[assignment].error(continuousVals);
68  if (normalized_) return factorError;
69  return factorError + this->nonlinearFactorLogNormalizingConstant(
70  this->factors_[assignment], continuousVals);
71  }
72 
73  size_t dim() const override {
74  // TODO(kevin) Need to modify this? Maybe we take discrete vals as parameter
75  // and DCContinuousFactor will pass this in as needed.
76  return (factors_.size() > 0) ? factors_[0].dim() : 0;
77  }
78 
79  bool equals(const DCFactor& other, double tol = 1e-9) const override {
80  // We attempt a dynamic cast from DCFactor to DCMixtureFactor. If it fails,
81  // return false.
82  if (!dynamic_cast<const DCMixtureFactor*>(&other)) return false;
83 
84  // If the cast is successful, we'll properly construct a DCMixtureFactor
85  // object from `other`
86  const DCMixtureFactor& f(static_cast<const DCMixtureFactor&>(other));
87 
88  // Ensure that this DCMixtureFactor and `f` have the same number of
89  // component factors in `factors_`.
90  if (factors_.size() != f.factors_.size()) return false;
91 
92  // If the number of factors is the same, we compare them individually (they
93  // should be in the same order!). If any fail to match, return false.
94  for (size_t i = 0; i < factors_.size(); i++) {
95  if (!factors_[i].equals(f.factors_[i])) return false;
96  }
97 
98  // If everything above passes, and the keys_, discreteKeys_ and normalized_
99  // member variables are identical, return true.
100  return (std::equal(keys_.begin(), keys_.end(), f.keys().begin()) &&
101  (discreteKeys_ == f.discreteKeys_) &&
102  (normalized_ == f.normalized_));
103  }
104 
105  boost::shared_ptr<gtsam::GaussianFactor> linearize(
106  const gtsam::Values& continuousVals,
107  const DiscreteValues& discreteVals) const override {
108  // Retrieve the assignment to our discrete key.
109  const size_t assignment = discreteVals.at(dk_.first);
110 
111  // `assignment` indexes the nonlinear factors we have stored to compute the
112  // error.
113  return factors_[assignment].linearize(continuousVals);
114  }
115 };
116 
117 } // namespace dcsam
Custom discrete-continuous factor.
Abstract class implementing a discrete-continuous factor.
Definition: DCFactor.h:33
gtsam::DiscreteKeys discreteKeys_
Definition: DCFactor.h:36
DCFactor & operator=(const DCFactor &rhs)
Definition: DCFactor.h:58
gtsam::Factor Base
Definition: DCFactor.h:39
double nonlinearFactorLogNormalizingConstant(const NonlinearFactorType &factor, const gtsam::Values &values) const
Definition: DCFactor.h:193
DCFactor()=default
Implementation of a discrete conditional mixture factor. Implements a joint discrete-continuous facto...
Definition: DCMixtureFactor.h:30
boost::shared_ptr< gtsam::GaussianFactor > linearize(const gtsam::Values &continuousVals, const DiscreteValues &discreteVals) const override
Definition: DCMixtureFactor.h:105
DCMixtureFactor(const gtsam::KeyVector &keys, const gtsam::DiscreteKey &dk, const std::vector< NonlinearFactorType > &factors, bool normalized=false)
Definition: DCMixtureFactor.h:41
DCMixtureFactor & operator=(const DCMixtureFactor &rhs)
Definition: DCMixtureFactor.h:52
double error(const gtsam::Values &continuousVals, const DiscreteValues &discreteVals) const override
Definition: DCMixtureFactor.h:60
size_t dim() const override
Definition: DCMixtureFactor.h:73
bool equals(const DCFactor &other, double tol=1e-9) const override
Definition: DCMixtureFactor.h:79
Definition: DCContinuousFactor.h:24
gtsam::DiscreteFactor::Values DiscreteValues
Definition: DCSAM_types.h:19
const double tol
Definition: testDCSAM.cpp:40