Skip to content

Conversation

@PatWie
Copy link
Owner

@PatWie PatWie commented Mar 13, 2025

It would be nice to build a type system on top of FunctionXd which supports hessian and gradient computation.

So far the API is

class QuadraticObjective2 : public Function2d<QuadraticObjective2> {
public:
  EIGEN_MAKE_ALIGNED_OPERATOR_NEW

  ScalarType operator()(const VectorType &x,
                        VectorType *gradient = nullptr) const {
    if (gradient) {
     .....
    }
    return (x(0) - 1) * (x(0) - 1) + (x(1) - 2) * (x(1) - 2);
  }
};



//
// Main demo: Solve the constrained problem with multiple constraints
//
int main() {
  // Initial guess for x. We choose a starting point that is not optimal.
  QuadraticObjective2::VectorType x(2);
  x << 1, 1;
  cppoptlib::function::AnyFunction objective = QuadraticObjective2();


  cppoptlib::function::AnyFunction eq = EqualityConstraint2();
  cppoptlib::function::AnyFunction ineq = InequalityConstraint3();

  cppoptlib::function::ConstrainedOptimizationProblem prob(
      objective,
      /* equality constraints */ {eq},
      /* inequality constraints */ {ineq});

  cppoptlib::solver::Lbfgs<AnyFunction2d> unconstrained_solver;
  cppoptlib::solver::AugmentedLagrangian solver(prob, unconstrained_solver);
  cppoptlib::solver::AugmentedLagrangeState<double, 2> l_state(x, 1, 1, 1.0);

  // Run the solver.
  auto [solution, solver_state] = solver.Minimize(prob, l_state);

  // Output the results.
  std::cout << "Optimal f(x): " << objective(solution.x) << std::endl;
  std::cout << "Optimal x: " << solution.x.transpose() << std::endl;
  std::cout << "Iterations: " << solver_state.num_iterations << std::endl;
  std::cout << "Solver status: " << solver_state.status << std::endl;

  
}

However, with the expressions not being derived from AnyFunctino (which is abstract), template type deduction failes here

  cppoptlib::function::ConstrainedOptimizationProblem prob(
      objective,
      /* equality constraints */ {eq},
      /* inequality constraints */ {-ineq});

as the latter is not an AnyFunction but a MulExpression. The big advantage is that we can write

// Equality penalty: P(x) = 0.5 * [f(x) - g(x)]^2
template <typename F> auto quadraticEqualityPenalty(const F &c) {
  return 0.5 * (c * c);
}

@PatWie PatWie force-pushed the expression-templates-de branch from 11c2544 to 0553570 Compare March 17, 2025 10:51
To add back a bit of flexibility solvers consume arbitrary functions and
operators +,-,* on top of functions are supported.
@PatWie PatWie force-pushed the expression-templates-de branch from 0553570 to e45e101 Compare March 17, 2025 10:53
#define INCLUDE_CPPOPTLIB_FUNCTION_H_

#include "Eigen/Core"
#include "function_base.h"

Choose a reason for hiding this comment

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

[cpplint] reported by reviewdog 🐶
Include the directory when naming header files [build/include_subdir] [4]


#include "Eigen/Core"
#include "function_base.h"
#include "function_expressions.h"

Choose a reason for hiding this comment

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

[cpplint] reported by reviewdog 🐶
Include the directory when naming header files [build/include_subdir] [4]

#include "Eigen/Core"
#include "function_base.h"
#include "function_expressions.h"
#include "function_penalty.h"

Choose a reason for hiding this comment

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

[cpplint] reported by reviewdog 🐶
Include the directory when naming header files [build/include_subdir] [4]

#include "function_base.h"
#include "function_expressions.h"
#include "function_penalty.h"
#include "function_problem.h"

Choose a reason for hiding this comment

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

[cpplint] reported by reviewdog 🐶
Include the directory when naming header files [build/include_subdir] [4]

}
}

virtual std::unique_ptr<FunctionInterface<TScalar, TMode, TDimension>> clone()

Choose a reason for hiding this comment

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

[cpplint] reported by reviewdog 🐶
"virtual" is redundant since function is already declared as "override" [readability/inheritance] [4]

// Shrink the simplex toward the best vertex.
void shrink(matrix_t &s, std::vector<int> &idx, std::vector<scalar_t> &f,
const function_t &function) {
void shrink(MatrixType &s, std::vector<int> &idx, std::vector<ScalarType> &f,

Choose a reason for hiding this comment

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

[cpplint] reported by reviewdog 🐶
Is this a non-const reference? If so, make const or use a pointer: std::vector &f [runtime/references] [2]

#include <utility>

#include "../function.h"
#include "progress.h"

Choose a reason for hiding this comment

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

[cpplint] reported by reviewdog 🐶
Include the directory when naming header files [build/include_subdir] [4]


#include "cppoptlib/function.h"

using namespace cppoptlib::function;

Choose a reason for hiding this comment

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

[cpplint] reported by reviewdog 🐶
Do not use namespace using-directives. Use using-declarations instead. [build/namespaces] [5]

: public cppoptlib::function::FunctionCRTP<ConstantFunction, double,
DifferentiabilityMode::None> {
double c;
ConstantFunction(double c_) : c(c_) {}

Choose a reason for hiding this comment

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

[cpplint] reported by reviewdog 🐶
Single-parameter constructors should be marked explicit. [runtime/explicit] [4]

MatrixType *hessian = nullptr

) const override {
) const {

Choose a reason for hiding this comment

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

[cpplint] reported by reviewdog 🐶
Closing ) should be moved to the previous line [whitespace/parens] [2]

@PatWie PatWie closed this Mar 17, 2025
@PatWie PatWie deleted the expression-templates-de branch March 17, 2025 10:58
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.

2 participants