| 1 |
#ifndef _NLMODEL_H_
|
| 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 |
|
| 14 |
|
| 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 |
virtual vector<double> getX() = 0;
|
| 28 |
|
| 29 |
virtual void setF(double f) = 0;
|
| 30 |
virtual double getF() = 0;
|
| 31 |
|
| 32 |
virtual int getDim() {return ndim;}
|
| 33 |
|
| 34 |
bool hasConstraints() { return constraints == NULL ? false : true;}
|
| 35 |
int getConsType() { return constraints->getConsType();}
|
| 36 |
|
| 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 |
int getLocalDim() {return localDim;}
|
| 47 |
|
| 48 |
virtual void update(); //a hook function to load balancing
|
| 49 |
#endif
|
| 50 |
|
| 51 |
protected:
|
| 52 |
ConstraintList* constraints; //constraints of nonlinear optimization model
|
| 53 |
int numOfFunEval; //number of function evaluation
|
| 54 |
int ndim;
|
| 55 |
|
| 56 |
#ifdef IS_MPI
|
| 57 |
bool mpiInitFlag;
|
| 58 |
int myRank; //rank of current node
|
| 59 |
int numOfProc; // number of processors
|
| 60 |
MPIINITFunctor * mpiInitFunc;
|
| 61 |
|
| 62 |
int localDim;
|
| 63 |
vector<int> procMappingArray;
|
| 64 |
int beginGlobalIndex;
|
| 65 |
#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 |
virtual void setX(const vector<double>& x) {currentX = x;}
|
| 76 |
vector<double> getX() {return currentX;}
|
| 77 |
|
| 78 |
void setF(double f) {currentF = f;}
|
| 79 |
double getF() {return currentF;}
|
| 80 |
|
| 81 |
//Using finite difference methods to approximate the gradient
|
| 82 |
//It is inappropriate to apply these methods in large scale problem
|
| 83 |
|
| 84 |
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 |
|
| 88 |
//Using finite difference methods to approximate the hessian
|
| 89 |
//It is inappropriate to apply this method in large scale problem
|
| 90 |
//virtual SymMatrix FiniteHessian(vector<double>& x, double fx, vector<double>& h);
|
| 91 |
SymMatrix FiniteHessian(vector<double>& x, double fx, vector<double>& h);
|
| 92 |
protected:
|
| 93 |
|
| 94 |
FDType fdType;
|
| 95 |
vector<double> currentX;
|
| 96 |
double currentF;
|
| 97 |
};
|
| 98 |
|
| 99 |
//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 |
virtual double calcF(vector<double>& x);
|
| 110 |
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 |
//abstract class of nonlinear optimization model with first derivatives
|
| 122 |
class NLModel1 : public NLModel0{
|
| 123 |
|
| 124 |
public:
|
| 125 |
|
| 126 |
//Using finite difference methods to approximate the hessian
|
| 127 |
//It is inappropriate to apply this method in large scale problem
|
| 128 |
virtual SymMatrix FiniteHessian(vector<double>& x, vector<double>& h);
|
| 129 |
|
| 130 |
void setGrad(vector<double>& grad) {currentGrad = grad;}
|
| 131 |
vector<double> getGrad() {return currentGrad;}
|
| 132 |
protected:
|
| 133 |
|
| 134 |
vector<double> currentGrad;
|
| 135 |
};
|
| 136 |
|
| 137 |
//concrete class of nonlinear optimization model with first derivatives
|
| 138 |
class ConcreteNLMode1 : NLModel1{
|
| 139 |
|
| 140 |
public:
|
| 141 |
|
| 142 |
ConcreteNLMode1(int dim, ObjFunctor1* func , ConstraintList* cons = NULL);
|
| 143 |
ConcreteNLMode1(int dim, ConstraintList* cons = NULL);
|
| 144 |
|
| 145 |
virtual double calcF();
|
| 146 |
virtual double calcF(vector<double>& x);
|
| 147 |
virtual vector<double> calcGrad();
|
| 148 |
virtual vector<double> calcGrad( vector<double>& x);
|
| 149 |
virtual SymMatrix calcHessian() ;
|
| 150 |
virtual SymMatrix calcHessian(vector<double>& x) ;
|
| 151 |
|
| 152 |
protected:
|
| 153 |
|
| 154 |
ObjFunctor1* objfunc;
|
| 155 |
};
|
| 156 |
|
| 157 |
/*
|
| 158 |
//abstract class of nonlinear optimization model with second derivatives
|
| 159 |
class NLModel2 : public NLModel1{
|
| 160 |
public:
|
| 161 |
|
| 162 |
protected:
|
| 163 |
SymMatrix currentHessian;
|
| 164 |
|
| 165 |
};
|
| 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 |
|
| 174 |
virtual double calcF();
|
| 175 |
virtual double calcF(vector<double>& x);
|
| 176 |
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
|