OpenMD 3.1
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
EndCriteria.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2006, 2007 Ferdinando Ametrano
5 Copyright (C) 2007 Marco Bianchetti
6 Copyright (C) 2001, 2002, 2003 Nicolas Di Césaré
7
8 This file is part of QuantLib, a free-software/open-source library
9 for financial quantitative analysts and developers - http://quantlib.org/
10
11 QuantLib is free software: you can redistribute it and/or modify it
12 under the terms of the QuantLib license. You should have received a
13 copy of the license along with this program; if not, please email
14 <quantlib-dev@lists.sf.net>. The license is also available online at
15 <http://quantlib.org/license.shtml>.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the license for more details.
20*/
21
23
24#include <cmath>
25#include <cstdio>
26
27#include "utils/simError.h"
28
29namespace QuantLib {
30
31 EndCriteria::EndCriteria(size_t maxIterations,
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) {
39 // replaced the QL_REQUIRE macro with OpenMD's simError calls
41 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
42 "maxStationaryStateIterations_ ( %lu ) "
43 "must be greater than one\n",
44 (unsigned long)maxStationaryStateIterations_);
45 painCave.isFatal = 1;
46 painCave.severity = OPENMD_ERROR;
47 simError();
48 }
50 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
51 "maxStationaryStateIterations_ ( %lu ) "
52 "must be less than maxIterations_ ( %lu )\n",
53 (unsigned long)maxStationaryStateIterations_,
54 (unsigned long)maxIterations_);
55 painCave.isFatal = 1;
56 painCave.severity = OPENMD_ERROR;
57 simError();
58 }
59 }
60
61 bool EndCriteria::checkMaxIterations(const size_t iteration,
62 EndCriteria::Type& ecType) const {
63 if (iteration < maxIterations_) return false;
64 ecType = MaxIterations;
65 return true;
66 }
67
68 bool EndCriteria::checkStationaryPoint(const RealType xOld,
69 const RealType xNew,
70 size_t& statStateIterations,
71 EndCriteria::Type& ecType) const {
72 if (std::fabs(xNew - xOld) >= rootEpsilon_) {
73 statStateIterations = 0;
74 return false;
75 }
76 ++statStateIterations;
77 if (statStateIterations <= maxStationaryStateIterations_) return false;
78 ecType = StationaryPoint;
79 return true;
80 }
81
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;
87 return false;
88 }
89 ++statStateIterations;
90 if (statStateIterations <= maxStationaryStateIterations_) return false;
91 ecType = StationaryFunctionValue;
92 return true;
93 }
94
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;
101 return true;
102 }
103
104 // bool EndCriteria::checkZerGradientNormValue(
105 // const RealType gNormOld,
106 // const RealType gNormNew,
107 // EndCriteria::Type& ecType) const {
108 // if (std::fabs(gNormNew-gNormOld) >= gradientNormEpsilon_)
109 // return false;
110 // ecType = StationaryGradient;
111 // return true;
112 //}
113
114 bool EndCriteria::checkZeroGradientNorm(const RealType gradientNorm,
115 EndCriteria::Type& ecType) const {
116 if (gradientNorm >= gradientNormEpsilon_) return false;
117 ecType = ZeroGradientNorm;
118 return true;
119 }
120
121 bool EndCriteria::operator()(const size_t iteration,
122 size_t& statStateIterations,
123 const bool positiveOptimization,
124 const RealType fold,
125 const RealType, // normgold,
126 const RealType fnew, const RealType normgnew,
127 EndCriteria::Type& ecType) const {
128 return checkMaxIterations(iteration, ecType) ||
129 checkStationaryFunctionValue(fold, fnew, statStateIterations,
130 ecType) ||
131 checkStationaryFunctionAccuracy(fnew, positiveOptimization,
132 ecType) ||
133 checkZeroGradientNorm(normgnew, ecType);
134 }
135
136 // Inspectors
137 size_t EndCriteria::maxIterations() const { return maxIterations_; }
138
139 size_t EndCriteria::maxStationaryStateIterations() const {
141 }
142
143 RealType EndCriteria::rootEpsilon() const { return rootEpsilon_; }
144
145 RealType EndCriteria::functionEpsilon() const { return functionEpsilon_; }
146
147 RealType EndCriteria::gradientNormEpsilon() const {
148 return gradientNormEpsilon_;
149 }
150
151 std::ostream& operator<<(std::ostream& out, EndCriteria::Type ec) {
152 switch (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";
167 default:
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;
172 simError();
173 }
174 return out;
175 }
176
177} // namespace QuantLib
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.