ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/trunk/src/optimization/LineSearchBasedMethod.cpp
Revision: 1969
Committed: Wed Feb 26 14:14:50 2014 UTC (11 years, 2 months ago) by gezelter
File size: 4969 byte(s)
Log Message:
Fixes to deal with deprecation of MPI C++ bindings.  We've reverted back to the
C calls.

File Contents

# Content
1 /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3 /*
4 Copyright (C) 2006 Ferdinando Ametrano
5 Copyright (C) 2009 Frédéric Degraeve
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19 */
20
21 #include "optimization/LineSearchBasedMethod.hpp"
22 #include "optimization/Problem.hpp"
23 #include "optimization/LineSearch.hpp"
24 #include "optimization/Armijo.hpp"
25 #include "utils/NumericConstant.hpp"
26
27 using namespace OpenMD;
28 namespace QuantLib {
29
30 LineSearchBasedMethod::LineSearchBasedMethod(LineSearch* lineSearch)
31 : lineSearch_(lineSearch) {
32 if (!lineSearch_)
33 lineSearch_ = new ArmijoLineSearch();
34 }
35
36 EndCriteria::Type
37 LineSearchBasedMethod::minimize(Problem& P,
38 const EndCriteria& endCriteria) {
39 // Initializations
40 RealType ftol = endCriteria.functionEpsilon();
41 size_t maxStationaryStateIterations_
42 = endCriteria.maxStationaryStateIterations();
43 EndCriteria::Type ecType = EndCriteria::None; // reset end criteria
44 P.reset(); // reset problem
45 DynamicVector<RealType> x_ = P.currentValue(); // store the starting point
46 size_t iterationNumber_ = 0;
47 // dimension line search
48 lineSearch_->searchDirection() = DynamicVector<RealType>(x_.size());
49 bool done = false;
50
51 // function and squared norm of gradient values;
52 RealType fnew, fold, gold2;
53 RealType fdiff;
54 // classical initial value for line-search step
55 RealType t = 1.0;
56 // Set gradient g at the size of the optimization problem
57 // search direction
58 size_t sz = lineSearch_->searchDirection().size();
59 DynamicVector<RealType> prevGradient(sz), d(sz), sddiff(sz), direction(sz);
60 // Initialize objective function, gradient prevGradient and
61 // search direction
62 P.setFunctionValue(P.valueAndGradient(prevGradient, x_));
63 P.setGradientNormValue(P.DotProduct(prevGradient, prevGradient));
64 lineSearch_->searchDirection() = -prevGradient;
65
66 bool first_time = true;
67 // Loop over iterations
68 do {
69 // Linesearch
70 if (!first_time)
71 prevGradient = lineSearch_->lastGradient();
72 t = (*lineSearch_)(P, ecType, endCriteria, t);
73 // don't throw: it can fail just because maxIterations exceeded
74 //QL_REQUIRE(lineSearch_->succeed(), "line-search failed!");
75 if (lineSearch_->succeed())
76 {
77 // Updates
78 // New point
79 x_ = lineSearch_->lastX();
80 // New function value
81 fold = P.functionValue();
82 P.setFunctionValue(lineSearch_->lastFunctionValue());
83 // New gradient and search direction vectors
84
85 // orthogonalization coef
86 gold2 = P.gradientNormValue();
87 P.setGradientNormValue(lineSearch_->lastGradientNorm2());
88
89 // conjugate gradient search direction
90 direction = getUpdatedDirection(P, gold2, prevGradient);
91 sddiff = direction - lineSearch_->searchDirection();
92 lineSearch_->searchDirection() = direction;
93 // Now compute accuracy and check end criteria
94 // Numerical Recipes exit strategy on fx (see NR in C++, p.423)
95
96 fnew = P.functionValue();
97 fdiff = 2.0*std::fabs(fnew-fold) /
98 (std::fabs(fnew) + std::fabs(fold) + NumericConstant::epsilon);
99
100 if (fdiff < ftol ||
101 endCriteria.checkMaxIterations(iterationNumber_, ecType)) {
102 endCriteria.checkStationaryFunctionValue(0.0, 0.0,
103 maxStationaryStateIterations_, ecType);
104 endCriteria.checkMaxIterations(iterationNumber_, ecType);
105 return ecType;
106 }
107
108 P.setCurrentValue(x_); // update problem current value
109 ++iterationNumber_; // Increase iteration number
110 first_time = false;
111 } else {
112 done = true;
113 }
114 } while (!done);
115 P.setCurrentValue(x_);
116 return ecType;
117 }
118
119 }

Properties

Name Value
svn:eol-style native