"docs/git@developer.sourcefind.cn:change/sglang.git" did not exist on "6c88f6c8d9086083e69bd8fbb604c2967d38d395"
optimization.cpp 11.7 KB
Newer Older
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
// Copyright (C) 2008  Davis E. King (davisking@users.sourceforge.net)
// License: Boost Software License   See LICENSE.txt for the full license.


#include <dlib/optimization.h>
#include <sstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <vector>
#include "../stl_checked.h"
#include "../array.h"

#include "tester.h"


namespace  
{

    using namespace test;
    using namespace dlib;
    using namespace std;

    logger dlog("test.optimization");

Davis King's avatar
Davis King committed
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
// ----------------------------------------------------------------------------------------

    long total_count = 0;


    template <typename T>
    double apq ( const T& x)
    {
        DLIB_ASSERT(x.nr() > 1 && x.nc() == 1,"");
        COMPILE_TIME_ASSERT(is_matrix<T>::value);
        double temp = 0;
        for (long r = 0; r < x.nr(); ++r)
        {
            temp += (r+1)*x(r)*x(r);
        }

        ++total_count;

        return temp + 1/100.0*(x(0) + x(x.nr()-1))*(x(0) + x(x.nr()-1));
    }

    template <typename T>
    T der_apq ( const T& x)
    {
        DLIB_ASSERT(x.nr() > 1 && x.nc() == 1,"");
        COMPILE_TIME_ASSERT(is_matrix<T>::value);
        T temp(x.nr());
        for (long r = 0; r < x.nr(); ++r)
        {
            temp(r) = 2*(r+1)*x(r) ;
        }

        temp(0) += 1/50.0*(x(0) + x(x.nr()-1));
        temp(x.nr()-1) += 1/50.0*(x(0) + x(x.nr()-1));

        ++total_count;

        return temp;
    }

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

    // Rosenbrock's function.  minimum at (1,1)
    double rosen ( const matrix<double,2,1>& x)
    {
        ++total_count;
        return 100*pow(x(1) - x(0)*x(0),2) + pow(1 - x(0),2);
    }

    matrix<double,2,1> der_rosen ( const matrix<double,2,1>& x)
    {
        ++total_count;
        matrix<double,2,1> res;
        res(0) = -400*x(0)*(x(1)-x(0)*x(0)) - 2*(1-x(0));
        res(1) = 200*(x(1)-x(0)*x(0));
        return res;
    }

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

    double simple ( const matrix<double,2,1>& x)
    {
        ++total_count;
        return 10*x(0)*x(0) + x(1)*x(1);
    }

    matrix<double,2,1> der_simple ( const matrix<double,2,1>& x)
    {
        ++total_count;
        matrix<double,2,1> res;
        res(0) = 20*x(0);
        res(1) = 2*x(1);
        return res;
    }

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

    double powell ( const matrix<double,4,1>& x)
    {
        ++total_count;
        return pow(x(0) + 10*x(1),2) +
            pow(std::sqrt(5)*(x(2) - x(3)),2) + 
            pow((x(1) - 2*x(2))*(x(1) - 2*x(2)),2) +
            pow(std::sqrt(10)*(x(0) - x(3))*(x(0) - x(3)),2);
    }


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

    void test_apq (
        const matrix<double,0,1> p
    )
    {
        typedef matrix<double,0,1> T;
        const double eps = 1e-9;
        const double minf = -10;
        matrix<double,0,1> x(p.nr()), opt(p.nr());
        set_all_elements(opt, 0);

        dlog << LINFO << "testing with apq and the start point: " << trans(p);

        total_count = 0;
        x = p;
        find_min_quasi_newton(apq<T>, der_apq<T>, x, minf, eps);
        DLIB_CASSERT(dlib::equal(x,opt, 1e-5),opt-x);
        dlog << LINFO << "find_min_quasi_newton got apq in " << total_count;

        total_count = 0;
        x = p;
        find_min_conjugate_gradient(apq<T>, der_apq<T>, x, minf, eps);
        DLIB_CASSERT(dlib::equal(x,opt, 1e-5),opt-x);
        dlog << LINFO << "find_min_conjugate_gradient got apq in " << total_count;

        total_count = 0;
        x = p;
        find_min_quasi_newton(apq<T>, derivative(apq<T>), x, minf, eps);
        DLIB_CASSERT(dlib::equal(x,opt, 1e-5),opt-x);
        dlog << LINFO << "find_min_quasi_newton got apq/noder in " << total_count;

        total_count = 0;
        x = p;
        find_min_conjugate_gradient(apq<T>, derivative(apq<T>), x, minf, eps);
        DLIB_CASSERT(dlib::equal(x,opt, 1e-5),opt-x);
        dlog << LINFO << "find_min_conjugate_gradient got apq/noder in " << total_count;
152
153
154
155
156
157
158
159
160
161
162
163

        total_count = 0;
        x = p;
        find_min_quasi_newton(apq<T>, x, minf, eps);
        DLIB_CASSERT(dlib::equal(x,opt, 1e-5),opt-x);
        dlog << LINFO << "find_min_quasi_newton got apq/noder2 in " << total_count;

        total_count = 0;
        x = p;
        find_min_conjugate_gradient(apq<T>, x, minf, eps);
        DLIB_CASSERT(dlib::equal(x,opt, 1e-5),opt-x);
        dlog << LINFO << "find_min_conjugate_gradient got apq/noder2 in " << total_count;
Davis King's avatar
Davis King committed
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
    }

    void test_powell (
        const matrix<double,4,1> p
    )
    {
        const double eps = 1e-15;
        const double minf = -1;
        matrix<double,4,1> x, opt;
        opt(0) = 0;
        opt(1) = 0;

        dlog << LINFO << "testing with powell and the start point: " << trans(p);

        total_count = 0;
        x = p;
        find_min_quasi_newton(powell, derivative(powell,1e-10), x, minf, eps);
        DLIB_CASSERT(dlib::equal(x,opt, 1e-2),opt-x);
        dlog << LINFO << "find_min_quasi_newton got powell/noder in " << total_count;

        total_count = 0;
        x = p;
        find_min_conjugate_gradient(powell, derivative(powell,1e-10), x, minf, eps);
        DLIB_CASSERT(dlib::equal(x,opt, 1e-2),opt-x);
        dlog << LINFO << "find_min_conjugate_gradient got powell/noder in " << total_count;
189
190
191
192
193
194
195
196
197
198
199
200

        total_count = 0;
        x = p;
        find_min_quasi_newton(powell, x, minf, eps);
        DLIB_CASSERT(dlib::equal(x,opt, 1e-2),opt-x);
        dlog << LINFO << "find_min_quasi_newton got powell/noder2 in " << total_count;

        total_count = 0;
        x = p;
        find_min_conjugate_gradient(powell, x, minf, eps);
        DLIB_CASSERT(dlib::equal(x,opt, 1e-2),opt-x);
        dlog << LINFO << "find_min_conjugate_gradient got powell/noder2 in " << total_count;
Davis King's avatar
Davis King committed
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
    }



    void test_simple (
        const matrix<double,2,1> p
    )
    {
        const double eps = 1e-9;
        const double minf = -10000;
        matrix<double,2,1> x, opt;
        opt(0) = 0;
        opt(1) = 0;

        dlog << LINFO << "testing with simple and the start point: " << trans(p);

        total_count = 0;
        x = p;
        find_min_quasi_newton(simple, der_simple, x, minf, eps);
        DLIB_CASSERT(dlib::equal(x,opt, 1e-5),opt-x);
        dlog << LINFO << "find_min_quasi_newton got simple in " << total_count;

        total_count = 0;
        x = p;
        find_min_conjugate_gradient(simple, der_simple, x, minf, eps);
        DLIB_CASSERT(dlib::equal(x,opt, 1e-5),opt-x);
        dlog << LINFO << "find_min_conjugate_gradient got simple in " << total_count;

        total_count = 0;
        x = p;
        find_min_quasi_newton(simple, derivative(simple), x, minf, eps);
        DLIB_CASSERT(dlib::equal(x,opt, 1e-5),opt-x);
        dlog << LINFO << "find_min_quasi_newton got simple/noder in " << total_count;

        total_count = 0;
        x = p;
        find_min_conjugate_gradient(simple, derivative(simple), x, minf, eps);
        DLIB_CASSERT(dlib::equal(x,opt, 1e-5),opt-x);
        dlog << LINFO << "find_min_conjugate_gradient got simple/noder in " << total_count;
240
241
242
243
244
245
246
247
248
249
250
251

        total_count = 0;
        x = p;
        find_min_quasi_newton(simple, x, minf, eps);
        DLIB_CASSERT(dlib::equal(x,opt, 1e-5),opt-x);
        dlog << LINFO << "find_min_quasi_newton got simple/noder2 in " << total_count;

        total_count = 0;
        x = p;
        find_min_conjugate_gradient(simple, x, minf, eps);
        DLIB_CASSERT(dlib::equal(x,opt, 1e-5),opt-x);
        dlog << LINFO << "find_min_conjugate_gradient got simple/noder2 in " << total_count;
Davis King's avatar
Davis King committed
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
    }


    void test_rosen (
        const matrix<double,2,1> p
    )
    {
        const double eps = 1e-9;
        const double minf = -10;
        matrix<double,2,1> x, opt;
        opt(0) = 1;
        opt(1) = 1;

        dlog << LINFO << "testing with rosen and the start point: " << trans(p);

        total_count = 0;
        x = p;
        find_min_quasi_newton(rosen, der_rosen, x, minf, eps);
        DLIB_CASSERT(dlib::equal(x,opt, 1e-5),opt-x);
        dlog << LINFO << "find_min_quasi_newton got rosen in " << total_count;

        total_count = 0;
        x = p;
        find_min_conjugate_gradient(rosen, der_rosen, x, minf, eps);
        DLIB_CASSERT(dlib::equal(x,opt, 1e-5),opt-x);
        dlog << LINFO << "find_min_conjugate_gradient got rosen in " << total_count;

        total_count = 0;
        x = p;
        find_min_quasi_newton(rosen, derivative(rosen), x, minf, eps);
282
        DLIB_CASSERT(dlib::equal(x,opt, 1e-4),opt-x);
Davis King's avatar
Davis King committed
283
284
285
286
287
        dlog << LINFO << "find_min_quasi_newton got rosen/noder in " << total_count;

        total_count = 0;
        x = p;
        find_min_conjugate_gradient(rosen, derivative(rosen), x, minf, eps);
288
        DLIB_CASSERT(dlib::equal(x,opt, 1e-4),opt-x);
Davis King's avatar
Davis King committed
289
        dlog << LINFO << "find_min_conjugate_gradient got rosen/noder in " << total_count;
290
291
292
293
294
295
296
297
298
299
300
301
302
303

        /* This test fails
        total_count = 0;
        x = p;
        find_min_quasi_newton(rosen, x, minf, eps, 1e-13);
        DLIB_CASSERT(dlib::equal(x,opt, 1e-2),opt-x);
        dlog << LINFO << "find_min_quasi_newton got rosen/noder2 in " << total_count;
        */

        total_count = 0;
        x = p;
        find_min_conjugate_gradient(rosen, x, minf, eps, 1e-11);
        DLIB_CASSERT(dlib::equal(x,opt, 1e-4),opt-x);
        dlog << LINFO << "find_min_conjugate_gradient got rosen/noder2 in " << total_count;
Davis King's avatar
Davis King committed
304
305
306
307
    }

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

308
309
310
311
312
313
314
    void optimization_test (
    )
    /*!
        ensures
            - runs tests on the optimization stuff compliance with the specs
    !*/
    {        
Davis King's avatar
Davis King committed
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
        matrix<double,0,1> p;

        p.set_size(2);


        // test with the rosen function
        p(0) = 9;
        p(1) = -4.9;
        test_rosen(p);

        p(0) = 0;
        p(1) = 0;
        test_rosen(p);

        p(0) = 5323;
        p(1) = 98248;
        test_rosen(p);

        // test with the simple function
        p(0) = 1;
        p(1) = 1;
        test_simple(p);

        p(0) = 0.5;
        p(1) = -9;
        test_simple(p);

        p(0) = 645;
        p(1) = 839485;
        test_simple(p);

        // test with the apq function
        p.set_size(5);

        p(0) = 1;
        p(1) = 1;
        p(2) = 1;
        p(3) = 1;
        p(4) = 1;
        test_apq(p);

        p(0) = 1;
        p(1) = 2;
        p(2) = 3;
        p(3) = 4;
        p(4) = 5;
        test_apq(p);

        p(0) = 1;
        p(1) = 2;
        p(2) = -3;
        p(3) = 4;
        p(4) = 5;
        test_apq(p);

        p(0) = 1;
        p(1) = 2324;
        p(2) = -3;
        p(3) = 4;
        p(4) = 534534;
        test_apq(p);

        p.set_size(10);
        p(0) = 1;
        p(1) = 2;
        p(2) = -3;
        p(3) = 4;
        p(4) = 5;
        p(5) = 1;
        p(6) = 2;
        p(7) = -3;
        p(8) = 4;
        p(9) = 5;
        test_apq(p);

        // test with the powell function
        p.set_size(4);

        p(0) = 3;
        p(1) = -1;
        p(2) = 0;
        p(3) = 1;
        test_powell(p);
398

Davis King's avatar
Davis King committed
399
400
401
402
403
        p(0) = 423;
        p(1) = -34.9;
        p(2) = 0.053;
        p(3) = 84;
        test_powell(p);
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
    }



    class optimization_tester : public tester
    {
    public:
        optimization_tester (
        ) :
            tester ("test_optimization",
                    "Runs tests on the optimization component.")
        {}

        void perform_test (
        )
        {
            optimization_test();
        }
    } a;

}