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