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 FDType {backward, forward, central} ; |
15 |
> |
typedef enum {backward, forward, central} FDType; |
16 |
|
|
17 |
|
// special property of nonlinear object function |
18 |
< |
typedef enum NLOFProp{linear, quadratic, general}; |
18 |
> |
typedef enum {linear, quadratic, general} NLOFProp; |
19 |
|
|
20 |
|
//abstract class of nonlinear optimization model |
21 |
|
class NLModel{ |
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 int getDim() const = 0; |
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 constrains->getConsType();} |
35 |
> |
int getConsType() { return constraints->getConsType();} |
36 |
|
|
37 |
|
virtual double calcF() = 0; |
38 |
|
virtual double calcF(const vector<double>& x) = 0; |
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; |
72 |
|
NLModel0(int dim, ConstraintList* cons = NULL); |
73 |
|
~NLModel0() {} |
74 |
|
|
75 |
< |
virtual void setX(const vector<double>& x); |
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); |
85 |
< |
vector<double> ForwardGrad(const vector<double>& x, double& fx, vector<double>& grad); |
86 |
< |
vector<double> CentralGrad(const vector<double>& x, double& fx, vector<double>& grad); |
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 |
< |
|
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 curretF; |
96 |
> |
double currentF; |
97 |
|
}; |
98 |
|
|
99 |
|
//concrete class of nonlinear optimization model without derivatives |
106 |
|
ConcreteNLMode0(int dim, ConstraintList* cons = NULL); |
107 |
|
|
108 |
|
virtual double calcF(); |
109 |
< |
virtual double calcF(const vector<double>& x); |
109 |
> |
virtual double calcF(vector<double>& x); |
110 |
|
virtual vector<double> calcGrad(); |
111 |
|
virtual vector<double> calcGrad(vector<double>& x); |
112 |
|
virtual SymMatrix calcHessian() ; |
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, double fx, vector<double>& h); |
129 |
< |
|
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; |
143 |
|
ConcreteNLMode1(int dim, ConstraintList* cons = NULL); |
144 |
|
|
145 |
|
virtual double calcF(); |
146 |
< |
virtual double calcF(const vector<double>& x); |
146 |
> |
virtual double calcF(vector<double>& x); |
147 |
|
virtual vector<double> calcGrad(); |
148 |
< |
virtual vector<double> calcGrad(vector<double>& x); |
148 |
> |
virtual vector<double> calcGrad( vector<double>& x); |
149 |
|
virtual SymMatrix calcHessian() ; |
150 |
|
virtual SymMatrix calcHessian(vector<double>& x) ; |
151 |
|
|
172 |
|
ConcreteNLModel2(int dim, ConstraintList* cons = NULL); |
173 |
|
|
174 |
|
virtual double calcF(); |
175 |
< |
virtual double calcF(const vector<double>& x); |
175 |
> |
virtual double calcF(vector<double>& x); |
176 |
|
virtual vector<double> calcGrad(); |
177 |
|
virtual vector<double> calcGrad(vector<double>& x); |
178 |
|
virtual SymMatrix calcHessian() ; |