2 |
|
#define _NLMODEL_H_ |
3 |
|
|
4 |
|
#include <vector> |
5 |
+ |
#include <utility> |
6 |
|
|
7 |
|
#include "SymMatrix.hpp" |
8 |
|
#include "Functor.hpp" |
9 |
|
|
10 |
|
using namespace std; |
11 |
|
|
12 |
+ |
|
13 |
|
typedef enum FDType {backward, forward, central} ; |
14 |
|
|
15 |
< |
typedef enum {linear, quadratic, general}; |
15 |
> |
// special property of nonlinear object function |
16 |
> |
typedef enum NLOFProp{linear, quadratic, general}; |
17 |
|
|
18 |
|
//abstract class of nonlinear optimization model |
19 |
|
class NLModel{ |
20 |
|
public: |
21 |
|
NLModel(ConstraintList* cons) {constraints = cons;} |
22 |
|
virtual ~NLModel() { if (constraints != NULL) delete constraints;} |
23 |
+ |
|
24 |
|
virtual void setX(const vector<double>& x)= 0; |
25 |
|
|
22 |
– |
virtual void setF(const vector<double>& fx)= 0; |
23 |
– |
|
26 |
|
virtual int getDim() const = 0; |
27 |
|
|
28 |
|
bool hasConstraints() { return constraints == NULL ? false : true;} |
37 |
|
|
38 |
|
#ifdef IS_MPI |
39 |
|
void setMPIINITFunctor(MPIINITFunctor* func); |
40 |
+ |
int getLocalDim() {return localDim;} |
41 |
+ |
|
42 |
+ |
virtual void update(); //a hook function to load balancing |
43 |
|
#endif |
44 |
|
|
45 |
|
protected: |
48 |
|
|
49 |
|
#ifdef IS_MPI |
50 |
|
bool mpiInitFlag; |
51 |
+ |
int myRank; //rank of current node |
52 |
+ |
int numOfProc; // number of processors |
53 |
|
MPIINITFunctor * mpiInitFunc; |
54 |
+ |
|
55 |
|
int localDim; |
56 |
+ |
vector<int> procMappingArray; |
57 |
+ |
int beginGlobalIndex; |
58 |
|
#endif |
59 |
|
}; |
60 |
|
|
65 |
|
NLModel0(int dim, ConstraintList* cons = NULL); |
66 |
|
~NLModel0() {} |
67 |
|
|
68 |
< |
protected: |
68 |
> |
virtual void setX(const vector<double>& x); |
69 |
|
|
70 |
|
//Using finite difference methods to approximate the gradient |
71 |
|
//It is inappropriate to apply these methods in large scale problem |
76 |
|
|
77 |
|
//Using finite difference methods to approximate the hessian |
78 |
|
//It is inappropriate to apply this method in large scale problem |
79 |
< |
virtual SymMatrix FDHessian(vector<double>& sx); |
79 |
> |
virtual SymMatrix FiniteHessian(vector<double>& x, double fx, vector<double>& h); |
80 |
|
|
81 |
+ |
protected: |
82 |
+ |
|
83 |
|
FDType fdType; |
84 |
|
vector<double> currentX; |
85 |
+ |
double curretF; |
86 |
|
}; |
87 |
|
|
88 |
+ |
//concrete class of nonlinear optimization model without derivatives |
89 |
+ |
|
90 |
+ |
class ConcreteNLMode0 : public NLModel0{ |
91 |
+ |
|
92 |
+ |
public: |
93 |
+ |
|
94 |
+ |
ConcreteNLMode0(int dim, ObjFunctor0* func , ConstraintList* cons = NULL); |
95 |
+ |
ConcreteNLMode0(int dim, ConstraintList* cons = NULL); |
96 |
+ |
|
97 |
+ |
virtual double calcF(); |
98 |
+ |
virtual double calcF(const vector<double>& x); |
99 |
+ |
virtual vector<double> calcGrad(); |
100 |
+ |
virtual vector<double> calcGrad(vector<double>& x); |
101 |
+ |
virtual SymMatrix calcHessian() ; |
102 |
+ |
virtual SymMatrix calcHessian(vector<double>& x) ; |
103 |
+ |
|
104 |
+ |
protected: |
105 |
+ |
|
106 |
+ |
ObjFunctor0* objfunc; |
107 |
+ |
|
108 |
+ |
}; |
109 |
+ |
|
110 |
|
//abstract class of nonlinear optimization model with first derivatives |
111 |
|
class NLModel1 : public NLModel0{ |
112 |
+ |
|
113 |
|
public: |
114 |
|
|
115 |
|
//Using finite difference methods to approximate the hessian |
116 |
|
//It is inappropriate to apply this method in large scale problem |
117 |
< |
virtual SymMatrix FDHessian(vector<double>& sx); |
117 |
> |
virtual SymMatrix FiniteHessian(vector<double>& x, double fx, vector<double>& h); |
118 |
|
|
119 |
|
protected: |
120 |
+ |
|
121 |
|
vector<double> currentGrad; |
122 |
|
}; |
123 |
|
|
124 |
< |
class NLF1 : NLModel1{ |
124 |
> |
//concrete class of nonlinear optimization model with first derivatives |
125 |
> |
class ConcreteNLMode1 : NLModel1{ |
126 |
> |
|
127 |
|
public: |
128 |
< |
NLModel1(int dim, ObjFunctor1* func , ConstraintList* cons = NULL); |
129 |
< |
NLModel1(int dim, ConstraintList* cons = NULL); |
128 |
> |
|
129 |
> |
ConcreteNLMode1(int dim, ObjFunctor1* func , ConstraintList* cons = NULL); |
130 |
> |
ConcreteNLMode1(int dim, ConstraintList* cons = NULL); |
131 |
|
|
132 |
|
virtual double calcF(); |
133 |
|
virtual double calcF(const vector<double>& x); |
137 |
|
virtual SymMatrix calcHessian(vector<double>& x) ; |
138 |
|
|
139 |
|
protected: |
140 |
+ |
|
141 |
|
ObjFunctor1* objfunc; |
142 |
|
}; |
143 |
|
|
103 |
– |
|
144 |
|
/* |
145 |
+ |
//abstract class of nonlinear optimization model with second derivatives |
146 |
|
class NLModel2 : public NLModel1{ |
147 |
|
public: |
148 |
+ |
|
149 |
+ |
protected: |
150 |
+ |
SymMatrix currentHessian; |
151 |
|
|
152 |
< |
NLModel2(int dim, ObjFunctor2* func , ConstraintList* cons = NULL); |
153 |
< |
~NLModel2() {} |
152 |
> |
}; |
153 |
> |
|
154 |
> |
//concrete class of nonlinear optimization model with second derivatives |
155 |
> |
class ConcreteNLModel2 : public NLModel2{ |
156 |
> |
public: |
157 |
> |
|
158 |
> |
ConcreteNLModel2(int dim, ObjFunctor2* func , ConstraintList* cons = NULL); |
159 |
> |
ConcreteNLModel2(int dim, ConstraintList* cons = NULL); |
160 |
|
|
161 |
|
virtual double calcF(); |
162 |
|
virtual double calcF(const vector<double>& x); |
167 |
|
|
168 |
|
protected: |
169 |
|
|
120 |
– |
SymMatrix hessian; |
170 |
|
ObjFunctor2* objFunc; |
171 |
|
}; |
172 |
|
*/ |