1 |
#include "OOPSEMinimizer.hpp" |
2 |
#include "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 |
//optimize along the search direction and reset minimum point value |
17 |
lsStatus = doLineSearch(direction, stepSize); |
18 |
|
19 |
if (lsStatus < 0) |
20 |
return -1; |
21 |
else |
22 |
return 1; |
23 |
} |
24 |
|
25 |
void PRCGMinimizer::prepareStep(){ |
26 |
vector<double> deltaGrad; |
27 |
double beta; |
28 |
size_t i; |
29 |
|
30 |
deltaGrad.resize(ndim); |
31 |
|
32 |
//calculate the new direction using Polak-Ribiere Conjugate Gradient |
33 |
|
34 |
for(i = 0; i < curG.size(); i++) |
35 |
deltaGrad[i] = curG[i] - prevG[i]; |
36 |
|
37 |
#ifndef IS_MPI |
38 |
beta = dotProduct(deltaGrad, curG) / dotProduct(prevG, prevG); |
39 |
#else |
40 |
double localDP1; |
41 |
double localDP2; |
42 |
double globalDP1; |
43 |
double globalDP2; |
44 |
|
45 |
localDP1 = dotProduct(deltaGrad, curG); |
46 |
localDP2 = dotProduct(prevG, prevG); |
47 |
|
48 |
MPI_Allreduce(&localDP1, &globalDP1, 1, MPI_DOUBLE,MPI_SUM, MPI_COMM_WORLD); |
49 |
MPI_Allreduce(&localDP2, &globalDP2, 1, MPI_DOUBLE,MPI_SUM, MPI_COMM_WORLD); |
50 |
|
51 |
beta = globalDP1 / globalDP2; |
52 |
#endif |
53 |
|
54 |
for(i = 0; i < direction.size(); i++) |
55 |
direction[i] = -curG[i] + beta * direction[i]; |
56 |
|
57 |
} |