From 12b4017126e231a16898997f2f5c03984bf291ad Mon Sep 17 00:00:00 2001 From: Laurent Belcour Date: Wed, 18 Sep 2013 11:24:01 +0200 Subject: [PATCH] Working Ipopt solver --- .../plugins/nonlinear_fitter_ipopt/fitter.cpp | 46 +++++++++++++++++-- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/sources/plugins/nonlinear_fitter_ipopt/fitter.cpp b/sources/plugins/nonlinear_fitter_ipopt/fitter.cpp index d489e84..f33e0d7 100644 --- a/sources/plugins/nonlinear_fitter_ipopt/fitter.cpp +++ b/sources/plugins/nonlinear_fitter_ipopt/fitter.cpp @@ -121,6 +121,11 @@ class altaNLP : public Ipopt::TNLP virtual bool eval_grad_f(Ipopt::Index n, const Ipopt::Number* x, bool new_x, Ipopt::Number* grad_f) { + // Update the parameters vector + vec _p(n); + for(int i=0; isetParameters(_p); + // Clean the value for(int i=0; isize(); ++s) { vec _x = _d->get(s); + + // Extract the objective from the current vector + vec _di = vec(_f->dimY()); + for(int i=0; i<_f->dimY(); ++i) + { + _di[i] = _x[_f->dimX() + i]; + } + + // Compute the difference vector and add its + // components to the obj_value + vec _y = _di - (*_f)(_x); // Get the jacobian of the function at position x_i for the current // set of parameters (set prior to function call) @@ -140,7 +156,14 @@ class altaNLP : public Ipopt::TNLP // vector row for(int i=0; i<_f->dimY(); ++i) { - grad_f[j] += -_jac[i*_f->nbParameters() + j]; + if(_y[i] > 0.0) + { + grad_f[j] += -_jac[i*_f->nbParameters() + j]; + } + else + { + grad_f[j] += _jac[i*_f->nbParameters() + j]; + } } } } @@ -179,7 +202,12 @@ class altaNLP : public Ipopt::TNLP Ipopt::Number obj_value, const Ipopt::IpoptData* ip_data, Ipopt::IpoptCalculatedQuantities* ip_cq) { + // Update the parameters vector + vec _p(n); + for(int i=0; isetParameters(_p); } + protected: const data* _d; @@ -248,11 +276,21 @@ bool nonlinear_fitter_ipopt::fit_data(const data* d, function* fit, const argume { #ifdef DEBUG Ipopt::Index iter_count = app->Statistics()->IterationCount(); - std::cout << "<> number of iterations: " << iter_count << std::endl + std::cout << "<> number of iterations: " << iter_count << std::endl; +#endif + std::cout << "<> found parameters: " << nf->parameters() << std::endl; + + return true; + } + else if(status == Ipopt::Maximum_Iterations_Exceeded) + { + std::cout << "<> the maximum number of iteration has been reached" << std::endl; +#ifdef DEBUG + Ipopt::Index iter_count = app->Statistics()->IterationCount(); + std::cout << "<> number of iterations: " << iter_count << std::endl; #endif - std::cout << "<> found parameters: " << p << std::endl; + std::cout << "<> found parameters: " << nf->parameters() << std::endl; - nf->setParameters(p); return true; } else -- GitLab