Filter löschen
Filter löschen

could anyone help me how to solve the error for the following code

4 Ansichten (letzte 30 Tage)
jaah navi
jaah navi am 11 Feb. 2019
Kommentiert: jaah navi am 12 Feb. 2019
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 Kommentare
jaah navi
jaah navi am 11 Feb. 2019
this was the function which I have used for the above code to execute
function f=ofun(x)
% objective function (minimization)
% x=9
% x0(i,j)=round(LB(j)+C*(UB(j)-LB(j)))
of=10*(x(1)-1)^2+20*(x(2)-2)^2+30*(x(3)-3)^2;
a=x(1);
b=x(2);
c=x(3);
% of=8*(x-1)^2+16*(x-2)^2+24*(x-3)^2
% constraints (all constraints must be converted into <=0 type)
% if there is no constraints then comments all c0 lines below
c0=[] ;
c0(1)=x(1)+x(2)+x(3)-5 ; % <=0 type constraints
c0(2)=x(1)^2+2*x(2)-x(3) ; % <=0 type constraints
% c0(1)=x+x+x-5 % <=0 type constraints
% c0(2)=-x^2+2*x-x % <=0 type constraints
% c0(3)=-x^3
% defining penalty for each constraint
for i=1:length(c0)
p=i;
if c0(i)>0
q=c0(i);
c(i)=1 ;
r=c(i);
else
c(i)=0;
s=c(i);
end
end
penalty=10000;
u=c;
t=sum(c);% penalty on each constraint violation
f=of+penalty*sum(c) ; % fitness function
end

Melden Sie sich an, um zu kommentieren.

Antworten (1)

KSSV
KSSV am 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 Kommentare
Walter Roberson
Walter Roberson am 11 Feb. 2019
you set tolerance to 1 and then you while tolerance < 1e-12 which would immediately fail .
jaah navi
jaah navi am 12 Feb. 2019
yes.Thanks for your comment.I have changed it.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Get Started with Optimization Toolbox finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by