"vscode:/vscode.git/clone" did not exist on "4839999b76b217e90ea6827b47fb001b373ac080"
optimization_search_strategies_abstract.h 11.8 KB
Newer Older
1
// Copyright (C) 2008  Davis E. King (davis@dlib.net)
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
// License: Boost Software License   See LICENSE.txt for the full license.
#undef DLIB_OPTIMIZATIOn_SEARCH_STRATEGIES_ABSTRACT_
#ifdef DLIB_OPTIMIZATIOn_SEARCH_STRATEGIES_ABSTRACT_

#include <cmath>
#include <limits>
#include "../matrix/matrix_abstract.h"
#include "../algs.h"


namespace dlib
{
    /*
        A good discussion of the search strategies in this file can be found in the 
        following book:  Numerical Optimization by Nocedal and Wright.
    */

// ----------------------------------------------------------------------------------------

    class cg_search_strategy
    {
        /*!
            WHAT THIS OBJECT REPRESENTS
                This object represents a strategy for determining which direction
                a line search should be carried out along.  This particular object
                is an implementation of the Polak-Ribiere conjugate gradient method
                for determining this direction.

                This method uses an amount of memory that is linear in the number
                of variables to be optimized.  So it is capable of handling problems
                with a very large number of variables.  However, it is generally
                not as good as the L-BFGS algorithm (which is defined below in
                the lbfgs_search_strategy class).
        !*/

    public:
        cg_search_strategy(
        );
        /*!
            ensures
                - This object is properly initialized and ready to generate
                  search directions.
        !*/

        double get_wolfe_rho (
        ) const;
        /*!
            ensures
                - returns the value of the Wolfe rho parameter that should be used when 
                  this search strategy is used with the line_search() function.
        !*/

        double get_wolfe_sigma (
        ) const; 
        /*!
            ensures
                - returns the value of the Wolfe sigma parameter that should be used when 
                  this search strategy is used with the line_search() function.
        !*/

62
63
64
65
66
67
68
69
        unsigned long get_max_line_search_iterations (
        ) const; 
        /*!
            ensures
                - returns the value of the max iterations parameter that should be used when 
                  this search strategy is used with the line_search() function.
        !*/

70
71
72
73
74
75
76
77
        template <typename T>
        const matrix<double,0,1>& get_next_direction (
            const T& x,
            const double funct_value,
            const T& funct_derivative
        );
        /*!
            requires
78
79
80
                - this function is only called once per search iteration
                - for some objective function f():
                    - x == the search point for the current iteration
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
                    - funct_value == f(x)
                    - funct_derivative == derivative(f)(x)
            ensures
                - Assuming that a line search is going to be conducted starting from the point x,
                  this function returns the direction in which the search should proceed.
        !*/

    };

// ----------------------------------------------------------------------------------------

    class bfgs_search_strategy
    {
        /*!
            WHAT THIS OBJECT REPRESENTS
                This object represents a strategy for determining which direction
                a line search should be carried out along.  This particular object
                is an implementation of the BFGS quasi-newton method for determining 
                this direction.

                This method uses an amount of memory that is quadratic in the number
                of variables to be optimized.  It is generally very effective but 
                if your problem has a very large number of variables then it isn't 
                appropriate.  Instead You should try the lbfgs_search_strategy.
        !*/

    public:
        bfgs_search_strategy(
        );
        /*!
            ensures
                - This object is properly initialized and ready to generate
                  search directions.
        !*/

        double get_wolfe_rho (
        ) const;
        /*!
            ensures
                - returns the value of the Wolfe rho parameter that should be used when 
                  this search strategy is used with the line_search() function.
        !*/

        double get_wolfe_sigma (
        ) const; 
        /*!
            ensures
                - returns the value of the Wolfe sigma parameter that should be used when 
                  this search strategy is used with the line_search() function.
        !*/

132
133
134
135
136
137
138
139
        unsigned long get_max_line_search_iterations (
        ) const; 
        /*!
            ensures
                - returns the value of the max iterations parameter that should be used when 
                  this search strategy is used with the line_search() function.
        !*/

140
141
142
143
144
145
146
147
        template <typename T>
        const matrix<double,0,1>& get_next_direction (
            const T& x,
            const double funct_value,
            const T& funct_derivative
        );
        /*!
            requires
148
149
150
                - this function is only called once per search iteration
                - for some objective function f():
                    - x == the search point for the current iteration
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
                    - funct_value == f(x)
                    - funct_derivative == derivative(f)(x)
            ensures
                - Assuming that a line search is going to be conducted starting from the point x,
                  this function returns the direction in which the search should proceed.
        !*/
    };

// ----------------------------------------------------------------------------------------

    class lbfgs_search_strategy
    {
        /*!
            WHAT THIS OBJECT REPRESENTS
                This object represents a strategy for determining which direction
                a line search should be carried out along.  This particular object
                is an implementation of the L-BFGS quasi-newton method for determining 
                this direction.

                This method uses an amount of memory that is linear in the number
                of variables to be optimized.  This makes it an excellent method 
                to use when an optimization problem has a large number of variables.
        !*/
    public:
        lbfgs_search_strategy(
            unsigned long max_size
        ); 
        /*!
            requires
                - max_size > 0
            ensures
                - This object is properly initialized and ready to generate
                  search directions.
                - L-BFGS works by remembering a certain number of position and gradient 
                  pairs.  It uses this remembered information to compute search directions.
                  The max_size argument determines how many of these pairs will be remembered.
                  Typically, using between 3 and 30 pairs performs well for many problems.
        !*/

        double get_wolfe_rho (
        ) const;
        /*!
            ensures
                - returns the value of the Wolfe rho parameter that should be used when 
                  this search strategy is used with the line_search() function.
        !*/

        double get_wolfe_sigma (
        ) const; 
        /*!
            ensures
                - returns the value of the Wolfe sigma parameter that should be used when 
203
204
205
206
207
208
209
210
                  this search strategy is used with the line_search() function.
        !*/

        unsigned long get_max_line_search_iterations (
        ) const; 
        /*!
            ensures
                - returns the value of the max iterations parameter that should be used when 
211
212
213
214
215
216
217
218
219
220
221
                  this search strategy is used with the line_search() function.
        !*/

        template <typename T>
        const matrix<double,0,1>& get_next_direction (
            const T& x,
            const double funct_value,
            const T& funct_derivative
        );
        /*!
            requires
222
223
224
                - this function is only called once per search iteration
                - for some objective function f():
                    - x == the search point for the current iteration
225
226
227
228
229
230
231
232
                    - funct_value == f(x)
                    - funct_derivative == derivative(f)(x)
            ensures
                - Assuming that a line search is going to be conducted starting from the point x,
                  this function returns the direction in which the search should proceed.
        !*/
    };

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
// ----------------------------------------------------------------------------------------

    template <typename hessian_funct>
    class newton_search_strategy_obj
    {
        /*!
            REQUIREMENTS ON hessian_funct
                Lets denote the function being optimized as f(x).  Then the 
                hessian_funct must be a function object that takes in an x
                and returns the hessian matrix at x.  hessian_funct must also
                be copy constructable.

            WHAT THIS OBJECT REPRESENTS
                This object represents a strategy for determining which direction
                a line search should be carried out along.  This particular object
                is an implementation of the newton method for determining this 
                direction.  That is, it uses the following formula to determine
                the direction:
                    search_direction = -inv(hessian(x))*derivative
        !*/
    public:
        newton_search_strategy_obj(
            const hessian_funct& hess
        ); 
        /*!
            ensures
                - This object is properly initialized and ready to generate
                  search directions.
        !*/

        double get_wolfe_rho (
        ) const;
        /*!
            ensures
                - returns the value of the Wolfe rho parameter that should be used when 
                  this search strategy is used with the line_search() function.
        !*/

        double get_wolfe_sigma (
        ) const; 
        /*!
            ensures
                - returns the value of the Wolfe sigma parameter that should be used when 
                  this search strategy is used with the line_search() function.
        !*/

        unsigned long get_max_line_search_iterations (
        ) const; 
        /*!
            ensures
                - returns the value of the max iterations parameter that should be used when 
                  this search strategy is used with the line_search() function.
        !*/

        template <typename T>
        const matrix<double,0,1> get_next_direction (
            const T& x,
            const double funct_value,
            const T& funct_derivative
        );
        /*!
            requires
                - for some objective function f():
                    - x == the search point for the current iteration
                    - funct_value == f(x)
                    - funct_derivative == derivative(f)(x)
            ensures
                - Assuming that a line search is going to be conducted starting from the 
                  point x, this function returns the direction in which the search should 
                  proceed.
        !*/

    };

    template <typename hessian_funct>
    newton_search_strategy_obj<hessian_funct> newton_search_strategy (
        const hessian_funct& hessian
    ) { return newton_search_strategy_obj<hessian_funct>(hessian); }
    /*!
        ensures
            - constructs and returns a newton_search_strategy_obj.  
              This function is just a helper to make the syntax for creating
              these objects a little simpler.
    !*/

318
319
320
321
322
323
// ----------------------------------------------------------------------------------------

}

#endif // DLIB_OPTIMIZATIOn_SEARCH_STRATEGIES_ABSTRACT_