summaryrefslogtreecommitdiffstats
path: root/ml/dlib/dlib/optimization/find_optimal_parameters.h
blob: 0884778c9ed82f6c78e867bc9e94f27d1b381495 (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
// Copyright (C) 2016  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.
#ifndef DLIB_fIND_OPTIMAL_PARAMETERS_Hh_
#define DLIB_fIND_OPTIMAL_PARAMETERS_Hh_

#include "../matrix.h"
#include "find_optimal_parameters_abstract.h"
#include "optimization_bobyqa.h"
#include "optimization_line_search.h"

namespace dlib
{

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

    template <
        typename funct
        >
    double find_optimal_parameters (
        double initial_search_radius,
        double eps,
        const unsigned int max_f_evals,
        matrix<double,0,1>& x,
        const matrix<double,0,1>& x_lower,
        const matrix<double,0,1>& x_upper,
        const funct& f
    ) 
    {
        DLIB_CASSERT(x.size() == x_lower.size() && x_lower.size() == x_upper.size() && x.size() > 0,
            "\t double find_optimal_parameters()"
            << "\n\t x.size():       " << x.size()
            << "\n\t x_lower.size(): " << x_lower.size()
            << "\n\t x_upper.size(): " << x_upper.size()
            );

        // check the requirements.  Also split the assert up so that the error message isn't huge.
        DLIB_CASSERT(max_f_evals > 1 && eps > 0 && initial_search_radius > eps,
            "\t double find_optimal_parameters()"
            << "\n\t Invalid arguments have been given to this function"
            << "\n\t initial_search_radius: " << initial_search_radius
            << "\n\t eps:                   " << eps
            << "\n\t max_f_evals:           " << max_f_evals
        );

        DLIB_CASSERT( min(x_upper - x_lower) > 0 &&
                     min(x - x_lower) >= 0 && min(x_upper - x) >= 0,
            "\t double find_optimal_parameters()"
            << "\n\t The bounds constraints have to make sense and also contain the starting point."
            << "\n\t min(x_upper - x_lower):                         " << min(x_upper - x_lower) 
            << "\n\t min(x - x_lower) >= 0 && min(x_upper - x) >= 0: " << (min(x - x_lower) >= 0 && min(x_upper - x) >= 0)
        );

        // if the search radius is too big then shrink it so it fits inside the bounds.
        if (initial_search_radius*2 >= min(x_upper-x_lower))
            initial_search_radius = 0.5*min(x_upper-x_lower)*0.99;


        double objective_val = std::numeric_limits<double>::infinity();
        size_t num_iter_used = 0;
        if (x.size() == 1)
        {
            // BOBYQA requires x to have at least 2 variables in it.  So we can't call it in
            // this case.  Instead we call find_min_single_variable().
            matrix<double,0,1> temp(1);
            auto ff = [&](const double& xx)
            {
                temp = xx;
                double obj = f(temp);  
                ++num_iter_used;
                // keep track of the best x.
                if (obj < objective_val)
                {
                    objective_val = obj;
                    x = temp;
                }
                return obj;
            };
            try
            {
                double dx = x(0);
                find_min_single_variable(ff, dx, x_lower(0), x_upper(0), eps, max_f_evals, initial_search_radius);
            } catch (optimize_single_variable_failure& )
            {
            }
        }
        else
        {
            auto ff = [&](const matrix<double,0,1>& xx)
            {
                double obj = f(xx); 
                ++num_iter_used;
                // keep track of the best x.
                if (obj < objective_val)
                {
                    objective_val = obj;
                    x = xx;
                }
                return obj;
            };
            try
            {
                matrix<double,0,1> start_x = x;
                find_min_bobyqa(ff, start_x, 2*x.size()+1, x_lower, x_upper, initial_search_radius, eps, max_f_evals);
            } catch (bobyqa_failure& )
            {
            }
        }

        return objective_val;
    }

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

}

#endif // DLIB_fIND_OPTIMAL_PARAMETERS_Hh_