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

How to write a constraint function for SUMT #69

Open
soonsk-vcu opened this issue Jun 14, 2024 · 2 comments
Open

How to write a constraint function for SUMT #69

soonsk-vcu opened this issue Jun 14, 2024 · 2 comments

Comments

@soonsk-vcu
Copy link

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
Copy link

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

@alesof
Copy link

alesof commented Nov 25, 2024

I tested some functions and it seems to work, therefore i created a PR (#71) to add a C++ example. Note that doc is not building so, check the api docs from the source code.

Anyway, thanks @kthohr this is great!

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

No branches or pull requests

3 participants