如何有效地找到函数的所有局部最小值

时间:2018-11-02 02:24:27

标签: random minimization

此问题与全局优化有关,并且更简单。任务是找到函数的所有局部最小值。有时这很有用,例如,在物理学中,我们可能希望找到相空间中真正的基态之外的亚稳态。我有一个幼稚的实现,该实现已通过在区间中随机搜索点而在标量函数x sin(x)+ x cos(2 * x)上进行了测试。但是显然这不是有效的。如果您有兴趣,请附上代码和输出。

#!/usr/bin/env python

from scipy import *
from numpy import *
from pylab import *
from numpy import random
"""
search all of the local minimums using random search  when the functional form of the target function is known.
"""
def function(x):
    return x*sin(x)+x*cos(2*x)
#   return x**4-3*x**3+2

def derivative(x):
    return sin(x)+x*cos(x)+cos(2*x)-2*x*sin(2*x)
#   return 4.*x**3-9.*x**2

def ploting(xr,yr,mls):
    plot(xr,yr)
    grid()
    for xm in mls:
        axvline(x=xm,c='r')
    savefig("plotf.png")
    show()

def findlocmin(x,Nit,step_def=0.1,err=0.0001,gamma=0.01):
    """
    we use gradient decent method to find local minumum using x as the starting point 
    """
    for i in range(Nit):
        slope=derivative(x)
        step=min(step_def,abs(slope)*gamma)
        x=x-step*slope/abs(slope)
#       print step,x
        if(abs(slope)<err):
           print "Found local minimum using "+str(i)+' iterations'
           break
        if i==Nit-1:
            raise Exception("local min is not found using Nit=",str(Nit),'iterations')
    return x

if __name__=="__main__":
    xleft=-9;xright=9
    xs=linspace(xleft,xright,100)
    ys=array([function(x) for x in xs ])
    minls=[]
    Nrand=100;it=0
    Nit=10000
    while it<Nrand:
          xint=random.uniform(xleft,xright)
          xlocm=findlocmin(xint,Nit)
          print xlocm
          minls.append(xlocm)
          it+=1
#   print minls 
    ploting(xs,ys,minls)`]

all local minimums of the tested function

我想知道是否存在更好的解决方案?

0 个答案:

没有答案