問(wèn)題
重點(diǎn)思路
爬山算法會(huì)收斂到局部最優(yōu),解決辦法是初始值在定義域上隨機(jī)取亂數(shù)100次,總不可能100次都那么倒霉。
實(shí)現(xiàn)
import numpy as np import matplotlib.pyplot as plt import math # 搜索步長(zhǎng) DELTA = 0.01 # 定義域x從5到8閉區(qū)間 BOUND = [5,8] # 隨機(jī)取亂數(shù)100次 GENERATION = 100 def F(x): return math.sin(x*x)+2.0*math.cos(2.0*x) def hillClimbing(x): while F(x+DELTA)>F(x) and x+DELTA<=BOUND[1] and x+DELTA>=BOUND[0]: x = x+DELTA while F(x-DELTA)>F(x) and x-DELTA<=BOUND[1] and x-DELTA>=BOUND[0]: x = x-DELTA return x,F(x) def findMax(): highest = [0,-1000] for i in range(GENERATION): x = np.random.rand()*(BOUND[1]-BOUND[0])+BOUND[0] currentValue = hillClimbing(x) print('current value is :',currentValue) if currentValue[1] > highest[1]: highest[:] = currentValue return highest [x,y] = findMax() print('highest point is x :{},y:{}'.format(x,y))
當(dāng)前名稱(chēng):python實(shí)現(xiàn)爬山算法的思路詳解-創(chuàng)新互聯(lián)
文章起源:http://chinadenli.net/article6/epdog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、標(biāo)簽優(yōu)化、網(wǎng)站營(yíng)銷(xiāo)、網(wǎng)站制作、虛擬主機(jī)、動(dòng)態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容