jeudi 1 novembre 2018

How to find all local minimums of a function efficiently

This question is related to global optimization and it is simpler. The task is to find all local minimums of a function. This is useful sometimes, for example, in physics we might want to find metastable states besides the true ground state in phase space. I have a naive implementation which has been tested on a scalar function xsin(x)+xcos(2*x) by randomly searching points in the interval. But clearly this is not efficient. The code and output are attached if you are interested. [All local minimums of a function`

#!/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

I'd like to know if there exists better solution to this?




Aucun commentaire:

Enregistrer un commentaire