鲸鱼优化算法matlab实现(附完整源码)

网上看了很多关于鲸鱼优化算法的介绍和案例,都没有完整的实现代码,所有这里直接根据算法详细原理,手敲实现了MATLAB代码,CEC测试函数f10求解效果还不错(附完整可运行源码):

附源码:

clc
clear 
close all
maxgen=100;
popsize=100;
dim=2;
ub=32.*ones(1,dim);
lb=-32.*ones(1,dim);
x=zeros(popsize,dim);
y=zeros(popsize,1);
for i=1:popsize
    x(i,:)=lb+(ub-lb).*rand(1,dim);
    y(i)=fitness(x(i,:));
end
[val,idx]=sort(y);
y=y(idx);
x=x(idx,:);
bestx=x(1,:);
besty=y(1);
newx=x;
newf=y;
rec=zeros(maxgen,1);
for iter=1:maxgen
    for i=1:popsize
        if rand<0.5
            %螺旋攻击
            D=abs(bestx-x(i,:));
            a2=-1-iter/maxgen;
            l=rand*(a2-1)+1;
    %         l=2*(rand-0.5);
            b=1;
            newx(i,:)=D.*exp(b*l).*cos(2*pi*l)+bestx;
            newf(i)=fitness(newx(i,:));
        else
            a1=2-2*(iter)/(maxgen);
            r1=rand;
            r2=rand;
            A=2*a1*r1;
            C=2.*r2;
            if A<1
                %包围捕食
%                 C=2.*r2;
                D=abs(C.*bestx-x(i,:));
                newx(i,:)=bestx-A.*D;
                newf(i)=fitness(newx(i,:));
            else
                %随机搜索
                idx=randperm(popsize,1);
                while idx==i
                    idx=randperm(popsize,1);
                end
                D=abs(C.*x(idx,:)-x(i,:));
                newx(i,:)=x(idx,:)-A.*D;
                newf(i)=fitness(newx(i,:));
            end
        end
    end
    tempy=[y;newf];
    tempx=[x;newx];
    [val,idx]=sort(tempy);
    x=tempx(idx(1:popsize),:);
    y=tempy(idx(1:popsize),:);
    bestx=x(1,:);
    besty=y(1);
    rec(iter)=besty;
end
bestx
besty
figure(1)
[x0,y0]=meshgrid(-32:0.1:32);
z0=-20.*exp(-0.2.*sqrt((x0.^2+y0.^2)./2))-exp((cos(2.*pi.*x0)+cos(2.*pi.*y0))/2)+20+exp(1);
mesh(x0,y0,z0);
title('f_1_0')
xlabel('x');
ylabel('y');
zlabel('z');

figure(2)
plot(1:length(rec),rec,'r-');
xlabel('iter');
ylabel('fit');
title('收敛曲线');



function [f]=fitness(x)
f=-20.*exp(-0.2*sqrt(sum(x.^2)/length(x)))-exp(sum(cos(2.*pi*x))/length(x))+20+exp(1);
end

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱拼就为银

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值