How to apply a algebraic function to n number of arrays in a template?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello everyone. I am working on Particle Swarm Optimisation(PSO) and am new to matlab. I am facing problem in applying algebraic calculations to the elements of array placed in template. Can anyone please let me know how to resolve this. I am providing my code below. The problem is at particle(it,i).VelChange and particle(it,i).VelSigmoid. Am not able to get any values in these two.
The following is my code:
clc;
clear;
close all;
[D, A, C] = problem3();
MaxIt=2;
nPop=2;
VMx=4;
VMn=-4;
c1=0.5;
c2=0.5;
r1=0.5;
r2=0.5;
CostHandle = @(SOLN) calc3(SOLN);
empty_particle.Sol = [];
empty_particle.Cost = [];
empty_particle.Velocity = [];
empty_particle.VelSigmoid = [];
empty_particle.VelChange = [];
empty_particle.Best.Sol = [];
empty_particle.Best.Cost = [];
particle=repmat(empty_particle,nPop,1);
GlobalBest.Cost=inf;
for i=1:nPop
% RANDOM SOLUTION
particle(i).Sol=solution3(D, A, C);
% PARTICLE VELOCITIES
particle(i).Velocity=VMx+(VMn-VMx).*rand(A,C);
% COST CALCULATION
particle(i).Cost=CostHandle(particle(i).Sol);
% ASSIGNING PERSONAL BEST
particle(i).Best.Sol=particle(i).Sol;
particle(i).Best.Cost=particle(i).Cost;
% ASSIGNING GLOBAL BEST
if particle(i).Best.Cost<GlobalBest.Cost
GlobalBest=particle(i).Best;
end
end
for it=1:MaxIt
for i=1:nPop
particle(it,i).VelChange= (c1*r1.*(particle(it,i).Best.Sol-particle(it,i).Sol) ...
+c2*r2.*(GlobalBest(it,i)-particle(it,i).Sol));
particle(it,i).VelSigmoid=(1./1+exp([particle(i).Velocity]));
end
end
BestCosts=zeros(MaxIt,1);
BestCosts=GlobalBest;
0 commentaires
Réponses (0)
Voir également
Catégories
En savoir plus sur Particle Swarm dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!