gtsam/gtsam_unstable/nonlinear/LinearInequalityFactorGraph.h

83 lines
2.6 KiB
C
Raw Normal View History

2014-12-23 08:49:32 +08:00
/* ----------------------------------------------------------------------------
* GTSAM Copyright 2010, Georgia Tech Research Corporation,
* Atlanta, Georgia 30332-0415
* All Rights Reserved
* Authors: Frank Dellaert, et al. (see THANKS for the full author list)
* See LICENSE for the license information
* -------------------------------------------------------------------------- */
2014-12-23 07:35:22 +08:00
/**
2015-02-25 12:40:53 +08:00
* @file LinearInequalityFactorGraph.h
2014-12-23 08:49:32 +08:00
* @author Duy-Nguyen Ta
* @author Krunal Chande
* @author Luca Carlone
* @date Dec 15, 2014
2014-12-23 07:35:22 +08:00
*/
#pragma once
2014-12-24 03:44:43 +08:00
#include <gtsam/linear/VectorValues.h>
2015-02-25 12:40:53 +08:00
#include <gtsam_unstable/linear/InequalityFactorGraph.h>
#include <gtsam_unstable/nonlinear/ConstrainedFactor.h>
2014-12-23 07:35:22 +08:00
namespace gtsam {
2015-02-25 12:40:53 +08:00
/**
* FactorGraph container for linear inequality factors c(x)<=0 at the nonlinear level
*/
class LinearInequalityFactorGraph: public FactorGraph<NonlinearFactor> {
2014-12-23 07:35:22 +08:00
public:
2014-12-24 03:44:43 +08:00
/// Default constructor
2015-02-25 12:40:53 +08:00
LinearInequalityFactorGraph() {
2014-12-23 07:35:22 +08:00
}
2015-02-25 12:40:53 +08:00
/// Linearize to a InequalityFactorGraph
InequalityFactorGraph::shared_ptr linearize(const Values& linearizationPoint) const {
InequalityFactorGraph::shared_ptr linearGraph(new InequalityFactorGraph());
BOOST_FOREACH(const NonlinearFactor::shared_ptr& factor, *this) {
2014-12-23 07:35:22 +08:00
JacobianFactor::shared_ptr jacobian = boost::dynamic_pointer_cast<JacobianFactor>(
factor->linearize(linearizationPoint));
2015-02-25 12:40:53 +08:00
ConstrainedFactor::shared_ptr constraint
= boost::dynamic_pointer_cast<ConstrainedFactor>(factor);
2014-12-23 07:35:22 +08:00
linearGraph->add(LinearInequality(*jacobian, constraint->dualKey()));
}
return linearGraph;
}
/**
* Return true if the all errors are <= 0.0
*/
2015-02-25 12:40:53 +08:00
bool checkFeasibilityAndComplimentary(const Values& values,
const VectorValues& dualValues, double tol) const {
BOOST_FOREACH(const NonlinearFactor::shared_ptr& factor, *this) {
NoiseModelFactor::shared_ptr noiseModelFactor
= boost::dynamic_pointer_cast<NoiseModelFactor>(factor);
2014-12-23 07:35:22 +08:00
Vector error = noiseModelFactor->unwhitenedError(values);
// Primal feasibility condition: all constraints need to be <= 0.0
2015-02-25 12:40:53 +08:00
if (error[0] > tol)
2014-12-23 07:35:22 +08:00
return false;
// Complimentary condition: errors of active constraints need to be 0.0
2015-02-25 12:40:53 +08:00
ConstrainedFactor::shared_ptr constraint
= boost::dynamic_pointer_cast<ConstrainedFactor>(factor);
2014-12-23 07:35:22 +08:00
Key dualKey = constraint->dualKey();
2015-02-25 12:40:53 +08:00
// if dualKey doesn't exist in dualValues, it must be an inactive constraint!
if (!dualValues.exists(dualKey))
continue;
// for active constraint, the error should be 0.0
if (fabs(error[0]) > tol)
return false;
2014-12-23 07:35:22 +08:00
}
2015-02-25 12:40:53 +08:00
2014-12-23 07:35:22 +08:00
return true;
}
};
}