I'm trying to use the the function find_min_single_variable from DLIB in C++. For my example I need to compute the min of sin(X) com 0 < X < 2*pi. The result should be -1. But the result is always 0. Any idea?
#include <iostream>
#include <cstdio>
#include <dlib/optimization.h>
#include <cmath>
using namespace dlib;
double my_sin(double x)
{
return std::sin(x);
}
void main()
{
// declare variables
const double begin = 0.0;
const double end = 6.28318530718;
double starting_point = 0.0;
const double eps = 1e-3;
const long max_iter = 100;
const double initial_search_radius = 0.01;
// print variables
std::cout << "result: \n" << find_min_single_variable(my_sin, starting_point, begin, end, eps, max_iter, initial_search_radius) << std::endl;
std::printf("press any key to continue \n");
std::getchar();
}
Thank you in advance
This is happening because 0 is the local minimizer of sin(x) in the neighborhood of 0 with a left bound at 0. So it's doing the right thing since dlib::find_min_single_variable() is a local optimizer. It finds the nearest local minimizer to the starting point.
But what you really want is a global optimizer. The newest dlib code on github includes a new optimizer for just such a case. If you get the new dlib code and try this:
auto result = dlib::find_min_global(my_sin, begin, end, max_function_calls(30));
cout << result.x/pi << endl;
cout << result.y << endl;
You will see that it finds the global min of x==1.5*pi, giving sin(x)==-1 as desired. dlib::find_min_global() also doesn't require you to specify any starting point or other tedious parameters.