1 |
#include "ConjugateMinimizer.hpp"
|
2 |
#include "Utility.hpp"
|
3 |
|
4 |
bool ConjugateMinimizerBase::isSolvable(){
|
5 |
|
6 |
//conjuage gradient can only solve unconstrained nonlinear model
|
7 |
|
8 |
if (!model->hasConstraints())
|
9 |
return true;
|
10 |
else
|
11 |
return false;
|
12 |
}
|
13 |
|
14 |
void ConjugateMinimizerBase::Init(){
|
15 |
|
16 |
}
|
17 |
|
18 |
void ConjugateMinimizerBase::printMinizerInfo(){
|
19 |
|
20 |
}
|
21 |
|
22 |
void ConjugateMinimizerBase::Minimize(){
|
23 |
int maxIteration;
|
24 |
int nextResetIter;
|
25 |
int resetFrq;
|
26 |
int nextWriteIter;
|
27 |
int writeFrq;
|
28 |
int lsStatus;
|
29 |
double gamma;
|
30 |
double lamda;
|
31 |
|
32 |
|
33 |
if (!isSolvable()){
|
34 |
cout << "ConjugateMinimizerBase Error: This nonlinear model can not be solved by " << minimizerName <<endl;
|
35 |
|
36 |
exit(1);
|
37 |
}
|
38 |
|
39 |
printMinizerInfo();
|
40 |
|
41 |
resetFrq = paramSet->getResetFrq();
|
42 |
nextResetIter = resetFrq;
|
43 |
|
44 |
writeFrq = paramSet->getWriteFrq();
|
45 |
nextWriteIter = writeFrq;
|
46 |
|
47 |
prevGrad = model->calcGrad();
|
48 |
|
49 |
direction = prevGrad;
|
50 |
|
51 |
maxIteration = paramSet->getMaxIteration();
|
52 |
|
53 |
for(currentIter = 0;currentIter < maxIteration; currentIter++){
|
54 |
|
55 |
// perform line search to minimize f(x + stepSize * direction) where stepSize > 0
|
56 |
lsMinimizer->minimize(direction, 0.0, 1.0);
|
57 |
|
58 |
lsStatus = lsMinimizer->getMinimizationStatus();
|
59 |
|
60 |
if(lsStatus ==MINSTATUS_ERROR){
|
61 |
minStatus = MINSTATUS_ERROR;
|
62 |
return;
|
63 |
}
|
64 |
|
65 |
prevMinX = minX;
|
66 |
lamda = lsMinimizer->getMinVar();
|
67 |
|
68 |
for(int i = 0; i < direction.size(); i++)
|
69 |
minX[i] = minX[i] + lamda * direction[i];
|
70 |
|
71 |
//calculate the gradient
|
72 |
prevGrad = gradient;
|
73 |
|
74 |
gradient = model->calcGrad();
|
75 |
|
76 |
// stop if converge
|
77 |
if (checkConvergence() > 0){
|
78 |
writeOut(minX, currentIter);
|
79 |
|
80 |
minStatus = MINSTATUS_CONVERGE;
|
81 |
return;
|
82 |
}
|
83 |
|
84 |
|
85 |
//calculate the
|
86 |
gamma = calcGamma(gradient, prevGrad);
|
87 |
|
88 |
// update new direction
|
89 |
prevDirection = direction;
|
90 |
|
91 |
for(int i = 0; i < direction.size(); i++)
|
92 |
direction[i] += gamma * direction[i];
|
93 |
|
94 |
//
|
95 |
if (currentIter == nextWriteIter){
|
96 |
nextWriteIter += writeFrq;
|
97 |
writeOut(minX, currentIter);
|
98 |
}
|
99 |
|
100 |
if (currentIter == nextResetIter){
|
101 |
reset();
|
102 |
nextResetIter += resetFrq;
|
103 |
}
|
104 |
|
105 |
}
|
106 |
|
107 |
// if writeFrq is not a multipiler of maxIteration, we need to write the final result
|
108 |
// otherwise, we already write it inside the loop, just skip it
|
109 |
if(currentIter != (nextWriteIter - writeFrq))
|
110 |
writeOut(minX, currentIter);
|
111 |
|
112 |
minStatus = MINSTATUS_MAXITER;
|
113 |
return;
|
114 |
}
|
115 |
|
116 |
int ConjugateMinimizerBase::checkConvergence(){
|
117 |
|
118 |
//test absolute gradient tolerance
|
119 |
|
120 |
if (sqrt(dot(gradient, gradient)) < paramSet->getGradTol())
|
121 |
return 1;
|
122 |
else
|
123 |
return -1;
|
124 |
}
|
125 |
|
126 |
void ConjugateMinimizerBase::reset(){
|
127 |
|
128 |
}
|
129 |
|
130 |
double FRCGMinimizer::calcGamma(vector<double>& newGrad, vector<double>& oldGrad){
|
131 |
return dot(newGrad, newGrad) / dot(oldGrad, newGrad);
|
132 |
}
|
133 |
|
134 |
double PRCGMinimizer::calcGamma(vector<double>& newGrad, vector<double>& oldGrad){
|
135 |
double gamma;
|
136 |
vector<double> deltaGrad;
|
137 |
|
138 |
for(int i = 0; i < newGrad.size(); i++)
|
139 |
deltaGrad.push_back(newGrad[i] - oldGrad[i]);
|
140 |
|
141 |
return dot(deltaGrad, newGrad) / dot(oldGrad, oldGrad);
|
142 |
|
143 |
}
|