【问题标题】:Finding all local maxima of a function查找函数的所有局部最大值
【发布时间】:2020-03-24 08:37:28
【问题描述】:

我已经编写了代码来使用模拟退火算法找到一个函数的全局最小值——在下面——但是如何使用相同的算法找到一个函数的所有局部最大值

我用于查找函数的局部最小值的代码,请注意,我对在xinteractor 询问 f(x) 的函数一无所知,即函数的成本一个特定的点。

#include <bits/stdc++.h>

using namespace std;

double myRand(double fMin, double fMax)
{
    double f = (double)rand() / RAND_MAX;
    return fMin + f * (fMax - fMin);
}


int main()
{
    cout.flush();


    double x,fx,xMin;
    double fMin;

    cout << "? "<<fixed << setprecision(6) << -1<<endl;
    cin>>fMin;


    for(double T = 1000; T>1; T*=.995)
    {
        x=myRand(-100,100);
        cout << "? "<<fixed << setprecision(6) << x <<endl;
        cin>>fx;

        if (fx<fMin)
        {
            fMin=fx;
            xMin = x;
        }
        else
        {
            double P=exp((fMin-fx)/T);

            if (P>myRand(1,100))
            {
                fMin=fx;
                xMin=x;
            }
        }
    }

    cout << "! "<<fixed << setprecision(6)<<xMin<<endl;

    return 0;

} 

我试图找到局部最大值是

#include <bits/stdc++.h>

using namespace std;

double myRand(double fMin, double fMax)
{
    double f = (double)rand() / RAND_MAX;
    return fMin + f * (fMax - fMin);
}


int main()
{
    cout.flush();


    double x,fx,xMax;
    double fMax;
    int n;
    double a,b;
    cin>>n>>a>>b;

    double answer[n];




    for(int i=0; i<n; i++)
    {
        cout << "? "<<fixed << setprecision(6) << a+i/5 <<endl;
        cin>>fMax;

        for(double T = 1000; T>1; T*=.995)
        {
            x=myRand(a,b);


// i am avoiding to get the same local max twice
            while(i>0&&answer[i-1]==x)
                x=myRand(a,b);
            cout << "? "<<fixed << setprecision(6) << x <<endl;
            cin>>fx;
            if (fx>fMax)
            {
                fMax=fx;
                xMax = x;
            }
            else
            {
                double P=exp((fMax-fx)/T);

                if (P<myRand(0,1))
                {
                    fMax=fx;
                    xMax=x;
                }
            }
        }
        answer[i]=xMax;
    }
    cout << "!";
    for(int i=0; i<n; i++)
    {
        cout<<" "<<fixed << setprecision(6)<<answer[i];
    }

    return 0;

}

【问题讨论】:

标签: c++ mathematical-optimization heuristics simulated-annealing


【解决方案1】:
  1. 将算法放入函数中:

    double my_unknown_function(double x)
    {
      cout << "? " << fixed << setprecision(6) << x << endl;
      cin >> fx;
    
      return fx;
    }
    
    using function = double(double);
    
    double minimum(function func)
    {
      double x, fx, xMin;
    
      /* ... */
    
      for(double T = 1000; T>1; T*=.995)
      {
        x = myRand(-100,100);
        fx = func(x);
    
        /* ... */
      }
    
      return xMin;
    }
    

    这样就可以简单的得到多个局部最小值:

    std::vector<double> lm;
    for (int i(0); i < 100; ++i)
      lm.push_back(minimum(my_unknown_function));
    

    如 cmets 中所述,模拟退火是一种优化启发式方法。它不是详尽的搜索,而且它没有找到所有最小值

    无论如何,多次调用minimum 可以获得不同的结果,因为它是随机的。可以预期,只要有足够多的重启,任何本地搜索方法总有一天会为您提供实际的全局最小值。

  2. 不要为最大化任务重写算法:您可能会引入错误并且测试更加困难。

    只需与你的功能相反:

    double my_unknown_function(double x)
    {
      cout << "? " << fixed << setprecision(6) << x << endl;
      cin >> fx;
    
      return -fx;
    }
    

同时考虑:

【讨论】:

  • 我想补充一点,如果给出了 n 个最小值,那么将段切割为 n 个部分,然后按照您的描述在每个部分上应用该算法将为我们提供所需的最小值点。因此,例如,如果局部最小值的数量是 n,并且它们都在 a 和 b 之间,那么我们应该将段划分为 n 个部分,每个部分等于 (b-a)/n
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-09
  • 1970-01-01
  • 2017-04-02
  • 1970-01-01
  • 2014-05-15
  • 2016-05-18
相关资源
最近更新 更多