Incorporate the SOR method in the multigridTest-m and apply the multig.pdf
PSOGlobalSearching
1. 8/31/16 11:07 AM C:Usersswei33D...PSOGlobalSearching.m 1 of 3
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Particle Swarm Optimization Code for Prof. Lentz' Project %
% Shiyan Wei %
% 08/21/2016 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This document is used for doing the global optimization searching for the
% MLE function
%object function MLEfunc.m
% input d r0 r1 r2
% output sum of lnlikelyhood
% constrain
%d>0; r0>0; r1>0; r2>0;
clear all
clc
close all
%%%% PSO parameter value initiate %%%%%%
%% initialization
%
swarm_size = 16; % number of the swarm particles
maxIter = 50; % maximum number of iterations
inertia = 0.8;
correction_factor = 2.0;
% set the position of the initial swarm
a=0.1:0.2:0.4;
b=1.1:0.2:1.4;
[X,Y,Z,A] = ndgrid(a,b,a,a);
C = cat(4,X,Y,Z,A);
D = reshape(C,[],4);
swarm(1:swarm_size,1,1:4) = D; % set the position of the particles in 2D
swarm(:,2,:) = 0; % set initial velocity for particles
swarm(:,4,1) = 0; % set the best value so far
%% calculate the value of each swarm
m1=zeros(length(maxIter));
m2=zeros(length(maxIter));
m3=zeros(length(maxIter));
m4=zeros(length(maxIter));
for iter = 1:maxIter
swarm(:, 1, 1) = real(swarm(:, 1, 1)) + real(swarm(:, 2, 1))/1.3; %update d
position with the velocity
swarm(:, 1, 2) = real(swarm(:, 1, 2)) + real(swarm(:, 2, 2))/1.3; %update r0
position with the velocity
2. 8/31/16 11:07 AM C:Usersswei33D...PSOGlobalSearching.m 2 of 3
swarm(:, 1, 3) = real(swarm(:, 1, 3)) + real(swarm(:, 2, 3))/1.3; %update r1
position with the velocity
swarm(:, 1, 4) = real(swarm(:, 1, 4)) + real(swarm(:, 2, 4))/1.3; %update r2
position with the velocity
d = swarm(:, 1, 1); % get the updated
position
r0 = swarm(:, 1, 2); % updated position
r1 = swarm(:, 1, 3); % updated position
r2 = swarm(:, 1, 4); % updated position
W=[d r0 r1 r2];
for i = 1:length(W)
W(i,5) = MLEfunc([W(i,1) W(i,2) W(i,3) W(i,4)]); % evaluate the function
using the position of the particle
end
fval=W(:,5)/1000000; % rescale the MLE
% compare the function values to find the best ones
for ii = 1:swarm_size
if fval(ii,1) < swarm(ii,4,1)
swarm(ii, 3, 1) = swarm(ii, 1, 1); % update best x position,
swarm(ii, 3, 2) = swarm(ii, 1, 2); % update best y postions
swarm(ii, 3, 3) = swarm(ii, 1, 3); % update best y postions
swarm(ii, 3, 4) = swarm(ii, 1, 4); % update best y postions
swarm(ii, 4, 1) = fval(ii,1); % update the best value
so far
end
end
[~, gbest] = min(swarm(:, 4, 1)); % find the best function
value in total
% update the velocity of the particles
swarm(:, 2, 1) = inertia*(.6*rand(swarm_size,1).*swarm(:, 2, 1)) + correction_factor*
(.6*rand(swarm_size,1).*(swarm(:, 3, 1) ...
- swarm(:, 1, 1))) + correction_factor*(.6*rand(swarm_size,1).*(swarm(gbest, 3,
1) - swarm(:, 1, 1))); %d velocity component
swarm(:, 2, 2) = inertia*(.6*rand(swarm_size,1).*swarm(:, 2, 2)) + correction_factor*
(.6*rand(swarm_size,1).*(swarm(:, 3, 2) ...
- swarm(:, 1, 2))) + correction_factor*(.6*rand(swarm_size,1).*(swarm(gbest, 3,
2) - swarm(:, 1, 2))); %r0 velocity component
swarm(:, 2, 3) = inertia*(.6*rand(swarm_size,1).*swarm(:, 2, 3)) + correction_factor*
(.6*rand(swarm_size,1).*(swarm(:, 3, 3) ...
- swarm(:, 1, 3))) + correction_factor*(.6*rand(swarm_size,1).*(swarm(gbest, 3,
3) - swarm(:, 1, 3))); %r1 velocity component
swarm(:, 2, 4) = inertia*(.6*rand(swarm_size,1).*swarm(:, 2, 4)) + correction_factor*
(.6*rand(swarm_size,1).*(swarm(:, 3, 4) ...
- swarm(:, 1, 4))) + correction_factor*(.6*rand(swarm_size,1).*(swarm(gbest, 3,
4) - swarm(:, 1, 4))); %r2 velocity component
m1(iter,1)=mean(swarm(:, 1, 1)); % Calculate d
3. 8/31/16 11:07 AM C:Usersswei33D...PSOGlobalSearching.m 3 of 3
m1(iter,2)=var(swarm(:, 1, 1)); % Calculate variance of d
m2(iter,1)=mean(swarm(:, 1, 2)); % Calculate r0
m2(iter,2)=var(swarm(:, 1, 2)); % Calculate variance of r0
m3(iter,1)=mean(swarm(:, 1, 3)); % Calculate r1
m3(iter,2)=var(swarm(:, 1, 3)); % Calculate variance of r1
m4(iter,1)=mean(swarm(:, 1, 4)); % Calculate r2
m4(iter,2)=var(swarm(:, 1, 4)); % Calculate variance of r2
% plot the particles
clf;scatter3(swarm(:, 1, 2), swarm(:, 1, 3), swarm(:, 1, 4),'bo'); %
drawing swarm movements
axis([.7 1.4 0 .4 0 .4]);
xlabel('r0');
ylabel('r1');
zlabel('r2');
pause(.1); % un-comment this line to
decrease the animation speed
disp(['iteration: ' num2str(iter)]);
disp(' d r0 r1 r2');
disp([m1(iter,1) m2(iter,1) m3(iter,1) m4(iter,1)]);
disp([m1(iter,2) m2(iter,2) m3(iter,2) m4(iter,2)]);
end
figure
stem(m1(:,2))