nlopt_solver.h
Go to the documentation of this file.
00001 //#include "eus_function.cpp"
00002 #include <sstream>
00003 #include "my_param.h"
00004 
00005 class NLoptSolver
00006 {
00007 public:
00008         NLoptSolver(double* x,
00009                         const double* x_min,
00010                         const double* x_max,
00011                         int (*f)(double*,double*), int (*df)(double*,double*),
00012                         int (*g)(double*,double*), int (*dg)(double*,double*),
00013                         int (*h)(double*,double*), int (*dh)(double*,double*),
00014                         int m_x, int m_g, int m_h,
00015                         double ftol, double xtol, double eqthre, int max_eval, double max_time,
00016                         Optimization::NLopt::Algorithm algorithm);
00017         ~NLoptSolver();
00018         int Optimize();
00023         double ObjectiveFunctionCost();
00025 
00029         void EqualityConstraintCost(double* g);
00031 
00035         void InequalityConstraintCost(double* h);
00036         //
00037         double *fbuf, *gbuf, *hbuf, *dfbuf, *dgbuf, *dhbuf, *x ;
00038         //
00039         //
00040         // util functions vv
00041         static void my_log(std::string comment) {
00042                 std::cout << comment << std::endl ;
00043         }
00044 
00045         static void my_log(std::string comment, double val){
00046                 std::ostringstream os;
00047                 os << comment ;
00048                 os << " " << val;
00049                 std::cout << os.str() << std::endl ;
00050         }
00051 
00052         static void my_log(std::string comment, double* val, uint cnt){
00053                 std::ostringstream os;
00054                 os << comment ;
00055                 if ( val && cnt > 0 ){
00056                         for ( uint i=0; i<cnt ; i++ ){
00057                                 os << " " << val[i] ;
00058                         }
00059                 }
00060                 std::cout << os.str() << std::endl ;
00061         }
00062 
00063         static void my_copy(double* in, double* out, int size){
00064                 for ( uint i=0 ; i<size ; i++ ){
00065                         out[i] = in[i] ;
00066                 }
00067         }
00068 
00069         void output_result(int result){
00070                 if(result == NLOPT_SUCCESS)
00071                 {
00072                         my_log("Succeed.");
00073                 }
00074                 else if(result == NLOPT_FTOL_REACHED)
00075                 {
00076                         my_log("Succeed: Relative tolerance on function value was reached. ");
00077                 }
00078                 else if(result == NLOPT_XTOL_REACHED)
00079                 {
00080                         my_log("Succeed: Relative tolerance on optimization parameters was reached. ");
00081                 }
00082                 else if(result == NLOPT_MAXEVAL_REACHED)
00083                 {
00084                         my_log("Succeed: Maximum number of function evaluations was reached. ");
00085                 }
00086                 else if(result == NLOPT_MAXTIME_REACHED)
00087                 {
00088                         my_log("Succeed: Maximum optimization time was reached. ");
00089                 }
00090                 else if(result == NLOPT_FAILURE)
00091                 {
00092                         my_log("Fail.");
00093                 }
00094                 else if(result == NLOPT_INVALID_ARGS)
00095                 {
00096                         my_log("Fail: Invalid arguments.");
00097                 }
00098                 else if(result == NLOPT_OUT_OF_MEMORY)
00099                 {
00100                         my_log("Fail: Run out of memory.");
00101                 }
00102                 else if(result == NLOPT_ROUNDOFF_LIMITED)
00103                 {
00104                         my_log("Fail: Roundoff errors limited progress.");
00105                 }
00106 
00107                 //
00108                 // if ( this->f ) (*(this->f))(this->x,fbuf) ;
00109                 // if ( this->g ) (*(this->g))(this->x,gbuf) ;
00110                 // if ( this->h ) (*(this->h))(this->x,hbuf) ;
00111                 //
00112                 my_log("Number of iteration.:   ", this->iteration - 1);
00113                 this->ObjectiveFunctionCost();
00114                 my_log("object function: ", this->fbuf[0]) ;
00115                 my_log("  | where     x: ", this->x, this->m_x) ;
00116                 this->EqualityConstraintCost(this->gbuf) ;
00117                 my_log("  |   eq constt: ", this->gbuf, this->m_g) ;
00118                 this->InequalityConstraintCost(this->hbuf) ;
00119                 my_log("  |  neq constt: ", this->hbuf, this->m_h) ;
00120         }
00121 
00122         void stop(){
00123                 nlopt_force_stop(this->solver) ;
00124         }
00125 
00126 private:
00127                 nlopt_opt solver;
00128                 nlopt_opt core_solver;
00129                 // double* x;
00130                 int (*f)(double*,double*), (*df)(double*,double*);
00132                 int (*g)(double*,double*), (*dg)(double*,double*);
00134                 int (*h)(double*,double*), (*dh)(double*,double*);
00135                 unsigned int m_x ;
00137                 unsigned int m_g;
00139                 unsigned int m_h;
00141                 unsigned int frequency;
00143                 unsigned int iteration;
00145                 unsigned int n_f;
00147                 unsigned int n_df;
00149                 unsigned int n_g;
00151                 unsigned int n_dg;
00153                 unsigned int n_h;
00155                 unsigned int n_dh;
00157 
00166                 static double ObjectiveFunctionWrapper(unsigned int n, const double* x, double* df, void* self);
00169 
00173                 void ObjectiveFunctionGradient(double* df);
00175 
00184                 static void EqualityConstraintWrapper(unsigned int m, double* g, unsigned int n, const double* x, double* dg, void* self);
00186 
00190                 void EqualityConstraintGradient(double* dg);
00192 
00201                 static void InequalityConstraintWrapper(unsigned int m, double* h, unsigned int n, const double* x, double* dh, void* self);
00203 
00207                 void InequalityConstraintGradient(double* dh);
00208 
00209 };


eus_nlopt
Author(s):
autogenerated on Wed Sep 16 2015 04:37:02