ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/NLModel.hpp
Revision: 1010
Committed: Tue Feb 3 20:43:08 2004 UTC (20 years, 5 months ago) by tim
File size: 5294 byte(s)
Log Message:
to avoid cyclic depency, refactory constraint class

File Contents

# User Rev Content
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    
28 tim 1010 virtual int getDim() {return ndim;}
29 tim 995
30     bool hasConstraints() { return constraints == NULL ? false : true;}
31 tim 1010 int getConsType() { return constraints->getConsType();}
32 tim 995
33     virtual double calcF() = 0;
34     virtual double calcF(const vector<double>& x) = 0;
35     virtual vector<double> calcGrad() = 0;
36     virtual vector<double> calcGrad(vector<double>& x) = 0;
37     virtual SymMatrix calcHessian() = 0;
38     virtual SymMatrix calcHessian(vector<double>& x) = 0;
39    
40     #ifdef IS_MPI
41     void setMPIINITFunctor(MPIINITFunctor* func);
42 tim 996 int getLocalDim() {return localDim;}
43    
44     virtual void update(); //a hook function to load balancing
45 tim 995 #endif
46    
47     protected:
48     ConstraintList* constraints; //constraints of nonlinear optimization model
49     int numOfFunEval; //number of function evaluation
50 tim 1010 int ndim;
51 tim 995
52     #ifdef IS_MPI
53     bool mpiInitFlag;
54 tim 996 int myRank; //rank of current node
55     int numOfProc; // number of processors
56 tim 995 MPIINITFunctor * mpiInitFunc;
57 tim 996
58 tim 995 int localDim;
59 tim 996 vector<int> procMappingArray;
60     int beginGlobalIndex;
61 tim 995 #endif
62     };
63    
64     //abstract class of nonlinear optimization model without derivatives
65     class NLModel0 : public NLModel{
66     public:
67    
68     NLModel0(int dim, ConstraintList* cons = NULL);
69     ~NLModel0() {}
70    
71 tim 1000 virtual void setX(const vector<double>& x);
72 tim 995
73     //Using finite difference methods to approximate the gradient
74     //It is inappropriate to apply these methods in large scale problem
75    
76 tim 1010 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 tim 995
80     //Using finite difference methods to approximate the hessian
81     //It is inappropriate to apply this method in large scale problem
82 tim 1010 //virtual SymMatrix FiniteHessian(vector<double>& x, double fx, vector<double>& h);
83 tim 995
84 tim 1000 protected:
85    
86 tim 995 FDType fdType;
87     vector<double> currentX;
88 tim 1010 double currentF;
89 tim 995 };
90    
91 tim 1000 //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 tim 995 //abstract class of nonlinear optimization model with first derivatives
114     class NLModel1 : public NLModel0{
115 tim 1000
116 tim 995 public:
117    
118     //Using finite difference methods to approximate the hessian
119     //It is inappropriate to apply this method in large scale problem
120 tim 1010 virtual SymMatrix FiniteHessian(vector<double>& x, vector<double>& h);
121 tim 995
122     protected:
123 tim 1000
124 tim 995 vector<double> currentGrad;
125     };
126    
127 tim 996 //concrete class of nonlinear optimization model with first derivatives
128     class ConcreteNLMode1 : NLModel1{
129 tim 1000
130 tim 995 public:
131 tim 1000
132 tim 996 ConcreteNLMode1(int dim, ObjFunctor1* func , ConstraintList* cons = NULL);
133     ConcreteNLMode1(int dim, ConstraintList* cons = NULL);
134 tim 995
135     virtual double calcF();
136     virtual double calcF(const vector<double>& x);
137     virtual vector<double> calcGrad();
138 tim 1010 virtual vector<double> calcGrad(const vector<double>& x);
139 tim 995 virtual SymMatrix calcHessian() ;
140     virtual SymMatrix calcHessian(vector<double>& x) ;
141    
142     protected:
143 tim 1000
144 tim 995 ObjFunctor1* objfunc;
145     };
146    
147     /*
148 tim 1000 //abstract class of nonlinear optimization model with second derivatives
149 tim 995 class NLModel2 : public NLModel1{
150     public:
151 tim 1000
152     protected:
153     SymMatrix currentHessian;
154 tim 995
155 tim 1000 };
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 tim 995
164     virtual double calcF();
165     virtual double calcF(const vector<double>& x);
166     virtual vector<double> calcGrad();
167     virtual vector<double> calcGrad(vector<double>& x);
168     virtual SymMatrix calcHessian() ;
169     virtual SymMatrix calcHessian(vector<double>& x) ;
170    
171     protected:
172    
173     ObjFunctor2* objFunc;
174     };
175     */
176     #endif

Properties

Name Value
svn:executable *