[Python] Powell’s method of minimizing user-supplied functi →→→→→进入此内容的聊天室

来自 , 2020-08-05, 写在 Python, 查看 109 次.
URL http://www.code666.cn/view/4275f897
  1. ''' xMin,nCyc = powell(F,x,h=0.1,tol=1.0e-6)
  2.    Powell's method of minimizing user-supplied function F(x).
  3.    x    = starting point
  4.    h   = initial search increment used in 'bracket'
  5.    xMin = mimimum point
  6.    nCyc = number of cycles
  7. '''
  8. from numpy import identity,array,dot,zeros,argmax
  9. from goldSearch import *
  10. from math import sqrt
  11.  
  12. def powell(F,x,h=0.1,tol=1.0e-6):
  13.  
  14.     def f(s): return F(x + s*v)    # F in direction of v
  15.  
  16.     n = len(x)                     # Number of design variables
  17.     df = zeros(n)                  # Decreases of F stored here
  18.     u = identity(n)                # Vectors v stored here by rows
  19.     for j in range(30):            # Allow for 30 cycles:
  20.         xOld = x.copy()            # Save starting point
  21.         fOld = F(xOld)
  22.       # First n line searches record decreases of F
  23.         for i in range(n):
  24.             v = u[i]
  25.             a,b = bracket(f,0.0,h)
  26.             s,fMin = search(f,a,b)
  27.             df[i] = fOld - fMin
  28.             fOld = fMin
  29.             x = x + s*v
  30.       # Last line search in the cycle    
  31.         v = x - xOld
  32.         a,b = bracket(f,0.0,h)
  33.         s,fLast = search(f,a,b)
  34.         x = x + s*v
  35.       # Check for convergence
  36.         if sqrt(dot(x-xOld,x-xOld)/n) < tol: return x,j+1
  37.       # Identify biggest decrease & update search directions
  38.         iMax = argmax(df)
  39.         for i in range(iMax,n-1):
  40.             u[i] = u[i+1]
  41.         u[n-1] = v
  42.     print "Powell did not converge"
  43. #//python/7404

回复 "Powell’s method of minimizing user-supplied functi"

这儿你可以回复上面这条便签

captcha