|
對(duì)于一個(gè)多元函數(shù)f(x)=f(x1,x2,?,xn),用最速下降法(又稱梯度下降法)求其極小值的迭代格式為
xk+1=xk+αkdk
其中dk=?gk=??f(xk)為負(fù)梯度方向,即最速下降方向,αk為搜索步長(zhǎng)。
一般情況下,最優(yōu)步長(zhǎng)αk的確定要用到線性搜索技術(shù),比如精確線性搜索,但是更常用的是不精確線性搜索,主要是Goldstein不精確線性搜索和Wolfe法線性搜索。
為了調(diào)用的方便,編寫一個(gè)Python文件,里面存放線性搜索的子函數(shù),命名為linesearch.py,這里先只編寫了Goldstein線性搜索的函數(shù),關(guān)于Goldstein原則,可以參看最優(yōu)化課本。
線性搜索的代碼如下(使用版本為Python3.3):
'''
線性搜索子函數(shù)
'''
import numpy as np
import random
def goldsteinsearch(f,df,d,x,alpham,rho,t):
flag=0
a=0
b=alpham
fk=f(x)
gk=df(x)
phi0=fk
dphi0=np.dot(gk,d)
alpha=b*random.uniform(0,1)
while(flag==0):
newfk=f(x+alpha*d)
phi=newfk
if(phi-phi0<=rho*alpha*dphi0):
if(phi-phi0>=(1-rho)*alpha*dphi0):
flag=1
else:
a=alpha
b=b
if(b<alpham):
alpha=(a+b)/2
else:
alpha=t*alpha
else:
a=a
b=alpha
alpha=(a+b)/2
return alpha
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
上述函數(shù)的輸入?yún)?shù)主要包括一個(gè)多元函數(shù)f,其導(dǎo)數(shù)df,當(dāng)前迭代點(diǎn)x和當(dāng)前搜索方向d,返回值是根據(jù)Goldstein準(zhǔn)則確定的搜索步長(zhǎng)。
我們?nèi)砸訰osenbrock函數(shù)為例,即有
f(x)=100(x2?x21)2+(1?x1)2
于是可得函數(shù)的梯度為
g(x)=?f(x)=(?400(x2?x21)x1?2(1?x1),200(x2?x21))T
最速下降法的代碼如下:
"""
最速下降法
Rosenbrock函數(shù)
函數(shù) f(x)=100*(x(2)-x(1).^2).^2+(1-x(1)).^2
梯度 g(x)=(-400*(x(2)-x(1)^2)*x(1)-2*(1-x(1)),200*(x(2)-x(1)^2))^(T)
"""
import numpy as np
import matplotlib.pyplot as plt
import random
import linesearch
from linesearch import goldsteinsearch
def rosenbrock(x):
return 100*(x[1]-x[0]**2)**2+(1-x[0])**2
def jacobian(x):
return np.array([-400*x[0]*(x[1]-x[0]**2)-2*(1-x[0]),200*(x[1]-x[0]**2)])
X1=np.arange(-1.5,1.5+0.05,0.05)
X2=np.arange(-3.5,2+0.05,0.05)
[x1,x2]=np.meshgrid(X1,X2)
f=100*(x2-x1**2)**2+(1-x1)**2; # 給定的函數(shù)
plt.contour(x1,x2,f,20) # 畫出函數(shù)的20條輪廓線
def steepest(x0):
print('初始點(diǎn)為:')
print(x0,'\n')
imax = 20000
W=np.zeros((2,imax))
W[:,0] = x0
i = 1
x = x0
grad = jacobian(x)
delta = sum(grad**2) # 初始誤差
while i<imax and delta>10**(-5):
p = -jacobian(x)
x0=x
alpha = goldsteinsearch(rosenbrock,jacobian,p,x,1,0.1,2)
x = x + alpha*p
W[:,i] = x
grad = jacobian(x)
delta = sum(grad**2)
i=i+1
print("迭代次數(shù)為:",i)
print("近似最優(yōu)解為:")
print(x,'\n')
W=W[:,0:i] # 記錄迭代點(diǎn)
return W
x0 = np.array([-1.2,1])
W=steepest(x0)
plt.plot(W[0,:],W[1,:],'g*',W[0,:],W[1,:]) # 畫出迭代點(diǎn)收斂的軌跡
plt.show()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
為了實(shí)現(xiàn)不同文件中函數(shù)的調(diào)用,我們先用import函數(shù)導(dǎo)入了線性搜索的子函數(shù),也就是下面的2行代碼
import linesearch
from linesearch import goldsteinsearch
當(dāng)然,如果把定義goldsteinsearch函數(shù)的代碼直接放到程序里面,就不需要這么麻煩了,但是那樣的話,不僅會(huì)使程序顯得很長(zhǎng),而且不便于goldsteinsearch函數(shù)的重用。
此外,Python對(duì)函數(shù)式編程也支持的很好,在定義goldsteinsearch函數(shù)時(shí),可以允許抽象的函數(shù)f,df作為其輸入?yún)?shù),只要在調(diào)用時(shí)實(shí)例化就可以了。與Matlab不同的是,傳遞函數(shù)作為參數(shù)時(shí),Python是不需要使用@將其變?yōu)楹瘮?shù)句柄的。
運(yùn)行結(jié)果為
初始點(diǎn)為:
[-1.2 1. ]
迭代次數(shù)為: 1504
近似最優(yōu)解為:
[ 1.00318532 1.00639618]
迭代點(diǎn)的軌跡為

由于在線性搜索子程序中使用了隨機(jī)函數(shù),初始搜索點(diǎn)是隨機(jī)產(chǎn)生的,因此每次運(yùn)行的結(jié)果不太相同,比如再運(yùn)行一次程序,得到
初始點(diǎn)為:
[-1.2 1. ]
迭代次數(shù)為: 1994
近似最優(yōu)解為:
[ 0.99735222 0.99469882]
所得圖像為

|