From 2b8becae972e29b5accd37b00c89634d1194d7ca Mon Sep 17 00:00:00 2001 From: Davis King Date: Mon, 20 Nov 2017 21:36:04 -0500 Subject: [PATCH] Added global_function_search unit test --- dlib/test/global_optimization.cpp | 70 +++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/dlib/test/global_optimization.cpp b/dlib/test/global_optimization.cpp index 500f2fd5e..e4f582e42 100644 --- a/dlib/test/global_optimization.cpp +++ b/dlib/test/global_optimization.cpp @@ -83,6 +83,75 @@ namespace } } +// ---------------------------------------------------------------------------------------- + + double complex_holder_table ( double x0, double x1) + { + // The regular HolderTable function + //return -std::abs(sin(x0)*cos(x1)*exp(std::abs(1-std::sqrt(x0*x0+x1*x1)/pi))); + + // My more complex version of it with discontinuities and more local minima. + double sign = 1; + for (double j = -4; j < 9; j += 0.5) + { + if (j < x0 && x0 < j+0.5) + x0 += sign*0.25; + sign *= -1; + } + // HolderTable function tilted towards 10,10 + return -std::abs(sin(x0)*cos(x1)*exp(std::abs(1-std::sqrt(x0*x0+x1*x1)/pi))) +(x0+x1)/10 + sin(x0*10)*cos(x1*10); + } + +// ---------------------------------------------------------------------------------------- + + void test_global_function_search() + { + + function_spec spec{{-10,-10}, {10,10}}; + function_spec spec2{{-10,-10, -50}, {10,10, 50}}; + global_function_search opt({spec, spec, spec2}); + + dlib::rand rnd; + bool found_optimal_point = false; + for (int i = 0; i < 400 && !found_optimal_point; ++i) + { + print_spinner(); + std::vector nexts; + for (int k = 0; k < rnd.get_integer_in_range(1,4); ++k) + nexts.emplace_back(opt.get_next_x()); + + for (auto& next : nexts) + { + switch (next.function_idx()) + { + case 0: next.set( -complex_holder_table(next.x()(0), next.x()(1))); break; + case 1: next.set( -10*complex_holder_table(next.x()(0), next.x()(1))); break; + case 2: next.set( -2*complex_holder_table(next.x()(0), next.x()(1))); break; + default: DLIB_TEST(false); break; + } + + matrix x; + double y; + size_t function_idx; + opt.get_best_function_eval(x,y,function_idx); + /* + cout << "\ni: "<< i << endl; + cout << "best eval x: "<< trans(x); + cout << "best eval y: "<< y << endl; + cout << "best eval function index: "<< function_idx << endl; + */ + + if (std::abs(y - 10*21.9210397) < 0.0001) + { + found_optimal_point = true; + break; + } + } + } + + DLIB_TEST(found_optimal_point); + } + // ---------------------------------------------------------------------------------------- class global_optimization_tester : public tester @@ -100,6 +169,7 @@ namespace test_upper_bound_function(0.1, 1e-6); test_upper_bound_function(0.0, 1e-6); test_upper_bound_function(0.0, 1e-1); + test_global_function_search(); } } a;