OpenMD 3.0
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
LineSearchBasedMethod.cpp
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
22
26
27using namespace OpenMD;
28namespace QuantLib {
29
30 LineSearchBasedMethod::LineSearchBasedMethod(LineSearch* lineSearch) :
31 lineSearch_(lineSearch) {
32 if (!lineSearch_) lineSearch_ = new ArmijoLineSearch();
33 }
34
35 LineSearchBasedMethod::~LineSearchBasedMethod() { delete lineSearch_; }
36
37 EndCriteria::Type LineSearchBasedMethod::minimize(
38 Problem& P, const EndCriteria& endCriteria,
39 RealType initialStepSize = 1.0) {
40 // Initializations
41 RealType ftol = endCriteria.functionEpsilon();
42 size_t maxStationaryStateIterations_ =
43 endCriteria.maxStationaryStateIterations();
44 EndCriteria::Type ecType = EndCriteria::None; // reset end criteria
45 P.reset(); // reset problem
46 DynamicVector<RealType> x_ = P.currentValue(); // store the starting point
47 size_t iterationNumber_ = 0;
48 // dimension line search
49 lineSearch_->searchDirection() = DynamicVector<RealType>(x_.size());
50 bool done = false;
51
52 // function and squared norm of gradient values;
53 RealType fnew, fold, gold2;
54 RealType fdiff;
55 // classical initial value for line-search step
56 // RealType t = 1.0;
57 // new initial value for line-search step
58 RealType t = initialStepSize;
59 // Set gradient g at the size of the optimization problem
60 // search direction
61 size_t sz = lineSearch_->searchDirection().size();
62 DynamicVector<RealType> prevGradient(sz), d(sz), sddiff(sz), direction(sz);
63 // Initialize objective function, gradient prevGradient and
64 // search direction
65
66 P.setFunctionValue(P.valueAndGradient(prevGradient, x_));
67 P.setGradientNormValue(P.DotProduct(prevGradient, prevGradient));
68 lineSearch_->searchDirection() = -prevGradient;
69
70 bool first_time = true;
71 // Loop over iterations
72 do {
73 fold = P.functionValue();
74 gold2 = P.gradientNormValue();
75
76 // Linesearch
77 if (!first_time) prevGradient = lineSearch_->lastGradient();
78
79 t = (*lineSearch_)(P, ecType, endCriteria, t);
80
81 // don't throw: it can fail just because maxIterations exceeded
82 // QL_REQUIRE(lineSearch_->succeed(), "line-search failed!");
83 if (lineSearch_->succeed()) {
84 // Updates
85 // New point
86 x_ = lineSearch_->lastX();
87 P.setCurrentValue(x_);
88 // New function value
89 P.setFunctionValue(lineSearch_->lastFunctionValue());
90 // New gradient and search direction vectors
91
92 // orthogonalization coef
93 P.setGradientNormValue(lineSearch_->lastGradientNorm2());
94
95 // conjugate gradient search direction
96 direction = getUpdatedDirection(P, gold2, prevGradient);
97
98 sddiff = direction - lineSearch_->searchDirection();
99
100 lineSearch_->searchDirection() = direction;
101 // Now compute accuracy and check end criteria
102 // Numerical Recipes exit strategy on fx (see NR in C++, p.423)
103
104 fnew = P.functionValue();
105 fdiff = 2.0 * std::fabs(fnew - fold) /
106 (std::fabs(fnew) + std::fabs(fold) +
107 std::numeric_limits<RealType>::epsilon());
108
109 if (fdiff < ftol ||
110 endCriteria.checkMaxIterations(iterationNumber_, ecType)) {
111 endCriteria.checkStationaryFunctionValue(
112 0.0, 0.0, maxStationaryStateIterations_, ecType);
113 endCriteria.checkMaxIterations(iterationNumber_, ecType);
114 return ecType;
115 }
116 P.setCurrentValue(x_); // update problem current value
117 ++iterationNumber_; // Increase iteration number
118 first_time = false;
119 } else {
120 done = true;
121 }
122 } while (!done);
123 P.setCurrentValue(x_);
124 return ecType;
125 }
126
127} // namespace QuantLib
Armijo line-search class.
Line search abstract class.
Abstract optimization method class.
Abstract optimization problem class.
Criteria to end optimization process:
Constrained optimization problem.
Definition Problem.hpp:37
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.