File: newton_search.h

package info (click to toggle)
libitpp 4.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 7,520 kB
  • ctags: 6,341
  • sloc: cpp: 51,608; sh: 9,248; makefile: 636; fortran: 8
file content (339 lines) | stat: -rw-r--r-- 10,751 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*!
 * \file
 * \brief Newton Search optimization algorithms - header file
 * \author Tony Ottosson
 *
 * -------------------------------------------------------------------------
 *
 * IT++ - C++ library of mathematical, signal processing, speech processing,
 *        and communications classes and functions
 *
 * Copyright (C) 1995-2008  (see AUTHORS file for a list of contributors)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * -------------------------------------------------------------------------
 */

#ifndef NEWTON_SEARCH_H
#define NEWTON_SEARCH_H

#include <itpp/base/vec.h>
#include <itpp/base/array.h>
#include <limits>


namespace itpp {

  /*!
    \brief Numerical optimization routines
    \addtogroup optimization
  */
  //@{


  //! Newton Search method
  enum Newton_Search_Method {BFGS};

  /*!
    \brief Newton Search

    Newton or Quasi-Newton optimization method that try to minimize the objective function \f$f(\mathbf{x})\f$
    given an initial guess \f$\mathbf{x}\f$.

    The search is stopped when either criterion 1:
    \f[
    \left\| \mathbf{f}'(\mathbf{x})\right\|_{\infty} \leq \varepsilon_1
    \f]
    or criterion 2:
    \f[
    \left\| d\mathbf{x}\right\|_{2} \leq \varepsilon_2 (\varepsilon_2 + \| \mathbf{x} \|_{2} )
    \f]
    is fulfilled. Another possibility is that the search is stopped when the number of function evaluations
    exceeds a threshold (100 per default).

    The default update rule for the inverse of the Hessian matrix is the BFGS algorithm with
    \f$\varepsilon_1 = 10^{-4}\f$ an \f$\varepsilon_2 = 10^{-8}\f$.

  */
  class Newton_Search {
  public:
    //! Default constructor
    Newton_Search();
    //! Destructor
    ~Newton_Search() {};

    //! Set function pointer
    void set_function(double(*function)(const vec&));
    //! Set gradient function pointer
    void set_gradient(vec(*gradient)(const vec&));
    //! Set both function and gradient function pointers
    void set_functions(double(*function)(const vec&), vec(*gradient)(const vec&)) { set_function(function); set_gradient(gradient); }

    //! Set start point \c x for search and approx inverse Hessian at \c x
    void set_start_point(const vec &x, const mat &D);

    //! Set start point \c x for search
    void set_start_point(const vec &x);

    //! Get solution, function value and gradient at solution point
    vec get_solution();

    //! Do the line search
    bool search();
    //! Do the line search and return solution
    bool search(vec &xn);
    //! Set starting point, do the Newton search, and return the solution
    bool search(const vec &x0, vec &xn);

    //! Set stop criterion values
    void set_stop_values(double epsilon_1, double epsilon_2);
    //! Return stop value rho
    double get_epsilon_1() { return stop_epsilon_1; }
    //! Return stop value beta
    double get_epsilon_2() { return stop_epsilon_2; }

    //! Set max number of function evaluations
    void set_max_evaluations(int value);
    //! Return max number of function evaluations
    int get_max_evaluations() { return max_evaluations; }

    //! Set max stepsize
    void set_initial_stepsize(double value);
    //! Return max number of iterations
    double get_initial_stepsize() { return initial_stepsize; }

    //! Set Line search method
    void set_method(const Newton_Search_Method &method);

    //! get function value at solution point
    double get_function_value();
    //! get value of stop criterion 1 at solution point
    double get_stop_1();
    //! get value of stop criterion 2 at solution point
    double get_stop_2();
    //! get number of iterations used to reach solution
    int get_no_iterations();
    //! get number of function evaluations used to reach solution
    int get_no_function_evaluations();

    //! enable trace mode
    void enable_trace() { trace = true; }
    //! disable trace
    void disable_trace() { trace = false; }

    /*! get trace outputs
      \c xvalues are the solutions of every iteration
      \c Fvalues are the function values
      \c ngvalues are the norm(gradient,inf) values
      \c dvalues are the delta values
    */
    void get_trace(Array<vec> & xvalues, vec &Fvalues, vec &ngvalues, vec &dvalues);

  private:
    int n; // dimension of problem, size(x)
    double (*f)(const vec&); // function to minimize
    vec (*df_dx)(const vec&); // df/dx, gradient of f

    // start variables
    vec x_start;
    mat D_start;

    // solution variables
    vec x_end;

    // trace variables
    Array<vec> x_values;
    vec F_values, ng_values, Delta_values;

    Newton_Search_Method method;

    // Parameters
    double initial_stepsize; // opts(1)
    double stop_epsilon_1; // opts(2)
    double stop_epsilon_2; // opt(3)
    int max_evaluations; // opts(4)

    // output parameters
    int no_feval; // number of function evaluations
    int no_iter; // number of iterations
    double F, ng, nh; // function value, stop_1, stop_2 values at solution point

    bool init, finished, trace;
  };



  //! Line Search method
  enum Line_Search_Method {Soft, Exact};

  /*!
    \brief Line Search

    The line search try to minimize the objective function \f$f(\mathbf{x})\f$
    along the direction \f$\mathbf{h}\f$ from the current position \f$\mathbf{x}\f$.

    Hence we look at
    \f[
    \varphi(\alpha) = f(\mathbf{x} + \alpha \mathbf{h})
    \f]
    and try to find an \f$\alpha_s\f$ that minimizes \f$f\f$.

    Two variants are used. Either the soft line search (default) or the exact line
    search.

    The soft line search stops when a point in the acceptable region is found, i.e.
    \f[
    \phi(\alpha_s) \leq \varphi(0) + \alpha_s \rho \varphi'(0)
    \f]
    and
    \f[
    \varphi'(\alpha_s) \geq \beta \varphi'(0),\: \rho < \beta
    \f]
    Default vales are \f$\rho = 10^{-3}\f$ and \f$\beta = 0.99\f$.

    The exact line search
    \f[
    \| \varphi(\alpha_s)\|  \leq \rho \| \varphi'(0) \|
    \f]
    and
    \f[
    b-a \leq \beta b,
    \f]
    where \f$\left[a,b\right]\f$ is the current interval for \f$\alpha_s\f$.
    Default vales are \f$\rho = 10^{-3}\f$ and \f$\beta = 10^{-3}\f$.

    The exact line search can at least in theory give the exact resutl, but it may require
    many extra function evaluations compared to soft line search.
  */
  class Line_Search {
  public:
    //! Default constructor
    Line_Search();
    //! Destructor
    ~Line_Search() {};

    //! Set function pointer
    void set_function(double(*function)(const vec&));
    //! Set gradient function pointer
    void set_gradient(vec(*gradient)(const vec&));
    //! Set both function and gradient function pointers
    void set_functions(double(*function)(const vec&), vec(*gradient)(const vec&)) { set_function(function); set_gradient(gradient); }

    //! Set start point for search
    void set_start_point(const vec &x, double F, const vec &g, const vec &h);

    //! Get solution, function value and gradient at solution point
    void get_solution(vec &xn, double &Fn, vec &gn);

    //! Do the line search
    bool search();
    //! Do the line search and return solution
    bool search(vec &xn, double &Fn, vec &gn);
    //! Set starting point, do the line search, and return the solution
    bool search(const vec &x, double F, const vec &g, const vec &h, vec &xn,
		double &Fn, vec &gn);


    //! return alpha at solution point, xn = x + alpha h
    double get_alpha();
    //! return the slope ratio at solution poin, xn
    double get_slope_ratio();
    //! return number of function evaluations used in search
    int get_no_function_evaluations();


    //! Set stop criterion values
    void set_stop_values(double rho, double beta);
    //! Return stop value rho
    double get_rho() { return stop_rho; }
    //! Return stop value beta
    double get_beta() { return stop_beta; }

    //! Set max number of iterations
    void set_max_iterations(int value);
    //! Return max number of iterations
    int get_max_iterations() { return max_iterations; }

    //! Set max stepsize
    void set_max_stepsize(double value);
    //! Return max number of iterations
    double get_max_stepsize() { return max_stepsize; }

    //! Set Line search method
    void set_method(const Line_Search_Method &method);

    //! enable trace mode
    void enable_trace() { trace = true; }
    //! disable trace
    void disable_trace() { trace = false; }

    /*! get trace outputs
      \c alphavalues are the solutions of every iteration
      \c Fvalues are the function values
      \c dFvalues
    */
    void get_trace(vec &alphavalues, vec &Fvalues, vec &dFvalues);

  private:
    int n; // dimension of problem, size(x)
    double (*f)(const vec&); // function to minimize
    vec (*df_dx)(const vec&); // df/dx, gradient of f

    // start variables
    vec x_start, g_start, h_start;
    double F_start;

    // solution variables
    vec x_end, g_end;
    double F_end;

    // trace variables
    vec alpha_values, F_values, dF_values;

    bool init; // true if functions and starting points are set
    bool finished; // true if functions and starting points are set
    bool trace; // true if trace is enabled

    // Parameters
    Line_Search_Method method;
    double stop_rho; // opts(2)
    double stop_beta; // opts(3)
    int max_iterations; // opts(4)
    double max_stepsize; // opts(5)

    // output parameters
    double alpha; // end value of alpha, info(1)
    double slope_ratio; // slope ratio at xn, info(2)
    int no_feval; // info(3)
  };

  /*!
    \brief Unconstrained minimization

    Unconstrained minimization using a Newton or Quasi-Newton optimization method
    that try to minimize the objective function \f$f(\mathbf{x})\f$ given an initial guess \f$\mathbf{x}\f$.

    The function and the gradient need to be known and supplied.

    The default algorithm is a Quasi-Newton search using BFGS updates of the inverse Hessian matrix.
  */
  vec fminunc(double(*function)(const vec&), vec(*gradient)(const vec&), const vec &x0);

  //@}

} // namespace itpp

#endif // #ifndef NEWTON_SEARCH_H