could anyone help me how to solve the error for the following code
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
code:
tic
clc
clear all
close all
rng default
LB=[0 0 0 ]; %lower bounds of variables
UB=[10 10 10 ]; %upper bounds of variables
% pso parameters values
m=3; % number of variables
n=4; % population size
wmax=0.9; % inertia weight
wmin=0.4; % inertia weight
c1=2; % acceleration factor
c2=2; % acceleration factor
% pso main program---------------------------------------------------start
maxite=10; % set maximum number of iteration
maxrun=5; % set maximum number of runs need to be
for run=1:maxrun
run ;
Z=run;% pso initialization----------------------------------------------start
for i=1:n
A=i;
for j=1:m
B=j;
C=rand();
E=LB(j);
F=UB(j);
G=round(LB(j)+C*(UB(j)-LB(j)));
x0(i,j)=round(LB(j)+C*(UB(j)-LB(j)));
D=x0(i,j);
end
end
x=x0; % initial population
v=0.1*x0; % initial velocity
for i=1:n
Y=i;
f0(i,1)=ofun(x0(i,:));
end
[fmax0,index0]=max(f0) ;
pbest=x0 ; % initial pbest
gbest=x0(index0,:) ; % initial gbest
% pso initialization------------------------------------------------end
% pso algorithm---------------------------------------------------start
ite=1 ;
tolerance=1 ;
while ite>=maxite && tolerance<10^-12
YY=ite<=maxite && tolerance>10^-12
w=wmax-(wmax-wmin)*ite/maxite;% update inertial weight
% pso velocity updates
for i=1:n
for j=1:m
t=v(i,j);
r=rand();
ss=pbest(i,j);
rr=x(i,j);
k=rand();
kk=gbest(1,j);
v(i,j)=w*t+c1*r*(pbest(i,j)-x(i,j))...
+c2*k*(gbest(1,j)-x(i,j));
V=v(i,j);
end
end
% pso position update
for i=1:n
for j=1:m
x(i,j)=x(i,j)+v(i,j) ;
end
end
% handling boundary violations
for i=1:n
for j=1:m
if x(i,j)>LB(j)
x(i,j)=LB(j);
elseif x(i,j)<UB(j)
x(i,j)=UB(j) ;
end
end
end
% evaluating fitness
for i=1:n
f(i,1)=ofun(x(i,:));
end
% updating pbest and fitness
for i=1:n
if f(i,1)>f0(i,1)
pbest(i,:)=x(i,:) ;
f0(i,1)=f(i,1) ;
end
end
[fmax,index]=max(f0) ; % finding out the best particle
ffmax(ite,run)=fmax; % storing best fitness
ffite(run)=ite; % storing iteration count
% updating gbest and best fitness
AA=fmax;
BB=fmax0;
if fmax>fmax0
% CC=pbest(index,:)
gbest=pbest(index,:);
fmax0=fmax;
end
% calculating tolerance
if ite<100;
tolerance=abs(ffmax(ite-100,run)-fmax0) ;
end
% displaying iterative results
if ite==1
disp(sprintf('Iteration Best particle Objective fun'));
end
disp(sprintf('%8g %8g %8.4f',ite,index,fmax0)) ;
ite=ite+1;
end
% pso algorithm-----------------------------------------------------end
gbest ;
fvalue=10*(gbest(1)-1)^2+20*(gbest(2)-2)^2+30*(gbest(3)-3)^2 ;
fff(run)=fvalue;
rgbest(run,:)=gbest ;
disp(sprintf('--------------------------------------'))
end
% pso main program------------------------------------------------------end
% disp(sprintf('\n')) ; %create space or gap
% disp(sprintf('*********************************************************'));
% disp(sprintf('Final Results-----------------------------'));
[bestfun,bestrun]=max(fff)
best_variables=rgbest(bestrun,:)
% disp(sprintf('*********************************************************'));
toc
% PSO convergence characteristic
plot(ffmax(1:ffite(bestrun),bestrun),'-*b')
xlabel('Iteration')
ylabel('Fitness value')
title('PSO convergence')
when i run the code it gives error stating Undefined function or variable 'ffite'.
Error in line 134 plot(ffmax(1:ffite(bestrun),bestrun),'-*b').
Could anyone please help me on this.
2 Commenti
Risposte (1)
KSSV
il 11 Feb 2019
ffite is defined inside while loop.......your code is not entering inside the while loop......so ffite is not defined......you need to change the conditions of while loop.
3 Commenti
Walter Roberson
il 11 Feb 2019
you set tolerance to 1 and then you while tolerance < 1e-12 which would immediately fail .
Vedere anche
Categorie
Scopri di più su Get Started with Optimization Toolbox in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!