1 |
|
#ifndef _CONSTRAINT_H_ |
2 |
|
#define _CONSTRAINT_H_ |
3 |
|
|
4 |
+ |
#include <iostream> |
5 |
|
#include <vector> |
6 |
|
|
7 |
< |
#include "NLModel.hpp" |
7 |
> |
#include "SymMatrix.hpp" |
8 |
|
|
9 |
|
#define ERROR_CONSTRAINT 10 |
10 |
|
|
11 |
|
using namespace std; |
12 |
|
|
13 |
< |
typedef enum ConsType {simpleBound = 1, linearEqu = 2, linearInequ = 4, |
14 |
< |
nonlinearEqu = 8, nonlinearInequ =16}; |
13 |
> |
typedef enum {simpleBound = 1, linearEqu = 2, linearInequ = 4, |
14 |
> |
nonlinearEqu = 8, nonlinearInequ =16} ConsType; |
15 |
|
|
16 |
< |
typedef enum BoundType{upper, lower, equ}; |
16 |
> |
typedef enum {btUpper, btLower, btEqu} BoundType; |
17 |
|
|
18 |
|
/** |
19 |
|
* Abstract class of constraint for nonlinear optimization problem |
24 |
|
ConstraintBase(int dim); |
25 |
|
|
26 |
|
virtual void setDim(int dim); |
27 |
< |
bool isDimSet(); |
27 |
> |
bool isDimSet() {return init_ndim;} |
28 |
|
|
29 |
< |
int getConsType() { return consType}; |
29 |
> |
int getConsType() { return consType;} |
30 |
|
virtual double calcResidual(vector<double>& x) = 0; |
31 |
|
virtual vector<double> calcConsGrad(vector<double>& x) = 0; |
32 |
|
virtual SymMatrix calcConsHessian(vector<double>& x) = 0; |
42 |
|
|
43 |
|
}; |
44 |
|
|
44 |
– |
/** |
45 |
– |
* Simple Bound Constraint for nonlinear optimization problem |
46 |
– |
* boundType is used to identify whether it is upper bound or lower bound |
47 |
– |
*/ |
48 |
– |
class SimpleBoundCons : public ConstraintBase{ |
49 |
– |
public: |
50 |
– |
|
51 |
– |
SimpleBoundCons(int theIndex, double b, bool flag); |
52 |
– |
SimpleBoundCons(int dim, int theIndex, double b, bool flag); |
53 |
– |
|
54 |
– |
virtual double calcResidual(vector<double>& x); |
55 |
– |
virtual vector<double> calcConsGrad(vector<double>& x); |
56 |
– |
virtual SymMatrix calcConsHessian(vector<double>& x); |
57 |
– |
|
58 |
– |
protected: |
59 |
– |
|
60 |
– |
int index; |
61 |
– |
}; |
62 |
– |
|
63 |
– |
/** |
64 |
– |
* Linear Constraint for nonlinear optimization problem |
65 |
– |
* boundType is used to identify whether it is linear equation constraint or linear inequality |
66 |
– |
* constraint. If it is inear inequality constraint |
67 |
– |
*/ |
68 |
– |
|
69 |
– |
class LinearCons : public ConstraintBase{ |
70 |
– |
|
71 |
– |
public: |
72 |
– |
|
73 |
– |
LinearCons(vector<int>& theIndex, vector<double>& , double b, BoundType bType); |
74 |
– |
LinearCons(int dim, vector<int>& theIndex, vector<double>& , double b, BoundType bType); |
75 |
– |
virtual double calcResidual(vector<double>& x); |
76 |
– |
virtual vector<double> calcConsGrad(vector<double>& x); |
77 |
– |
virtual SymMatrix calcConsHessian(vector<double>& x); |
78 |
– |
|
79 |
– |
protected: |
80 |
– |
|
81 |
– |
vector<int> index; |
82 |
– |
vector<double> coeff; |
83 |
– |
}; |
84 |
– |
|
85 |
– |
/** |
86 |
– |
* Linear Constraint for nonlinear optimization problem |
87 |
– |
* boundType is used to identify whether it is linear equality constraint or linear inequality |
88 |
– |
* constraint |
89 |
– |
*/ |
90 |
– |
|
91 |
– |
|
92 |
– |
class NonlinearCons : public ConstraintBase{ |
93 |
– |
|
94 |
– |
public: |
95 |
– |
NonLinearCons(vector<int>& theIndex, NLModel* theModel , double b, BoundType bType); |
96 |
– |
NonLinearCons(int dim, vector<int>& theIndex, NLModel* theModel , double b, BoundType bType); |
97 |
– |
|
98 |
– |
void setDim(int dim); |
99 |
– |
|
100 |
– |
virtual double calcResidual(vector<double>& x); |
101 |
– |
virtual vector<double> calcConsGrad(vector<double>& x); |
102 |
– |
virtual SymMatrix calcConsHessian(vector<double>& x); |
103 |
– |
|
104 |
– |
protected: |
105 |
– |
|
106 |
– |
vector<int> index; |
107 |
– |
NLModel* model; |
108 |
– |
|
109 |
– |
}; |
110 |
– |
|
111 |
– |
class ConstraintList{ |
112 |
– |
public: |
113 |
– |
ConstraintList(); |
114 |
– |
~ConstraintList(); |
115 |
– |
|
116 |
– |
addConstraint(ConstraintBase* cons); |
117 |
– |
int getNumOfCons() {return constraints.size();} |
118 |
– |
int getConsType() {return consType;} |
119 |
– |
vector<ConstraintBase*> getConstraints() {return constraints;} |
120 |
– |
|
121 |
– |
protected: |
122 |
– |
|
123 |
– |
vector<ConstraintBase*> constraints; |
124 |
– |
int consType; |
125 |
– |
}; |
126 |
– |
|
45 |
|
#endif |