2009-11-20 00:50:18 +08:00
|
|
|
/*
|
|
|
|
* @file NonlinearConstraint.h
|
2010-07-19 03:55:53 +08:00
|
|
|
* @brief Implements nonlinear constraints that can be linearized using
|
|
|
|
* direct linearization and solving through a quadratic merit function
|
2009-11-20 00:50:18 +08:00
|
|
|
* @author Alex Cunningham
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <map>
|
2009-12-02 03:45:47 +08:00
|
|
|
#include <boost/function.hpp>
|
2009-11-20 00:50:18 +08:00
|
|
|
#include "NonlinearFactor.h"
|
|
|
|
|
|
|
|
namespace gtsam {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for nonlinear constraints
|
|
|
|
* This allows for both equality and inequality constraints,
|
|
|
|
* where equality constraints are active all the time (even slightly
|
|
|
|
* nonzero constraint functions will still be active - inequality
|
|
|
|
* constraints should be sure to force to actual zero)
|
|
|
|
*
|
2010-07-19 03:55:53 +08:00
|
|
|
* NOTE: inequality constraints removed for now
|
2010-02-24 02:37:17 +08:00
|
|
|
*
|
2010-07-19 03:55:53 +08:00
|
|
|
* Nonlinear constraints evaluate their error as a part of a quadratic
|
|
|
|
* error function: ||h(x)-z||^2 + mu * ||c(x)|| where mu is a gain
|
|
|
|
* on the constraint function that should be made high enough to be
|
|
|
|
* significant
|
2009-11-20 00:50:18 +08:00
|
|
|
*/
|
|
|
|
template <class Config>
|
|
|
|
class NonlinearConstraint : public NonlinearFactor<Config> {
|
|
|
|
|
|
|
|
protected:
|
Large gtsam refactoring
To support faster development *and* better performance Richard and I pushed through a large refactoring of NonlinearFactors.
The following are the biggest changes:
1) NonLinearFactor1 and NonLinearFactor2 are now templated on Config, Key type, and X type, where X is the argument to the measurement function.
2) The measurement itself is no longer kept in the nonlinear factor. Instead, a derived class (see testVSLAMFactor, testNonlinearEquality, testPose3Factor etc...) has to implement a function to compute the errors, "evaluateErrors". Instead of (h(x)-z), it needs to return (z-h(x)), so Ax-b is an approximation of the error. IMPORTANT: evaluateErrors needs - if asked - *combine* the calculation of the function value h(x) and the derivatives dh(x)/dx. This was a major performance issue. To do this, boost::optional<Matrix&> arguments are provided, and tin EvaluateErrors you just says something like
if (H) *H = Matrix_(3,6,....);
3) We are no longer using int or strings for nonlinear factors. Instead, the preferred key type is now Symbol, defined in Key.h. This is both fast and cool: you can construct it from an int, and cast it to a strong. It also does type checking: a Symbol<Pose3,'x'> will not match a Symbol<Pose2,'x'>
4) minor: take a look at LieConfig.h: it help you avoid writing a lot of code bu automatically creating configs for a certain type. See e.g. Pose3Config.h. A "double" LieConfig is on the way - Thanks Richard and Manohar !
2010-01-14 06:25:03 +08:00
|
|
|
|
2010-07-19 03:55:53 +08:00
|
|
|
double mu_; // gain for quadratic merit function
|
2009-11-29 02:35:36 +08:00
|
|
|
|
2009-11-20 00:50:18 +08:00
|
|
|
public:
|
2009-11-29 02:35:36 +08:00
|
|
|
|
2009-11-20 00:50:18 +08:00
|
|
|
/** Constructor - sets the cost function and the lagrange multipliers
|
2010-07-19 03:55:53 +08:00
|
|
|
* @param dim is the dimension of the factor
|
|
|
|
* @param mu is the gain used at error evaluation (forced to be positive)
|
2009-11-20 00:50:18 +08:00
|
|
|
*/
|
2010-07-19 03:55:53 +08:00
|
|
|
NonlinearConstraint(size_t dim, double mu = 1000.0);
|
2009-11-20 00:50:18 +08:00
|
|
|
|
2010-07-19 03:55:53 +08:00
|
|
|
/** returns the gain mu */
|
|
|
|
double mu() const { return mu_; }
|
2009-11-29 02:35:36 +08:00
|
|
|
|
2009-11-20 00:50:18 +08:00
|
|
|
/** Print */
|
2010-07-19 03:55:53 +08:00
|
|
|
virtual void print(const std::string& s = "") const=0;
|
2009-11-20 00:50:18 +08:00
|
|
|
|
|
|
|
/** Check if two factors are equal */
|
|
|
|
virtual bool equals(const Factor<Config>& f, double tol=1e-9) const=0;
|
|
|
|
|
2010-07-19 03:55:53 +08:00
|
|
|
/** error function - returns the quadratic merit function */
|
|
|
|
virtual double error(const Config& c) const;
|
2009-11-20 00:50:18 +08:00
|
|
|
|
|
|
|
/**
|
2010-07-19 03:55:53 +08:00
|
|
|
* Linearizes around a given config
|
|
|
|
* @param config is the configuration
|
2010-02-05 00:08:11 +08:00
|
|
|
* @return a combined linear factor containing both the constraint and the constraint factor
|
2009-11-20 00:50:18 +08:00
|
|
|
*/
|
2010-02-05 00:08:11 +08:00
|
|
|
virtual boost::shared_ptr<GaussianFactor> linearize(const Config& c) const=0;
|
2009-11-20 00:50:18 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-12-18 11:05:47 +08:00
|
|
|
* A unary constraint with arbitrary cost and jacobian functions
|
2010-02-07 09:18:17 +08:00
|
|
|
* This is an example class designed for easy testing, but real uses should probably
|
|
|
|
* subclass NonlinearConstraint and implement virtual functions directly
|
2009-11-20 00:50:18 +08:00
|
|
|
*/
|
2010-01-19 13:33:44 +08:00
|
|
|
template <class Config, class Key, class X>
|
2009-11-20 11:04:49 +08:00
|
|
|
class NonlinearConstraint1 : public NonlinearConstraint<Config> {
|
2009-11-20 00:50:18 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
/**
|
2009-12-18 11:05:47 +08:00
|
|
|
* Calculates the jacobian of the constraint function
|
2009-11-20 00:50:18 +08:00
|
|
|
* returns a pxn matrix
|
2009-12-02 03:45:47 +08:00
|
|
|
* Use boost.bind to create the function object
|
2009-11-20 00:50:18 +08:00
|
|
|
* @param config to use for linearization
|
|
|
|
* @return the jacobian of the constraint in terms of key
|
|
|
|
*/
|
2009-12-18 11:05:47 +08:00
|
|
|
boost::function<Matrix(const Config& config)> G_;
|
2009-11-20 00:50:18 +08:00
|
|
|
|
2010-02-07 09:18:17 +08:00
|
|
|
/** calculates the constraint function of the current config
|
|
|
|
* If the value is zero, the constraint is not active
|
|
|
|
* Use boost.bind to create the function object
|
|
|
|
* @param config is a configuration of all the variables
|
|
|
|
* @return the cost for each of p constraints, arranged in a vector
|
|
|
|
*/
|
|
|
|
boost::function<Vector(const Config& config)> g_;
|
|
|
|
|
2009-11-20 00:50:18 +08:00
|
|
|
/** key for the constrained variable */
|
2010-01-19 13:33:44 +08:00
|
|
|
Key key_;
|
2009-11-20 00:50:18 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Basic constructor
|
|
|
|
* @param key is the identifier for the variable constrained
|
2009-12-18 11:05:47 +08:00
|
|
|
* @param G gives the jacobian of the constraint function
|
2009-11-20 00:50:18 +08:00
|
|
|
* @param g is the constraint function
|
2010-07-19 03:55:53 +08:00
|
|
|
* @param dim is the size of the constraint (p)
|
|
|
|
* @param mu is the gain for the factor
|
2009-11-20 00:50:18 +08:00
|
|
|
*/
|
|
|
|
NonlinearConstraint1(
|
2009-12-18 10:39:02 +08:00
|
|
|
Vector (*g)(const Config& config),
|
2010-01-19 13:33:44 +08:00
|
|
|
const Key& key,
|
2009-12-18 11:05:47 +08:00
|
|
|
Matrix (*G)(const Config& config),
|
2010-07-19 03:55:53 +08:00
|
|
|
size_t dim,
|
|
|
|
double mu = 1000.0);
|
2009-11-20 00:50:18 +08:00
|
|
|
|
2009-12-02 03:45:47 +08:00
|
|
|
/**
|
|
|
|
* Basic constructor with boost function pointers
|
|
|
|
* @param key is the identifier for the variable constrained
|
2009-12-18 11:05:47 +08:00
|
|
|
* @param G gives the jacobian of the constraint function
|
2009-12-02 03:45:47 +08:00
|
|
|
* @param g is the constraint function as a boost function pointer
|
2010-07-19 03:55:53 +08:00
|
|
|
* @param dim is the size of the constraint (p)
|
|
|
|
* @param mu is the gain for the factor
|
2009-12-02 03:45:47 +08:00
|
|
|
*/
|
|
|
|
NonlinearConstraint1(
|
2009-12-18 10:39:02 +08:00
|
|
|
boost::function<Vector(const Config& config)> g,
|
2010-01-19 13:33:44 +08:00
|
|
|
const Key& key,
|
2009-12-18 11:05:47 +08:00
|
|
|
boost::function<Matrix(const Config& config)> G,
|
2010-07-19 03:55:53 +08:00
|
|
|
size_t dim,
|
|
|
|
double mu = 1000.0);
|
2009-12-02 03:45:47 +08:00
|
|
|
|
2009-11-20 00:50:18 +08:00
|
|
|
/** Print */
|
2009-11-20 11:04:49 +08:00
|
|
|
void print(const std::string& s = "") const;
|
2009-11-20 00:50:18 +08:00
|
|
|
|
|
|
|
/** Check if two factors are equal */
|
2009-11-20 11:04:49 +08:00
|
|
|
bool equals(const Factor<Config>& f, double tol=1e-9) const;
|
2009-11-20 00:50:18 +08:00
|
|
|
|
2010-02-07 09:18:17 +08:00
|
|
|
/** Error function */
|
|
|
|
virtual inline Vector unwhitenedError(const Config& c) const { return g_(c); }
|
|
|
|
|
2010-07-19 03:55:53 +08:00
|
|
|
/** Linearize from config */
|
2010-02-05 00:08:11 +08:00
|
|
|
virtual boost::shared_ptr<GaussianFactor> linearize(const Config& c) const;
|
2009-11-20 00:50:18 +08:00
|
|
|
};
|
|
|
|
|
2009-11-20 11:50:48 +08:00
|
|
|
/**
|
2009-12-18 11:05:47 +08:00
|
|
|
* A binary constraint with arbitrary cost and jacobian functions
|
2009-11-20 11:50:48 +08:00
|
|
|
*/
|
2010-01-19 13:33:44 +08:00
|
|
|
template <class Config, class Key1, class X1, class Key2, class X2>
|
2009-11-20 11:50:48 +08:00
|
|
|
class NonlinearConstraint2 : public NonlinearConstraint<Config> {
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
/**
|
2009-12-18 11:05:47 +08:00
|
|
|
* Calculates the jacobians of the constraint function in terms of
|
2009-11-20 11:50:48 +08:00
|
|
|
* the first and second variables
|
|
|
|
* returns a pxn matrix
|
|
|
|
* @param config to use for linearization
|
|
|
|
* @return the jacobian of the constraint in terms of key
|
|
|
|
*/
|
2009-12-18 11:05:47 +08:00
|
|
|
boost::function<Matrix(const Config& config)> G1_;
|
|
|
|
boost::function<Matrix(const Config& config)> G2_;
|
2009-11-20 00:50:18 +08:00
|
|
|
|
2010-02-07 09:18:17 +08:00
|
|
|
/** calculates the constraint function of the current config
|
|
|
|
* If the value is zero, the constraint is not active
|
|
|
|
* Use boost.bind to create the function object
|
|
|
|
* @param config is a configuration of all the variables
|
|
|
|
* @return the cost for each of p constraints, arranged in a vector
|
|
|
|
*/
|
|
|
|
boost::function<Vector(const Config& config)> g_;
|
|
|
|
|
2009-11-20 11:50:48 +08:00
|
|
|
/** keys for the constrained variables */
|
2010-01-19 13:33:44 +08:00
|
|
|
Key1 key1_;
|
|
|
|
Key2 key2_;
|
2009-11-20 00:50:18 +08:00
|
|
|
|
2009-11-20 11:50:48 +08:00
|
|
|
public:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Basic constructor
|
|
|
|
* @param key is the identifier for the variable constrained
|
2009-12-18 11:05:47 +08:00
|
|
|
* @param G gives the jacobian of the constraint function
|
2009-11-20 11:50:48 +08:00
|
|
|
* @param g is the constraint function
|
2010-07-19 03:55:53 +08:00
|
|
|
* @param dim is the size of the constraint (p)
|
|
|
|
* @param mu is the gain for the factor
|
2009-11-20 11:50:48 +08:00
|
|
|
*/
|
|
|
|
NonlinearConstraint2(
|
2009-12-18 10:39:02 +08:00
|
|
|
Vector (*g)(const Config& config),
|
2010-01-19 13:33:44 +08:00
|
|
|
const Key1& key1,
|
2009-12-18 11:05:47 +08:00
|
|
|
Matrix (*G1)(const Config& config),
|
2010-01-19 13:33:44 +08:00
|
|
|
const Key2& key2,
|
2009-12-18 11:05:47 +08:00
|
|
|
Matrix (*G2)(const Config& config),
|
2010-07-19 03:55:53 +08:00
|
|
|
size_t dim,
|
|
|
|
double mu = 1000.0);
|
2009-11-20 11:50:48 +08:00
|
|
|
|
2009-12-02 03:45:47 +08:00
|
|
|
/**
|
|
|
|
* Basic constructor with direct function objects
|
|
|
|
* Use boost.bind to construct the function objects
|
|
|
|
* @param key is the identifier for the variable constrained
|
2009-12-18 11:05:47 +08:00
|
|
|
* @param G gives the jacobian of the constraint function
|
2009-12-02 03:45:47 +08:00
|
|
|
* @param g is the constraint function
|
2010-07-19 03:55:53 +08:00
|
|
|
* @param dim is the size of the constraint (p)
|
|
|
|
* @param mu is the gain for the factor
|
2009-12-02 03:45:47 +08:00
|
|
|
*/
|
|
|
|
NonlinearConstraint2(
|
2009-12-18 10:39:02 +08:00
|
|
|
boost::function<Vector(const Config& config)> g,
|
2010-01-19 13:33:44 +08:00
|
|
|
const Key1& key1,
|
2009-12-18 11:05:47 +08:00
|
|
|
boost::function<Matrix(const Config& config)> G1,
|
2010-01-19 13:33:44 +08:00
|
|
|
const Key2& key2,
|
2009-12-18 11:05:47 +08:00
|
|
|
boost::function<Matrix(const Config& config)> G2,
|
2010-07-19 03:55:53 +08:00
|
|
|
size_t dim,
|
|
|
|
double mu = 1000.0);
|
2009-12-02 03:45:47 +08:00
|
|
|
|
2009-11-20 11:50:48 +08:00
|
|
|
/** Print */
|
|
|
|
void print(const std::string& s = "") const;
|
|
|
|
|
|
|
|
/** Check if two factors are equal */
|
|
|
|
bool equals(const Factor<Config>& f, double tol=1e-9) const;
|
|
|
|
|
2010-02-07 09:18:17 +08:00
|
|
|
/** Error function */
|
|
|
|
virtual inline Vector unwhitenedError(const Config& c) const { return g_(c); }
|
|
|
|
|
2010-07-19 03:55:53 +08:00
|
|
|
/** Linearize from config */
|
2010-02-05 00:08:11 +08:00
|
|
|
virtual boost::shared_ptr<GaussianFactor> linearize(const Config& c) const;
|
2009-11-20 11:50:48 +08:00
|
|
|
};
|
2009-11-20 00:50:18 +08:00
|
|
|
|
|
|
|
}
|