1 // Copyright (C) 2018  Davis E. King (davis@dlib.net)
2 // License: Boost Software License   See LICENSE.txt for the full license.
3 
4 
5 #include <dlib/optimization.h>
6 #include <dlib/global_optimization.h>
7 #include <sstream>
8 #include <string>
9 #include <cstdlib>
10 #include <ctime>
11 #include <vector>
12 
13 #include "tester.h"
14 
15 
16 namespace
17 {
18 
19     using namespace test;
20     using namespace dlib;
21     using namespace std;
22 
23     logger dlog("test.isotonic_regression");
24 
25 // ----------------------------------------------------------------------------------------
26 
27     class optimization_tester : public tester
28     {
29     public:
optimization_tester()30         optimization_tester (
31         ) :
32             tester ("test_isotonic_regression",
33                     "Runs tests on the isotonic_regression object.")
34         {}
35 
perform_test()36         void perform_test (
37         )
38         {
39             dlib::rand rnd;
40 
41             for (int round = 0; round < 100; ++round)
42             {
43                 print_spinner();
44                 std::vector<double> vect;
45                 for (int i = 0; i < 5; ++i)
46                     vect.push_back(put_in_range(-1,1,rnd.get_random_gaussian()));
47 
48 
49                 auto f = [&](const matrix<double,0,1>& x)
50                 {
51                     double dist = 0;
52                     double sum = 0;
53                     for (long i = 0; i < x.size(); ++i)
54                     {
55                         sum += x(i);
56                         dist += (sum-vect[i])*(sum-vect[i]);
57                     }
58                     return dist;
59                 };
60 
61                 auto objval = [vect](const matrix<double,0,1>& x)
62                 {
63                     return sum(squared(mat(vect)-x));
64                 };
65 
66                 auto is_monotonic = [](const matrix<double,0,1>& x)
67                 {
68                     for (long i = 1; i < x.size(); ++i)
69                     {
70                         if (x(i-1) > x(i))
71                             return false;
72                     }
73                     return true;
74                 };
75 
76                 matrix<double,0,1> lower(5), upper(5);
77                 lower = 0;
78                 lower(0) = -4;
79                 upper = 4;
80                 // find the solution with find_min_global() and then check that it matches
81                 auto result = find_min_global(f, lower, upper, max_function_calls(40));
82 
83                 for (long i = 1; i < result.x.size(); ++i)
84                     result.x(i) += result.x(i-1);
85 
86                 isotonic_regression mr;
87                 mr(vect);
88 
89                 dlog << LINFO << "err: "<<  objval(mat(vect)) - objval(result.x);
90 
91                 DLIB_CASSERT(is_monotonic(mat(vect)));
92                 DLIB_CASSERT(is_monotonic(result.x));
93                 // isotonic_regression should be at least as good as find_min_global().
94                 DLIB_CASSERT(objval(mat(vect)) - objval(result.x) < 1e-13);
95             }
96 
97         }
98     } a;
99 
100 }
101 
102 
103 
104