-
Notifications
You must be signed in to change notification settings - Fork 919
Feature/alm #2414
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yetongumich
wants to merge
6
commits into
develop
Choose a base branch
from
feature/ALM
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Feature/alm #2414
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
caf9a1e
implement bound constrained augmented lagrangian
tianxin-shen 8d92d73
Fix naming
dellaert a1ac087
Fix name in comment
dellaert 881be79
bug fix
tianxin-shen 88b59fe
convergence check update
tianxin-shen 8dc42f4
Apply suggestions from code review
dellaert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| /* ---------------------------------------------------------------------------- | ||
|
|
||
| * 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 | ||
|
|
||
| * -------------------------------------------------------------------------- */ | ||
|
|
||
| /** | ||
| * @file AugmentedLagrangian.cpp | ||
| * @brief Augmented Lagrangian method for nonlinear constrained optimization. | ||
| * @author Yetong Zhang | ||
| * @date Aug 3, 2024 | ||
| */ | ||
|
|
||
dellaert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #include <gtsam/constrained/AugmentedLagrangian.h> | ||
| #include <gtsam/constrained/AugmentedLagrangianOptimizer.h> | ||
| #include <gtsam/slam/AntiFactor.h> | ||
| #include <iostream> | ||
|
|
||
| namespace gtsam { | ||
|
|
||
| /** A factor that adds a constant bias term to the original factor. | ||
| * This factor is used in augmented Lagrangian optimizer to create biased cost | ||
| * functions. | ||
| * Note that the noise model is stored twice (both in original factor and the | ||
| * noisemodel of substitute factor. The noisemodel in the original factor will | ||
| * be ignored. */ | ||
| class GTSAM_EXPORT BiasedFactor : public NoiseModelFactor { | ||
| protected: | ||
| typedef NoiseModelFactor Base; | ||
| typedef BiasedFactor This; | ||
|
|
||
| // original factor | ||
| Base::shared_ptr originalFactor_; | ||
| Vector bias_; | ||
|
|
||
| public: | ||
| typedef std::shared_ptr<This> shared_ptr; | ||
|
|
||
| /** Default constructor for I/O only */ | ||
| BiasedFactor() {} | ||
|
|
||
| /** Destructor */ | ||
| ~BiasedFactor() override {} | ||
|
|
||
| /** | ||
| * Constructor | ||
| * @param originalFactor original factor on X | ||
| * @param bias the bias term | ||
| */ | ||
| BiasedFactor(const Base::shared_ptr& originalFactor, const Vector& bias) | ||
| : Base(originalFactor->noiseModel(), originalFactor->keys()), | ||
| originalFactor_(originalFactor), | ||
| bias_(bias) {} | ||
|
|
||
| /** | ||
| * Error function *without* the NoiseModel, \f$ z-h(x) \f$. | ||
| * Override this method to finish implementing an N-way factor. | ||
| * If the optional arguments is specified, it should compute | ||
| * both the function evaluation and its derivative(s) in H. | ||
| */ | ||
| virtual Vector unwhitenedError( | ||
| const Values& x, | ||
| gtsam::OptionalMatrixVecType H = nullptr) const override { | ||
| return originalFactor_->unwhitenedError(x, H) + bias_; | ||
| } | ||
|
|
||
| /** print */ | ||
| void print(const std::string& s, const KeyFormatter& keyFormatter = | ||
| DefaultKeyFormatter) const override { | ||
| std::cout << s << "BiasedFactor " << bias_.transpose() | ||
| << " version of:" << std::endl; | ||
| originalFactor_->print(s, keyFormatter); | ||
| } | ||
|
|
||
| /** Return a deep copy of this factor. */ | ||
| gtsam::NonlinearFactor::shared_ptr clone() const override { | ||
| return std::static_pointer_cast<gtsam::NonlinearFactor>( | ||
| gtsam::NonlinearFactor::shared_ptr(new This(*this))); | ||
| } | ||
|
|
||
| }; // \class BiasedFactor | ||
|
|
||
| NonlinearFactorGraph AugmentedLagrangianFunction( | ||
| const ConstrainedOptProblem& problem, const std::vector<Vector>& lambdaEq, | ||
| const std::vector<double>& lambdaIneq, double muEq, double muIneq, | ||
| InequalityPenaltyFunction::shared_ptr ineqConstraintPenaltyFunction, | ||
| const double epsilon) { | ||
| // Initialize by adding in cost factors. | ||
| NonlinearFactorGraph graph = problem.costs(); | ||
|
|
||
| // Create factors corresponding to equality constraints. | ||
| const NonlinearEqualityConstraints& eqConstraints = problem.eConstraints(); | ||
| for (size_t i = 0; i < eqConstraints.size(); i++) { | ||
| const auto& constraint = eqConstraints.at(i); | ||
| Vector bias = lambdaEq[i] / muEq * constraint->sigmas(); | ||
| auto penalty_l2 = constraint->penaltyFactor(muEq); | ||
| graph.emplace_shared<BiasedFactor>(penalty_l2, bias); | ||
dellaert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // Create factors corresponding to penalty terms of inequality constraints. | ||
| const NonlinearInequalityConstraints& ineqConstraints = | ||
| problem.iConstraints(); | ||
| graph.add(ineqConstraints.penaltyGraphCustom(ineqConstraintPenaltyFunction, | ||
| muIneq)); | ||
|
|
||
| // Create factors corresponding to Lagrange multiplier terms of i-constraints. | ||
| for (size_t i = 0; i < ineqConstraints.size(); i++) { | ||
| const auto& constraint = ineqConstraints.at(i); | ||
| Vector bias = lambdaIneq[i] / epsilon * constraint->sigmas(); | ||
| auto penalty_l2 = constraint->penaltyFactorEquality(epsilon); | ||
| graph.emplace_shared<BiasedFactor>(penalty_l2, bias); | ||
| graph.emplace_shared<AntiFactor>(penalty_l2); | ||
| } | ||
|
|
||
| return graph; | ||
| } | ||
|
|
||
| } // namespace gtsam | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /* ---------------------------------------------------------------------------- | ||
|
|
||
| * 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 | ||
|
|
||
| * -------------------------------------------------------------------------- */ | ||
|
|
||
| /** | ||
| * @file AugmentedLagrangian.h | ||
| * @brief Augmented Lagrangian function implemented as a nonlinear factor | ||
| * graph. | ||
| * @author Yetong Zhang | ||
| * @date Aug 3, 2024 | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <gtsam/constrained/ConstrainedOptProblem.h> | ||
|
|
||
| namespace gtsam { | ||
|
|
||
| /** | ||
| * Lagrange dual function for equality constraints and inequality constraints | ||
| * m(x) = 0.5 * ||f(x)||^2 - lambdaEq * h(x) + 0.5 * muEq * ||h(x)||^2 | ||
| * - lambdaIneq * g(x) + 0.5 * muIneq * ||g(x)_-||^2 | ||
| * To express in nonlinear least squares form, it is rewritten as | ||
| * m(x) | ||
| * = m(x) + 0.5 * epsilon * ||g(x)||^2 | ||
| * = 0.5 * ||f(x)||^2 | ||
| * + (0.5 * muEq * ||h(x)||^2 - lambdaEq * h(x)) | ||
| * + (0.5 * epsilon * ||g(x)||^2 - lambdaIneq * g(x)) | ||
| * + 0.5 * muIneq * ||g(x)_-||^2 | ||
| * - 0.5 * epsilon * ||g(x)||^2 | ||
| * = 0.5 * ||f(x)||^2 | ||
| * + 0.5 * muEq * ||h(x)- lambdaEq/muEq||^2 | ||
| * + 0.5 * epsilon * ||g(x)-lambdaIneq/epsilon||^2 | ||
| * + 0.5 * muIneq * ||g(x)_-||^2 | ||
| * - 0.5 * epsilon * ||g(x)||^2 | ||
| * - c | ||
| * where | ||
| * c = ||lambdaEq||^2 / (2 * muEq) + ||lambdaIneq||^2 / (2 * epsilon) | ||
| * is a constant term, | ||
| * and epsilon can be any positive scalar value. | ||
| * | ||
| * Notice: the purpose of epsilon is to incorporate (-lambdaIneq * g(x)) in | ||
| * nonlinear least squares form. To do so, we manually create an additional | ||
| * term (0.5 * epsilon * ||g(x)||^2), which is added and then subtracted in | ||
| * the merit function. The term (-lambdaIneq * g(x)) and (0.5 * epsilon * | ||
| * ||g(x)||^2) can be combined as a least-square term, and the subtraction of | ||
| * (0.5 * epsilon * ||g(x)||^2) can be performed with anti-factor. | ||
| * @return: factor graph representing m(x) + 0.5 * epsilon * ||g(x)||^2 + c | ||
| */ | ||
| GTSAM_EXPORT NonlinearFactorGraph AugmentedLagrangianFunction( | ||
| const ConstrainedOptProblem& problem, const std::vector<Vector>& lambdaEq, | ||
| const std::vector<double>& lambdaIneq, double muEq, double muIneq, | ||
| InequalityPenaltyFunction::shared_ptr ineqConstraintPenaltyFunction, | ||
| const double epsilon = 1.0); | ||
| } // namespace gtsam |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.