pso doesnt go to main loop
조회 수: 1 (최근 30일)
이전 댓글 표시
i edit the code for optimize ,y cost function
%
% Copyright (c) 2015, Mostapha Kalami Heris & Yarpiz (www.yarpiz.com)
% All rights reserved. Please read the "LICENSE" file for license terms.
%
% Project Code: YPEA102
% Project Title: Implementation of Particle Swarm Optimization in MATLAB
% Publisher: Yarpiz (www.yarpiz.com)
%
% Developer: Mostapha Kalami Heris (Member of Yarpiz Team)
%
% Cite as:
% Mostapha Kalami Heris, Particle Swarm Optimization in MATLAB (URL: https://yarpiz.com/50/ypea102-particle-swarm-optimization), Yarpiz, 2015.
%
% Contact Info: sm.kalami@gmail.com, info@yarpiz.com
%
%edit by MR.N
% function y = CostFunction(amp,teta)
clear;
close all;
% clc;
toc
%% Problem Definition
% CostFunction = @(x) Sphere(x); % Cost Function
nVarAmr = 10; % Number of Decision Variables
nVarTetar = 10;
VarSizeAmr = [1 nVarAmr]; % Size of Decision Variables Matrix
VarSizeTetar = [1 nVarTetar];
VarMinAmr = 0; % Lower Bound of Variables
VarMaxAmr = 1; % Upper Bound of Variables
VarMinTetar = -90; % Lower Bound of Variables
VarMaxTetar = 90; % Upper Bound of Variables
%% PSO Parameters
MaxIt = 10000; % Maximum Number of Iterations
nPop = 1000; % 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
% If you would like to use Constriction Coefficients for PSO,
% uncomment the following block and comment the above set of parameters.
% % Constriction Coefficients
% phi1 = 2.05;
% phi2 = 2.05;
% phi = phi1+phi2;
% chi = 2/(phi-2+sqrt(phi^2-4*phi));
% w = chi; % Inertia Weight
% wdamp = 1; % Inertia Weight Damping Ratio
% c1 = chi*phi1; % Personal Learning Coefficient
% c2 = chi*phi2; % Global Learning Coefficient
% Velocity Limits
VelMaxAmr = 0.1*(VarMaxAmr-VarMinAmr);
VelMinAmr = -VelMaxAmr;
VelMaxTetar = 0.1*(VarMaxTetar-VarMinTetar);
VelMinTetar = -VelMaxTetar;
%% Initialization
empty_particle.PositionAmr = [];
empty_particle.CostAmr = [];
empty_particle.VelocityAmr = [];
empty_particle.Best.PositionAmr = [];
empty_particle.Best.CostAmr = [];
empty_particle.PositionTetar = [];
empty_particle.CostTetar = [];
empty_particle.VelocityTetar = [];
empty_particle.Best.PositionTetar = [];
empty_particle.Best.CostTetar = [];
particleAmr = repmat(empty_particle.PositionAmr , nPop, 1);
particleTetar = repmat(empty_particle.PositionTetar, nPop, 1);
GlobalBest.Cost = inf;
for i = 1:nPop
% Initialize Position
particle(i).PositionAmr = unifrnd(VarMinAmr, VarMaxAmr,VarSizeAmr);
particle(i).PositionTetar = unifrnd(VarMinTetar, VarMaxTetar, VarSizeTetar);
particle(i).Position = [particle(i).PositionAmr,particle(i).PositionTetar];
% Initialize Velocity
particle(i).VelocityAmr = zeros(VarSizeAmr)+0.00001;
particle(i).VelocityTetar = zeros(VarSizeTetar)+0.00001;
% Evaluation
particle(i).Cost = CostFunction(particle(i).PositionAmr,particle(i).PositionTetar);
% Update Personal Best
particle(i).Best.PositionAmr = particle(i).PositionAmr;
particle(i).Best.PositionTetar = particle(i).PositionTetar;
particle(i).Best.Cost = particle(i).Cost;
% 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).VelocityAmr = w*particle(i).VelocityAmr ...
+c1*rand(VarSizeAmr).*(particle(i).Best.PositionAmr-particle(i).PositionAmr) ...
+c2*rand(VarSizeAmr).*(GlobalBest.PositionAmr-particle(i).PositionAmr);
particle(i).VelocityTetar = w*particle(i).VelocityTetar ...
+c1*rand(VarSizeTetar).*(particle(i).Best.PositionTetar-particle(i).PositionTetar) ...
+c2*rand(VarSizeTetar).*(GlobalBest.PositionTetar-particle(i).PositionTetar);
% Apply Velocity Limits
particle(i).VelocityAmr = max(particle(i).VelocityAmr, VelMinAmr);
particle(i).VelocityAmr = min(particle(i).VelocityAmr, VelMaxAmr);
particle(i).VelocityTetar = max(particle(i).VelocityTetar, VelMinTetar);
particle(i).VelocityTetar = min(particle(i).VelocityTetar, VelMaxTetar);
% Update Position
particle(i).PositionAmr = particle(i).PositionAmr + particle(i).VelocityAmr;
particle(i).PositionTetar = particle(i).PositionTetar + particle(i).VelocityTetar;
% Velocity Mirror Effect
IsOutside = (particle(i).PositionAmr<VarMinAmr | particle(i).PositionAmr>VarMaxAmr);
particle(i).VelocityAmr(IsOutside) = -particle(i).VelocityAmr(IsOutside);
IsOutside = (particle(i).PositionTetar<VarMinTetar | particle(i).PositionTetar>VarMaxTetar);
particle(i).VelocityTetar(IsOutside) = -particle(i).VelocityTetar(IsOutside);
% Apply Position Limits
particle(i).PositionAmr = max(particle(i).PositionAmr, VarMinAmr);
particle(i).PositionAmr = min(particle(i).PositionAmr, VarMaxAmr);
particle(i).PositionTetar = max(particle(i).PositionTetar, VarMinTetar);
particle(i).PositionTetar = min(particle(i).PositionTetar, VarMaxTetar);
% Evaluation
% particle(i).Cost = CostFunction(particle(i).Position);
% change above according to chatgpt
particle(i).Cost = CostFunction(particle(i).PositionAmr, particle(i).PositionTetar);
% 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;
% Update Global Best
if particle(i).Best.Cost<GlobalBest.Cost
GlobalBest = particle(i).Best;
end
end
end
% disp(['Particle ' num2str(i) ', Iteration ' num2str(it) ':']);
% disp([' VelocityAmr = ' num2str(particle(i).VelocityAmr)]);
% disp([' VelocityTetar = ' num2str(particle(i).VelocityTetar)]);
% BestCost(it) = GlobalBest.Cost;
%
% disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCost(it))]);
w = w*wdamp;
end
% function y = CostFunction (Amr,deltaTeta)
function y = CostFunction (PositionAmr,PositionTetar)
freq = 9*10^9; %Freq
j = sqrt(-1); %Define Imaginary
l =(3*10^8)/freq; %Lambda
k = (2*pi)/l; %Constant
d = 0.5*l; %Distant of each element
elementNumb = 10;
step = 1;
teta1 = ((-80) :step: (-15));
teta2 = ((-15) :step: (0));
teta3 = ((0) :step: (15));
teta4 = ((25) :step: (40));
teta5 = ((40) :step: (60));
tetat = [teta1,teta2,teta3,teta4,teta5];
lenteta1 = length(teta1);
sumLenteta12 = length(teta1) + length(teta2);
sumLenteta123 = length(teta3) + sumLenteta12;
sumLenteta1234 = sumLenteta123 + length(teta4);
g = zeros(elementNumb,length(tetat));
for h = 1:elementNumb
%%teta1
for aa = 1:length(teta1)
g(h,aa) = g(h,aa)+(PositionAmr(h) * ( exp(j*(h-1) * (k*d*sind(teta1(aa)+PositionTetar(h)))))); %w W
end
%%teta2
for bb=(lenteta1+1):(sumLenteta12)
g(h,bb) =g(h,bb)+( PositionAmr(h) * exp(j*(h-1) * (k*d*sind(teta2(bb-lenteta1)+PositionTetar(h))))); %with W
end
%%teta3
for cc = (sumLenteta12+1):(length(teta3)+sumLenteta12)
g(h,cc) = (g(h,cc)+( PositionAmr(h) * exp(j*(h-1) * (k*d*sind(teta3(cc-sumLenteta12)...
+PositionTetar(h)))))); %w W
end
%%teta4
for dd = (sumLenteta123+1):(sumLenteta123+length(teta4))
g(h,dd) = g(h,dd)+( PositionAmr(h) * exp(j*(h-1) * (k*d*sind(teta4(dd-(sumLenteta123))...
+PositionTetar(h)))));
end
%%teta5
for ee = (sumLenteta1234+1):(sumLenteta1234+length(teta5))
g(h,ee) = g(h,ee)+( PositionAmr(h) * exp(j*(h-1) * (k*d*sind(teta5(ee-(sumLenteta1234))...
+PositionTetar(h))))); %w W
end
end
y = abs(sum(g,1));
end
it has 20 variable which those consist of amplitude (Amr) and angle (Tetar)
and separated my cost function to 5 section for my next goal ( i want to prioritize each section for example area two should be more minimize than others or i want to inverse my cost function
1/( PositionAmr(h) * exp(j*(h-1) * (k*d*sind(teta(ee-(sumLenteta1234))...
+PositionTetar(h)))))
to maximize in some area
it has a loop which optimize my variable but after several time check i recognize it doesnt go to this loop and the first value always remain up to 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;
% Update Global Best
if particle(i).Best.Cost<GlobalBest.Cost
GlobalBest = particle(i).Best;
end
should i put some condition for it ?
댓글 수: 0
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with Optimization Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!