59 SharedGaussian model_inlier_;
60 SharedGaussian model_outlier_;
63 double prior_outlier_;
65 bool flag_bump_up_near_zero_probs_;
66 mutable bool start_with_M_step_;
69 GTSAM_CONCEPT_LIE_TYPE(T)
70 GTSAM_CONCEPT_TESTABLE_TYPE(T)
75 typedef typename std::shared_ptr<TransformBtwRobotsUnaryFactorEM>
shared_ptr;
83 const SharedGaussian& model_inlier,
const SharedGaussian& model_outlier,
84 const double prior_inlier,
const double prior_outlier,
85 const bool flag_bump_up_near_zero_probs =
false,
86 const bool start_with_M_step =
false) :
87 Base(
KeyVector{key}), key_(key), measured_(measured), keyA_(keyA), keyB_(keyB),
88 model_inlier_(model_inlier), model_outlier_(model_outlier),
89 prior_inlier_(prior_inlier), prior_outlier_(prior_outlier), flag_bump_up_near_zero_probs_(flag_bump_up_near_zero_probs),
90 start_with_M_step_(false){
100 NonlinearFactor::shared_ptr
clone()
const override {
return std::make_shared<This>(*
this); }
107 std::cout << s <<
"TransformBtwRobotsUnaryFactorEM("
108 << keyFormatter(key_) <<
")\n";
109 std::cout <<
"MR between factor keys: "
110 << keyFormatter(keyA_) <<
","
111 << keyFormatter(keyB_) <<
"\n";
112 measured_.print(
" measured: ");
113 model_inlier_->print(
" noise model inlier: ");
114 model_outlier_->print(
" noise model outlier: ");
115 std::cout <<
"(prior_inlier, prior_outlier_) = ("
116 << prior_inlier_ <<
","
117 << prior_outlier_ <<
")\n";
123 const This *t =
dynamic_cast<const This*
> (&f);
126 return key_ == t->key_ && measured_.equals(t->measured_) &&
129 prior_outlier_ == t->prior_outlier_ && prior_inlier_ == t->prior_inlier_;
139 throw(
"something is wrong!");
154 double error(
const Values& x)
const override {
168 return std::shared_ptr<JacobianFactor>();
172 std::vector<Matrix> A(this->
size());
189 Matrix H_compose, H_between1, H_dummy;
191 T orgA_T_currA = valA_.at<T>(keyA_);
192 T orgB_T_currB = valB_.at<T>(keyB_);
194 T orgA_T_orgB = x.
at<T>(key_);
196 T orgA_T_currB = orgA_T_orgB.compose(orgB_T_currB, H_compose, H_dummy);
198 T currA_T_currB_pred = orgA_T_currA.between(orgA_T_currB, H_dummy, H_between1);
200 T currA_T_currB_msr = measured_;
202 Vector err = currA_T_currB_msr.localCoordinates(currA_T_currB_pred);
205 Vector p_inlier_outlier = calcIndicatorProb(x, err);
206 double p_inlier = p_inlier_outlier[0];
207 double p_outlier = p_inlier_outlier[1];
209 if (start_with_M_step_){
210 start_with_M_step_ =
false;
216 Vector err_wh_inlier = model_inlier_->whiten(err);
217 Vector err_wh_outlier = model_outlier_->whiten(err);
219 Matrix invCov_inlier = model_inlier_->R().transpose() * model_inlier_->R();
220 Matrix invCov_outlier = model_outlier_->R().transpose() * model_outlier_->R();
223 err_wh_eq.resize(err_wh_inlier.rows()*2);
224 err_wh_eq << sqrt(p_inlier) * err_wh_inlier.array() , sqrt(p_outlier) * err_wh_outlier.array();
226 Matrix H_unwh = H_compose * H_between1;
230 Matrix H_inlier = sqrt(p_inlier)*model_inlier_->Whiten(H_unwh);
231 Matrix H_outlier = sqrt(p_outlier)*model_outlier_->Whiten(H_unwh);
232 Matrix H_aug = stack(std::vector<Matrix>{H_inlier, H_outlier});
234 (*H)[0].resize(H_aug.rows(),H_aug.cols());
256 Vector calcIndicatorProb(
const Values& x)
const {
258 Vector err = unwhitenedError(x);
260 return this->calcIndicatorProb(x, err);
264 Vector calcIndicatorProb(
const Values& x,
const Vector& err)
const {
267 Vector err_wh_inlier = model_inlier_->whiten(err);
268 Vector err_wh_outlier = model_outlier_->whiten(err);
270 Matrix invCov_inlier = model_inlier_->R().transpose() * model_inlier_->R();
271 Matrix invCov_outlier = model_outlier_->R().transpose() * model_outlier_->R();
273 double p_inlier = prior_inlier_ * sqrt(invCov_inlier.norm()) * exp( -0.5 * err_wh_inlier.dot(err_wh_inlier) );
274 double p_outlier = prior_outlier_ * sqrt(invCov_outlier.norm()) * exp( -0.5 * err_wh_outlier.dot(err_wh_outlier) );
276 double sumP = p_inlier + p_outlier;
280 if (flag_bump_up_near_zero_probs_){
283 if (p_inlier < minP || p_outlier < minP){
286 if (p_outlier < minP)
288 sumP = p_inlier + p_outlier;
294 return Vector{{p_inlier, p_outlier}};
298 Vector unwhitenedError(
const Values& x)
const {
300 T orgA_T_currA = valA_.at<T>(keyA_);
301 T orgB_T_currB = valB_.at<T>(keyB_);
303 T orgA_T_orgB = x.at<T>(key_);
305 T orgA_T_currB = orgA_T_orgB.compose(orgB_T_currB);
307 T currA_T_currB_pred = orgA_T_currA.between(orgA_T_currB);
309 T currA_T_currB_msr = measured_;
311 return currA_T_currB_msr.localCoordinates(currA_T_currB_pred);
315 SharedGaussian get_model_inlier()
const {
316 return model_inlier_;
320 SharedGaussian get_model_outlier()
const {
321 return model_outlier_;
325 Matrix get_model_inlier_cov()
const {
326 return (model_inlier_->R().transpose()*model_inlier_->R()).inverse();
330 Matrix get_model_outlier_cov()
const {
331 return (model_outlier_->R().transpose()*model_outlier_->R()).inverse();
335 void updateNoiseModels(
const Values& values,
const Marginals& marginals) {
339 Keys.push_back(keyA_);
340 Keys.push_back(keyB_);
341 JointMarginal joint_marginal12 = marginals.jointMarginalCovariance(Keys);
342 Matrix cov1 = joint_marginal12(keyA_, keyA_);
343 Matrix cov2 = joint_marginal12(keyB_, keyB_);
344 Matrix cov12 = joint_marginal12(keyA_, keyB_);
346 updateNoiseModels_givenCovs(values, cov1, cov2, cov12);
350 void updateNoiseModels(
const Values& values,
const NonlinearFactorGraph& graph){
362 Marginals marginals(graph, values, Marginals::QR);
364 this->updateNoiseModels(values, marginals);
368 void updateNoiseModels_givenCovs(
const Values& values,
const Matrix& cov1,
const Matrix& cov2,
const Matrix& cov12){
378 const T& p1 = values.at<T>(keyA_);
379 const T& p2 = values.at<T>(keyB_);
382 p1.between(p2, H1, H2);
385 H.resize(H1.rows(), H1.rows()+H2.rows());
389 joint_cov.resize(cov1.rows()+cov2.rows(), cov1.cols()+cov2.cols());
390 joint_cov << cov1, cov12,
391 cov12.transpose(), cov2;
393 Matrix cov_state = H*joint_cov*H.transpose();
398 Matrix covRinlier = (model_inlier_->R().transpose()*model_inlier_->R()).inverse();
401 Matrix covRoutlier = (model_outlier_->R().transpose()*model_outlier_->R()).inverse();
411 size_t dim()
const override {
412 return model_inlier_->R().rows() + model_inlier_->R().cols();
417#if GTSAM_ENABLE_BOOST_SERIALIZATION
419 friend class boost::serialization::access;
420 template<
class ARCHIVE>
421 void serialize(ARCHIVE & ar,
const unsigned int ) {
422 ar & boost::serialization::make_nvp(
"NonlinearFactor",
423 boost::serialization::base_object<Base>(*
this));
430 template<
class VALUE>
432 public Testable<TransformBtwRobotsUnaryFactorEM<VALUE> > {
Concept check for values that can be used in unit tests.
Base class and basic functions for Lie types.
A factor with a quadratic error function - a Gaussian.
A class for computing marginals in a NonlinearFactorGraph.
Factor Graph consisting of non-linear factors.
Non-linear factor base classes.
Global functions in a separate testing namespace.
Definition chartTesting.h:28
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition Key.cpp:30
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition Key.h:91
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::vector< Matrix > * OptionalMatrixVecType
The OptionalMatrixVecType is a pointer to a vector of matrices.
Definition NonlinearFactor.h:63
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:43
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition Group.h:37
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:152
size_t size() const
Definition Factor.h:160
std::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition GaussianFactor.h:42
A Gaussian factor in the squared-error form.
Definition JacobianFactor.h:92
static shared_ptr Covariance(const Matrix &covariance, bool smart=true)
A Gaussian noise model created by specifying a covariance matrix.
Definition NoiseModel.cpp:116
static shared_ptr Create(size_t dim)
Create a unit covariance noise model.
Definition NoiseModel.h:673
virtual bool equals(const NonlinearFactor &f, double tol=1e-9) const
Check if two factors are equal.
Definition NonlinearFactor.cpp:55
NonlinearFactor()
Default constructor for I/O only.
Definition NonlinearFactor.h:86
virtual bool active(const Values &c) const
Checks whether a factor should be used based on a set of values.
Definition NonlinearFactor.h:143
A non-templated config holding any types of Manifold-group elements.
Definition Values.h:65
const ValueType at(Key j) const
Retrieve a variable by key j.
Definition Values-inl.h:260
bool exists(Key j) const
Check if a value exists with key j.
Definition Values.cpp:95
A class for a measurement predicted by "between(config[key1],config[key2])".
Definition TransformBtwRobotsUnaryFactorEM.h:37
size_t dim() const override
get the dimension of the factor (number of rows on linearization)
Definition TransformBtwRobotsUnaryFactorEM.h:411
void print(const std::string &s, const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
implement functions needed for Testable
Definition TransformBtwRobotsUnaryFactorEM.h:106
TransformBtwRobotsUnaryFactorEM()
default constructor - only use for serialization
Definition TransformBtwRobotsUnaryFactorEM.h:78
void setValAValB(const Values &valA, const Values &valB)
implement functions needed to derive from Factor
Definition TransformBtwRobotsUnaryFactorEM.h:137
bool equals(const NonlinearFactor &f, double tol=1e-9) const override
equals
Definition TransformBtwRobotsUnaryFactorEM.h:122
NonlinearFactor::shared_ptr clone() const override
Clone.
Definition TransformBtwRobotsUnaryFactorEM.h:100
std::shared_ptr< GaussianFactor > linearize(const Values &x) const override
Linearize a non-linearFactorN to get a GaussianFactor, Hence .
Definition TransformBtwRobotsUnaryFactorEM.h:165
std::shared_ptr< TransformBtwRobotsUnaryFactorEM > shared_ptr
concept check by type
Definition TransformBtwRobotsUnaryFactorEM.h:75
Vector whitenedError(const Values &x, OptionalMatrixVecType H=nullptr) const
A function overload to accept a vector<matrix> instead of a pointer to the said type.
Definition TransformBtwRobotsUnaryFactorEM.h:185
TransformBtwRobotsUnaryFactorEM(Key key, const VALUE &measured, Key keyA, Key keyB, const Values &valA, const Values &valB, const SharedGaussian &model_inlier, const SharedGaussian &model_outlier, const double prior_inlier, const double prior_outlier, const bool flag_bump_up_near_zero_probs=false, const bool start_with_M_step=false)
Constructor.
Definition TransformBtwRobotsUnaryFactorEM.h:81
In nonlinear factors, the error function returns the negative log-likelihood as a non-linear function...