Don't do diagonal damping for variables not in linear system

release/4.3a0
dellaert 2014-03-04 01:06:15 -05:00
parent e321ac60c0
commit 63f8c75fb2
1 changed files with 27 additions and 27 deletions

View File

@ -121,39 +121,39 @@ GaussianFactorGraph LevenbergMarquardtOptimizer::buildDampedSystem(
gttic(damp); gttic(damp);
if (params_.verbosityLM >= LevenbergMarquardtParams::DAMPED) if (params_.verbosityLM >= LevenbergMarquardtParams::DAMPED)
cout << "building damped system with lambda " << state_.lambda << endl; cout << "building damped system with lambda " << state_.lambda << endl;
GaussianFactorGraph dampedSystem = linear;
{
double sigma = 1.0 / std::sqrt(state_.lambda);
dampedSystem.reserve(dampedSystem.size() + state_.values.size());
// Only retrieve diagonal vector when reuse_diagonal = false // Only retrieve diagonal vector when reuse_diagonal = false
if (params_.diagonalDamping && params_.reuse_diagonal_ == false) { if (params_.diagonalDamping && params_.reuse_diagonal_ == false) {
state_.hessianDiagonal = linear.hessianDiagonal(); state_.hessianDiagonal = linear.hessianDiagonal();
BOOST_FOREACH(Vector& v, state_.hessianDiagonal | map_values) BOOST_FOREACH(Vector& v, state_.hessianDiagonal | map_values) {
{ for (size_t aa = 0; aa < v.size(); aa++) {
for( size_t aa = 0 ; aa < v.size() ; aa++) v(aa) = std::min(std::max(v(aa), min_diagonal_), max_diagonal_);
{
v(aa) = std::min(std::max(v(aa), min_diagonal_),
max_diagonal_);
v(aa) = sqrt(v(aa)); v(aa) = sqrt(v(aa));
} }
} }
} } // reuse diagonal
// for each of the variables, add a prior // for each of the variables, add a prior
double sigma = 1.0 / std::sqrt(state_.lambda);
GaussianFactorGraph damped = linear;
damped.reserve(damped.size() + state_.values.size());
BOOST_FOREACH(const Values::KeyValuePair& key_value, state_.values) { BOOST_FOREACH(const Values::KeyValuePair& key_value, state_.values) {
size_t dim = key_value.value.dim(); size_t dim = key_value.value.dim();
Matrix A = Matrix::Identity(dim, dim); Matrix A = Matrix::Identity(dim, dim);
//Replace the identity matrix with diagonal of Hessian //Replace the identity matrix with diagonal of Hessian
if (params_.diagonalDamping) { if (params_.diagonalDamping)
try {
A.diagonal() = state_.hessianDiagonal.at(key_value.key); A.diagonal() = state_.hessianDiagonal.at(key_value.key);
} catch (std::exception e) {
// Don't attempt diagonal damping if no key given
} }
Vector b = Vector::Zero(dim); Vector b = Vector::Zero(dim);
SharedDiagonal model = noiseModel::Isotropic::Sigma(dim, sigma); SharedDiagonal model = noiseModel::Isotropic::Sigma(dim, sigma);
dampedSystem += boost::make_shared<JacobianFactor>(key_value.key, A, b, damped += boost::make_shared<JacobianFactor>(key_value.key, A, b, model);
model);
}
} }
gttoc(damp); gttoc(damp);
return dampedSystem; return damped;
} }
/* ************************************************************************* */ /* ************************************************************************* */