| 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;} |
| 24 |
|
virtual ~NLModel() { if (constraints != NULL) delete constraints;} |
| 25 |
+ |
|
| 26 |
|
virtual void setX(const vector<double>& x)= 0; |
| 27 |
|
|
| 28 |
< |
virtual void setF(const vector<double>& fx)= 0; |
| 28 |
> |
virtual int getDim() {return ndim;} |
| 29 |
|
|
| 24 |
– |
virtual int getDim() const = 0; |
| 25 |
– |
|
| 30 |
|
bool hasConstraints() { return constraints == NULL ? false : true;} |
| 31 |
< |
int getConsType() { return constrains->getConsType();} |
| 31 |
> |
int getConsType() { return constraints->getConsType();} |
| 32 |
|
|
| 33 |
|
virtual double calcF() = 0; |
| 34 |
|
virtual double calcF(const vector<double>& x) = 0; |
| 39 |
|
|
| 40 |
|
#ifdef IS_MPI |
| 41 |
|
void setMPIINITFunctor(MPIINITFunctor* func); |
| 42 |
+ |
int getLocalDim() {return localDim;} |
| 43 |
+ |
|
| 44 |
+ |
virtual void update(); //a hook function to load balancing |
| 45 |
|
#endif |
| 46 |
|
|
| 47 |
|
protected: |
| 48 |
|
ConstraintList* constraints; //constraints of nonlinear optimization model |
| 49 |
|
int numOfFunEval; //number of function evaluation |
| 50 |
+ |
int ndim; |
| 51 |
|
|
| 52 |
|
#ifdef IS_MPI |
| 53 |
|
bool mpiInitFlag; |
| 54 |
+ |
int myRank; //rank of current node |
| 55 |
+ |
int numOfProc; // number of processors |
| 56 |
|
MPIINITFunctor * mpiInitFunc; |
| 57 |
+ |
|
| 58 |
|
int localDim; |
| 59 |
+ |
vector<int> procMappingArray; |
| 60 |
+ |
int beginGlobalIndex; |
| 61 |
|
#endif |
| 62 |
|
}; |
| 63 |
|
|
| 68 |
|
NLModel0(int dim, ConstraintList* cons = NULL); |
| 69 |
|
~NLModel0() {} |
| 70 |
|
|
| 71 |
< |
protected: |
| 71 |
> |
virtual void setX(const vector<double>& x); |
| 72 |
|
|
| 73 |
|
//Using finite difference methods to approximate the gradient |
| 74 |
|
//It is inappropriate to apply these methods in large scale problem |
| 75 |
|
|
| 76 |
< |
vector<double> BackwardGrad(const vector<double>& x, double& fx, vector<double>& grad); |
| 77 |
< |
vector<double> ForwardGrad(const vector<double>& x, double& fx, vector<double>& grad); |
| 78 |
< |
vector<double> CentralGrad(const vector<double>& x, double& fx, vector<double>& grad); |
| 76 |
> |
vector<double> BackwardGrad(const vector<double>& x, double& fx, vector<double>& grad, const vector<double>& h); |
| 77 |
> |
vector<double> ForwardGrad(const vector<double>& x, double& fx, vector<double>& grad, const vector<double>& h); |
| 78 |
> |
vector<double> CentralGrad(const vector<double>& x, double& fx, vector<double>& grad, const vector<double>& h); |
| 79 |
|
|
| 80 |
|
//Using finite difference methods to approximate the hessian |
| 81 |
|
//It is inappropriate to apply this method in large scale problem |
| 82 |
< |
virtual SymMatrix FDHessian(vector<double>& sx); |
| 82 |
> |
//virtual SymMatrix FiniteHessian(vector<double>& x, double fx, vector<double>& h); |
| 83 |
|
|
| 84 |
+ |
protected: |
| 85 |
+ |
|
| 86 |
|
FDType fdType; |
| 87 |
|
vector<double> currentX; |
| 88 |
+ |
double currentF; |
| 89 |
|
}; |
| 90 |
|
|
| 91 |
+ |
//concrete class of nonlinear optimization model without derivatives |
| 92 |
+ |
|
| 93 |
+ |
class ConcreteNLMode0 : public NLModel0{ |
| 94 |
+ |
|
| 95 |
+ |
public: |
| 96 |
+ |
|
| 97 |
+ |
ConcreteNLMode0(int dim, ObjFunctor0* func , ConstraintList* cons = NULL); |
| 98 |
+ |
ConcreteNLMode0(int dim, ConstraintList* cons = NULL); |
| 99 |
+ |
|
| 100 |
+ |
virtual double calcF(); |
| 101 |
+ |
virtual double calcF(const vector<double>& x); |
| 102 |
+ |
virtual vector<double> calcGrad(); |
| 103 |
+ |
virtual vector<double> calcGrad(vector<double>& x); |
| 104 |
+ |
virtual SymMatrix calcHessian() ; |
| 105 |
+ |
virtual SymMatrix calcHessian(vector<double>& x) ; |
| 106 |
+ |
|
| 107 |
+ |
protected: |
| 108 |
+ |
|
| 109 |
+ |
ObjFunctor0* objfunc; |
| 110 |
+ |
|
| 111 |
+ |
}; |
| 112 |
+ |
|
| 113 |
|
//abstract class of nonlinear optimization model with first derivatives |
| 114 |
|
class NLModel1 : public NLModel0{ |
| 115 |
+ |
|
| 116 |
|
public: |
| 117 |
|
|
| 118 |
|
//Using finite difference methods to approximate the hessian |
| 119 |
|
//It is inappropriate to apply this method in large scale problem |
| 120 |
< |
virtual SymMatrix FDHessian(vector<double>& sx); |
| 120 |
> |
virtual SymMatrix FiniteHessian(vector<double>& x, vector<double>& h); |
| 121 |
|
|
| 122 |
|
protected: |
| 123 |
+ |
|
| 124 |
|
vector<double> currentGrad; |
| 125 |
|
}; |
| 126 |
|
|
| 127 |
< |
class NLF1 : NLModel1{ |
| 127 |
> |
//concrete class of nonlinear optimization model with first derivatives |
| 128 |
> |
class ConcreteNLMode1 : NLModel1{ |
| 129 |
> |
|
| 130 |
|
public: |
| 131 |
< |
NLModel1(int dim, ObjFunctor1* func , ConstraintList* cons = NULL); |
| 132 |
< |
NLModel1(int dim, ConstraintList* cons = NULL); |
| 131 |
> |
|
| 132 |
> |
ConcreteNLMode1(int dim, ObjFunctor1* func , ConstraintList* cons = NULL); |
| 133 |
> |
ConcreteNLMode1(int dim, ConstraintList* cons = NULL); |
| 134 |
|
|
| 135 |
|
virtual double calcF(); |
| 136 |
|
virtual double calcF(const vector<double>& x); |
| 137 |
|
virtual vector<double> calcGrad(); |
| 138 |
< |
virtual vector<double> calcGrad(vector<double>& x); |
| 138 |
> |
virtual vector<double> calcGrad(const vector<double>& x); |
| 139 |
|
virtual SymMatrix calcHessian() ; |
| 140 |
|
virtual SymMatrix calcHessian(vector<double>& x) ; |
| 141 |
|
|
| 142 |
|
protected: |
| 143 |
+ |
|
| 144 |
|
ObjFunctor1* objfunc; |
| 145 |
|
}; |
| 146 |
|
|
| 103 |
– |
|
| 147 |
|
/* |
| 148 |
+ |
//abstract class of nonlinear optimization model with second derivatives |
| 149 |
|
class NLModel2 : public NLModel1{ |
| 150 |
|
public: |
| 151 |
+ |
|
| 152 |
+ |
protected: |
| 153 |
+ |
SymMatrix currentHessian; |
| 154 |
|
|
| 155 |
< |
NLModel2(int dim, ObjFunctor2* func , ConstraintList* cons = NULL); |
| 156 |
< |
~NLModel2() {} |
| 155 |
> |
}; |
| 156 |
> |
|
| 157 |
> |
//concrete class of nonlinear optimization model with second derivatives |
| 158 |
> |
class ConcreteNLModel2 : public NLModel2{ |
| 159 |
> |
public: |
| 160 |
> |
|
| 161 |
> |
ConcreteNLModel2(int dim, ObjFunctor2* func , ConstraintList* cons = NULL); |
| 162 |
> |
ConcreteNLModel2(int dim, ConstraintList* cons = NULL); |
| 163 |
|
|
| 164 |
|
virtual double calcF(); |
| 165 |
|
virtual double calcF(const vector<double>& x); |
| 170 |
|
|
| 171 |
|
protected: |
| 172 |
|
|
| 120 |
– |
SymMatrix hessian; |
| 173 |
|
ObjFunctor2* objFunc; |
| 174 |
|
}; |
| 175 |
|
*/ |