1 |
#include "NonlinearCons.hpp"
|
2 |
|
3 |
NonlinearCons::NonlinearCons(vector<int>& theIndex, NLModel* theModel , double b, BoundType bType)
|
4 |
:ConstraintBase(){
|
5 |
|
6 |
if(theIndex.size() != theModel->getDim()){
|
7 |
cout << "NonlinearCons Error: the dimension of index and the model does not match" << endl;
|
8 |
exit(ERROR_CONSTRAINT);
|
9 |
}
|
10 |
|
11 |
bound = b;
|
12 |
boundType = bType;
|
13 |
|
14 |
if(bType == btEqu){
|
15 |
consType = nonlinearEqu;
|
16 |
}
|
17 |
else{
|
18 |
consType = nonlinearInequ;
|
19 |
}
|
20 |
|
21 |
index = theIndex;
|
22 |
model = theModel;
|
23 |
}
|
24 |
|
25 |
NonlinearCons::NonlinearCons(int dim, vector<int>& theIndex, NLModel* theModel , double b, BoundType bType)
|
26 |
:ConstraintBase(dim){
|
27 |
|
28 |
if(dim != theModel->getDim()){
|
29 |
cout << "NonlinearCons Error: the dimension of index and the model does not match" << endl;
|
30 |
exit(ERROR_CONSTRAINT);
|
31 |
}
|
32 |
|
33 |
bound = b;
|
34 |
boundType = bType;
|
35 |
|
36 |
if(bType == btEqu){
|
37 |
consType = nonlinearEqu;
|
38 |
}
|
39 |
else{
|
40 |
consType = nonlinearInequ;
|
41 |
}
|
42 |
|
43 |
index = theIndex;
|
44 |
model = theModel;
|
45 |
}
|
46 |
|
47 |
|
48 |
void NonlinearCons::setDim(int dim){
|
49 |
|
50 |
if(dim != model->getDim()){
|
51 |
cout << "NonlinearCons Error: the dimension of index and the model does not match" << endl;
|
52 |
exit(ERROR_CONSTRAINT);
|
53 |
}
|
54 |
|
55 |
ConstraintBase::setDim(dim);
|
56 |
|
57 |
}
|
58 |
|
59 |
double NonlinearCons::calcResidual(vector<double>& x){
|
60 |
double fVal;
|
61 |
|
62 |
fVal = model->calcF(x);
|
63 |
|
64 |
fVal -= bound;
|
65 |
|
66 |
if(boundType == btLower)
|
67 |
fVal = -fVal;
|
68 |
|
69 |
return fVal;
|
70 |
}
|
71 |
|
72 |
vector<double> NonlinearCons::calcConsGrad(vector<double>& x){
|
73 |
vector<double> consGrad(ndim);
|
74 |
|
75 |
consGrad = model->calcGrad(x);
|
76 |
|
77 |
if(boundType == btLower){
|
78 |
for(int i = 0; i < consGrad.size(); i++)
|
79 |
consGrad[i] = -consGrad[i];
|
80 |
}
|
81 |
|
82 |
return consGrad;
|
83 |
}
|
84 |
|
85 |
SymMatrix NonlinearCons::calcConsHessian(vector<double>& x){
|
86 |
SymMatrix H(ndim);
|
87 |
|
88 |
H = model->calcHessian(x);
|
89 |
|
90 |
if(boundType == btLower){
|
91 |
//H = -H;
|
92 |
}
|
93 |
|
94 |
return H;
|
95 |
}
|