27#include "utils/simError.h"
32 size_t maxStationaryStateIterations,
33 RealType rootEpsilon, RealType functionEpsilon,
34 RealType gradientNormEpsilon) :
35 maxIterations_(maxIterations),
36 maxStationaryStateIterations_(maxStationaryStateIterations),
37 rootEpsilon_(rootEpsilon), functionEpsilon_(functionEpsilon),
38 gradientNormEpsilon_(gradientNormEpsilon) {
41 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
42 "maxStationaryStateIterations_ ( %lu ) "
43 "must be greater than one\n",
46 painCave.severity = OPENMD_ERROR;
50 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
51 "maxStationaryStateIterations_ ( %lu ) "
52 "must be less than maxIterations_ ( %lu )\n",
56 painCave.severity = OPENMD_ERROR;
62 EndCriteria::Type& ecType)
const {
64 ecType = MaxIterations;
70 size_t& statStateIterations,
71 EndCriteria::Type& ecType)
const {
73 statStateIterations = 0;
76 ++statStateIterations;
78 ecType = StationaryPoint;
83 const RealType fxOld,
const RealType fxNew,
size_t& statStateIterations,
84 EndCriteria::Type& ecType)
const {
85 if (std::fabs(fxNew - fxOld) >= functionEpsilon_) {
86 statStateIterations = 0;
89 ++statStateIterations;
91 ecType = StationaryFunctionValue;
96 const RealType f,
const bool positiveOptimization,
97 EndCriteria::Type& ecType)
const {
98 if (!positiveOptimization)
return false;
99 if (f >= functionEpsilon_)
return false;
100 ecType = StationaryFunctionAccuracy;
115 EndCriteria::Type& ecType)
const {
116 if (gradientNorm >= gradientNormEpsilon_)
return false;
117 ecType = ZeroGradientNorm;
122 size_t& statStateIterations,
123 const bool positiveOptimization,
126 const RealType fnew,
const RealType normgnew,
127 EndCriteria::Type& ecType)
const {
137 size_t EndCriteria::maxIterations()
const {
return maxIterations_; }
139 size_t EndCriteria::maxStationaryStateIterations()
const {
143 RealType EndCriteria::rootEpsilon()
const {
return rootEpsilon_; }
145 RealType EndCriteria::functionEpsilon()
const {
return functionEpsilon_; }
147 RealType EndCriteria::gradientNormEpsilon()
const {
148 return gradientNormEpsilon_;
151 std::ostream& operator<<(std::ostream& out, EndCriteria::Type ec) {
153 case QuantLib::EndCriteria::None:
154 return out <<
"None";
155 case QuantLib::EndCriteria::MaxIterations:
156 return out <<
"MaxIterations";
157 case QuantLib::EndCriteria::StationaryPoint:
158 return out <<
"StationaryPoint";
159 case QuantLib::EndCriteria::StationaryFunctionValue:
160 return out <<
"StationaryFunctionValue";
161 case QuantLib::EndCriteria::StationaryFunctionAccuracy:
162 return out <<
"StationaryFunctionAccuracy";
163 case QuantLib::EndCriteria::ZeroGradientNorm:
164 return out <<
"ZeroGradientNorm";
165 case QuantLib::EndCriteria::Unknown:
166 return out <<
"Unknown";
168 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
169 "unknown EndCriteria::Type ( %d )\n",
int(ec));
170 painCave.isFatal = 1;
171 painCave.severity = OPENMD_ERROR;
Optimization criteria class.
bool checkStationaryPoint(const RealType xOld, const RealType xNew, size_t &statStateIterations, EndCriteria::Type &ecType) const
Test if the root variation is below rootEpsilon.
RealType rootEpsilon_
root, function and gradient epsilons
bool checkStationaryFunctionAccuracy(const RealType f, const bool positiveOptimization, EndCriteria::Type &ecType) const
Test if the function value is below functionEpsilon.
bool operator()(const size_t iteration, size_t &statState, const bool positiveOptimization, const RealType fold, const RealType normgold, const RealType fnew, const RealType normgnew, EndCriteria::Type &ecType) const
Test if the number of iterations is not too big and if a minimum point is not reached.
size_t maxStationaryStateIterations_
Maximun number of iterations in stationary state.
bool checkStationaryFunctionValue(const RealType fxOld, const RealType fxNew, size_t &statStateIterations, EndCriteria::Type &ecType) const
Test if the function variation is below functionEpsilon.
bool checkZeroGradientNorm(const RealType gNorm, EndCriteria::Type &ecType) const
Test if the gradient norm value is below gradientNormEpsilon.
bool checkMaxIterations(const size_t iteration, EndCriteria::Type &ecType) const
Test if the number of iteration is below MaxIterations.
EndCriteria(size_t maxIterations, size_t maxStationaryStateIterations, RealType rootEpsilon, RealType functionEpsilon, RealType gradientNormEpsilon)
Initialization constructor.
size_t maxIterations_
Maximum number of iterations.