MATLAB 數學建模: 人工魚群算法
1. 基本原理
人工魚群算法是一種受魚群聚集規律而啟發的優化算法. 在人工魚群算法中, 我們假定魚群的活動行為分為: 覓食行為, 群聚行為, 追隨行為和隨機行為.
-
覓食行為, 基于 “魚傾向于游向食物最多的水域” 這一假設, 等價于在尋找最優解的過程中, 向相對較優的方向行進的迭代原則.
-
群聚行為, 借鑒了真實魚群中, 落單的個體總傾向于回到群體的特性. 這一行為受三條子規則所限定:
- 分隔規則: 避免和臨近伙伴之間過于擁擠
- 對準規則: 確保和臨近伙伴的方向一致.
- 內聚規則: 盡量向臨近伙伴的中心移動.
-
追尾行為, 確保了每一個人工魚個體均會追逐臨近的最活躍個體. 它等價于在優化過程中, 向位于當前點附近的, 極優化點前進的過程.
-
隨機行為, 保證了人工魚在 “目力所及范圍內” 隨機移動. 這樣有助于在尋找最優解過程中跳出局部最優.
2. 程序設計
我們規定以下變量和符號:
- n 目標空間維度
- N 人工魚數量
- X 每條人工魚狀態 X=(x1,x2,?,xn)X = (x_1, x_2, \cdots, x_n)X=(x1?,x2?,?,xn?), 各分量為需要尋找最優的變量
- Y Y=f(X)Y = f(X)Y=f(X), 人工魚所在位置的食物濃度, 即尋優過程中的適應度.
- d 人工魚個體間距離, d=∣∣xi?xj∣∣d = ||x_i - x_j||d=∣∣xi??xj?∣∣
- v 人工魚感知范圍
- s 人工魚移動步長
- δ 擁擠度因子, 反映擁擠程度
- t 人工魚每次覓食最大嘗試次數
2.1 覓食行為
覓食行為, 指人工魚總是傾向于沿食物較多的方向游動的行為. 邏輯如下:
對于每一條人工魚 XiX_iXi?, 它按照規則
Xj=Xi+rand()?vX_j = X_i + rand()\cdot vXj?=Xi?+rand()?v
隨機選定一個新狀態 XjX_jXj?, 并比照新舊兩個狀態的適應度.
若 XjX_jXj? 適應度高于 XiX_iXi?, 則該魚按照規則
Xi′=Xi+rand()?s?Xj?Xi∣∣Xj?Xi∣∣X_i' = X_i + rand()\cdot s\cdot \frac{X_j - X_i}{||X_j - X_i||}Xi′?=Xi?+rand()?s?∣∣Xj??Xi?∣∣Xj??Xi??
向 XjX_jXj? 方向前進一個步長.
若反復判斷 ttt 次后仍然無法在附近找到優于現有狀態 XiX_iXi? 的新狀態, 人工魚將依據規則
Xi′=Xi+rand()?sX_i' = X_i + rand()\cdot sXi′?=Xi?+rand()?s
隨機移動一個步長.
2.2 群聚行為
真實世界中的魚群在游弋過程中為了躲避敵害會自然地群聚. 邏輯如下:
對于每一條人工魚 XiX_iXi?, 搜索其視野內
dij?vd_{ij}\leqslant vdij??v
的伙伴并統計其數量, 記為 nfn_fnf?, 并且確定其周邊伙伴的中間位置 XcX_cXc?.判斷伙伴所處的中心位置狀態 Ycnf\frac{Y_c}{n_f}nf?Yc??. 若狀態較優且不太擁擠,即
Ycnf<δYi\frac{Y_c}{n_f}<\delta Y_inf?Yc??<δYi?
則向該中心位置移動一個步長, 否則將執行覓食行為.
function [x_swarm, x_swarm_fitness] = swarm(x,i,N,v,f,delta,t,d,ub,lb,s)
%% Compute fitness rate under the circunstance of SWARMINGnf_swarm = 0;Xc = 0;label_swarm = 0; % The sign of swarming happened or not% Fix the Position of the center, compute the number of companionsfor j = 1:Nif norm(x(j,:) - x(i,:)) < vnf_swarm = nf_swarm + 1; % COunt the number of companionsXc = Xc + x(j,:); % Add up the number of SakanasendendXc = Xc - x(i,:); % Delete myselfnf_swarm = nf_swarm - 1;Xc = Xc / nf_swarm;% Judge the center is narrow or notif (f(Xc)/nf_swarm < delta*f(x(i,:))) && (f(Xc) < f(x(i,:)))x_swarm = x(i,:) + rand*s.*(Xc - x(i,:)) ./ norm(Xc - x(i,:));%Bound processingub_flag = x_swarm > ub;lb_flag = x_swarm < lb;x_swarm = (x_swarm .* (~(ub_flag + lb_flag))) + ub .* ub_flag + lb .* lb_flag;x_swarm_fitness = f(x_swarm);else% PREYINGlabel_prey = 0; % A sign of whether preying will find a better state than current statefor j = 1:t%Find a state randomlyx_prey_rand = x(i,:) + v .* (-1 + 2 .* rand(1,d));ub_flag2 = x_prey_rand > ub;lb_flag2 = x_prey_rand < lb;x_prey_rand = (x_prey_rand .* (~(ub_flag2 + lb_flag2))) + ub .* ub_flag2 + lb .* lb_flag2;%judgement: better, or not?if f(x(i,:)) > f(x_prey_rand)x_swarm = x(i,:) + rand*s .* (x_prey_rand - x(i,:)) ./ norm(x_prey_rand - x(i,:));ub_flag2 = x_swarm > ub;lb_flag2 = x_swarm < lb;x_swarm = (x_swarm .* (~(ub_flag2 + lb_flag2))) + ub .* ub_flag2 + lb .* lb_flag2;x_swarm_fitness = f(x_swarm);label_prey = 1;break;endend% Acting randomlyif label_prey == 0x_swarm = x(i,:) + s * (-1 + 2*rand(1,d));ub_flag2 = x_swarm > ub;lb_flag2 = x_swarm < lb;x_swarm = (x_swarm .* (~(ub_flag2 + lb_flag2))) + ub .* ub_flag2 + lb .* lb_flag2;x_swarm_fitness = f(x_swarm);endend
end
2.3 追尾行為
執行追尾過程時, 人工魚將向其視野內適應度最高的個體移動. 邏輯如下:
XiX_iXi? 搜索其視野內適應度最高的個體, 記為 XjX_jXj?, 同時對目標個體 XjX_jXj? 視野內所有個體的數量計數 nfn_fnf?.判斷目標個體位置狀態 Yjnf\frac{Y_j}{n_f}nf?Yj??. 若該位置狀態較優且不太擁擠, 則 XiX_iXi? 向目標 XjX_jXj? 移動一個步長, 否則執行覓食行為.
function [x_follow, x_follow_fitness] = follow(x,i,N,v,f,delta,t,d,ub,lb,s)fitness_follow = inf;label_follow = 0; % A Sign marking following has happened or not% Search the *best* individual in sightfor j = 1:Nif (norm(x(j,:) - x(i,:)) < v) && (f(x(j,:)) < fitness_follow)best_pos = x(j,:);fitness_follow = f(x(j,:));endend%Search The Number Of Companions In Sightnf_follow = 0;for j = 1:Nif norm(x(j,:) - best_pos) < vnf_follow = nf_follow + 1;endendnf_follow = nf_follow - 1; % Delete myself% Judge: The Center Is Narrow or not?if (fitness_follow/nf_follow)<delta * f(x(i,:)) && (fitness_follow < f(x(i,:)))x_follow = x(i,:) + rand *s .* (best_pos - x(i,:)) ./ norm(best_pos - x(i,:));%Boundry Processingub_flag2 = x_follow > ub;lb_flag2 = x_follow < lb;x_follow = (x_follow .* (~(ub_flag2 + lb_flag2))) + ub .* ub_flag2 + lb .* lb_flag2;label_follow = 1;x_follow_fitness = f(x_follow);else% PREYINGlabel_prey = 0; % A sign of whether preying will find a better state than current statefor j = 1:t%Find a state randomlyx_prey_rand = x(i,:) + v .* (-1 + 2 .* rand(1,d));ub_flag2 = x_prey_rand > ub;lb_flag2 = x_prey_rand < lb;x_prey_rand = (x_prey_rand .* (~(ub_flag2 + lb_flag2))) + ub .* ub_flag2 + lb .* lb_flag2;%judgement: better, or not?if f(x(i,:)) > f(x_prey_rand)x_follow = x(i,:) + rand*s .* (x_prey_rand - x(i,:)) ./ norm(x_prey_rand - x(i,:));ub_flag2 = x_follow > ub;lb_flag2 = x_follow < lb;x_follow = (x_follow .* (~(ub_flag2 + lb_flag2))) + ub .* ub_flag2 + lb .* lb_flag2;x_follow_fitness = f(x_follow);label_prey = 1;break;endend%random behaviourif label_prey == 0x_follow = x(i,:) + s .* (-1 + 2*rand(1,d));ub_flag2 = x_follow > ub;lb_flag2 = x_follow < lb;x_follow = (x_follow .* (~(ub_flag2 + lb_flag2))) + ub .* ub_flag2 + lb .* lb_flag2;x_follow_fitness = f(x_follow);end
end
2.4 隨機行為
在上文所提到的 “覓食行為” 的最后一種判斷情況中, 人工魚將會執行的行為就是隨機行為.
3. 代碼實現
附主函數代碼如下:
clc; clear all;%% Define variables
v = 25; %Perception Distance
s = 3; %Maximum Step Size
N = 30; %The Number Of Artificial Sakana
d = 10; %Current Dimension
t = 50; %Maximum Iteration Time
delta = 27; %Factor of Congestion%% Construct a function for testing
f = @(x) sum(3*x.^4);
ub = 100; %Upper Limit Of Boundrary
lb = -100; %Lower Limit Of Boundrarydf = []; %Store Target Function value of 50 states
Iteration = 1; %Initialize Iteration Rate
Max_iteration = 500; %Maximum Iteration Rate%% Initialize Artificial Sakana Groupx = lb + rand(N,d).*(ub - lb);%% Compute fitness rate of 10 initial state
for i = 1:Nfitness_sakana(i) = f(x(i,:));
end%% Define Best Fitness of Initial State
[best_fitness,I] = min(fitness_sakana);
best_x = x(I); %Best Artificial Sakana At Initial Statewhile Iteration <= Max_iterationfor i = 1:N%%Swarm[x_swarm, x_swarm_fitness] = swarm(x,i,N,v,f,delta,t,d,ub,lb,s);%%Follow[x_follow, x_follow_fitness] = follow(x,i,N,v,f,delta,t,d,ub,lb,s);%%Judgementif x_follow_fitness < x_swarm_fitnessx(i,:) = x_follow;elsex(i,:) = x_swarm;endend%% Update Information In Timefor i = 1:Nif (f(x(i,:)) < best_fitness)best_fitness = f(x(i,:));best_x = x(i,:);endendConvergence_curve(Iteration) = best_fitness;Iteration = Iteration + 1;%% Display Iteration Time And Current Best Fit Rateif mod(Iteration, 25) == 0display(['Iteration times: ',num2str(Iteration),'BestFitRate: ',num2str(best_fitness)]);display(['BestArtificialSakana: ', num2str(best_x)]);end
end%% Print
figure('Position', [284 214 660 290])
subplot(1,2,1);
x = -100:1:100;
y = x;
L = length(x);
for i = 1:Lfor j = 1:LF(i,j) = (3*x(i) .^4) + (3*y(j) .^4);end
end
surfc(x,y,F,'LineStyle', 'none');
title('Test Function')
xlabel('x_1');
ylabel('y_1');
zlabel(['sum','(x_1,x_2)'])
grid onsubplot(1,2,2);
semilogy(Convergence_curve, 'Color','r')
title('Convergence Curve')
xlabel('Iteration');
ylabel('Best Fitness');
axis tight
grid on
box on
總結
以上是生活随笔為你收集整理的MATLAB数学建模:智能优化算法-人工鱼群算法的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。