| 340 | // ---------------------------------------------------------------------------------------- |
| 341 | |
| 342 | void test_find_min_global( |
| 343 | ) |
| 344 | { |
| 345 | print_spinner(); |
| 346 | auto rosen = [](const matrix<double,0,1>& x) { return +1*( 100*std::pow(x(1) - x(0)*x(0),2.0) + std::pow(1 - x(0),2)); }; |
| 347 | |
| 348 | auto result = find_min_global(rosen, {0.1, 0.1}, {2, 2}, max_function_calls(100), 0); |
| 349 | matrix<double,0,1> true_x = {1,1}; |
| 350 | |
| 351 | dlog << LINFO << "rosen: " << trans(result.x); |
| 352 | DLIB_TEST_MSG(min(abs(true_x-result.x)) < 1e-5, min(abs(true_x-result.x))); |
| 353 | print_spinner(); |
| 354 | |
| 355 | result = find_min_global(rosen, {0.1, 0.1}, {2, 2}, max_function_calls(100)); |
| 356 | dlog << LINFO << "rosen: " << trans(result.x); |
| 357 | DLIB_TEST_MSG(min(abs(true_x-result.x)) < 1e-5, min(abs(true_x-result.x))); |
| 358 | print_spinner(); |
| 359 | |
| 360 | result = find_min_global(rosen, {0.1, 0.1}, {2, 2}, std::chrono::seconds(5)); |
| 361 | dlog << LINFO << "rosen: " << trans(result.x); |
| 362 | DLIB_TEST_MSG(min(abs(true_x-result.x)) < 1e-5, min(abs(true_x-result.x))); |
| 363 | print_spinner(); |
| 364 | |
| 365 | result = find_min_global(rosen, {0.1, 0.1}, {2, 2}, {false,false}, max_function_calls(100)); |
| 366 | dlog << LINFO << "rosen: " << trans(result.x); |
| 367 | DLIB_TEST_MSG(min(abs(true_x-result.x)) < 1e-5, min(abs(true_x-result.x))); |
| 368 | print_spinner(); |
| 369 | |
| 370 | result = find_min_global(rosen, {0, 0}, {0.9, 0.9}, {false,false}, max_function_calls(100)); |
| 371 | true_x = {0.9, 0.81}; |
| 372 | dlog << LINFO << "rosen, bounded at 0.9: " << trans(result.x); |
| 373 | DLIB_TEST_MSG(min(abs(true_x-result.x)) < 1e-5, min(abs(true_x-result.x))); |
| 374 | print_spinner(); |
| 375 | |
| 376 | result = find_min_global([](double x){ return std::pow(x-2,2.0); }, -10, 10, max_function_calls(10), 0); |
| 377 | dlog << LINFO << "(x-2)^2: " << trans(result.x); |
| 378 | DLIB_TEST(result.x.size()==1); |
| 379 | DLIB_TEST(std::abs(result.x - 2) < 1e-9); |
| 380 | print_spinner(); |
| 381 | |
| 382 | result = find_min_global([](double x){ return std::pow(x-2,2.0); }, -10, 1, max_function_calls(10)); |
| 383 | dlog << LINFO << "(x-2)^2, bound at 1: " << trans(result.x); |
| 384 | DLIB_TEST(result.x.size()==1); |
| 385 | DLIB_TEST(std::abs(result.x - 1) < 1e-9); |
| 386 | print_spinner(); |
| 387 | |
| 388 | result = find_min_global([](double x){ return std::pow(x-2,2.0); }, -10, 1, std::chrono::seconds(2)); |
| 389 | dlog << LINFO << "(x-2)^2, bound at 1: " << trans(result.x); |
| 390 | DLIB_TEST(result.x.size()==1); |
| 391 | DLIB_TEST(std::abs(result.x - 1) < 1e-9); |
| 392 | print_spinner(); |
| 393 | |
| 394 | |
| 395 | result = find_min_global([](double a, double b){ return complex_holder_table(a,b);}, |
| 396 | {-10, -10}, {10, 10}, max_function_calls(400), 0); |
| 397 | dlog << LINFO << "complex_holder_table y: "<< result.y; |
| 398 | DLIB_TEST_MSG(std::abs(result.y + 21.9210397) < 0.0001, std::abs(result.y + 21.9210397)); |
| 399 | } |
no test coverage detected