| 1 |
|
#include "SteepestDescent.hpp" |
| 2 |
+ |
#include "Utility.hpp" |
| 3 |
+ |
void SteepestDescent::minimize(){ |
| 4 |
+ |
int maxIteration; |
| 5 |
+ |
int nextRestIter; |
| 6 |
+ |
int resetFrq; |
| 7 |
+ |
int nextWriteIter; |
| 8 |
+ |
int writeFrq; |
| 9 |
+ |
|
| 10 |
+ |
if (!isSolvable()){ |
| 11 |
+ |
cout << "ConjugateMinimizerBase Error: This nonlinear model can not be solved by " << methodName <<endl; |
| 12 |
|
|
| 13 |
+ |
exit(1); |
| 14 |
+ |
} |
| 15 |
+ |
|
| 16 |
+ |
printMinizerInfo(); |
| 17 |
+ |
|
| 18 |
+ |
resetFrq = paramSet->getResetFrq(); |
| 19 |
+ |
nextRestIter = resetFrq; |
| 20 |
+ |
|
| 21 |
+ |
writeFrq = paramSet->getWriteFrq(); |
| 22 |
+ |
nextWriteIter = writeFrq; |
| 23 |
+ |
|
| 24 |
+ |
direction = model->calcGrad(); |
| 25 |
+ |
|
| 26 |
+ |
maxIteration = paramSet->getMaxIteration(); |
| 27 |
+ |
|
| 28 |
+ |
for(currentIter = 0;currentIter < maxIteration; currentIter++){ |
| 29 |
+ |
|
| 30 |
+ |
// perform line search to minimize f(x + stepSize * direction) where stepSize > 0 |
| 31 |
+ |
lsMinimizer->minimize(direction, 0.0, 1.0); |
| 32 |
+ |
|
| 33 |
+ |
lsStatus = lsMinimizer->getMinimizationStatus(); |
| 34 |
+ |
|
| 35 |
+ |
if(lsStatus ==MINSTATUS_ERROR){ |
| 36 |
+ |
minStatus = MINSTATUS_ERROR; |
| 37 |
+ |
return; |
| 38 |
+ |
} |
| 39 |
+ |
|
| 40 |
+ |
prevMinX = minX; |
| 41 |
+ |
minX = minX + lsMinimizer->getMinVar() * direction; |
| 42 |
+ |
|
| 43 |
+ |
//calculate the gradient |
| 44 |
+ |
|
| 45 |
+ |
direction = model->calcGrad(minX); |
| 46 |
+ |
|
| 47 |
+ |
// stop if converge |
| 48 |
+ |
if (checkConvergence() > 0){ |
| 49 |
+ |
writeOut(minX, currentIter); |
| 50 |
+ |
|
| 51 |
+ |
minStatus = MINSTATUS_CONVERGE; |
| 52 |
+ |
return; |
| 53 |
+ |
} |
| 54 |
+ |
|
| 55 |
+ |
// |
| 56 |
+ |
if (currentIter == nextWriteIter){ |
| 57 |
+ |
nextWriteIter += writeFrq; |
| 58 |
+ |
writeOut(minX, currentIter); |
| 59 |
+ |
} |
| 60 |
+ |
|
| 61 |
+ |
if (currentIter == nextResetIter){ |
| 62 |
+ |
reset(); |
| 63 |
+ |
nextResetIter += resetFrq; |
| 64 |
+ |
} |
| 65 |
+ |
|
| 66 |
+ |
} |
| 67 |
+ |
|
| 68 |
+ |
// if writeFrq is not a multipiler of maxIteration, we need to write the final result |
| 69 |
+ |
// otherwise, we already write it inside the loop, just skip it |
| 70 |
+ |
if(currentIter != (nextWriteIter - writeFrq)) |
| 71 |
+ |
writeOut(minX, currentIter); |
| 72 |
+ |
|
| 73 |
+ |
minStatus = MINSTATUS_MAXITER; |
| 74 |
+ |
return; |
| 75 |
+ |
} |