nlopt_solver.h
Go to the documentation of this file.
1 //#include "eus_function.cpp"
2 #include <sstream>
3 #include "my_param.h"
4 
6 {
7 public:
8  NLoptSolver(double* x,
9  const double* x_min,
10  const double* x_max,
11  int (*f)(double*,double*), int (*df)(double*,double*),
12  int (*g)(double*,double*), int (*dg)(double*,double*),
13  int (*h)(double*,double*), int (*dh)(double*,double*),
14  int m_x, int m_g, int m_h,
15  double ftol, double xtol, double eqthre, int max_eval, double max_time,
17  ~NLoptSolver();
18  int Optimize();
23  double ObjectiveFunctionCost();
25 
29  void EqualityConstraintCost(double* g);
31 
35  void InequalityConstraintCost(double* h);
36  //
37  double *fbuf, *gbuf, *hbuf, *dfbuf, *dgbuf, *dhbuf, *x ;
38  //
39  //
40  // util functions vv
41  static void my_log(std::string comment) {
42  std::cout << comment << std::endl ;
43  }
44 
45  static void my_log(std::string comment, double val){
46  std::ostringstream os;
47  os << comment ;
48  os << " " << val;
49  std::cout << os.str() << std::endl ;
50  }
51 
52  static void my_log(std::string comment, double* val, uint cnt){
53  std::ostringstream os;
54  os << comment ;
55  if ( val && cnt > 0 ){
56  for ( uint i=0; i<cnt ; i++ ){
57  os << " " << val[i] ;
58  }
59  }
60  std::cout << os.str() << std::endl ;
61  }
62 
63  static void my_copy(double* in, double* out, int size){
64  for ( uint i=0 ; i<size ; i++ ){
65  out[i] = in[i] ;
66  }
67  }
68 
69  void output_result(int result){
70  if(result == NLOPT_SUCCESS)
71  {
72  my_log("Succeed.");
73  }
74  else if(result == NLOPT_FTOL_REACHED)
75  {
76  my_log("Succeed: Relative tolerance on function value was reached. ");
77  }
78  else if(result == NLOPT_XTOL_REACHED)
79  {
80  my_log("Succeed: Relative tolerance on optimization parameters was reached. ");
81  }
82  else if(result == NLOPT_MAXEVAL_REACHED)
83  {
84  my_log("Succeed: Maximum number of function evaluations was reached. ");
85  }
86  else if(result == NLOPT_MAXTIME_REACHED)
87  {
88  my_log("Succeed: Maximum optimization time was reached. ");
89  }
90  else if(result == NLOPT_FAILURE)
91  {
92  my_log("Fail.");
93  }
94  else if(result == NLOPT_INVALID_ARGS)
95  {
96  my_log("Fail: Invalid arguments.");
97  }
98  else if(result == NLOPT_OUT_OF_MEMORY)
99  {
100  my_log("Fail: Run out of memory.");
101  }
102  else if(result == NLOPT_ROUNDOFF_LIMITED)
103  {
104  my_log("Fail: Roundoff errors limited progress.");
105  }
106 
107  //
108  // if ( this->f ) (*(this->f))(this->x,fbuf) ;
109  // if ( this->g ) (*(this->g))(this->x,gbuf) ;
110  // if ( this->h ) (*(this->h))(this->x,hbuf) ;
111  //
112  my_log("Number of iteration.: ", this->iteration - 1);
113  this->ObjectiveFunctionCost();
114  my_log("object function: ", this->fbuf[0]) ;
115  my_log(" | where x: ", this->x, this->m_x) ;
116  this->EqualityConstraintCost(this->gbuf) ;
117  my_log(" | eq constt: ", this->gbuf, this->m_g) ;
118  this->InequalityConstraintCost(this->hbuf) ;
119  my_log(" | neq constt: ", this->hbuf, this->m_h) ;
120  }
121 
122  void stop(){
123  nlopt_force_stop(this->solver) ;
124  }
125 
126 private:
127  nlopt_opt solver;
128  nlopt_opt core_solver;
129  // double* x;
130  int (*f)(double*,double*), (*df)(double*,double*);
132  int (*g)(double*,double*), (*dg)(double*,double*);
134  int (*h)(double*,double*), (*dh)(double*,double*);
135  unsigned int m_x ;
137  unsigned int m_g;
139  unsigned int m_h;
141  unsigned int frequency;
143  unsigned int iteration;
145  unsigned int n_f;
147  unsigned int n_df;
149  unsigned int n_g;
151  unsigned int n_dg;
153  unsigned int n_h;
155  unsigned int n_dh;
157 
166  static double ObjectiveFunctionWrapper(unsigned int n, const double* x, double* df, void* self);
169 
173  void ObjectiveFunctionGradient(double* df);
175 
184  static void EqualityConstraintWrapper(unsigned int m, double* g, unsigned int n, const double* x, double* dg, void* self);
186 
190  void EqualityConstraintGradient(double* dg);
192 
201  static void InequalityConstraintWrapper(unsigned int m, double* h, unsigned int n, const double* x, double* dh, void* self);
203 
207  void InequalityConstraintGradient(double* dh);
208 
209 };
double ObjectiveFunctionCost()
評価関数の和を計算する
int(* f)(double *, double *)
Definition: nlopt_solver.h:130
unsigned int n_df
評価関数の勾配の計算回数
Definition: nlopt_solver.h:147
void InequalityConstraintGradient(double *dh)
不等式制約条件の勾配
unsigned int m_g
等式制約条件の次元
Definition: nlopt_solver.h:137
unsigned int m_x
Definition: nlopt_solver.h:135
int(*)(*) df(double *, double *)
Definition: nlopt_solver.h:130
unsigned int n_g
等式制約条件の計算回数
Definition: nlopt_solver.h:149
static void InequalityConstraintWrapper(unsigned int m, double *h, unsigned int n, const double *x, double *dh, void *self)
不等式制約条件のラッパー
double * fbuf
Definition: nlopt_solver.h:37
unsigned int frequency
最適化計算の出力頻度
Definition: nlopt_solver.h:141
int result
Algorithm
NLoptに実装されているアルゴリズム
Definition: my_param.h:41
void InequalityConstraintCost(double *h)
不等式制約条件
double * x
Definition: nlopt_solver.h:37
static void EqualityConstraintWrapper(unsigned int m, double *g, unsigned int n, const double *x, double *dg, void *self)
等式制約条件のラッパー
double * gbuf
Definition: nlopt_solver.h:37
static void my_log(std::string comment)
Definition: nlopt_solver.h:41
double * hbuf
Definition: nlopt_solver.h:37
unsigned int n_f
評価関数の計算回数
Definition: nlopt_solver.h:145
void ObjectiveFunctionGradient(double *df)
評価関数の和の勾配を計算する
double * dfbuf
Definition: nlopt_solver.h:37
int(* h)(double *, double *)
不等式制約条件
Definition: nlopt_solver.h:134
void EqualityConstraintCost(double *g)
等式制約条件
int(* g)(double *, double *)
等式制約条件
Definition: nlopt_solver.h:132
double * dhbuf
Definition: nlopt_solver.h:37
double * dgbuf
Definition: nlopt_solver.h:37
void output_result(int result)
Definition: nlopt_solver.h:69
unsigned int iteration
最適化計算のイテレーション数
Definition: nlopt_solver.h:143
static void my_copy(double *in, double *out, int size)
Definition: nlopt_solver.h:63
nlopt_opt solver
Definition: nlopt_solver.h:127
unsigned int n_dh
不等式制約条件の勾配の計算回数
Definition: nlopt_solver.h:155
void EqualityConstraintGradient(double *dg)
等式制約条件の勾配
unsigned int n_dg
等式制約条件の勾配の計算回数
Definition: nlopt_solver.h:151
static void my_log(std::string comment, double val)
Definition: nlopt_solver.h:45
unsigned int n_h
不等式制約条件の計算回数
Definition: nlopt_solver.h:153
unsigned int m_h
不等式制約条件の次元
Definition: nlopt_solver.h:139
NLoptSolver(double *x, const double *x_min, const double *x_max, int(*f)(double *, double *), int(*df)(double *, double *), int(*g)(double *, double *), int(*dg)(double *, double *), int(*h)(double *, double *), int(*dh)(double *, double *), int m_x, int m_g, int m_h, double ftol, double xtol, double eqthre, int max_eval, double max_time, Optimization::NLopt::Algorithm algorithm)
Definition: nlopt_solver.cpp:8
static double ObjectiveFunctionWrapper(unsigned int n, const double *x, double *df, void *self)
評価関数のラッパー
nlopt_opt core_solver
Definition: nlopt_solver.h:128
int(*)(*) dh(double *, double *)
Definition: nlopt_solver.h:134
int(*)(*) dg(double *, double *)
Definition: nlopt_solver.h:132
static void my_log(std::string comment, double *val, uint cnt)
Definition: nlopt_solver.h:52


eus_nlopt
Author(s):
autogenerated on Fri May 14 2021 02:51:41