算法优化笔记|蝙蝠算法的理解及实现
蝙蝠算法(Bat Algorithm,BA )理解及實(shí)現(xiàn)
- 一、蝙蝠算法背景介紹
- 二、蝙蝠算法原理
- 三、蝙蝠模型構(gòu)建
- 四、蝙蝠算法的Python實(shí)現(xiàn)
- 五、總結(jié)
一、蝙蝠算法背景介紹
蝙蝠算法是2010年楊教授基于群體智能提出的啟發(fā)式搜索算法,是一種搜索全局最優(yōu)解的有效方法。
該算法基于迭代優(yōu)化,初始化為一組隨機(jī)解,然后迭代搜尋最優(yōu)解,且在最優(yōu)解周圍通過隨機(jī)飛行產(chǎn)生局部新解,加強(qiáng)局部搜索速度。
該算法具有實(shí)現(xiàn)簡(jiǎn)單、參數(shù)少等特點(diǎn)。
二、蝙蝠算法原理
將種群中的蝙蝠個(gè)體映射為D維問題空間中的NP個(gè)可行解,將優(yōu)化過程和搜索模擬成種群蝙蝠個(gè)體移動(dòng)過程和搜尋獵物,利用求解問題的適應(yīng)度函數(shù)值來衡量蝙蝠所處位置的優(yōu)劣,將個(gè)體的優(yōu)勝劣汰過程類比為優(yōu)化和搜索過程中用好的可行解替代較差可行解的迭代過程。
在蝙蝠的隨機(jī)搜索過程中,為了更方便的模擬蝙蝠探測(cè)獵物、避免障礙物,需假設(shè)如下三個(gè)近似的或理想化的規(guī)則:
(1)所有蝙蝠都采用回聲定位感知距離;
(2)蝙蝠在位置xi以速度vi隨機(jī)飛行,具有固定的頻率fmin,同時(shí)根據(jù)自身與獵物的距離,自動(dòng)調(diào)整波長(zhǎng)和脈沖響度;
(3)假設(shè)脈沖響度的變化方式為從一個(gè)最大值A(chǔ)0 整數(shù)變化到固定最小值A(chǔ)min,變化區(qū)間根據(jù)問題調(diào)整。
三、蝙蝠模型構(gòu)建
算法描述:
每個(gè)虛擬蝙蝠以隨機(jī)的速度Vi在位置Xi(問題的解)飛行,同時(shí)蝙蝠具有不同的波長(zhǎng)、響度Ai和脈沖發(fā)射率r。蝙蝠狩獵和發(fā)現(xiàn)獵物時(shí),它改變頻率、響度和脈沖發(fā)射率,進(jìn)行最佳解的選擇,直到目標(biāo)停止或條件得到滿足。這本質(zhì)上就是使用調(diào)諧技術(shù)來控制蝙蝠群的動(dòng)態(tài)行為,平衡調(diào)整算法相關(guān)的參數(shù),以取得蝙蝠算法的最優(yōu)。通過對(duì)多個(gè)標(biāo)準(zhǔn)測(cè)試函數(shù)的測(cè)試,展現(xiàn)了在連續(xù)性優(yōu)化問題中的較好應(yīng)用。
模型構(gòu)建
算法流程
四、蝙蝠算法的Python實(shí)現(xiàn)
#!/usr/bin/env python3"""An implementation of Bat Algorithm """import numpy as np from numpy.random import random as rand# Parameters setting # objfun: objective function # N_pop: population size, typically 10 to 40 # N_gen: number of generation # A: loudness (constant or decreasing) # r: pulse rate (constant or decreasing) # This frequency range determines the scalings # You should change these values if necessary # Qmin: frequency minmum # Qmax: frequency maxmum # d: number of dimensions # lower: lower bound # upper: upper bound def bat_algorithm(objfun, N_pop=20, N_gen=1000, A=0.5, r=0.5,Qmin=0, Qmax=2, d=10, lower=-2, upper=2):N_iter = 0 # Total number of function evaluations# Limit boundsLower_bound = lower * np.ones((1,d))Upper_bound = upper * np.ones((1,d))Q = np.zeros((N_pop, 1)) # Frequencyv = np.zeros((N_pop, d)) # VelocitiesS = np.zeros((N_pop, d))# Initialize the population/soutions# Sol = np.random.uniform(Lower_bound, Upper_bound, (N_pop, d))# Fitness = objfun(Sol)Sol = np.zeros((N_pop, d))Fitness = np.zeros((N_pop, 1))for i in range(N_pop):Sol[i] = np.random.uniform(Lower_bound, Upper_bound, (1, d))Fitness[i] = objfun(Sol[i])# Find the initial best solutionfmin = min(Fitness)Index = list(Fitness).index(fmin)best = Sol[Index]# Start the iterationsfor t in range(N_gen):# Loop over all bats/solutionsfor i in range(N_pop):# Q[i] = Qmin + (Qmin - Qmax) * np.random.randQ[i] = np.random.uniform(Qmin, Qmax)v[i] = v[i] + (Sol[i] - best) * Q[i]S[i] = Sol[i] + v[i]# Apply simple bounds/limitsSol[i] = simplebounds(Sol[i], Lower_bound, Upper_bound)# Pulse rateif rand() > r:# The factor 0.001 limits the step sizes of random walksS[i] = best + 0.001*np.random.randn(1, d)# Evaluate new solutions# print(i)Fnew = objfun(S[i])# Update if the solution improves, or not too loudif (Fnew <= Fitness[i]) and (rand() < A):Sol[i] = S[i]Fitness[i] = Fnew# update the current best solutionif Fnew <= fmin:best = S[i]fmin = FnewN_iter = N_iter + N_popprint('Number of evaluations: ', N_iter)print("Best = ", best, '\n fmin = ', fmin)return bestdef simplebounds(s, Lower_bound, Upper_bound):Index = s > Lower_bounds = Index * s + ~Index * Lower_boundIndex = s < Upper_bounds = Index * s + ~Index * Upper_boundreturn s# u: array-like def test_function(u):a = u ** 2return a.sum(axis=0)if __name__ == '__main__':# print(bat_algorithm(test_function))bat_algorithm(test_function)運(yùn)行結(jié)果顯示:
Matlab實(shí)現(xiàn)代碼
五、總結(jié)
蝙蝠算法與遺傳算法、粒子群算法相比,收斂速度更快,訓(xùn)練神經(jīng)網(wǎng)絡(luò)時(shí)也更有優(yōu)勢(shì)。已用于工程設(shè)計(jì)、分類等應(yīng)用。
蝙蝠算法的性能主要包括:局部搜索、局部最優(yōu)解、收斂速度、最優(yōu)解和適應(yīng)度值。
參考:
https://blog.csdn.net/u011835903/article/details/107937903
https://www.cnblogs.com/caoer/p/12641369.html
https://www.omegaxyz.com/2019/02/12/ba-matlab/
https://blog.csdn.net/qq_40456829/article/details/92775377
總結(jié)
以上是生活随笔為你收集整理的算法优化笔记|蝙蝠算法的理解及实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux 系统应用编程——进程间通信(
- 下一篇: CorelDraw x6【Cdr x6】