1 |
tim |
995 |
#ifndef _NLMODEL_H_
|
2 |
|
|
#define _NLMODEL_H_
|
3 |
|
|
|
4 |
|
|
#include <vector>
|
5 |
|
|
|
6 |
|
|
#include "SymMatrix.hpp"
|
7 |
|
|
#include "Functor.hpp"
|
8 |
|
|
|
9 |
|
|
using namespace std;
|
10 |
|
|
|
11 |
|
|
typedef enum FDType {backward, forward, central} ;
|
12 |
|
|
|
13 |
|
|
typedef enum {linear, quadratic, general};
|
14 |
|
|
|
15 |
|
|
//abstract class of nonlinear optimization model
|
16 |
|
|
class NLModel{
|
17 |
|
|
public:
|
18 |
|
|
NLModel(ConstraintList* cons) {constraints = cons;}
|
19 |
|
|
virtual ~NLModel() { if (constraints != NULL) delete constraints;}
|
20 |
|
|
virtual void setX(const vector<double>& x)= 0;
|
21 |
|
|
|
22 |
|
|
virtual void setF(const vector<double>& fx)= 0;
|
23 |
|
|
|
24 |
|
|
virtual int getDim() const = 0;
|
25 |
|
|
|
26 |
|
|
bool hasConstraints() { return constraints == NULL ? false : true;}
|
27 |
|
|
int getConsType() { return constrains->getConsType();}
|
28 |
|
|
|
29 |
|
|
virtual double calcF() = 0;
|
30 |
|
|
virtual double calcF(const vector<double>& x) = 0;
|
31 |
|
|
virtual vector<double> calcGrad() = 0;
|
32 |
|
|
virtual vector<double> calcGrad(vector<double>& x) = 0;
|
33 |
|
|
virtual SymMatrix calcHessian() = 0;
|
34 |
|
|
virtual SymMatrix calcHessian(vector<double>& x) = 0;
|
35 |
|
|
|
36 |
|
|
#ifdef IS_MPI
|
37 |
|
|
void setMPIINITFunctor(MPIINITFunctor* func);
|
38 |
|
|
#endif
|
39 |
|
|
|
40 |
|
|
protected:
|
41 |
|
|
ConstraintList* constraints; //constraints of nonlinear optimization model
|
42 |
|
|
int numOfFunEval; //number of function evaluation
|
43 |
|
|
|
44 |
|
|
#ifdef IS_MPI
|
45 |
|
|
bool mpiInitFlag;
|
46 |
|
|
MPIINITFunctor * mpiInitFunc;
|
47 |
|
|
int localDim;
|
48 |
|
|
#endif
|
49 |
|
|
};
|
50 |
|
|
|
51 |
|
|
//abstract class of nonlinear optimization model without derivatives
|
52 |
|
|
class NLModel0 : public NLModel{
|
53 |
|
|
public:
|
54 |
|
|
|
55 |
|
|
NLModel0(int dim, ConstraintList* cons = NULL);
|
56 |
|
|
~NLModel0() {}
|
57 |
|
|
|
58 |
|
|
protected:
|
59 |
|
|
|
60 |
|
|
//Using finite difference methods to approximate the gradient
|
61 |
|
|
//It is inappropriate to apply these methods in large scale problem
|
62 |
|
|
|
63 |
|
|
vector<double> BackwardGrad(const vector<double>& x, double& fx, vector<double>& grad);
|
64 |
|
|
vector<double> ForwardGrad(const vector<double>& x, double& fx, vector<double>& grad);
|
65 |
|
|
vector<double> CentralGrad(const vector<double>& x, double& fx, vector<double>& grad);
|
66 |
|
|
|
67 |
|
|
//Using finite difference methods to approximate the hessian
|
68 |
|
|
//It is inappropriate to apply this method in large scale problem
|
69 |
|
|
virtual SymMatrix FDHessian(vector<double>& sx);
|
70 |
|
|
|
71 |
|
|
FDType fdType;
|
72 |
|
|
vector<double> currentX;
|
73 |
|
|
};
|
74 |
|
|
|
75 |
|
|
//abstract class of nonlinear optimization model with first derivatives
|
76 |
|
|
class NLModel1 : public NLModel0{
|
77 |
|
|
public:
|
78 |
|
|
|
79 |
|
|
//Using finite difference methods to approximate the hessian
|
80 |
|
|
//It is inappropriate to apply this method in large scale problem
|
81 |
|
|
virtual SymMatrix FDHessian(vector<double>& sx);
|
82 |
|
|
|
83 |
|
|
protected:
|
84 |
|
|
vector<double> currentGrad;
|
85 |
|
|
};
|
86 |
|
|
|
87 |
|
|
class NLF1 : NLModel1{
|
88 |
|
|
public:
|
89 |
|
|
NLModel1(int dim, ObjFunctor1* func , ConstraintList* cons = NULL);
|
90 |
|
|
NLModel1(int dim, ConstraintList* cons = NULL);
|
91 |
|
|
|
92 |
|
|
virtual double calcF();
|
93 |
|
|
virtual double calcF(const vector<double>& x);
|
94 |
|
|
virtual vector<double> calcGrad();
|
95 |
|
|
virtual vector<double> calcGrad(vector<double>& x);
|
96 |
|
|
virtual SymMatrix calcHessian() ;
|
97 |
|
|
virtual SymMatrix calcHessian(vector<double>& x) ;
|
98 |
|
|
|
99 |
|
|
protected:
|
100 |
|
|
ObjFunctor1* objfunc;
|
101 |
|
|
};
|
102 |
|
|
|
103 |
|
|
|
104 |
|
|
/*
|
105 |
|
|
class NLModel2 : public NLModel1{
|
106 |
|
|
public:
|
107 |
|
|
|
108 |
|
|
NLModel2(int dim, ObjFunctor2* func , ConstraintList* cons = NULL);
|
109 |
|
|
~NLModel2() {}
|
110 |
|
|
|
111 |
|
|
virtual double calcF();
|
112 |
|
|
virtual double calcF(const vector<double>& x);
|
113 |
|
|
virtual vector<double> calcGrad();
|
114 |
|
|
virtual vector<double> calcGrad(vector<double>& x);
|
115 |
|
|
virtual SymMatrix calcHessian() ;
|
116 |
|
|
virtual SymMatrix calcHessian(vector<double>& x) ;
|
117 |
|
|
|
118 |
|
|
protected:
|
119 |
|
|
|
120 |
|
|
SymMatrix hessian;
|
121 |
|
|
ObjFunctor2* objFunc;
|
122 |
|
|
};
|
123 |
|
|
*/
|
124 |
|
|
#endif
|