| 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 |
<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 |
|
| 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 "math/DynamicVector.hpp" |
| 29 |
#include "optimization/EndCriteria.hpp" |
| 30 |
|
| 31 |
using namespace OpenMD; |
| 32 |
namespace QuantLib { |
| 33 |
|
| 34 |
class Problem; |
| 35 |
class Constraint; |
| 36 |
class EndCriteria; |
| 37 |
|
| 38 |
//! Base class for line search |
| 39 |
class LineSearch { |
| 40 |
public: |
| 41 |
//! Default constructor |
| 42 |
LineSearch(RealType = 0.0) |
| 43 |
: qt_(0.0), qpt_(0.0), succeed_(true) {} |
| 44 |
//! Destructor |
| 45 |
virtual ~LineSearch() {} |
| 46 |
|
| 47 |
//! return last x value |
| 48 |
const DynamicVector<RealType>& lastX() { return xtd_; } |
| 49 |
//! return last objective function value |
| 50 |
RealType lastFunctionValue() { return qt_; } |
| 51 |
//! return last gradient |
| 52 |
const DynamicVector<RealType>& lastGradient() { return gradient_; } |
| 53 |
//! return square norm of last gradient |
| 54 |
RealType lastGradientNorm2() { return qpt_;} |
| 55 |
|
| 56 |
bool succeed() { return succeed_; } |
| 57 |
|
| 58 |
//! Perform line search |
| 59 |
virtual RealType operator()(Problem& P, // Optimization problem |
| 60 |
EndCriteria::Type& ecType, |
| 61 |
const EndCriteria&, |
| 62 |
const RealType t_ini) = 0; // initial value of line-search step |
| 63 |
RealType update(DynamicVector<RealType>& params, |
| 64 |
const DynamicVector<RealType>& direction, |
| 65 |
RealType beta, |
| 66 |
const Constraint& constraint); |
| 67 |
|
| 68 |
//! current value of the search direction |
| 69 |
const DynamicVector<RealType>& searchDirection() const { return searchDirection_; } |
| 70 |
DynamicVector<RealType>& searchDirection() { return searchDirection_; } |
| 71 |
protected: |
| 72 |
//! current values of the search direction |
| 73 |
DynamicVector<RealType> searchDirection_; |
| 74 |
//! new x and its gradient |
| 75 |
DynamicVector<RealType> xtd_, gradient_; |
| 76 |
//! objective function value and gradient norm corresponding to xtd_ |
| 77 |
RealType qt_, qpt_; |
| 78 |
//! flag to know if linesearch succeed |
| 79 |
bool succeed_; |
| 80 |
|
| 81 |
}; |
| 82 |
} |
| 83 |
|
| 84 |
#endif |