How to apply a algebraic function to n number of arrays in a template?
조회 수: 2 (최근 30일)
이전 댓글 표시
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
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Random Number Generation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!