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