Cpp optimization - icsl-Jeon/image-tracking GitHub Wiki

Roboptim

some errors detected.. http://roboptim.net/getting-started.html

ceres-solver

only bounded constraint case can be solved http://ceres-solver.org/features.html

Opt++

https://software.sandia.gov/opt++/opt++2.4_doc/html/SetUp.html can be used in general nonlinear case

Ipopt

too complicated... , redundant handcodings are required

sudo apt-get install coinor-libipopt-dev

NLopt

good to use. do not forget target_link_libraries(nlopt) https://nlopt.readthedocs.io/en/latest/NLopt_Tutorial/

/**
 * I want test nonlinear optimization
**/
#include <iostream>
#include "math.h"
#include <nlopt.h>
#include "ctime"

/**
 *
 */

double myfunc(unsigned n, const double *x, double *grad, void *my_func_data)
{
    if (grad) {
        grad[0] = 0.0;
        grad[1] = 0.5 / sqrt(x[1]);
    }
    return sqrt(x[1]);
}

typedef struct {
    double a, b;
} my_constraint_data;

/** manually add constraint one by one  **/
double myconstraint(unsigned n, const double *x, double *grad, void *data)
{
    my_constraint_data *d = (my_constraint_data *) data;
    double a = d->a, b = d->b;
    if (grad) {
        grad[0] = 3 * a * (a*x[0] + b) * (a*x[0] + b);
        grad[1] = -1.0;
    }
    // g_i <= 0
    return ((a*x[0] + b) * (a*x[0] + b) * (a*x[0] + b) - x[1]);
}


int main()
{

    clock_t begin = clock();

    double lb[2] = { -HUGE_VAL, 0 }; /* lower bounds */
    nlopt_opt opt;
    opt = nlopt_create(NLOPT_LD_SLSQP, 2); /* algorithm and dimensionality */
    nlopt_set_lower_bounds(opt, lb);
    nlopt_set_min_objective(opt, myfunc, NULL);
    my_constraint_data data[2] = { {2,0}, {-1,1} };
    nlopt_add_inequality_constraint(opt, myconstraint, &data[0], 1e-8);
    nlopt_add_inequality_constraint(opt, myconstraint, &data[1], 1e-8);

    nlopt_set_xtol_rel(opt, 1e-4);

    double x[2] = { 1.234, 5.678 };  // initial guess
    double minf; /* `*`the` `minimum` `objective` `value,` `upon` `return`*` */
    if (nlopt_optimize(opt, x, &minf) < 0) {
        printf("nlopt failed!\n");
    }
    else {
        clock_t end = clock();
        double elapsed_secs = double(end - begin);

        printf("found minimum at f(%g,%g) = %0.10g\n / elapsed time : %d", x[0], x[1], minf,elapsed_secs);
    }


}
⚠️ **GitHub.com Fallback** ⚠️