✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信。
🍎个人主页:Matlab科研工作室
🍊个人信条:格物致知。
更多Matlab仿真内容点击👇
⛄ 内容介绍
针对当前火力分配(WTA)的难题,论文提出了一种基于粒子群优化算法(PSO)和萤火虫算法火力分配优化方法.介绍了算法的具体实现步骤,并通过在计算机上进行MATLAB仿真实验,验证了此方法的可行性和科学性,是一种有益的尝试与探索,对现代战争中指挥决策和理论研究有一定的参考价值.
⛄ 部分代码
%
clc;
clear;
close all;
%% Problem Definition
model=CreateModel();
CostFunction=@(s) MyCost(s, model); % Cost Function
nVar=model.m; % Number of Decision Variables
VarSize=[1 nVar]; % Size of Decision Variables Matrix
VarMin=0; % Lower Bound of Variables
VarMax=1; % Upper Bound of Variables
%% PSO Parameters
MaxIt=1000; % Maximum Number of Iterations
nPop=80; % Population Size (Swarm Size)
% PSO Parameters
w=1; % Inertia Weight
wdamp=0.99; % Inertia Weight Damping Ratio
c1=1.5; % Personal Learning Coefficient
c2=2.0; % Global Learning Coefficient
% Velocity Limits
VelMax=0.1*(VarMax-VarMin);
VelMin=-VelMax;
nParticleMutation = 1; % Number of Mutations Performed on Each Particle
nGlobalBestMutation = 3; % Number of Mutations Performed on Global Best
%% Initialization
empty_particle.Position=[];
empty_particle.Cost=[];
empty_particle.Sol=[];
empty_particle.Velocity=[];
empty_particle.Best.Position=[];
empty_particle.Best.Cost=[];
empty_particle.Best.Sol=[];
particle=repmat(empty_particle,nPop,1);
GlobalBest.Cost=inf;
for i=1:nPop
% Initialize Position
particle(i).Position=unifrnd(VarMin,VarMax,VarSize);
% Initialize Velocity
particle(i).Velocity=zeros(VarSize);
% Evaluation
[particle(i).Cost, particle(i).Sol]=CostFunction(particle(i).Position);
% Update Personal Best
particle(i).Best.Position=particle(i).Position;
particle(i).Best.Cost=particle(i).Cost;
particle(i).Best.Sol=particle(i).Sol;
% Update Global Best
if particle(i).Best.Cost<GlobalBest.Cost
GlobalBest=particle(i).Best;
end
end
BestCost=zeros(MaxIt,1);
%% PSO Main Loop
for it=1:MaxIt
for i=1:nPop
% Update Velocity
particle(i).Velocity = w*particle(i).Velocity ...
+c1*rand(VarSize).*(particle(i).Best.Position-particle(i).Position) ...
+c2*rand(VarSize).*(GlobalBest.Position-particle(i).Position);
% Apply Velocity Limits
particle(i).Velocity = max(particle(i).Velocity,VelMin);
particle(i).Velocity = min(particle(i).Velocity,VelMax);
% Update Position
particle(i).Position = particle(i).Position + particle(i).Velocity;
% Velocity Mirror Effect
IsOutside=(particle(i).Position<VarMin | particle(i).Position>VarMax);
particle(i).Velocity(IsOutside)=-particle(i).Velocity(IsOutside);
% Apply Position Limits
particle(i).Position = max(particle(i).Position,VarMin);
particle(i).Position = min(particle(i).Position,VarMax);
% Evaluation
[particle(i).Cost, particle(i).Sol] = CostFunction(particle(i).Position);
% Perform Mutation
for j=1:nParticleMutation
NewParticle = particle(i);
NewParticle.Position = Mutate(particle(i).Position);
[NewParticle.Cost, NewParticle.Sol] = CostFunction(NewParticle.Position);
if NewParticle.Cost <= particle(i).Cost
particle(i) = NewParticle;
end
end
% Update Personal Best
if particle(i).Cost<particle(i).Best.Cost
particle(i).Best.Position=particle(i).Position;
particle(i).Best.Cost=particle(i).Cost;
particle(i).Best.Sol=particle(i).Sol;
% Update Global Best
if particle(i).Best.Cost<GlobalBest.Cost
GlobalBest=particle(i).Best;
end
end
end
% Perform Mutation on Global Best
for i=1:nGlobalBestMutation
NewParticle = GlobalBest;
NewParticle.Position = Mutate(GlobalBest.Position);
[NewParticle.Cost, NewParticle.Sol] = CostFunction(NewParticle.Position);
if NewParticle.Cost <= GlobalBest.Cost
GlobalBest = NewParticle;
end
end
BestCost(it)=GlobalBest.Cost;
disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCost(it))]);
w=w*wdamp;
figure(1);
PlotSolution(GlobalBest.Position, model);
pause(0.01);
end
BestSol = GlobalBest;
%% Results
figure;
plot(BestCost,'LineWidth',2);
xlabel('迭代次数');
ylabel('最优值');
grid on;
⛄ 运行结果
⛄ 参考文献
[1] 王光源, 徐鹏飞, 赵勇. 基于粒子群优化算法求解火力分配问题[J]. 舰船电子工程, 2013, 33(11):34-36.
[2] 周洪斌, 吕强. 利用混合粒子群优化算法求解二次分配问题[J]. 计算机应用与软件, 2009, 26(11):3.
[3] 朱渊萍, 陈素芬. 萤火虫群优化算法在公差分配优化的应用[J]. 机械设计与制造, 2014(6):3.