| 1 |
#include "minimizers/OOPSEMinimizer.hpp" |
| 2 |
#include "utils/Utility.hpp" |
| 3 |
void PRCGMinimizer::init(){ |
| 4 |
|
| 5 |
calcG(); |
| 6 |
|
| 7 |
for(int i = 0; i < direction.size(); i++){ |
| 8 |
direction[i] = -curG[i]; |
| 9 |
} |
| 10 |
|
| 11 |
} |
| 12 |
|
| 13 |
int PRCGMinimizer::step(){ |
| 14 |
int lsStatus; |
| 15 |
|
| 16 |
prevF = curF; |
| 17 |
prevG = curG; |
| 18 |
prevX = curX; |
| 19 |
|
| 20 |
//optimize along the search direction and reset minimum point value |
| 21 |
lsStatus = doLineSearch(direction, stepSize); |
| 22 |
|
| 23 |
if (lsStatus < 0) |
| 24 |
return -1; |
| 25 |
else |
| 26 |
return 1; |
| 27 |
} |
| 28 |
|
| 29 |
void PRCGMinimizer::prepareStep(){ |
| 30 |
vector<double> deltaGrad; |
| 31 |
double beta; |
| 32 |
size_t i; |
| 33 |
|
| 34 |
deltaGrad.resize(ndim); |
| 35 |
|
| 36 |
//calculate the new direction using Polak-Ribiere Conjugate Gradient |
| 37 |
|
| 38 |
for(i = 0; i < curG.size(); i++) |
| 39 |
deltaGrad[i] = curG[i] - prevG[i]; |
| 40 |
|
| 41 |
#ifndef IS_MPI |
| 42 |
beta = dotProduct(deltaGrad, curG) / dotProduct(prevG, prevG); |
| 43 |
#else |
| 44 |
double localDP1; |
| 45 |
double localDP2; |
| 46 |
double globalDP1; |
| 47 |
double globalDP2; |
| 48 |
|
| 49 |
localDP1 = dotProduct(deltaGrad, curG); |
| 50 |
localDP2 = dotProduct(prevG, prevG); |
| 51 |
|
| 52 |
MPI_Allreduce(&localDP1, &globalDP1, 1, MPI_DOUBLE,MPI_SUM, MPI_COMM_WORLD); |
| 53 |
MPI_Allreduce(&localDP2, &globalDP2, 1, MPI_DOUBLE,MPI_SUM, MPI_COMM_WORLD); |
| 54 |
|
| 55 |
beta = globalDP1 / globalDP2; |
| 56 |
#endif |
| 57 |
|
| 58 |
for(i = 0; i < direction.size(); i++) |
| 59 |
direction[i] = -curG[i] + beta * direction[i]; |
| 60 |
|
| 61 |
} |