返回顶部

收藏

Powell’s method of minimizing user-supplied function in Python

更多
''' xMin,nCyc = powell(F,x,h=0.1,tol=1.0e-6)
    Powell's method of minimizing user-supplied function F(x).
    x    = starting point
    h   = initial search increment used in 'bracket'
    xMin = mimimum point
    nCyc = number of cycles
'''
from numpy import identity,array,dot,zeros,argmax
from goldSearch import *
from math import sqrt

def powell(F,x,h=0.1,tol=1.0e-6):

    def f(s): return F(x + s*v)    # F in direction of v

    n = len(x)                     # Number of design variables
    df = zeros(n)                  # Decreases of F stored here
    u = identity(n)                # Vectors v stored here by rows
    for j in range(30):            # Allow for 30 cycles:
        xOld = x.copy()            # Save starting point
        fOld = F(xOld)
      # First n line searches record decreases of F
        for i in range(n):
            v = u[i]
            a,b = bracket(f,0.0,h)
            s,fMin = search(f,a,b)
            df[i] = fOld - fMin
            fOld = fMin
            x = x + s*v
      # Last line search in the cycle    
        v = x - xOld
        a,b = bracket(f,0.0,h)
        s,fLast = search(f,a,b)
        x = x + s*v
      # Check for convergence
        if sqrt(dot(x-xOld,x-xOld)/n) < tol: return x,j+1
      # Identify biggest decrease & update search directions
        iMax = argmax(df)
        for i in range(iMax,n-1):
            u[i] = u[i+1]
        u[n-1] = v
    print "Powell did not converge"

标签:function,函数,python

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. Chen Zhidong 发表 2011-01-29 01:41:14 Python笔记——数字类型的几个函数
  2. colstuwjx 发表 2014-07-27 13:19:00 Python开发之几个内联函数的介绍
  3. 博主 发表 2016-10-26 05:32:33 Function variations in Python
  4. zhongsuan 发表 2018-08-03 03:53:27 python – 函数后
  5. 阿安 发表 2013-09-11 19:35:50 理解ECMAScript6中的箭头函数
  6. lerry 发表 2011-04-28 05:41:00 增加分页功能&python通用分页函数
  7. zhongsuan 发表 2018-08-03 03:29:17 python-函数2
  8. 博主 发表 2015-07-10 01:00:00 Python的function和unbound/bound method
  9. 博主 发表 2016-03-21 10:56:26 PyBooks:免费编程书籍《Python函数式编程》
  10. 博主 发表 2012-02-11 03:52:00 python 列表解析 匿名函数 对象概念
  11. zhu329599788@126 发表 2016-10-04 03:13:39 JS自执行函数 (function(x,y){return x+y;})(3,4)
  12. 博主 发表 2013-03-22 00:00:00 Python id() 函数