// Copyright (C) 2017 Davis E. King (davis@dlib.net) // License: Boost Software License See LICENSE.txt for the full license. #ifndef DLIB_FiND_GLOBAL_MAXIMUM_hH_ #define DLIB_FiND_GLOBAL_MAXIMUM_hH_ #include "find_max_global_abstract.h" #include "global_function_search.h" #include "../metaprogramming.h" #include #include #include #include #include #include "../threads/thread_pool_extension.h" #include "../statistics/statistics.h" #include "../enable_if.h" namespace dlib { namespace gopt_impl { // ---------------------------------------------------------------------------------------- class disable_decay_to_scalar { const matrix& a; public: disable_decay_to_scalar(const matrix& a) : a(a){} operator const matrix&() const { return a;} }; template auto _cwv ( T&& f, const matrix& a, compile_time_integer_list ) -> decltype(f(a(indices-1)...)) { DLIB_CASSERT(a.size() == sizeof...(indices), "You invoked dlib::call_function_and_expand_args(f,a) but the number of arguments expected by f() doesn't match the size of 'a'. " << "Expected " << sizeof...(indices) << " arguments but got " << a.size() << "." ); return f(a(indices-1)...); } // Visual studio, as of November 2017, doesn't support C++11 and can't compile this code. // So we write the terrible garbage in the #else for visual studio. When Visual Studio supports C++11 I'll update this #ifdef to use the C++11 code. #ifndef _MSC_VER template struct call_function_and_expand_args { template static auto go(T&& f, const matrix& a) -> decltype(_cwv(std::forward(f),a,typename make_compile_time_integer_range::type())) { return _cwv(std::forward(f),a,typename make_compile_time_integer_range::type()); } template static auto go(T&& f, const matrix& a) -> decltype(call_function_and_expand_args::template go(std::forward(f),a)) { return call_function_and_expand_args::go(std::forward(f),a); } }; template <> struct call_function_and_expand_args<0> { template static auto go(T&& f, const matrix& a) -> decltype(f(disable_decay_to_scalar(a))) { return f(disable_decay_to_scalar(a)); } }; #else template struct call_function_and_expand_args { template static auto go(T&& f, const matrix& a) -> decltype(f(disable_decay_to_scalar(a))) {return f(disable_decay_to_scalar(a)); } template static auto go(T&& f, const matrix& a) -> decltype(f(a(0))) { DLIB_CASSERT(a.size() == 1); return f(a(0)); } template static auto go(T&& f, const matrix& a) -> decltype(f(a(0),a(1))) { DLIB_CASSERT(a.size() == 2); return f(a(0),a(1)); } template static auto go(T&& f, const matrix& a) -> decltype(f(a(0), a(1), a(2))) { DLIB_CASSERT(a.size() == 3); return f(a(0), a(1),a(2)); } template static auto go(T&& f, const matrix& a) -> decltype(f(a(0), a(1), a(2), a(3))) { DLIB_CASSERT(a.size() == 4); return f(a(0), a(1), a(2), a(3)); } template static auto go(T&& f, const matrix& a) -> decltype(f(a(0), a(1), a(2), a(3), a(4))) { DLIB_CASSERT(a.size() == 5); return f(a(0), a(1), a(2), a(3), a(4)); } template static auto go(T&& f, const matrix& a) -> decltype(f(a(0), a(1), a(2), a(3), a(4), a(5))) { DLIB_CASSERT(a.size() == 6); return f(a(0), a(1), a(2), a(3), a(4), a(5)); } template static auto go(T&& f, const matrix& a) -> decltype(f(a(0), a(1), a(2), a(3), a(4), a(5), a(6))) { DLIB_CASSERT(a.size() == 7); return f(a(0), a(1), a(2), a(3), a(4), a(5), a(6)); } }; #endif } // ---------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------- template auto call_function_and_expand_args( T&& f, const matrix& a ) -> decltype(gopt_impl::call_function_and_expand_args<40>::go(f,a)) { // unpack up to 40 parameters when calling f() return gopt_impl::call_function_and_expand_args<40>::go(std::forward(f),a); } // ---------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------- struct max_function_calls { max_function_calls() = default; explicit max_function_calls(size_t max_calls) : max_calls(max_calls) {} size_t max_calls = std::numeric_limits::max(); }; // ---------------------------------------------------------------------------------------- const auto FOREVER = std::chrono::hours(24*365*290); // 290 years using stop_condition = std::function; const stop_condition never_stop_early = [](double) { return false; }; // ---------------------------------------------------------------------------------------- namespace impl { template < typename funct > std::pair find_max_global ( double ymult, thread_pool& tp, std::vector& functions, std::vector specs, const max_function_calls num, const std::chrono::nanoseconds max_runtime = FOREVER, double solver_epsilon = 0, std::vector> initial_function_evals = {}, stop_condition should_stop = never_stop_early ) { // Decide which parameters should be searched on a log scale. Basically, it's // common for machine learning models to have parameters that should be searched on // a log scale (e.g. SVM C). These parameters are usually identifiable because // they have bounds like [1e-5 1e10], that is, they span a very large range of // magnitudes from really small to really big. So there we are going to check for // that and if we find parameters with that kind of bound constraints we will // transform them to a log scale automatically. std::vector> log_scale(specs.size()); for (size_t i = 0; i < specs.size(); ++i) { for (long j = 0; j < specs[i].lower.size(); ++j) { if (!specs[i].is_integer_variable[j] && specs[i].lower(j) > 0 && specs[i].upper(j)/specs[i].lower(j) >= 1000) { log_scale[i].push_back(true); specs[i].lower(j) = std::log(specs[i].lower(j)); specs[i].upper(j) = std::log(specs[i].upper(j)); } else { log_scale[i].push_back(false); } } } if (initial_function_evals.empty()) { initial_function_evals.resize(specs.size()); } for (auto& evals : initial_function_evals) { for (auto& eval : evals) { eval.y *= ymult; } } global_function_search opt(specs, {initial_function_evals}); opt.set_solver_epsilon(solver_epsilon); running_stats_decayed objective_funct_eval_time(functions.size()*5); std::mutex eval_time_mutex; using namespace std::chrono; const auto time_to_stop = steady_clock::now() + max_runtime; //atomic doesn't support .fetch_or, use std::atomic instead std::atomic this_should_stop{false}; double max_solver_overhead_time = 0; // Now run the main solver loop. for (size_t i = 0; i < num.max_calls && steady_clock::now() < time_to_stop && !this_should_stop.load(); ++i) { const auto get_next_x_start_time = steady_clock::now(); auto next = std::make_shared(opt.get_next_x()); const auto get_next_x_runtime = steady_clock::now() - get_next_x_start_time; auto execute_call = [&functions,&ymult,&log_scale,&eval_time_mutex,&objective_funct_eval_time,next,&should_stop,&this_should_stop]() { matrix x = next->x(); // Undo any log-scaling that was applied to the variables before we pass them // to the functions being optimized. for (long j = 0; j < x.size(); ++j) { if (log_scale[next->function_idx()][j]) x(j) = std::exp(x(j)); } const auto funct_eval_start = steady_clock::now(); double y = ymult*call_function_and_expand_args(functions[next->function_idx()], x); const double funct_eval_runtime = duration_cast(steady_clock::now() - funct_eval_start).count(); this_should_stop.fetch_or(should_stop(y*ymult)); next->set(y); std::lock_guard lock(eval_time_mutex); objective_funct_eval_time.add(funct_eval_runtime); }; tp.add_task_by_value(execute_call); std::lock_guard lock(eval_time_mutex); const double obj_funct_time = objective_funct_eval_time.mean()/std::max(1ul,tp.num_threads_in_pool()); const double solver_overhead_time = duration_cast(get_next_x_runtime).count(); max_solver_overhead_time = std::max(max_solver_overhead_time, solver_overhead_time); // Don't start thinking about the logic below until we have at least 5 objective // function samples for each objective function. This way we have a decent idea how // fast these things are. The solver overhead is really small initially so none of // the stuff below really matters in the beginning anyway. if (objective_funct_eval_time.current_n() > functions.size()*5) { // If calling opt.get_next_x() is taking a long time relative to how long it takes // to evaluate the objective function then we should spend less time grinding on the // internal details of the optimizer and more time running the actual objective // function. E.g. if we could just run 2x more objective function calls in the same // amount of time then we should just do that. The main slowness in the solver is // from the Monte Carlo sampling, which we can turn down if the objective function // is really fast to evaluate. This is because the point of the Monte Carlo part is // to try really hard to avoid calls to really expensive objective functions. But // if the objective function is not expensive then we should just call it. if (obj_funct_time < solver_overhead_time) { // Reduce the amount of Monte Carlo sampling we do. If it goes low enough // we will disable it altogether. const size_t new_val = static_cast(std::floor(opt.get_monte_carlo_upper_bound_sample_num()*0.8)); opt.set_monte_carlo_upper_bound_sample_num(std::max(1, new_val)); // At this point just disable the upper bounding Monte Carlo search stuff and // use only pure random search since the objective function is super cheap to // evaluate, making this more fancy search a waste of time. if (opt.get_monte_carlo_upper_bound_sample_num() == 1) { opt.set_pure_random_search_probability(1); } } else if (obj_funct_time > 1.5*max_solver_overhead_time) // Consider reenabling { // The Monte Carlo overhead grows over time as the solver accumulates more // information about the objective function. So we only want to reenable it // or make it bigger if the objective function really is more expensive. So // we compare to the max solver runtime we have seen so far. If the // objective function has suddenly gotten more expensive then we start to // turn the Monte Carlo modeling back on. const size_t new_val = static_cast(std::ceil(opt.get_monte_carlo_upper_bound_sample_num()*1.28)); opt.set_monte_carlo_upper_bound_sample_num(std::min(5000, new_val)); // Set this back to its default value. opt.set_pure_random_search_probability(0.02); } } } tp.wait_for_all_tasks(); matrix x; double y; size_t function_idx; opt.get_best_function_eval(x,y,function_idx); // Undo any log-scaling that was applied to the variables before we output them. for (long j = 0; j < x.size(); ++j) { if (log_scale[function_idx][j]) x(j) = std::exp(x(j)); } return std::make_pair(function_idx, function_evaluation(x,y/ymult)); } // This overload allows the order of max_runtime and num to be reversed. template < typename funct, typename ...Args > std::pair find_max_global ( double ymult, thread_pool& tp, std::vector& functions, std::vector specs, const std::chrono::nanoseconds max_runtime, const max_function_calls num, double solver_epsilon = 0, Args&& ...args ) { return find_max_global(ymult, tp, functions, std::move(specs), num, max_runtime, solver_epsilon, std::forward(args)...); } // This overload allows the num argument to be skipped. template < typename funct, typename ...Args > std::pair find_max_global ( double ymult, thread_pool& tp, std::vector& functions, std::vector specs, const std::chrono::nanoseconds max_runtime, double solver_epsilon = 0, Args&& ...args ) { return find_max_global(ymult, tp, functions, std::move(specs), max_function_calls(), max_runtime, solver_epsilon, std::forward(args)...); } // This overload allows the max_runtime argument to be skipped. template < typename funct, typename ...Args > std::pair find_max_global ( double ymult, thread_pool& tp, std::vector& functions, std::vector specs, const max_function_calls num, double solver_epsilon, Args&& ...args ) { return find_max_global(ymult, tp, functions, std::move(specs), num, FOREVER, solver_epsilon, std::forward(args)...); } // This overload makes the thread_pool argument optional. template < typename funct, typename ...Args > std::pair find_max_global ( double ymult, std::vector& functions, Args&& ...args ) { // disabled, don't use any threads thread_pool tp(0); return find_max_global(ymult, tp, functions, std::forward(args)...); } // The point of normalize() is to handle some of the overloaded argument types in // find_max_global() instances below and turn them into the argument types expected by // find_max_global() above. template const T& normalize(const T& item) { return item; } inline std::vector> normalize( const std::vector& initial_function_evals ) { return {initial_function_evals}; } } // ---------------------------------------------------------------------------------------- template < typename funct, typename ...Args > std::pair find_max_global ( std::vector& functions, std::vector specs, Args&& ...args ) { return impl::find_max_global(+1, functions, std::move(specs), std::forward(args)...); } template < typename funct, typename ...Args > std::pair find_min_global ( std::vector& functions, std::vector specs, Args&& ...args ) { return impl::find_max_global(-1, functions, std::move(specs), std::forward(args)...); } template < typename funct, typename ...Args > std::pair find_max_global ( thread_pool& tp, std::vector& functions, std::vector specs, Args&& ...args ) { return impl::find_max_global(+1, tp, functions, std::move(specs), std::forward(args)...); } template < typename funct, typename ...Args > std::pair find_min_global ( thread_pool& tp, std::vector& functions, std::vector specs, Args&& ...args ) { return impl::find_max_global(-1, tp, functions, std::move(specs), std::forward(args)...); } // ---------------------------------------------------------------------------------------- // Overloads that take function objects and simple matrix bounds instead of function_specs. template < typename funct, typename ...Args > function_evaluation find_max_global ( funct f, const matrix& bound1, const matrix& bound2, const std::vector& is_integer_variable, Args&& ...args ) { std::vector functions(1,std::move(f)); std::vector specs(1, function_spec(bound1, bound2, is_integer_variable)); return find_max_global(functions, std::move(specs), impl::normalize(args)...).second; } template < typename funct, typename ...Args > function_evaluation find_min_global ( funct f, const matrix& bound1, const matrix& bound2, const std::vector& is_integer_variable, Args&& ...args ) { std::vector functions(1,std::move(f)); std::vector specs(1, function_spec(bound1, bound2, is_integer_variable)); return find_min_global(functions, std::move(specs), impl::normalize(args)...).second; } template < typename funct, typename ...Args > function_evaluation find_max_global ( thread_pool& tp, funct f, const matrix& bound1, const matrix& bound2, const std::vector& is_integer_variable, Args&& ...args ) { std::vector functions(1,std::move(f)); std::vector specs(1, function_spec(bound1, bound2, is_integer_variable)); return find_max_global(tp, functions, std::move(specs), impl::normalize(args)...).second; } template < typename funct, typename ...Args > function_evaluation find_min_global ( thread_pool& tp, funct f, const matrix& bound1, const matrix& bound2, const std::vector& is_integer_variable, Args&& ...args ) { std::vector functions(1,std::move(f)); std::vector specs(1, function_spec(bound1, bound2, is_integer_variable)); return find_min_global(tp, functions, std::move(specs), impl::normalize(args)...).second; } // ---------------------------------------------------------------------------------------- // overloads that are the same as above, but is_integer_variable defaulted to false for all parameters. template < typename funct, typename T, typename ...Args > typename disable_if>, function_evaluation>::type find_max_global ( funct f, const matrix& bound1, const matrix& bound2, const T& arg, Args&& ...args ) { const std::vector is_integer_variable(bound1.size(),false); return find_max_global(std::move(f), bound1, bound2, is_integer_variable, arg, impl::normalize(args)...); } template < typename funct, typename T, typename ...Args > typename disable_if>, function_evaluation>::type find_min_global ( funct f, const matrix& bound1, const matrix& bound2, const T& arg, Args&& ...args ) { const std::vector is_integer_variable(bound1.size(),false); return find_min_global(std::move(f), bound1, bound2, is_integer_variable, arg, impl::normalize(args)...); } template < typename funct, typename T, typename ...Args > typename disable_if>, function_evaluation>::type find_max_global ( thread_pool& tp, funct f, const matrix& bound1, const matrix& bound2, const T& arg, Args&& ...args ) { const std::vector is_integer_variable(bound1.size(),false); return find_max_global(tp, std::move(f), bound1, bound2, is_integer_variable, arg, impl::normalize(args)...); } template < typename funct, typename T, typename ...Args > typename disable_if>, function_evaluation>::type find_min_global ( thread_pool& tp, funct f, const matrix& bound1, const matrix& bound2, const T& arg, Args&& ...args ) { const std::vector is_integer_variable(bound1.size(),false); return find_min_global(tp, std::move(f), bound1, bound2, is_integer_variable, arg, impl::normalize(args)...); } // ---------------------------------------------------------------------------------------- // overloads for a function taking a single scalar. template < typename funct, typename T, typename ...Args > function_evaluation find_max_global ( funct f, const double bound1, const double bound2, const T& arg, Args&& ...args ) { return find_max_global(std::move(f), matrix({bound1}), matrix({bound2}), arg, impl::normalize(args)...); } template < typename funct, typename T, typename ...Args > function_evaluation find_min_global ( funct f, const double bound1, const double bound2, const T& arg, Args&& ...args ) { return find_min_global(std::move(f), matrix({bound1}), matrix({bound2}), arg, impl::normalize(args)...); } template < typename funct, typename T, typename ...Args > function_evaluation find_max_global ( thread_pool& tp, funct f, const double bound1, const double bound2, const T& arg, Args&& ...args ) { return find_max_global(tp, std::move(f), matrix({bound1}), matrix({bound2}), arg, impl::normalize(args)...); } template < typename funct, typename T, typename ...Args > function_evaluation find_min_global ( thread_pool& tp, funct f, const double bound1, const double bound2, const T& arg, Args&& ...args ) { return find_min_global(tp, std::move(f), matrix({bound1}), matrix({bound2}), arg, impl::normalize(args)...); } // ---------------------------------------------------------------------------------------- } #endif // DLIB_FiND_GLOBAL_MAXIMUM_hH_