简体   繁体   English

如何在C ++中使用DLIB的find_min_single_variable

[英]How to use find_min_single_variable of DLIB in C++

I'm trying to use the the function find_min_single_variable from DLIB in C++ . 我正在尝试使用C ++中 DLIB的功能find_min_single_variable For my example I need to compute the min of sin(X) com 0 < X < 2*pi. 对于我的示例,我需要计算sin(X)com的最小值0 <X <2 * pi。 The result should be -1. 结果应为-1。 But the result is always 0. Any idea? 但是结果始终为0。有什么想法吗?

#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 . 发生这种情况是因为0是0附近的sin(x)的局部最小化器,左边界为0。因此,由于dlib :: find_min_single_variable()是局部优化器,所以它做对了。 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. github上最新的dlib代码包括针对这种情况的新优化器。 If you get the new dlib code and try this: 如果您获得了新的dlib代码,请尝试以下操作:

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. 您将看到它找到x == 1.5 * pi的全局最小值,并根据需要给出sin(x)==-1。 dlib::find_min_global() also doesn't require you to specify any starting point or other tedious parameters. dlib :: find_min_global()也不需要您指定任何起点或其他繁琐的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM