问题描述:
用PSO优化桌子问题(weight=1*(length+width)),其中length,width为桌子的两个变量,在约束下weight最小即问题最优
文件目录:
PSO.m
OurTableDesignProblem.m
PSO.m
clear all
close all
clc
%Define the details of the table design problem
nVar = 2; %number of varibles
ub = [7 7];
lb = [2 2];
fobj = @OurTableDesignProblem;
%Define the PSO's parametres
noP = 5; %number of particles
maxIter = 10;
wMax = 0.9;
wMin = 0.2;
c1 = 2;
c2 = 2;
vMax = (ub - lb) .* 0.2;
vMin = -vMax;
%Initialize the particles
for k = 1 : noP
Swarm.Particles(k).X = (ub-lb) .* rand(1,nVar) + lb;
Swarm.Particles(k).V = zeros(1, nVar);
Swarm.Particles(k).PBEST.X = zeros(1, nVar);
Swarm.Particles(k).PBEST.O = inf;
Swarm.GBEST.X = zeros(1, nVar);
Swarm.GBEST.O = inf;
end
%Main loop
for t = 1 : maxIter
%Calculate the objective value
for k = 1 : noP
currentX = Swarm.Particles(k).X;
Swarm.Particles(k).O = fobj(currentX);
if Swarm.Particles(k).O < Swarm.Particles(k).PBEST.O %Update the PBEST
Swarm.Particles(k).PBEST.X = currentX;
Swarm.Particles(k).PBEST.O = Swarm.Particles(k).O;
end
if Swarm.Particles(k).O < Swarm.GBEST.O %Update the GBEST
Swarm.GBEST.X = currentX;
Swarm.GBEST.O = Swarm.Particles(k).O;
end
end
%Update the X and V vectors
w = wMax - t .* ((wMax - wMin) / maxIter);
for k = 1 : noP
Swarm.Particles(k).V = w .* Swarm.Particles(k).V + c1 .* rand(1,nVar) .* (Swarm.Particles(k).PBEST.X-Swarm.Particles(k).X)...
+ c2 .* rand(1,nVar) .* (Swarm.GBEST.X-Swarm.Particles(k).X);
index1 = find(Swarm.Particles(k).V > vMax); %Check velocities
index2 = find(Swarm.Particles(k).V < vMin);
Swarm.Particles(k).V(index1) = vMax(index1);
Swarm.Particles(k).V(index2) = vMin(index2);
Swarm.Particles(k).X = Swarm.Particles(k).X + Swarm.Particles(k).V;
index1 = find(Swarm.Particles(k).X > ub); %Check positions
index1 = find(Swarm.Particles(k).X < lb);
Swarm.Particles(k).X(index1) = ub(index1);
Swarm.Particles(k).X(index2) = lb(index2);
end
outmsg = ['Iteration#' , num2str(t) , 'Swarm.GBEST.O=' , num2str(Swarm.GBEST.O)];
disp(outmsg);
cgCurve(t) = Swarm.GBEST.O;
end
semilogy(cgCurve);
xlabel('Iteration#')
ylabel('Weight')
OurTableDesignProblem.m
function [ weight ] = OurTableDesignProblem ( x )
length = x(1);
width = x(2);
weight = 1 * (length + width);
end
结果:
Iteration#1Swarm.GBEST.O=7.6495
Iteration#2Swarm.GBEST.O=4
Iteration#3Swarm.GBEST.O=4
Iteration#4Swarm.GBEST.O=4
Iteration#5Swarm.GBEST.O=4
Iteration#6Swarm.GBEST.O=4
Iteration#7Swarm.GBEST.O=4
Iteration#8Swarm.GBEST.O=4
Iteration#9Swarm.GBEST.O=4
Iteration#10Swarm.GBEST.O=4
心得:
1. 把weight看成一个particle,length,width是其dimension;
2. 5 particles构成particle swarm,实际计算次数=nop×maxIter;
3. 权重线性递减,使搜索比固定值更加合理,更精确;
4.为使搜索范围更加精确,check velocities and positions,使其均在范围之内