算法优化笔记|蝙蝠算法的理解及实现
蝙蝠算法(Bat Algorithm,BA )理解及實現
- 一、蝙蝠算法背景介紹
- 二、蝙蝠算法原理
- 三、蝙蝠模型構建
- 四、蝙蝠算法的Python實現
- 五、總結
一、蝙蝠算法背景介紹
蝙蝠算法是2010年楊教授基于群體智能提出的啟發式搜索算法,是一種搜索全局最優解的有效方法。
該算法基于迭代優化,初始化為一組隨機解,然后迭代搜尋最優解,且在最優解周圍通過隨機飛行產生局部新解,加強局部搜索速度。
該算法具有實現簡單、參數少等特點。
二、蝙蝠算法原理
將種群中的蝙蝠個體映射為D維問題空間中的NP個可行解,將優化過程和搜索模擬成種群蝙蝠個體移動過程和搜尋獵物,利用求解問題的適應度函數值來衡量蝙蝠所處位置的優劣,將個體的優勝劣汰過程類比為優化和搜索過程中用好的可行解替代較差可行解的迭代過程。
在蝙蝠的隨機搜索過程中,為了更方便的模擬蝙蝠探測獵物、避免障礙物,需假設如下三個近似的或理想化的規則:
(1)所有蝙蝠都采用回聲定位感知距離;
(2)蝙蝠在位置xi以速度vi隨機飛行,具有固定的頻率fmin,同時根據自身與獵物的距離,自動調整波長和脈沖響度;
(3)假設脈沖響度的變化方式為從一個最大值A0 整數變化到固定最小值Amin,變化區間根據問題調整。
三、蝙蝠模型構建
算法描述:
每個虛擬蝙蝠以隨機的速度Vi在位置Xi(問題的解)飛行,同時蝙蝠具有不同的波長、響度Ai和脈沖發射率r。蝙蝠狩獵和發現獵物時,它改變頻率、響度和脈沖發射率,進行最佳解的選擇,直到目標停止或條件得到滿足。這本質上就是使用調諧技術來控制蝙蝠群的動態行為,平衡調整算法相關的參數,以取得蝙蝠算法的最優。通過對多個標準測試函數的測試,展現了在連續性優化問題中的較好應用。
模型構建
算法流程
四、蝙蝠算法的Python實現
#!/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)運行結果顯示:
Matlab實現代碼
五、總結
蝙蝠算法與遺傳算法、粒子群算法相比,收斂速度更快,訓練神經網絡時也更有優勢。已用于工程設計、分類等應用。
蝙蝠算法的性能主要包括:局部搜索、局部最優解、收斂速度、最優解和適應度值。
參考:
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
總結
以上是生活随笔為你收集整理的算法优化笔记|蝙蝠算法的理解及实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux 系统应用编程——进程间通信(
- 下一篇: CorelDraw x6【Cdr x6】