# 基于改进海洋捕食者算法求解单目标优化问题附matlab代码(NMPA)

+关注继续查看

✅作者简介：热爱科研的Matlab仿真开发者，修心和技术同步精进，matlab项目合作可私信。

🍎个人主页：Matlab科研工作室

🍊个人信条：格物致知。

## ⛄ 部分代码

%_________________________________________________________________________

%  Marine Predators Algorithm source code (Developed in MATLAB R2015a)

%

%  programming: Afshin Faramarzi & Seyedali Mirjalili

%

% paper:

%  A. Faramarzi, M. Heidarinejad, S. Mirjalili, A.H. Gandomi,

%  Marine Predators Algorithm: A Nature-inspired Metaheuristic

%  Expert Systems with Applications

%  DOI: doi.org/10.1016/j.eswa.2020.113377

%

%  E-mails: afaramar@hawk.iit.edu            (Afshin Faramarzi)

%           ali.mirjalili@laureate.edu.au    (Seyedali Mirjalili)

%           gandomi@uts.edu.au               (Amir H Gandomi)

%_________________________________________________________________________

function [Top_predator_fit,Top_predator_pos,Convergence_curve,T]=MPA(SearchAgents_no,Max_iter,lb,ub,dim,fobj)

tic;

Top_predator_pos=zeros(1,dim);

Top_predator_fit=inf;

Convergence_curve=zeros(1,Max_iter);

stepsize=zeros(SearchAgents_no,dim);

fitness=inf(SearchAgents_no,1);

Prey=initialization(SearchAgents_no,dim,ub,lb);

Xmin=repmat(ones(1,dim).*lb,SearchAgents_no,1);

Xmax=repmat(ones(1,dim).*ub,SearchAgents_no,1);

Iter=0;

P=0.5;

while Iter<Max_iter

%------------------- Detecting top predator -----------------

for i=1:size(Prey,1)

Flag4ub=Prey(i,:)>ub;

Flag4lb=Prey(i,:)<lb;

Prey(i,:)=(Prey(i,:).*(~(Flag4ub+Flag4lb)))+ub.*Flag4ub+lb.*Flag4lb;

fitness(i,1)=fobj(Prey(i,:));

if fitness(i,1)<Top_predator_fit

Top_predator_fit=fitness(i,1);

Top_predator_pos=Prey(i,:);

end

end

%------------------- Marine Memory saving -------------------

if Iter==0

fit_old=fitness;    Prey_old=Prey;

end

Inx=(fit_old<fitness);

Indx=repmat(Inx,1,dim);

Prey=Indx.*Prey_old+~Indx.*Prey;

fitness=Inx.*fit_old+~Inx.*fitness;

fit_old=fitness;    Prey_old=Prey;

%------------------------------------------------------------

Elite=repmat(Top_predator_pos,SearchAgents_no,1);  %(Eq. 10)

CF=(1-Iter/Max_iter)^(2*Iter/Max_iter);

RL=0.05*levy(SearchAgents_no,dim,1.5);   %Levy random number vector

RB=randn(SearchAgents_no,dim);          %Brownian random number vector

for i=1:size(Prey,1)

for j=1:size(Prey,2)

R=rand();

%------------------ Phase 1 (Eq.12) -------------------

if Iter<Max_iter/3

stepsize(i,j)=RB(i,j)*(Elite(i,j)-RB(i,j)*Prey(i,j));

Prey(i,j)=Prey(i,j)+P*R*stepsize(i,j);

%--------------- Phase 2 (Eqs. 13 & 14)----------------

elseif Iter>Max_iter/3 && Iter<2*Max_iter/3

if i>size(Prey,1)/2

stepsize(i,j)=RB(i,j)*(RB(i,j)*Elite(i,j)-Prey(i,j));

Prey(i,j)=Elite(i,j)+P*CF*stepsize(i,j);

else

stepsize(i,j)=RL(i,j)*(Elite(i,j)-RL(i,j)*Prey(i,j));

Prey(i,j)=Prey(i,j)+P*R*stepsize(i,j);

end

%----------------- Phase 3 (Eq. 15)-------------------

else

stepsize(i,j)=RL(i,j)*(RL(i,j)*Elite(i,j)-Prey(i,j));

Prey(i,j)=Elite(i,j)+P*CF*stepsize(i,j);

end

end

end

%------------------ Detecting top predator ------------------

for i=1:size(Prey,1)

Flag4ub=Prey(i,:)>ub;

Flag4lb=Prey(i,:)<lb;

Prey(i,:)=(Prey(i,:).*(~(Flag4ub+Flag4lb)))+ub.*Flag4ub+lb.*Flag4lb;

fitness(i,1)=fobj(Prey(i,:));

if fitness(i,1)<Top_predator_fit

Top_predator_fit=fitness(i,1);

Top_predator_pos=Prey(i,:);

end

end

%---------------------- Marine Memory saving ----------------

if Iter==0

fit_old=fitness;    Prey_old=Prey;

end

Inx=(fit_old<fitness);

Indx=repmat(Inx,1,dim);

Prey=Indx.*Prey_old+~Indx.*Prey;

fitness=Inx.*fit_old+~Inx.*fitness;

fit_old=fitness;    Prey_old=Prey;

%---------- Eddy formation and FADs?effect (Eq 16) -----------

Prey=Prey+CF*((Xmin+rand(SearchAgents_no,dim).*(Xmax-Xmin)).*U);

else

r=rand();  Rs=size(Prey,1);

Prey=Prey+stepsize;

end

Iter=Iter+1;

Convergence_curve(Iter)=Top_predator_fit;

end

T=toc;

## ⛄ 参考文献

[1]胡顺强,崔东文.基于海洋捕食者算法优化的长短期记忆记忆网络趋势预测[J].中国农村水利水电,2021(02):78-82+90.

## ⛄ Matlab代码关注

##### ❤️ 关注我领取海量matlab电子书和数学建模资料

|
17天前
|

14 0
|
22天前
|

21 0
|
2月前
|

【XGBoost回归预测】基于鲸鱼算法WOA优化XGBoost实现数据回归预测附matlab代码
【XGBoost回归预测】基于鲸鱼算法WOA优化XGBoost实现数据回归预测附matlab代码
66 1
|
3月前
|

58 0
|
3月前
|

【地震】基于有限差分 (FDTD) 模拟地震超材料（晶体）时域分析附matlab代码
【地震】基于有限差分 (FDTD) 模拟地震超材料（晶体）时域分析附matlab代码
42 0
|
3月前
|

43 0
|
3月前
|

【微电网】基于麻雀算法的微电网优化调度附matlab代码
【微电网】基于麻雀算法的微电网优化调度附matlab代码
50 0
|
3月前
|

【BP回归预测】基于粒子群算法PSO优化BP神经网络实现预测多输入多输出附matlab代码
【BP回归预测】基于粒子群算法PSO优化BP神经网络实现预测多输入多输出附matlab代码
32 0
|
3月前
|

【高强度聚焦超声模拟器】模拟分层介质中的高强度聚焦超声波束和加热效应（Matlab代码）
【高强度聚焦超声模拟器】模拟分层介质中的高强度聚焦超声波束和加热效应（Matlab代码）
22 1
|
3月前
|

40 0