/src/quantlib/ql/math/optimization/armijo.cpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2001, 2002, 2003 Nicolas Di Césaré |
5 | | |
6 | | This file is part of QuantLib, a free-software/open-source library |
7 | | for financial quantitative analysts and developers - http://quantlib.org/ |
8 | | |
9 | | QuantLib is free software: you can redistribute it and/or modify it |
10 | | under the terms of the QuantLib license. You should have received a |
11 | | copy of the license along with this program; if not, please email |
12 | | <quantlib-dev@lists.sf.net>. The license is also available online at |
13 | | <https://www.quantlib.org/license.shtml>. |
14 | | |
15 | | This program is distributed in the hope that it will be useful, but WITHOUT |
16 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
17 | | FOR A PARTICULAR PURPOSE. See the license for more details. |
18 | | */ |
19 | | |
20 | | #include <ql/math/optimization/armijo.hpp> |
21 | | #include <ql/math/optimization/method.hpp> |
22 | | #include <ql/math/optimization/problem.hpp> |
23 | | |
24 | | namespace QuantLib { |
25 | | |
26 | | Real ArmijoLineSearch::operator()(Problem& P, |
27 | | EndCriteria::Type& ecType, |
28 | | const EndCriteria& endCriteria, |
29 | | const Real t_ini) |
30 | 0 | { |
31 | | //OptimizationMethod& method = P.method(); |
32 | 0 | Constraint& constraint = P.constraint(); |
33 | 0 | succeed_=true; |
34 | 0 | bool maxIter = false; |
35 | 0 | Real qtold, t = t_ini; |
36 | 0 | Size loopNumber = 0; |
37 | |
|
38 | 0 | Real q0 = P.functionValue(); |
39 | 0 | Real qp0 = P.gradientNormValue(); |
40 | |
|
41 | 0 | qt_ = q0; |
42 | 0 | qpt_ = (gradient_.empty()) ? qp0 : -DotProduct(gradient_,searchDirection_); |
43 | | |
44 | | // Initialize gradient |
45 | 0 | gradient_ = Array(P.currentValue().size()); |
46 | | // Compute new point |
47 | 0 | xtd_ = P.currentValue(); |
48 | 0 | t = update(xtd_, searchDirection_, t, constraint); |
49 | | // Compute function value at the new point |
50 | 0 | qt_ = P.value (xtd_); |
51 | | |
52 | | // Enter in the loop if the criterion is not satisfied |
53 | 0 | if ((qt_-q0) > -alpha_*t*qpt_) { |
54 | 0 | do { |
55 | 0 | loopNumber++; |
56 | | // Decrease step |
57 | 0 | t *= beta_; |
58 | | // Store old value of the function |
59 | 0 | qtold = qt_; |
60 | | // New point value |
61 | 0 | xtd_ = P.currentValue(); |
62 | 0 | t = update(xtd_, searchDirection_, t, constraint); |
63 | | |
64 | | // Compute function value at the new point |
65 | 0 | qt_ = P.value (xtd_); |
66 | 0 | P.gradient (gradient_, xtd_); |
67 | | // and it squared norm |
68 | 0 | maxIter = endCriteria.checkMaxIterations(loopNumber, ecType); |
69 | 0 | } while ( |
70 | 0 | (((qt_ - q0) > (-alpha_ * t * qpt_)) || |
71 | 0 | ((qtold - q0) <= (-alpha_ * t * qpt_ / beta_))) && |
72 | 0 | (!maxIter)); |
73 | 0 | } |
74 | |
|
75 | 0 | if (maxIter) |
76 | 0 | succeed_ = false; |
77 | | |
78 | | // Compute new gradient |
79 | 0 | P.gradient(gradient_, xtd_); |
80 | | // and it squared norm |
81 | 0 | qpt_ = DotProduct(gradient_, gradient_); |
82 | | |
83 | | // Return new step value |
84 | 0 | return t; |
85 | 0 | } |
86 | | |
87 | | } |