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