Coverage Report

Created: 2025-09-04 07:11

/src/quantlib/ql/math/optimization/linesearch.hpp
Line
Count
Source (jump to first uncovered line)
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) 2001, 2002, 2003 Nicolas Di Césaré
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
 <https://www.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
/*! \file linesearch.hpp
22
    \brief Line search abstract class
23
*/
24
25
#ifndef quantlib_optimization_line_search_h_
26
#define quantlib_optimization_line_search_h_
27
28
#include <ql/math/array.hpp>
29
#include <ql/math/optimization/endcriteria.hpp>
30
31
namespace QuantLib {
32
33
    class Problem;
34
    class Constraint;
35
    class EndCriteria;
36
37
    //! Base class for line search
38
    class LineSearch {
39
      public:
40
        //! Default constructor
41
0
        explicit LineSearch(Real = 0.0) {}
42
        //! Destructor
43
0
        virtual ~LineSearch() = default;
44
45
        //! return last x value
46
0
        const Array& lastX() { return xtd_; }
47
        //! return last cost function value
48
0
        Real lastFunctionValue() const { return qt_; }
49
        //! return last gradient
50
0
        const Array& lastGradient() { return gradient_; }
51
        //! return square norm of last gradient
52
0
        Real lastGradientNorm2() const { return qpt_; }
53
54
0
        bool succeed() const { return succeed_; }
55
56
        //! Perform line search
57
        virtual Real operator()(Problem& P, // Optimization problem
58
                                EndCriteria::Type& ecType,
59
                                const EndCriteria&,
60
                                Real t_ini) = 0; // initial value of line-search step
61
        Real update(Array& params,
62
                    const Array& direction,
63
                    Real beta,
64
                    const Constraint& constraint);
65
66
        //! current value of the search direction
67
0
        const Array& searchDirection() const { return searchDirection_; }
68
0
        Array& searchDirection() { return searchDirection_; }
69
      protected:
70
        //! current values of the search direction
71
        Array searchDirection_;
72
        //! new x and its gradient
73
        Array xtd_, gradient_;
74
        //! cost function value and gradient norm corresponding to xtd_
75
        Real qt_ = 0.0, qpt_ = 0.0;
76
        //! flag to know if linesearch succeed
77
        bool succeed_ = true;
78
    };
79
}
80
81
#endif