00001
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 ;
00038
00039
00040
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 my_log("Number of iteration.: ", this->iteration - 1);
00108 my_log("object function: ", this->fbuf[0]) ;
00109 my_log(" | where x: ", this->x, this->m_x) ;
00110
00111 my_log(" | eq constt: ", this->gbuf, this->m_g) ;
00112
00113 my_log(" | neq constt: ", this->hbuf, this->m_h) ;
00114 }
00115
00116 void stop(){
00117 nlopt_force_stop(this->solver) ;
00118 }
00119
00120 private:
00121 nlopt_opt solver;
00122 double* x;
00123 int (*f)(double*,double*), (*df)(double*,double*);
00125 int (*g)(double*,double*), (*dg)(double*,double*);
00127 int (*h)(double*,double*), (*dh)(double*,double*);
00128 unsigned int m_x ;
00130 unsigned int m_g;
00132 unsigned int m_h;
00134 unsigned int frequency;
00136 unsigned int iteration;
00138 unsigned int n_f;
00140 unsigned int n_df;
00142 unsigned int n_g;
00144 unsigned int n_dg;
00146 unsigned int n_h;
00148 unsigned int n_dh;
00150
00159 static double ObjectiveFunctionWrapper(unsigned int n, const double* x, double* df, void* self);
00162
00166 void ObjectiveFunctionGradient(double* df);
00168
00177 static void EqualityConstraintWrapper(unsigned int m, double* g, unsigned int n, const double* x, double* dg, void* self);
00179
00183 void EqualityConstraintGradient(double* dg);
00185
00194 static void InequalityConstraintWrapper(unsigned int m, double* h, unsigned int n, const double* x, double* dh, void* self);
00196
00200 void InequalityConstraintGradient(double* dh);
00201
00202 };