Skip to content
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

add definition of constraints #1789

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from

Conversation

yetongumich
Copy link
Contributor

@yetongumich yetongumich commented Aug 6, 2024

First PR in the 4 PRs to implement nonlinear constrained optimization in GTSAM.

  1. Introducing factors representing equality and inequality constraints; refactor existing constraint factors to inherit from it.
  2. Implementation of existing constrained optimization methods that are compatible with the current gtsam framework, including penalty methods, augmented Lagrangian methods, SQP methods (potentially also interior point methods), with a document explaining the math of the implementations.
  3. Constraint manifold optimization that solves problems with equality constraints.
  4. Constraint manifold with corners optimization that solves problems with both equality and inequality constraints.
  5. (potentially 1 additional PR) That solves an equality-constrained factor graph using nonlinear variable elimination (resulting in an behavior identical to iLQR or DDP).

This PR

  • Introduce NonlinearEqualityConstraint and NonlinearInequalityConstraint inheriting from NoiseModelFactor to represent constraints.
  • Refactor the existing classesNonlinearEquality to inherit from NonlinearEqualityConstraint, and BoundingConstraint to inherit from NonlinearInequalityConstraint.

@dellaert
Copy link
Member

CI fails. Talking about CI, try to save on CO2 by not pushing every commit, but only after you think CI will pass.

@yetongumich
Copy link
Contributor Author

CI fails. Talking about CI, try to save on CO2 by not pushing every commit, but only after you think CI will pass.

It seems pretty weird that only the Windows CI fails. Unfortunately I do not have a Windows environment to test this on. I was trying to tweak with different changes to see which causes the Windows CI to fail.

*/

#include <gtsam/constraint/NonlinearInequalityConstraint.h>

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Include "
#include <gtsam/inference/FactorGraph-inst.h>"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this line, CI still reports the same error in Windows environments.

@dellaert
Copy link
Member

@yetongatech error is due to a argument type not being recognized:

D:\a\gtsam\gtsam\gtsam/inference/FactorGraph-inst.h(69): error C2664: 'double gtsam::NoiseModelFactor::error(const gtsam::Values &) const': cannot convert argument 1 from 'const gtsam::HybridValues' to 'const gtsam::Values &'

Try figuring out what type you want and somehow being explicit? I just looked at the error above but maybe you can dig deeper.

Copy link
Member

@dellaert dellaert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool ! Some comments/questions.
Still need to pass CI

@@ -6,6 +6,7 @@ project(gtsam LANGUAGES CXX)
set (gtsam_subdirs
base
basis
constraint
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this directory only contains constraint classes, this is ok. Otherwise (if optimization code will land here) could we rename this to constrained ?

double k_;

public:
SoftPlusFunction(const double k = 1) : Base(), k_(k) {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doxygen block describing argument

double b_;

public:
SmoothRampPoly3(const double epsilon = 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doxygen block describing argument

double a_;

public:
SmoothRampPoly2(const double epsilon = 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doxygen block describing argument

* @brief Constructor.
*
* @param factor NoiseModel factor.
* @param tolerance vector representing tolerance in each dimension.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stale?

Vector unwhitenedError(const Values& x, OptionalMatrixVecType H = nullptr) const override {
VALUE x1 = x.at<VALUE>(key());
if (H) {
return evaluateError(x1, &(H->front()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does the base class not have unwhitenedError? Why not? Would that not be easier?

@@ -60,14 +59,23 @@ struct BoundingConstraint1: public NoiseModelFactorN<VALUE> {
virtual double value(const X& x, OptionalMatrixType H =
OptionalNone) const = 0;

/** active when constraint *NOT* met */
bool active(const Values& c) const override {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is active not used? Or is it in base class? Backwards compat...

// note: still active at equality to avoid zigzagging
double x = value(c.at<X>(this->key()));
return (isGreaterThan_) ? x <= threshold_ : x >= threshold_;
Vector unwhitenedExpr(const Values& x, OptionalMatrixVecType H = nullptr) const override {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same, base class unwhitenedError?

// note: still active at equality to avoid zigzagging
double x = value(c.at<X1>(this->key1()), c.at<X2>(this->key2()));
return (isGreaterThan_) ? x <= threshold_ : x >= threshold_;
Vector unwhitenedExpr(const Values& x, OptionalMatrixVecType H = nullptr) const override {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same question about active
what is unwhitenedExpr? Not documented

Vector evaluateError(const X1& x1, const X2& x2,
OptionalMatrixType H1, OptionalMatrixType H2) const override {
OptionalMatrixType H1 = nullptr, OptionalMatrixType H2 = nullptr) const {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You used {} elsewhere

@ProfFan
Copy link
Collaborator

ProfFan commented Sep 26, 2024

I think if you rebase on current develop it should fix stuff

@yetongumich
Copy link
Contributor Author

I think if you rebase on current develop it should fix stuff

Seems still not working

@dellaert
Copy link
Member

@yetongatech you might have to investigate a bit to get past windows issue. None of us have the answers exactly :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants