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