SlideShare uma empresa Scribd logo
1 de 3
Baixar para ler offline
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
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
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))

Mais conteúdo relacionado

Semelhante a PSOGlobalSearching

Radar cross section project
Radar cross section projectRadar cross section project
Radar cross section project
Assignmentpedia
 
Final Design Project - Memo (with GUI)
Final Design Project - Memo (with GUI)Final Design Project - Memo (with GUI)
Final Design Project - Memo (with GUI)
Alex Larcheveque
 
function [M]=trajectory3(x,y,z,pitcher,roll,yaw,scale_factor,step,se.pdf
function [M]=trajectory3(x,y,z,pitcher,roll,yaw,scale_factor,step,se.pdffunction [M]=trajectory3(x,y,z,pitcher,roll,yaw,scale_factor,step,se.pdf
function [M]=trajectory3(x,y,z,pitcher,roll,yaw,scale_factor,step,se.pdf
singhanubhav1234
 
Please use the same variables and only write the TODO part #!-usr-bi.pdf
Please use the same variables and only write the TODO part   #!-usr-bi.pdfPlease use the same variables and only write the TODO part   #!-usr-bi.pdf
Please use the same variables and only write the TODO part #!-usr-bi.pdf
asenterprisestyagi
 
Consider a 4-Link robot manipulator shown below. Use the forward kine.pdf
Consider a 4-Link robot manipulator shown below. Use the forward kine.pdfConsider a 4-Link robot manipulator shown below. Use the forward kine.pdf
Consider a 4-Link robot manipulator shown below. Use the forward kine.pdf
meerobertsonheyde608
 
Incorporate the SOR method in the multigridTest-m and apply the multig.pdf
Incorporate the SOR method in the multigridTest-m and apply the multig.pdfIncorporate the SOR method in the multigridTest-m and apply the multig.pdf
Incorporate the SOR method in the multigridTest-m and apply the multig.pdf
aartechindia
 

Semelhante a PSOGlobalSearching (20)

Radar cross section project
Radar cross section projectRadar cross section project
Radar cross section project
 
Mnistauto 5
Mnistauto 5Mnistauto 5
Mnistauto 5
 
Data Analysis
Data AnalysisData Analysis
Data Analysis
 
Particle Swarm Optimization Matlab code Using 50, 5000 Swarms
Particle Swarm Optimization Matlab code Using 50, 5000 SwarmsParticle Swarm Optimization Matlab code Using 50, 5000 Swarms
Particle Swarm Optimization Matlab code Using 50, 5000 Swarms
 
Kamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, codeKamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, code
 
Final Design Project - Memo (with GUI)
Final Design Project - Memo (with GUI)Final Design Project - Memo (with GUI)
Final Design Project - Memo (with GUI)
 
function [M]=trajectory3(x,y,z,pitcher,roll,yaw,scale_factor,step,se.pdf
function [M]=trajectory3(x,y,z,pitcher,roll,yaw,scale_factor,step,se.pdffunction [M]=trajectory3(x,y,z,pitcher,roll,yaw,scale_factor,step,se.pdf
function [M]=trajectory3(x,y,z,pitcher,roll,yaw,scale_factor,step,se.pdf
 
dplyr
dplyrdplyr
dplyr
 
Data manipulation with dplyr
Data manipulation with dplyrData manipulation with dplyr
Data manipulation with dplyr
 
Pattern Matching: Small Enhancement or Major Feature? (talk from jLove 2021)
Pattern Matching: Small Enhancement or Major Feature? (talk from jLove 2021)Pattern Matching: Small Enhancement or Major Feature? (talk from jLove 2021)
Pattern Matching: Small Enhancement or Major Feature? (talk from jLove 2021)
 
Pattern Matching: From Small Enhancement to Major Feature (talk from JavaLand...
Pattern Matching: From Small Enhancement to Major Feature (talk from JavaLand...Pattern Matching: From Small Enhancement to Major Feature (talk from JavaLand...
Pattern Matching: From Small Enhancement to Major Feature (talk from JavaLand...
 
FAIQ MANUAL.pdf
FAIQ MANUAL.pdfFAIQ MANUAL.pdf
FAIQ MANUAL.pdf
 
Please use the same variables and only write the TODO part #!-usr-bi.pdf
Please use the same variables and only write the TODO part   #!-usr-bi.pdfPlease use the same variables and only write the TODO part   #!-usr-bi.pdf
Please use the same variables and only write the TODO part #!-usr-bi.pdf
 
Consider a 4-Link robot manipulator shown below. Use the forward kine.pdf
Consider a 4-Link robot manipulator shown below. Use the forward kine.pdfConsider a 4-Link robot manipulator shown below. Use the forward kine.pdf
Consider a 4-Link robot manipulator shown below. Use the forward kine.pdf
 
Write a Matlab code (a computerized program) for calculating plane st.docx
 Write a Matlab code (a computerized program) for calculating plane st.docx Write a Matlab code (a computerized program) for calculating plane st.docx
Write a Matlab code (a computerized program) for calculating plane st.docx
 
R/Finance 2009 Chicago
R/Finance 2009 ChicagoR/Finance 2009 Chicago
R/Finance 2009 Chicago
 
array
arrayarray
array
 
Predicting organic reaction outcomes with weisfeiler lehman network
Predicting organic reaction outcomes with weisfeiler lehman networkPredicting organic reaction outcomes with weisfeiler lehman network
Predicting organic reaction outcomes with weisfeiler lehman network
 
Vcs16
Vcs16Vcs16
Vcs16
 
Incorporate the SOR method in the multigridTest-m and apply the multig.pdf
Incorporate the SOR method in the multigridTest-m and apply the multig.pdfIncorporate the SOR method in the multigridTest-m and apply the multig.pdf
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))