summaryrefslogtreecommitdiffstats
path: root/ml/dlib/dlib/test/optimization_test_functions.cpp
blob: 1cded50ee7607a5330fe1616866dee21ecbc334c (plain)
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// Copyright (C) 2010  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.
#include "optimization_test_functions.h"

/*

    Most of the code in this file is converted from the set of Fortran 90 routines 
    created by John Burkardt.

    The original Fortran can be found here: http://orion.math.iastate.edu/burkardt/f_src/testopt/testopt.html

*/


namespace dlib
{
    namespace test_functions
    {

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

        matrix<double,0,1> chebyquad_residuals(const matrix<double,0,1>& x)
        {
            matrix<double,0,1> fvec(x.size());
            const int n = x.size();
            int i;
            int j;
            double t;
            double t1;
            double t2;
            double th;
            fvec = 0;

            for (j = 1; j <= n; ++j)
            {
                t1 = 1.0E+00;
                t2 = 2.0E+00 * x(j-1) - 1.0E+00;
                t = 2.0E+00 * t2;
                for (i = 1; i <= n; ++i)
                {
                    fvec(i-1) = fvec(i-1) + t2;
                    th = t * t2 - t1;
                    t1 = t2;
                    t2 = th;
                }
            }

            for (i = 1; i <= n; ++i)
            {
                fvec(i-1) = fvec(i-1) / (double) ( n );
                if ( ( i%2 ) == 0 ) 
                    fvec(i-1) = fvec(i-1) + 1.0E+00 / ( (double)i*i - 1.0E+00 );
            }

            return fvec;
        }

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

        double chebyquad_residual(int i, const matrix<double,0,1>& x)
        {
            return chebyquad_residuals(x)(i);
        }

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

        int& chebyquad_calls() 
        {
            static int count = 0;
            return count;
        }

        double chebyquad(const matrix<double,0,1>& x )
        {
            chebyquad_calls()++;
            return sum(squared(chebyquad_residuals(x)));
        }

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

        matrix<double,0,1> chebyquad_derivative (const matrix<double,0,1>& x)
        {
            const int n = x.size();
            matrix<double,0,1> fvec = chebyquad_residuals(x);
            matrix<double,0,1> g(n);
            int i;
            int j;
            double s1;
            double s2;
            double t;
            double t1;
            double t2;
            double th;

            for (j = 1; j <= n; ++j)
            {
                g(j-1) = 0.0E+00;
                t1 = 1.0E+00;
                t2 = 2.0E+00 * x(j-1) - 1.0E+00;
                t = 2.0E+00 * t2;
                s1 = 0.0E+00;
                s2 = 2.0E+00;
                for (i = 1; i <= n; ++i)
                {
                    g(j-1) = g(j-1) + fvec(i-1) * s2;
                    th = 4.0E+00 * t2 + t * s2 - s1;
                    s1 = s2;
                    s2 = th;
                    th = t * t2 - t1;
                    t1 = t2;
                    t2 = th;
                }
            }

            g = 2.0E+00 * g / (double) ( n );

            return g;
        }

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

        matrix<double,0,1> chebyquad_start (int n)
        {
            int i;
            matrix<double,0,1> x(n);

            for (i = 1; i <= n; ++i)
                x(i-1) = double ( i ) / double ( n + 1 );

            return x;
        }

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

        matrix<double,0,1> chebyquad_solution (int n)
        {
            matrix<double,0,1> x(n);

            x = 0;
            switch (n)
            {
                case 2:
                    x = 0.2113249E+00, 0.7886751E+00;
                    break;
                case 4:
                    x = 0.1026728E+00, 0.4062037E+00, 0.5937963E+00, 0.8973272E+00;
                    break;
                case 6:
                    x = 0.066877E+00, 0.288741E+00, 0.366682E+00, 0.633318E+00, 0.711259E+00, 0.933123E+00;
                    break;
                case 8:
                    x = 0.043153E+00, 0.193091E+00, 0.266329E+00, 0.500000E+00, 0.500000E+00, 0.733671E+00, 0.806910E+00, 0.956847E+00;
                    break;
                default:
                    std::ostringstream sout;
                    sout << "don't know chebyquad solution for n = " << n;
                    throw dlib::error(sout.str());
                    break;
            }

            return x;
        }

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

        matrix<double> chebyquad_hessian(const matrix<double,0,1>& x)
        {
            const int lda = x.size();
            const int n = x.size();
            double d1;
            double d2;
            matrix<double,0,1> fvec = chebyquad_residuals(x);
            matrix<double,0,1> gvec(n);
            matrix<double> h(lda,n);
            int i;
            int j;
            int k;
            double p1;
            double p2;
            double s1;
            double s2;
            double ss1;
            double ss2;
            double t;
            double t1;
            double t2;
            double th;
            double tt;
            double tth;
            double tt1;
            double tt2;
            h = 0;

            d1 = 1.0E+00 / double ( n );
            d2 = 2.0E+00 * d1;

            for (j = 1; j <= n; ++j)
            {

                h(j-1,j-1) = 4.0E+00 * d1;
                t1 = 1.0E+00;
                t2 = 2.0E+00 * x(j-1) - 1.0E+00;
                t = 2.0E+00 * t2;
                s1 = 0.0E+00;
                s2 = 2.0E+00;
                p1 = 0.0E+00;
                p2 = 0.0E+00;
                gvec(0) = s2;

                for (i = 2; i <= n; ++i)
                {
                    th = 4.0E+00 * t2 + t * s2 - s1;
                    s1 = s2;
                    s2 = th;
                    th = t * t2 - t1;
                    t1 = t2;
                    t2 = th;
                    th = 8.0E+00 * s1 + t * p2 - p1;
                    p1 = p2;
                    p2 = th;
                    gvec(i-1) = s2;
                    h(j-1,j-1) = h(j-1,j-1) + fvec(i-1) * th + d1 * s2*s2;
                }

                h(j-1,j-1) = d2 * h(j-1,j-1);

                for (k = 1; k <= j-1; ++k)
                {

                    h(j-1,k-1) = 0.0;
                    tt1 = 1.0E+00;
                    tt2 = 2.0E+00 * x(k-1) - 1.0E+00;
                    tt = 2.0E+00 * tt2;
                    ss1 = 0.0E+00;
                    ss2 = 2.0E+00;

                    for (i = 1; i <= n; ++i)
                    {
                        h(j-1,k-1) = h(j-1,k-1) + ss2 * gvec(i-1);
                        tth = 4.0E+00 * tt2 + tt * ss2 - ss1;
                        ss1 = ss2;
                        ss2 = tth;
                        tth = tt * tt2 - tt1;
                        tt1 = tt2;
                        tt2 = tth;
                    }

                    h(j-1,k-1) = d2 * d1 * h(j-1,k-1);

                }

            }

            h = make_symmetric(h);
            return h;
        }

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

        double brown_residual (int i, const matrix<double,4,1>& x)
        /*!
            requires
                - 1 <= i <= 20
            ensures
                - returns the ith brown residual
        !*/
        {
            double c;
            double f;
            double f1;
            double f2;

            f = 0.0E+00;


            c = double ( i ) / 5.0E+00;
            f1 = x(0) + c * x(1) - std::exp ( c );
            f2 = x(2) + std::sin ( c ) * x(3) - std::cos ( c );

            f = f1*f1 + f2*f2; 

            return f;
        }

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

        double brown ( const matrix<double,4,1>& x)
        {
            double f;
            int i;

            f = 0;

            for (i = 1; i <= 20; ++i)
            {
                f += std::pow(brown_residual(i, x), 2);
            }

            return f;
        }

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

        matrix<double,4,1> brown_derivative ( const matrix<double,4,1>& x)
        {
            double c;
            double df1dx1;
            double df1dx2;
            double df2dx3;
            double df2dx4;
            double f1;
            double f2;
            matrix<double,4,1> g;
            int i;

            g = 0;

            for (i = 1; i <= 20; ++i)
            {

                c = double ( i ) / 5.0E+00;

                f1 = x(0) + c * x(1) - std::exp ( c );
                f2 = x(2) + std::sin ( c ) * x(3) - std::cos ( c );

                df1dx1 = 1.0E+00;
                df1dx2 = c;
                df2dx3 = 1.0E+00;
                df2dx4 = std::sin ( c );

                using std::pow;
                g(0) = g(0) + 4.0E+00 * ( pow(f1,3) * df1dx1 + f1 * pow(f2,2) * df1dx1 );
                g(1) = g(1) + 4.0E+00 * ( pow(f1,3) * df1dx2 + f1 * pow(f2,2) * df1dx2 );
                g(2) = g(2) + 4.0E+00 * ( pow(f1,2) * f2 * df2dx3 + pow(f2,3) * df2dx3 );
                g(3) = g(3) + 4.0E+00 * ( pow(f1,2) * f2 * df2dx4 + pow(f2,3) * df2dx4 );

            }

            return g;
        }

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

        matrix<double,4,4> brown_hessian ( const matrix<double,4,1>& x)
        {
            double c;
            double df1dx1;
            double df1dx2;
            double df2dx3;
            double df2dx4;
            double f1;
            double f2;
            matrix<double,4,4> h;
            int i;

            h = 0;

            for (i = 1; i <= 20; ++i)
            {

                c = double ( i ) / 5.0E+00;

                f1 = x(0) + c * x(1) - std::exp ( c );
                f2 = x(2) + std::sin ( c ) * x(3) - std::cos ( c );

                df1dx1 = 1.0E+00;
                df1dx2 = c;
                df2dx3 = 1.0E+00;
                df2dx4 = std::sin ( c );

                using std::pow;
                h(0,0) = h(0,0) + 12.0E+00 * pow(f1,2) * df1dx1 * df1dx1 + 4.0E+00 * pow(f2,2) * df1dx1 * df1dx1;
                h(0,1) = h(0,1) + 12.0E+00 * pow(f1,2) * df1dx1 * df1dx2 + 4.0E+00 * pow(f2,2) * df1dx1 * df1dx2;
                h(0,2) = h(0,2) + 8.0E+00 * f1 * f2 * df1dx1 * df2dx3;
                h(0,3) = h(0,3) + 8.0E+00 * f1 * f2 * df1dx1 * df2dx4;

                h(1,0) = h(1,0) + 12.0E+00 * pow(f1,2) * df1dx2 * df1dx1 + 4.0E+00 * pow(f2,2) * df1dx2 * df1dx1;
                h(1,1) = h(1,1) + 12.0E+00 * pow(f1,2) * df1dx2 * df1dx2 + 4.0E+00 * pow(f2,2) * df1dx2 * df1dx2;
                h(1,2) = h(1,2) + 8.0E+00 * f1 * f2 * df1dx2 * df2dx3;
                h(1,3) = h(1,3) + 8.0E+00 * f1 * f2 * df1dx2 * df2dx4;

                h(2,0) = h(2,0) + 8.0E+00 * f1 * f2 * df2dx3 * df1dx1;
                h(2,1) = h(2,1) + 8.0E+00 * f1 * f2 * df2dx3 * df1dx2;
                h(2,2) = h(2,2) + 4.0E+00 * pow(f1,2) * df2dx3 * df2dx3 + 12.0E+00 * pow(f2,2) * df2dx3 * df2dx3;
                h(2,3) = h(2,3) + 4.0E+00 * pow(f1,2) * df2dx4 * df2dx3 + 12.0E+00 * pow(f2,2) * df2dx3 * df2dx4;

                h(3,0) = h(3,0) + 8.0E+00 * f1 * f2 * df2dx4 * df1dx1;
                h(3,1) = h(3,1) + 8.0E+00 * f1 * f2 * df2dx4 * df1dx2;
                h(3,2) = h(3,2) + 4.0E+00 * pow(f1,2) * df2dx3 * df2dx4 + 12.0E+00 * pow(f2,2) * df2dx4 * df2dx3;
                h(3,3) = h(3,3) + 4.0E+00 * pow(f1,2) * df2dx4 * df2dx4 + 12.0E+00 * pow(f2,2) * df2dx4 * df2dx4;

            }

            return make_symmetric(h);
        }

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

        matrix<double,4,1> brown_start ()
        {
            matrix<double,4,1> x;
            x = 25.0E+00, 5.0E+00, -5.0E+00, -1.0E+00;
            return x;
        }

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

        matrix<double,4,1> brown_solution ()
        {
            matrix<double,4,1> x;
            // solution from original documentation.
            //x = -11.5844E+00, 13.1999E+00, -0.406200E+00, 0.240998E+00;
            x = -11.594439905669450042, 13.203630051593080452, -0.40343948856573402795, 0.23677877338218666914;
            return x;
        }

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

    }
}