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