| 1 | 
gezelter | 
1741 | 
/* -*- 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 | 
  | 
  | 
 <http://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 "optimization/Armijo.hpp" | 
| 21 | 
  | 
  | 
#include "optimization/Method.hpp" | 
| 22 | 
  | 
  | 
#include "optimization/Problem.hpp" | 
| 23 | 
  | 
  | 
 | 
| 24 | 
  | 
  | 
namespace QuantLib { | 
| 25 | 
  | 
  | 
     | 
| 26 | 
  | 
  | 
    RealType ArmijoLineSearch::operator()(Problem& P, | 
| 27 | 
  | 
  | 
                                          EndCriteria::Type& ecType, | 
| 28 | 
  | 
  | 
                                          const EndCriteria& endCriteria, | 
| 29 | 
  | 
  | 
                                          const RealType t_ini) | 
| 30 | 
  | 
  | 
    { | 
| 31 | 
  | 
  | 
        //OptimizationMethod& method = P.method(); | 
| 32 | 
  | 
  | 
        Constraint& constraint = P.constraint(); | 
| 33 | 
  | 
  | 
        succeed_=true; | 
| 34 | 
  | 
  | 
        bool maxIter = false; | 
| 35 | 
gezelter | 
1879 | 
        RealType t = t_ini; | 
| 36 | 
  | 
  | 
 | 
| 37 | 
gezelter | 
1741 | 
         | 
| 38 | 
  | 
  | 
        RealType q0 = P.functionValue(); | 
| 39 | 
  | 
  | 
        RealType qp0 = P.gradientNormValue(); | 
| 40 | 
  | 
  | 
         | 
| 41 | 
  | 
  | 
        qt_ = q0; | 
| 42 | 
  | 
  | 
        qpt_ = (gradient_.empty()) ? qp0 : -P.DotProduct(gradient_,searchDirection_); | 
| 43 | 
  | 
  | 
         | 
| 44 | 
  | 
  | 
        // Initialize gradient | 
| 45 | 
  | 
  | 
        gradient_ = DynamicVector<RealType>(P.currentValue().size()); | 
| 46 | 
  | 
  | 
        // Compute new point | 
| 47 | 
  | 
  | 
        xtd_ = P.currentValue(); | 
| 48 | 
  | 
  | 
        t = update(xtd_, searchDirection_, t, constraint); | 
| 49 | 
  | 
  | 
        // Compute function value at the new point | 
| 50 | 
  | 
  | 
        qt_ = P.value (xtd_); | 
| 51 | 
  | 
  | 
         | 
| 52 | 
  | 
  | 
        // Enter in the loop if the criterion is not satisfied | 
| 53 | 
  | 
  | 
        if ((qt_-q0) > -alpha_*t*qpt_) { | 
| 54 | 
gezelter | 
1879 | 
            RealType qtold; | 
| 55 | 
  | 
  | 
            size_t loopNumber = 0; | 
| 56 | 
gezelter | 
1741 | 
            do { | 
| 57 | 
  | 
  | 
                loopNumber++; | 
| 58 | 
  | 
  | 
                // Decrease step | 
| 59 | 
  | 
  | 
                t *= beta_; | 
| 60 | 
  | 
  | 
                // Store old value of the function | 
| 61 | 
  | 
  | 
                qtold = qt_; | 
| 62 | 
  | 
  | 
                // New point value | 
| 63 | 
  | 
  | 
                xtd_ = P.currentValue(); | 
| 64 | 
  | 
  | 
                t = update(xtd_, searchDirection_, t, constraint); | 
| 65 | 
  | 
  | 
                 | 
| 66 | 
  | 
  | 
                // Compute function value at the new point | 
| 67 | 
  | 
  | 
                qt_ = P.value (xtd_); | 
| 68 | 
  | 
  | 
                P.gradient (gradient_, xtd_); | 
| 69 | 
  | 
  | 
                // and it squared norm | 
| 70 | 
  | 
  | 
                maxIter = endCriteria.checkMaxIterations(loopNumber, ecType); | 
| 71 | 
  | 
  | 
            } while ( | 
| 72 | 
  | 
  | 
                     (((qt_ - q0) > (-alpha_ * t * qpt_)) || | 
| 73 | 
  | 
  | 
                      ((qtold - q0) <= (-alpha_ * t * qpt_ / beta_))) && | 
| 74 | 
  | 
  | 
                     (!maxIter)); | 
| 75 | 
  | 
  | 
        } | 
| 76 | 
  | 
  | 
         | 
| 77 | 
  | 
  | 
        if (maxIter) | 
| 78 | 
  | 
  | 
            succeed_ = false; | 
| 79 | 
  | 
  | 
         | 
| 80 | 
  | 
  | 
        // Compute new gradient | 
| 81 | 
  | 
  | 
        P.gradient(gradient_, xtd_); | 
| 82 | 
  | 
  | 
        // and it squared norm | 
| 83 | 
  | 
  | 
        qpt_ = P.computeGradientNormValue(gradient_); | 
| 84 | 
  | 
  | 
        //qpt_ = P.DotProduct(gradient_, gradient_); | 
| 85 | 
  | 
  | 
         | 
| 86 | 
  | 
  | 
        // Return new step value | 
| 87 | 
  | 
  | 
        return t; | 
| 88 | 
  | 
  | 
    } | 
| 89 | 
  | 
  | 
     | 
| 90 | 
  | 
  | 
} |