ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/trunk/src/optimization/EndCriteria.cpp
Revision: 1981
Committed: Mon Apr 14 18:32:51 2014 UTC (11 years ago) by gezelter
File size: 7904 byte(s)
Log Message:
Added the ConstraintWriter

File Contents

# User Rev Content
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 gezelter 1879
28 gezelter 1741 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 gezelter 1981 sprintf(painCave.errMsg,
148     "\n\tOptimization: Current iteration Count: ( %lu )\n",
149     (unsigned long)iteration);
150     painCave.isFatal = 0;
151     painCave.severity = OPENMD_INFO;
152     simError();
153 gezelter 1741 }
154    
155     // Inspectors
156     size_t EndCriteria::maxIterations() const {
157     return maxIterations_;
158     }
159    
160     size_t EndCriteria::maxStationaryStateIterations() const {
161     return maxStationaryStateIterations_;
162     }
163    
164     RealType EndCriteria::rootEpsilon() const {
165     return rootEpsilon_;
166     }
167    
168     RealType EndCriteria::functionEpsilon() const {
169     return functionEpsilon_;
170     }
171    
172     RealType EndCriteria::gradientNormEpsilon() const {
173     return gradientNormEpsilon_;
174     }
175    
176     std::ostream& operator<<(std::ostream& out, EndCriteria::Type ec) {
177     switch (ec) {
178     case QuantLib::EndCriteria::None:
179     return out << "None";
180     case QuantLib::EndCriteria::MaxIterations:
181     return out << "MaxIterations";
182     case QuantLib::EndCriteria::StationaryPoint:
183     return out << "StationaryPoint";
184     case QuantLib::EndCriteria::StationaryFunctionValue:
185     return out << "StationaryFunctionValue";
186     case QuantLib::EndCriteria::StationaryFunctionAccuracy:
187     return out << "StationaryFunctionAccuracy";
188     case QuantLib::EndCriteria::ZeroGradientNorm:
189     return out << "ZeroGradientNorm";
190     case QuantLib::EndCriteria::Unknown:
191     return out << "Unknown";
192     default:
193 gezelter 1880 sprintf(painCave.errMsg, "unknown EndCriteria::Type ( %d )\n",
194 gezelter 1741 int(ec));
195     painCave.isFatal = 1;
196     painCave.severity = OPENMD_ERROR;
197     simError();
198     }
199 gezelter 1879 return out;
200 gezelter 1741 }
201    
202     }

Properties

Name Value
svn:eol-style native