kthohr / optim

OptimLib: a lightweight C++ library of numerical optimization methods for nonlinear functions
https://optimlib.readthedocs.io/en/latest/
Apache License 2.0
809 stars 133 forks source link

How to write a constraint function for SUMT #69

Open soonsk-vcu opened 3 months ago

soonsk-vcu commented 3 months ago

Hello,

I am somewhat new to C++, and wanted to use this library for a constrained minimization problem. I am practicing with sumt using a simple case $f(x) = x^2$ with initial value $2$, and I intend to minimize $f(x)$ with constraint $x \geq 1$ (i.e. the solution should be $x=1$). However, running the code outputs $x=2$ instead.

From reading the function description on the website, I originally assumed that the constraint function needed to return a vector in the form $[g_1(x), g_2(x)... g_n(x)]$ matching constraint $g_i(x) \geq 0$ for each $i \in 1...n$. This assumption seems to be wrong though, given the output. Could someone please clarify the correct implementation for the constraint function? I have included my code below.

Many Thanks, Sam

double square(const Eigen::VectorXd& vals_inp, Eigen::VectorXd* grad_out, void* opt_data){
  return pow(vals_inp(0),2);
}

Eigen::VectorXd square_cons(const Eigen::VectorXd& vals_inp, Eigen::MatrixXd* jacob_out, void* constr_data){
  Eigen::VectorXd ones = Eigen::VectorXd::Ones(vals_inp.size());
  return vals_inp - ones;
}

Eigen::VectorXd test(){
  Eigen::VectorXd guess = 2.0 * Eigen::VectorXd::Ones(1);
  bool success = optim::sumt(guess, square, nullptr, square_cons, nullptr);

  return guess;
}
Kyota-exe commented 2 months ago

I am also wondering this, as there are no examples on how to define the constraint function.