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