建模可能用到的算法

狼群算法

%% 清空环境
clc
clear all
close all
%% 数据初始化
 
%下载数据
load  HeightData
% HeightData=HeightData';
%网格划分
LevelGrid=10;
PortGrid=21;
 
%起点终点网格点
starty=6;starth=1;
endy=8;endh=21;
m=1;
%算法参数
PopNumber=10;         %种群个数
BestFitness=[];    %最佳个体
iter=100;
%初始信息素
pheromone=ones(21,21,21);
dim=PortGrid*2;
Max_iter=100;
ub=PortGrid;
lb=1;
% initialize alpha, beta, and delta_pos
Alpha_pos=zeros(1,dim);
Alpha_score=inf; %change this to -inf for maximization problems
Beta_pos=zeros(1,dim);
Beta_score=inf; %change this to -inf for maximization problems
Delta_pos=zeros(1,dim);
Delta_score=inf; %change this to -inf for maximization problems
%Initialize the path of search agents
Convergence_curve=zeros(1,Max_iter);
l=0;% Loop counter
%% 初始搜索路径
[path,pheromone]=searchpath(PopNumber,LevelGrid,PortGrid,pheromone, ...
    HeightData,starty,starth,endy,endh);
fitness=CacuFit(path);                          %适应度计算
[bestfitness,bestindex]=min(fitness);           %最佳适应度
bestpath=path(bestindex,:);                     %最佳路径
BestFitness=[BestFitness;bestfitness];          %适应度值记录
 
l=0;% Loop counter
 
% Main loop
while l<Max_iter
    for i=1:size(path,1)
        
        % Return back the search agents that go beyond the boundaries of the search space
        Flag4ub=path(i,:)>ub;
        Flag4lb=path(i,:)<lb;
        path(i,:)=round((path(i,:).*(~(Flag4ub+Flag4lb)))+ub.*Flag4ub+lb.*Flag4lb);
        
        % Calculate objective function for each search agent
        fitness=CacuFit(path(i,:));
        
        % Update Alpha, Beta, and Delta
        if fitness<Alpha_score
            Alpha_score=fitness; % Update alpha
            Alpha_pos=path(i,:);
        end
        
        if fitness>Alpha_score && fitness<Beta_score
            Beta_score=fitness; % Update beta
            Beta_pos=path(i,:);
        end
        
end

案例

粒子群算法

求函数 [公式] 的最小值

求函数 [公式] 的最小值,其中x的取值范围是[-4,4],y的取值范围是[-4,4]。这是一个有多个局部极值的函数。函数图形如图:

clear all;
close all;
clc;
x=-4:0.02:4;
y=-4:0.02:4;
N=size(x,2);
for i=1:N
    for j=1:N
        z(i,j)=3*cos(x(i)*y(j))+x(i)+y(j)*y(j);
    end
end
mesh(x,y,z)
xlabel('x')
ylabel('y')

在这里插入图片描述

clear all;
close all;
clc;
N=100;                         %群体粒子个数
D=2;                           %粒子维数
T=200;                         %最大迭代次数
c1=1.5;                        %学习因子1
c2=1.5;                        %学习因子2
Wmax=0.8;                      %惯性权重最大值
Wmin=0.4;                      %惯性权重最小值
Xmax=4;                        %位置最大值
Xmin=-4;                       %位置最小值
Vmax=1;                        %速度最大值
Vmin=-1;                       %速度最小值
%初始化个体
x=rand(N,D)*(Xmax-Xmin)+Xmin;
v=rand(N,D)*(Vmax-Vmin)+Vmin;
%初始化个体最优位置和最优值
p=x;
pbest=ones(1,D);
for i=1:N
    pbest(i)=func2(x(i,:));
end
%初始化全局最优位置和最优值
g=ones(1,D);
gbest=inf;
for i=1:N
    if (pbest(i)<gbest)
        g=p(i,:);
        gbest=pbest(i);
    end
end
gb=ones(1,T);
%按照公式依次迭代直到满足精度或者迭代次数
for i=1:T
    for j=1:N
        %更新个体最优位置和最优值
        if (func2(x(j,:))<pbest(j))
            p(j,:)=x(j,:);
            pbest(j)=func2(x(j,:));
        end
        %更新全局最优位置和最优值
        if (pbest(j)<gbest)
            g=p(j,:);
            gbest=pbest(j);
        end
        %计算动态惯性权重值
        w=Wmax-(Wmax-Wmin)*i/T;
        %更新位置和速度
        v(j,:)=w*v(j,:)+c1*rand*(p(j,:)-x(j,:))+c2*rand*(g-x(j,:));
        x(j,:)=x(j,:)+v(j,:);
        %边界条件处理
        for ii=1:D
            if (v(j,ii)<Vmin)||(v(j,ii)>Vmax)
               v(j,ii)=rand*(Vmax-Vmin)+Vmin;
            end
            if (x(j,ii)<Xmin)||(x(j,ii)>Xmax)
                x(j,ii)=rand*(Xmax-Xmin)+Xmin;
            end
        end
    end
    gb(i)=gbest;
end
g;%最优个体
gb(end);%最优值
figure
plot(gb)
xlabel('迭代次数')
ylabel('适应度值')
title('适应度进化曲线')
%适应度函数
function value=func2(x)
value=3*cos(x(1)*x(2))+x(1)+x(2)^2;
end

0-1背包问题

有N件物品和容积为V的包,第i件物品的容积是c(i),价值是w(i),求将这些物品放入包中,使物体的总容积不超过背包容积,且总价值和最大。假设物品数量为10,背包容量为300.每件物品的体积为[95,75,23,73,50,22,6,57,89,98];价值为[89,59,19,43,100,72,44,16,7,64];

解:

clear all;
close all;
clc;
N=100;                          %群体粒子个数
D=10;                           %粒子维数
T=200;                          %最大迭代次数
c1=1.5;                         %学习因子1
c2=1.5;                         %学习因子2
Wmax=0.8;                       %惯性权重最大值
Wmin=0.4;                       %惯性权重最小值
Vmax=10;                        %速度最大值
Vmin=-10;                       %速度最小值
V=300;                          %背包容量
C=[95,75,23,73,50,22,6,57,89,98];            %物品体积
W=[89,59,19,43,100,72,44,16,7,64];           %物品价值
afa=2;                                       %惩罚函数系数
%初始化个体
x=randi([0,1],N,D);
v=rand(N,D)*(Vmax-Vmin)+Vmin;
%初始化个体最优位置和最优值
p=x;
pbest=ones(N,1);
for i=1:N
    pbest(i)=func4(x(i,:),C,W,V,afa);
end
%初始化全局最优位置和最优值
g=ones(1,D);
gbest=eps;
for i=1:N
    if (pbest(i)>gbest)
        g=p(i,:);
        gbest=pbest(i);
    end
end
gb=ones(1,T);
%按照公式依次迭代直到满足精度或者迭代次数
for i=1:T
    for j=1:N
         %更新个体最优位置和最优值
        if (func4(x(j,:),C,W,V,afa)>pbest(j))
            p(j,:)=x(j,:);
            pbest(j)=func4(x(j,:),C,W,V,afa);
        end
        %更新全局最优位置和最优值
        if (pbest(j)>gbest)
            g=p(j,:);
            gbest=pbest(j);
        end
         %计算动态惯性权重值
          w=Wmax-(Wmax-Wmin)*i/T;
          %更新位置和速度
          v(j,:)=w*v(j,:)+c1*rand*(p(j,:)-x(j,:))+c2*rand*(g-x(j,:));
     %边界条件处理
        for ii=1:D
             if (v(j,ii)<Vmin)||(v(j,ii)>Vmax)
             v(j,ii)=rand*(Vmax-Vmin)+Vmin;
             end
        end
        vx(j,:)=1./(1+exp(-v(j,:)));
        for jj=1:D
             if vx(j,jj)>rand
             x(j,jj)=1;
             else
             x(j,jj)=0;
             end
        end
    end
    gb(i)=gbest;
end
g;%最优个体
figure
plot(gb)
xlabel('迭代次数')
ylabel('适应度值')
title('适应度变化曲线')
%适应度函数
function result=func4(f,C,W,V,afa)
fit=sum(f.*W);
TotalSize=sum(f.*C);
if TotalSize<=V
    fit=fit;
else
    fit=fit-afa*(TotalSize-V);
end
result=fit;
end

Guess you like

Origin blog.csdn.net/weixin_51552144/article/details/119978995