LineSearch.h
Go to the documentation of this file.
1 //===========================================================================
2 /*!
3  *
4  *
5  * \brief LineSearch
6  *
7  *
8  *
9  * \author O. Krause, S. Dahlgaard
10  * \date 2010-2013
11  *
12  *
13  * \par Copyright 1995-2015 Shark Development Team
14  *
15  * <BR><HR>
16  * This file is part of Shark.
17  * <http://image.diku.dk/shark/>
18  *
19  * Shark is free software: you can redistribute it and/or modify
20  * it under the terms of the GNU Lesser General Public License as published
21  * by the Free Software Foundation, either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * Shark is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU Lesser General Public License for more details.
28  *
29  * You should have received a copy of the GNU Lesser General Public License
30  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
31  *
32  */
33 //===========================================================================
34 
35 #ifndef SHARK_ALGORITHMS_GRADIENTDESCENT_LINESEARCH_H
36 #define SHARK_ALGORITHMS_GRADIENTDESCENT_LINESEARCH_H
37 
38 #include <shark/LinAlg/Base.h>
40 #include "Impl/wolfecubic.inl"
41 #include "Impl/dlinmin.inl"
43 
44 namespace shark {
45 ///\brief Wrapper for the linesearch class of functions in the linear algebra library.
46 ///
47 ///This class is a wrapper for the linesearch class of functions of the linear algebra library.
48 ///The class is used for example in CG or BFGS for their internal linesearch learning steps.
49 ///It is NOT an Optimizer on its own, since it needs the Newton direction to be specified.
50 class LineSearch:public ISerializable {
51 public:
55  };
57 
58  ///Initializes the internal variables of the class to useful default values.
59  ///Dlinmin is used as default
61  m_minInterval=0;
62  m_maxInterval=1;
64  }
65 
67  return m_lineSearchType;
68  }
70  return m_lineSearchType;
71  }
72  ///minInterval sets the minimum initial bracket
73  double minInterval()const {
74  return m_minInterval;
75  }
76  ///minInterval sets the minimum initial bracket
77  double &minInterval() {
78  return m_minInterval;
79  }
80  ///maxInterval sets the maximum initial bracket
81  double maxInterval()const {
82  return m_maxInterval;
83  }
84  ///maxInterval sets the maximum initial bracket
85  double &maxInterval() {
86  return m_maxInterval;
87  }
88 
89  ///initializes the internal state of the LineSearch class and sets the function on which the lineSearch is to be evaluated
90  void init(ObjectiveFunction const& objectiveFunction) {
91  m_function = &objectiveFunction;
92  }
93 
94  ///performs a linesearch on the objectiveFunction given the starting point, its value the newton direction and optionally the derivative at the starting point
95  ///@param searchPoint the point where the linesearch start
96  ///@param pointValue the value of the function at searchPoint
97  ///@param newtonDirection the search direction of the line search
98  ///@param derivative the derivative of the function at searchPoint
99  ///@param stepLength initial step length guess for guiding the line search
100  virtual void operator()(RealVector &searchPoint,double &pointValue,RealVector const& newtonDirection, RealVector &derivative, double stepLength = 1.0)const {
101  switch (m_lineSearchType) {
102  case Dlinmin:
103  detail::dlinmin(searchPoint, newtonDirection, pointValue, *m_function, m_minInterval, m_maxInterval);
104  m_function->evalDerivative(searchPoint, derivative);
105  break;
106  case WolfeCubic:
107  detail::wolfecubic(searchPoint, newtonDirection, pointValue, *m_function, derivative, stepLength);
108  break;
109  }
110  }
111 
112  //ISerializable
113  virtual void read(InArchive &archive) {
114  archive>>m_minInterval;
115  archive>>m_maxInterval;
116  archive>>m_lineSearchType;
117  }
118 
119  virtual void write(OutArchive &archive) const {
120  archive<<m_minInterval;
121  archive<<m_maxInterval;
122  archive<<m_lineSearchType;
123  }
124 
125 
126 protected:
127  ///initial [min,max] bracket for linesearch
129  ///initial [min,max] bracket for linesearch
131 
133 
134  ///function to optimize
135  ObjectiveFunction const* m_function;
136 };
137 }
138 
139 #endif