1 |
gezelter |
1741 |
/* -*- 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 |
|
|
|
22 |
|
|
#include "optimization/EndCriteria.hpp" |
23 |
|
|
#include "utils/simError.h" |
24 |
|
|
#include <cmath> |
25 |
|
|
|
26 |
|
|
|
27 |
|
|
|
28 |
|
|
namespace QuantLib { |
29 |
|
|
|
30 |
|
|
EndCriteria::EndCriteria(size_t maxIterations, |
31 |
|
|
size_t maxStationaryStateIterations, |
32 |
|
|
RealType rootEpsilon, |
33 |
|
|
RealType functionEpsilon, |
34 |
|
|
RealType gradientNormEpsilon) |
35 |
|
|
: maxIterations_(maxIterations), |
36 |
|
|
maxStationaryStateIterations_(maxStationaryStateIterations), |
37 |
|
|
rootEpsilon_(rootEpsilon), |
38 |
|
|
functionEpsilon_(functionEpsilon), |
39 |
|
|
gradientNormEpsilon_(gradientNormEpsilon) { |
40 |
|
|
|
41 |
|
|
|
42 |
|
|
// replaced the QL_REQUIRE macro with OpenMD's simError calls |
43 |
|
|
if (maxStationaryStateIterations_ <= 1) { |
44 |
|
|
sprintf(painCave.errMsg, |
45 |
|
|
"maxStationaryStateIterations_ ( %lu ) " |
46 |
|
|
"must be greater than one\n", |
47 |
|
|
(unsigned long)maxStationaryStateIterations_); |
48 |
|
|
painCave.isFatal = 1; |
49 |
|
|
painCave.severity = OPENMD_ERROR; |
50 |
|
|
simError(); |
51 |
|
|
} |
52 |
|
|
if (maxStationaryStateIterations_ > maxIterations_) { |
53 |
|
|
sprintf(painCave.errMsg, |
54 |
|
|
"maxStationaryStateIterations_ ( %lu ) " |
55 |
|
|
"must be less than maxIterations_ ( %lu )\n", |
56 |
|
|
(unsigned long)maxStationaryStateIterations_, |
57 |
|
|
(unsigned long)maxIterations_); |
58 |
|
|
painCave.isFatal = 1; |
59 |
|
|
painCave.severity = OPENMD_ERROR; |
60 |
|
|
simError(); |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
bool EndCriteria::checkMaxIterations(const size_t iteration, |
66 |
|
|
EndCriteria::Type& ecType) const{ |
67 |
|
|
if (iteration < maxIterations_) |
68 |
|
|
return false; |
69 |
|
|
ecType = MaxIterations; |
70 |
|
|
return true; |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
bool EndCriteria::checkStationaryPoint(const RealType xOld, |
74 |
|
|
const RealType xNew, |
75 |
|
|
size_t& statStateIterations, |
76 |
|
|
EndCriteria::Type& ecType) const { |
77 |
|
|
if (std::fabs(xNew-xOld) >= rootEpsilon_) { |
78 |
|
|
statStateIterations = 0; |
79 |
|
|
return false; |
80 |
|
|
} |
81 |
|
|
++statStateIterations; |
82 |
|
|
if (statStateIterations <= maxStationaryStateIterations_) |
83 |
|
|
return false; |
84 |
|
|
ecType = StationaryPoint; |
85 |
|
|
return true; |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
bool EndCriteria::checkStationaryFunctionValue( |
89 |
|
|
const RealType fxOld, |
90 |
|
|
const RealType fxNew, |
91 |
|
|
size_t& statStateIterations, |
92 |
|
|
EndCriteria::Type& ecType) const { |
93 |
|
|
if (std::fabs(fxNew-fxOld) >= functionEpsilon_) { |
94 |
|
|
statStateIterations = 0; |
95 |
|
|
return false; |
96 |
|
|
} |
97 |
|
|
++statStateIterations; |
98 |
|
|
if (statStateIterations <= maxStationaryStateIterations_) |
99 |
|
|
return false; |
100 |
|
|
ecType = StationaryFunctionValue; |
101 |
|
|
return true; |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
bool EndCriteria::checkStationaryFunctionAccuracy( |
105 |
|
|
const RealType f, |
106 |
|
|
const bool positiveOptimization, |
107 |
|
|
EndCriteria::Type& ecType) const { |
108 |
|
|
if (!positiveOptimization) |
109 |
|
|
return false; |
110 |
|
|
if (f >= functionEpsilon_) |
111 |
|
|
return false; |
112 |
|
|
ecType = StationaryFunctionAccuracy; |
113 |
|
|
return true; |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
//bool EndCriteria::checkZerGradientNormValue( |
117 |
|
|
// const RealType gNormOld, |
118 |
|
|
// const RealType gNormNew, |
119 |
|
|
// EndCriteria::Type& ecType) const { |
120 |
|
|
// if (std::fabs(gNormNew-gNormOld) >= gradientNormEpsilon_) |
121 |
|
|
// return false; |
122 |
|
|
// ecType = StationaryGradient; |
123 |
|
|
// return true; |
124 |
|
|
//} |
125 |
|
|
|
126 |
|
|
bool EndCriteria::checkZeroGradientNorm(const RealType gradientNorm, |
127 |
|
|
EndCriteria::Type& ecType) const { |
128 |
|
|
if (gradientNorm >= gradientNormEpsilon_) |
129 |
|
|
return false; |
130 |
|
|
ecType = ZeroGradientNorm; |
131 |
|
|
return true; |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
bool EndCriteria::operator()(const size_t iteration, |
135 |
|
|
size_t& statStateIterations, |
136 |
|
|
const bool positiveOptimization, |
137 |
|
|
const RealType fold, |
138 |
|
|
const RealType, //normgold, |
139 |
|
|
const RealType fnew, |
140 |
|
|
const RealType normgnew, |
141 |
|
|
EndCriteria::Type& ecType) const { |
142 |
|
|
return |
143 |
|
|
checkMaxIterations(iteration, ecType) || |
144 |
|
|
checkStationaryFunctionValue(fold, fnew, statStateIterations, ecType) || |
145 |
|
|
checkStationaryFunctionAccuracy(fnew, positiveOptimization, ecType) || |
146 |
|
|
checkZeroGradientNorm(normgnew, ecType); |
147 |
|
|
} |
148 |
|
|
|
149 |
|
|
// Inspectors |
150 |
|
|
size_t EndCriteria::maxIterations() const { |
151 |
|
|
return maxIterations_; |
152 |
|
|
} |
153 |
|
|
|
154 |
|
|
size_t EndCriteria::maxStationaryStateIterations() const { |
155 |
|
|
return maxStationaryStateIterations_; |
156 |
|
|
} |
157 |
|
|
|
158 |
|
|
RealType EndCriteria::rootEpsilon() const { |
159 |
|
|
return rootEpsilon_; |
160 |
|
|
} |
161 |
|
|
|
162 |
|
|
RealType EndCriteria::functionEpsilon() const { |
163 |
|
|
return functionEpsilon_; |
164 |
|
|
} |
165 |
|
|
|
166 |
|
|
RealType EndCriteria::gradientNormEpsilon() const { |
167 |
|
|
return gradientNormEpsilon_; |
168 |
|
|
} |
169 |
|
|
|
170 |
|
|
std::ostream& operator<<(std::ostream& out, EndCriteria::Type ec) { |
171 |
|
|
switch (ec) { |
172 |
|
|
case QuantLib::EndCriteria::None: |
173 |
|
|
return out << "None"; |
174 |
|
|
case QuantLib::EndCriteria::MaxIterations: |
175 |
|
|
return out << "MaxIterations"; |
176 |
|
|
case QuantLib::EndCriteria::StationaryPoint: |
177 |
|
|
return out << "StationaryPoint"; |
178 |
|
|
case QuantLib::EndCriteria::StationaryFunctionValue: |
179 |
|
|
return out << "StationaryFunctionValue"; |
180 |
|
|
case QuantLib::EndCriteria::StationaryFunctionAccuracy: |
181 |
|
|
return out << "StationaryFunctionAccuracy"; |
182 |
|
|
case QuantLib::EndCriteria::ZeroGradientNorm: |
183 |
|
|
return out << "ZeroGradientNorm"; |
184 |
|
|
case QuantLib::EndCriteria::Unknown: |
185 |
|
|
return out << "Unknown"; |
186 |
|
|
default: |
187 |
|
|
sprintf(painCave.errMsg, "unknown EndCriteria::Type ( %d )\n", |
188 |
|
|
int(ec)); |
189 |
|
|
painCave.isFatal = 1; |
190 |
|
|
painCave.severity = OPENMD_ERROR; |
191 |
|
|
simError(); |
192 |
|
|
} |
193 |
gezelter |
1824 |
return out; |
194 |
gezelter |
1741 |
} |
195 |
|
|
|
196 |
|
|
} |