필터 지우기
필터 지우기

Particle Swarm Optimization help

조회 수: 4 (최근 30일)
john pag
john pag 2014년 1월 26일
댓글: younes youyou 2019년 12월 16일
I new in matlab i need some help about with a code in matlab. I want make the Particle Swarm Optimization and i want put a mouse click to define a point in space with a window size [min1, max1] and [min2, max2]. Then a cluster consisting of n = 10 particles initialized and searches for the point set initially by the user.
my code is this:
clear all;
numofdims = 30;
numofparticles = 50;
c1 = 2;
c2 = 2;
numofiterations = 1000;
V = zeros(50, 30);
initialpop = V;
Vmin = zeros(30, 1);
Vmax = Vmin;
Xmax = ones(30, 1) * 100;
Xmin = -Xmax;
pbestfits = zeros(50, 1);
worsts = zeros(50, 1);
bests = zeros(50, 1);
meanfits = zeros(50, 1);
pbests = zeros(50, 30);
initialpop = Xmin + (Xmax - Xmin) .* rand(numofparticles, numofdims);
X = initialpop;
fitnesses = testfunc1(X);
[minfit, minfitidx] = min(fitnesses);
gbestfit = minfit;
gbest = X(minfitidx, :);
for i = 1:numofdims
Vmax(i) = 0.2 * (Xmax(i) - Xmin(i));
Vmin(i) = -Vmax(i);
end
for t = 1:1000
w = 0.9 - 0.7 * (t / numofiterations);
for i = 1:numofparticles
if(fitnesses(i) < pbestfits(i))
pbestfits(i) = fitnesses(i);
pbests(i, :) = X(i, :);
end
end
for i = 1:numofparticles
for j = 1:numofdims
V(i, j) = min(max((w * V(i, j) + rand * c1 * (pbests(i, j) - X(i, j))...
+ rand * c2 * (gbest(j) - X(i, j))), Vmin(j)), Vmax(j));
X(i, j) = min(max((X(i, j) + V(i, j)), Xmin(j)), Xmax(j));
end
end
fitnesses = testfunc1(X);
[minfit, minfitidx] = min(fitnesses);
if(minfit < gbestfit)
gbestfit = minfit;
gbest = X(minfitidx, :);
end
worsts(t) = max(fitnesses);
bests(t) = gbestfit;
meanfits(t) = mean(fitnesses);
end
  댓글 수: 1
scorpio 09
scorpio 09 2014년 4월 4일
  • i am getting error in line
  • initialpop = Xmin + (Xmax - Xmin) .* rand(numofparticles, numofdims);
  • Error using *
  • Inner matrix dimensions must agree.
  • please help me

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
Walter Roberson 2014년 1월 27일
"I want to do this" is not something we can act on. Give us a question. A question about MATLAB. A statement of what you are aiming for is not a question. "Why am I unable to color my graphics input cursor 'Gold' in MATLAB?" is an example of a question. "Why do all my MATLAB programs that start with 'clear all' fail to work?' is a question. "What are some good resources for learning MATLAB?" is a question. "I want a really good cup of Hot Cocoa." is a statement.

Yarpiz / Mostapha Heris
Yarpiz / Mostapha Heris 2015년 9월 13일
A structured and well-commented MATLAB implementation of Particle Swarm Optimization (PSO) is available to download in the following FEX link:
  댓글 수: 1
younes youyou
younes youyou 2019년 12월 16일
i have this error with this PSO algorithm ? how can i solve it many thanks
Index exceeds matrix dimensions.
Error in Sphere (line 16) z= 2.633*x(1)+2.992*x(2)+3.134*x(3)+3.678*x(4)+3.620*x(5)+2.948*x(6)+1.607*x(7)+2.952*x(8)+3.348*x(9)+3.680*x(10)+3.774*x(11)+2.995*x(12)+3.237*x(13)+1.608*x(14);
Error in pso>@(x)Sphere(x)
Error in pso (line 160) disp ([ 'CostFunction = ' (CostFunction(it))])

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Particle Swarm에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by