1 |
#ifndef _NLMODEL_H_
|
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 |
// 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 |
virtual void setX(const vector<double>& x)= 0;
|
24 |
|
25 |
virtual void setF(const vector<double>& fx)= 0;
|
26 |
|
27 |
virtual int getDim() const = 0;
|
28 |
|
29 |
bool hasConstraints() { return constraints == NULL ? false : true;}
|
30 |
int getConsType() { return constrains->getConsType();}
|
31 |
|
32 |
virtual double calcF() = 0;
|
33 |
virtual double calcF(const vector<double>& x) = 0;
|
34 |
virtual vector<double> calcGrad() = 0;
|
35 |
virtual vector<double> calcGrad(vector<double>& x) = 0;
|
36 |
virtual SymMatrix calcHessian() = 0;
|
37 |
virtual SymMatrix calcHessian(vector<double>& x) = 0;
|
38 |
|
39 |
#ifdef IS_MPI
|
40 |
void setMPIINITFunctor(MPIINITFunctor* func);
|
41 |
int getLocalDim() {return localDim;}
|
42 |
|
43 |
virtual void update(); //a hook function to load balancing
|
44 |
#endif
|
45 |
|
46 |
protected:
|
47 |
ConstraintList* constraints; //constraints of nonlinear optimization model
|
48 |
int numOfFunEval; //number of function evaluation
|
49 |
|
50 |
#ifdef IS_MPI
|
51 |
bool mpiInitFlag;
|
52 |
int myRank; //rank of current node
|
53 |
int numOfProc; // number of processors
|
54 |
MPIINITFunctor * mpiInitFunc;
|
55 |
|
56 |
int localDim;
|
57 |
vector<int> procMappingArray;
|
58 |
int beginGlobalIndex;
|
59 |
#endif
|
60 |
};
|
61 |
|
62 |
//abstract class of nonlinear optimization model without derivatives
|
63 |
class NLModel0 : public NLModel{
|
64 |
public:
|
65 |
|
66 |
NLModel0(int dim, ConstraintList* cons = NULL);
|
67 |
~NLModel0() {}
|
68 |
|
69 |
protected:
|
70 |
|
71 |
//Using finite difference methods to approximate the gradient
|
72 |
//It is inappropriate to apply these methods in large scale problem
|
73 |
|
74 |
vector<double> BackwardGrad(const vector<double>& x, double& fx, vector<double>& grad);
|
75 |
vector<double> ForwardGrad(const vector<double>& x, double& fx, vector<double>& grad);
|
76 |
vector<double> CentralGrad(const vector<double>& x, double& fx, vector<double>& grad);
|
77 |
|
78 |
//Using finite difference methods to approximate the hessian
|
79 |
//It is inappropriate to apply this method in large scale problem
|
80 |
virtual SymMatrix FiniteHessian(vector<double>& sx);
|
81 |
|
82 |
FDType fdType;
|
83 |
vector<double> currentX;
|
84 |
};
|
85 |
|
86 |
//abstract class of nonlinear optimization model with first derivatives
|
87 |
class NLModel1 : public NLModel0{
|
88 |
public:
|
89 |
|
90 |
//Using finite difference methods to approximate the hessian
|
91 |
//It is inappropriate to apply this method in large scale problem
|
92 |
virtual SymMatrix ForwardHessian(vector<double>& sx);
|
93 |
|
94 |
protected:
|
95 |
vector<double> currentGrad;
|
96 |
};
|
97 |
|
98 |
//concrete class of nonlinear optimization model with first derivatives
|
99 |
class ConcreteNLMode1 : NLModel1{
|
100 |
public:
|
101 |
ConcreteNLMode1(int dim, ObjFunctor1* func , ConstraintList* cons = NULL);
|
102 |
ConcreteNLMode1(int dim, ConstraintList* cons = NULL);
|
103 |
|
104 |
virtual double calcF();
|
105 |
virtual double calcF(const vector<double>& x);
|
106 |
virtual vector<double> calcGrad();
|
107 |
virtual vector<double> calcGrad(vector<double>& x);
|
108 |
virtual SymMatrix calcHessian() ;
|
109 |
virtual SymMatrix calcHessian(vector<double>& x) ;
|
110 |
|
111 |
protected:
|
112 |
ObjFunctor1* objfunc;
|
113 |
};
|
114 |
|
115 |
|
116 |
/*
|
117 |
class NLModel2 : public NLModel1{
|
118 |
public:
|
119 |
|
120 |
NLModel2(int dim, ObjFunctor2* func , ConstraintList* cons = NULL);
|
121 |
~NLModel2() {}
|
122 |
|
123 |
virtual double calcF();
|
124 |
virtual double calcF(const vector<double>& x);
|
125 |
virtual vector<double> calcGrad();
|
126 |
virtual vector<double> calcGrad(vector<double>& x);
|
127 |
virtual SymMatrix calcHessian() ;
|
128 |
virtual SymMatrix calcHessian(vector<double>& x) ;
|
129 |
|
130 |
protected:
|
131 |
|
132 |
SymMatrix hessian;
|
133 |
ObjFunctor2* objFunc;
|
134 |
};
|
135 |
*/
|
136 |
#endif
|