QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
goldstein.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2015 Cheng Li
5 This file is part of QuantLib, a free-software/open-source library
6 for financial quantitative analysts and developers - http://quantlib.org/
7
8 QuantLib is free software: you can redistribute it and/or modify it
9 under the terms of the QuantLib license. You should have received a
10 copy of the license along with this program; if not, please email
11 <quantlib-dev@lists.sf.net>. The license is also available online at
12 <http://quantlib.org/license.shtml>.
13
14 This program is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19#include <ql/math/optimization/goldstein.hpp>
20#include <ql/math/optimization/method.hpp>
21#include <ql/math/optimization/problem.hpp>
22#include <ql/math/comparison.hpp>
23
24namespace QuantLib {
25
27 EndCriteria::Type& ecType,
28 const EndCriteria& endCriteria,
29 const Real t_ini)
30 {
31 Constraint& constraint = P.constraint();
32 succeed_=true;
33 bool maxIter = false;
34 Real t = t_ini;
35 Size loopNumber = 0;
36
37 Real q0 = P.functionValue();
38 Real qp0 = P.gradientNormValue();
39
40 Real tl = 0.0;
41 Real tr = 0.0;
42
43 qt_ = q0;
45
46 // Initialize gradient
48 // Compute new point
49 xtd_ = P.currentValue();
50 t = update(xtd_, searchDirection_, t, constraint);
51 // Compute function value at the new point
52 qt_ = P.value (xtd_);
53
54 while ((qt_ - q0) < -beta_*t*qpt_ || (qt_ - q0) > -alpha_*t*qpt_) {
55 if ((qt_ - q0) > -alpha_*t*qpt_)
56 tr = t;
57 else
58 tl = t;
59 ++loopNumber;
60
61 // calculate the new step
62 if (close_enough(tr, 0.0))
63 t *= extrapolation_;
64 else
65 t = (tl + tr) / 2.0;
66
67 // New point value
68 xtd_ = P.currentValue();
69 t = update(xtd_, searchDirection_, t, constraint);
70
71 // Compute function value at the new point
72 qt_ = P.value (xtd_);
74 // and it squared norm
75 maxIter = endCriteria.checkMaxIterations(loopNumber, ecType);
76
77 if (maxIter)
78 break;
79 }
80
81 if (maxIter)
82 succeed_ = false;
83
84 // Compute new gradient
86 // and it squared norm
88
89 // Return new step value
90 return t;
91 }
92
93}
1-D array used in linear algebra.
Definition: array.hpp:52
bool empty() const
whether the array is empty
Definition: array.hpp:499
Size size() const
dimension of the array
Definition: array.hpp:495
Base constraint class.
Definition: constraint.hpp:35
Criteria to end optimization process:
Definition: endcriteria.hpp:40
bool checkMaxIterations(Size iteration, EndCriteria::Type &ecType) const
Definition: endcriteria.cpp:56
Real operator()(Problem &P, EndCriteria::Type &ecType, const EndCriteria &, Real t_ini) override
Perform line search.
Definition: goldstein.cpp:26
bool succeed_
flag to know if linesearch succeed
Definition: linesearch.hpp:77
Array searchDirection_
current values of the search direction
Definition: linesearch.hpp:71
Real update(Array &params, const Array &direction, Real beta, const Constraint &constraint)
Definition: linesearch.cpp:26
Array xtd_
new x and its gradient
Definition: linesearch.hpp:73
Real qt_
cost function value and gradient norm corresponding to xtd_
Definition: linesearch.hpp:75
Constrained optimization problem.
Definition: problem.hpp:42
const Array & currentValue() const
current value of the local minimum
Definition: problem.hpp:81
Real functionValue() const
value of cost function
Definition: problem.hpp:88
Real gradientNormValue() const
value of cost function gradient norm
Definition: problem.hpp:94
Constraint & constraint() const
Constraint.
Definition: problem.hpp:71
Real value(const Array &x)
call cost function computation and increment evaluation counter
Definition: problem.hpp:116
void gradient(Array &grad_f, const Array &x)
call cost function gradient computation and increment
Definition: problem.hpp:126
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
Real DotProduct(const Array &v1, const Array &v2)
Definition: array.hpp:553
bool close_enough(const Quantity &m1, const Quantity &m2, Size n)
Definition: quantity.cpp:182