1 |
#include "minimizers/OOPSEMinimizer.hpp" |
2 |
#include "utils/Utility.hpp" |
3 |
|
4 |
SDMinimizer::SDMinimizer(SimInfo *theInfo, MinimizerParameterSet *param) : |
5 |
OOPSEMinimizer(theInfo, the_ff, param) { |
6 |
direction.resize(ndim); |
7 |
stepSize = paramSet->getStepSize(); |
8 |
} |
9 |
|
10 |
void SDMinimizer::init() { |
11 |
calcG(); |
12 |
|
13 |
for(int i = 0; i < direction.size(); i++) { |
14 |
direction[i] = -curG[i]; |
15 |
} |
16 |
} |
17 |
|
18 |
int SDMinimizer::step() { |
19 |
int lsStatus; |
20 |
|
21 |
prevF = curF; |
22 |
|
23 |
//optimize along the search direction and reset minimum point value |
24 |
lsStatus = doLineSearch(direction, stepSize); |
25 |
|
26 |
if (lsStatus < 0) |
27 |
return -1; |
28 |
else |
29 |
return 1; |
30 |
} |
31 |
|
32 |
void SDMinimizer::prepareStep() { |
33 |
for(int i = 0; i < direction.size(); i++) { |
34 |
direction[i] = -curG[i]; |
35 |
} |
36 |
} |
37 |
|
38 |
int SDMinimizer::checkConvg() { |
39 |
double fTol; |
40 |
double relativeFTol; // relative tolerance |
41 |
double deltaF; |
42 |
double gTol; |
43 |
double relativeGTol; |
44 |
double gnorm; |
45 |
|
46 |
// test function tolerance test |
47 |
fTol = paramSet->getFTol(); |
48 |
relativeFTol = fTol * std::max(1.0, fabs(curF)); // relative tolerance |
49 |
deltaF = prevF - curF; |
50 |
|
51 |
if (fabs(deltaF) <= relativeFTol) { |
52 |
if (bVerbose) { |
53 |
cout << "function value tolerance test passed" << std::endl; |
54 |
cout << "ftol = " << fTol << "\tdeltaf = " << deltaF << std::endl; |
55 |
} |
56 |
|
57 |
return CONVG_FTOL; |
58 |
} |
59 |
|
60 |
//gradient tolerance test |
61 |
gTol = paramSet->getGTol(); |
62 |
relativeGTol = gTol * std::max(1.0, fabs(curF)); |
63 |
|
64 |
#ifndef IS_MPI |
65 |
|
66 |
gnorm = sqrt(dotProduct(curG, curG)); |
67 |
|
68 |
#else |
69 |
|
70 |
double localDP; |
71 |
double globalDP; |
72 |
|
73 |
localDP = dotProduct(curG, curG); |
74 |
MPI_Allreduce(&localDP, &globalDP, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD); |
75 |
gnorm = sqrt(globalDP); |
76 |
|
77 |
#endif |
78 |
|
79 |
if (gnorm <= relativeGTol) { |
80 |
cout << "gradient tolerance test" << std::endl; |
81 |
cout << "gnorm = " << gnorm << "\trelativeGTol = " << relativeGTol |
82 |
<< std::endl; |
83 |
return CONVG_GTOL; |
84 |
} |
85 |
|
86 |
//absolute gradient tolerance test |
87 |
|
88 |
if (gnorm <= gTol) { |
89 |
cout << "absolute gradient tolerance test" << std::endl; |
90 |
cout << "gnorm = " << gnorm << "\tgTol = " << gTol << std::endl; |
91 |
return CONVG_ABSGTOL; |
92 |
} |
93 |
|
94 |
return CONVG_UNCONVG; |
95 |
} |