Why do i receive the error:" unrecognized function or variable "Fun"?

% Particle Swarm Optimization
function [Y,X,cg_curve]=PSO(N,Max_iteration,lb,ub,dim,fhandle,fnonlin)
%PSO Infotmation
Vmax=6;
noP=N;
wMax=0.9;
wMin=0.2;
c1=2;
c2=2;
% Initializations
for k = 1 : N
Swarm.Particles(k).X = (ub-lb) .* rand(1,dim) + lb;
Swarm.Particles(k).V = zeros(1, dim);
Swarm.Particles(k).PBEST.X = zeros(1,dim);
Swarm.Particles(k).PBEST.O = inf;
Swarm.GBEST.X = zeros(1,dim);
Swarm.GBEST.O = inf;
end
cg_curve=zeros(1,Max_iteration);
%Main
for t = 1 : Max_iteration
% Calcualte the objective value
for k = 1 : N
currentX = Swarm.Particles(k).X;
Swarm.Particles(k).O = fun(fhandle,fnonlin,currentX);
% Update the PBEST
if Swarm.Particles(k).O < Swarm.Particles(k).PBEST.O
Swarm.Particles(k).PBEST.X = currentX;
Swarm.Particles(k).PBEST.O = Swarm.Particles(k).O;
end
% Update the GBEST
if Swarm.Particles(k).O < Swarm.GBEST.O
Swarm.GBEST.X = currentX;
Swarm.GBEST.O = Swarm.Particles(k).O;
end
end
% Update the X and V vectors
w = wMax - t .* ((wMax - wMin) / Max_iteration);
for k = 1 : N
Swarm.Particles(k).V = w .* Swarm.Particles(k).V + c1 .* rand(1,dim) .* (Swarm.Particles(k).PBEST.X - Swarm.Particles(k).X) ...
+ c2 .* rand(1,dim) .* (Swarm.GBEST.X - Swarm.Particles(k).X);
% Check velocities
index1 = find(Swarm.Particles(k).V > Vmax);
index2 = find(Swarm.Particles(k).V < -Vmax);
Swarm.Particles(k).V(index1) = Vmax;
Swarm.Particles(k).V(index2) = -Vmax;
Swarm.Particles(k).X = Swarm.Particles(k).X + Swarm.Particles(k).V;
% Check positions
index1 = find(Swarm.Particles(k).X > ub);
index2 = find(Swarm.Particles(k).X < lb);
Swarm.Particles(k).X(index1) = ub(index1);
Swarm.Particles(k).X(index2) = lb(index2);
end
cg_curve(t) = Swarm.GBEST.O;
end
X=Swarm.GBEST.X;
Y=Swarm.GBEST.O;
end
Reference: Grey Wold Optimizer (GWO) source codes version 1.0
The code was developed on R2011b(7.13), i tried to use it to learn more about PSO but came out as error:
unrecognized function or variable "Fun"
Error in PSO:
Swarm.Particles(k).O = fun(fhandle,fnonlin,currentX);

1 件のコメント

ScottB
ScottB 2024 年 1 月 12 日
Kevin,
There isn't a native ML function called "fun". Is it possible you mean "funm"? It evaluates general matrix function.

サインインしてコメントする。

 採用された回答

Voss
Voss 2024 年 1 月 12 日

0 投票

Maybe you mean feval instead of fun.

7 件のコメント

Kevin
Kevin 2024 年 1 月 12 日
After i switched it to
Swarm.Particles(k).O = feval(fhandle,fnonlin,currentX);
And input PSO(N,Max_iteration,lb,ub,dim,fhandle,fnonlin) as
PSO(40,5000,100,-100,20,2,[])
The result came back as
Error using feval Function to evaluate must be represented as a string scalar, character vector, or function_handle object.
Why does it happen and what should i do?
Voss
Voss 2024 年 1 月 12 日
The PSO function is defined to take seven inputs, with input #6 being fhandle:
function [Y,X,cg_curve]=PSO(N,Max_iteration,lb,ub,dim,fhandle,fnonlin)
When you call it, your input #6 is the number 2:
PSO(40,5000,100,-100,20,2,[])
The inputs are not described in the code, so I have no idea what they represent, but based on the name "fhandle" it seems like fhandle should be a function handle, not a number.
Where did you download this PSO code from? I found several Grey Wolf Optimizer submissions on the File Exchange, but none of the ones I looked at had a PSO function. Are there examples of what inputs it expects shown at the place you got it? Feel free to post the link where I can download this PSO function, and I'll take a look myself.
Kevin
Kevin 2024 年 1 月 12 日
編集済み: Kevin 2024 年 1 月 12 日
I got it from here:
Grey Wold Optimizer (GWO) source codes version 1.0
Developed in MATLAB R2011b(7.13)
Author and programmer: Seyedali Mirjalil
e-Mail: ali.mirjalili@gmail.com
seyedali.mirjalili@griffithuni.edu.au
Homepage: http://www.alimirjalili.com
Main paper: S. Mirjalili, S. M. Mirjalili, A. Lewis Grey Wolf Optimizer, Advances in Engineering Software, in press, DOI: 10.1016/j.advengsoft.2013.12.007
P/s: I also think its a function handle and fnonlin is short for function nonlinear but i don't know what the function objective is so i just put 2 as a placeholder then if the feval code worked, it would show the input/ the content inside it was the problem. And i think the function handle should be in the following zip, since my work is to compare the result between PSO and EO, EO i have managed to but for PSO, i still stuck at the feval
Voss
Voss 2024 年 1 月 12 日
編集済み: Voss 2024 年 1 月 12 日
Maybe you can use MATLAB's builtin particle swarm optimization function, particleswarm, to learn about PSO. It seems well-documented.
In any case, I got that GWO code. I'm not sure how it's related to the PSO function in your question. Here's what I found:
%___________________________________________________________________%
% Grey Wolf Optimizer (GWO) source codes version 1.0 %
% %
% Developed in MATLAB R2011b(7.13) %
% %
% Author and programmer: Seyedali Mirjalili %
% %
% e-Mail: ali.mirjalili@gmail.com %
% seyedali.mirjalili@griffithuni.edu.au %
% %
% Homepage: http://www.alimirjalili.com %
% %
% Main paper: S. Mirjalili, S. M. Mirjalili, A. Lewis %
% Grey Wolf Optimizer, Advances in Engineering %
% Software , in press, %
% DOI: 10.1016/j.advengsoft.2013.12.007 %
% %
%___________________________________________________________________%
% Grey Wolf Optimizer
function [Alpha_score,Alpha_pos,Convergence_curve]=GWO(SearchAgents_no,Max_iter,lb,ub,dim,fobj)
% initialize alpha, beta, and delta_pos
Alpha_pos=zeros(1,dim);
Alpha_score=inf; %change this to -inf for maximization problems
Beta_pos=zeros(1,dim);
Beta_score=inf; %change this to -inf for maximization problems
Delta_pos=zeros(1,dim);
Delta_score=inf; %change this to -inf for maximization problems
%Initialize the positions of search agents
Positions=initialization(SearchAgents_no,dim,ub,lb);
Convergence_curve=zeros(1,Max_iter);
l=0;% Loop counter
% Main loop
while l<Max_iter
for i=1:size(Positions,1)
% Return back the search agents that go beyond the boundaries of the search space
Flag4ub=Positions(i,:)>ub;
Flag4lb=Positions(i,:)<lb;
Positions(i,:)=(Positions(i,:).*(~(Flag4ub+Flag4lb)))+ub.*Flag4ub+lb.*Flag4lb;
% Calculate objective function for each search agent
fitness=fobj(Positions(i,:));
% Update Alpha, Beta, and Delta
if fitness<Alpha_score
Alpha_score=fitness; % Update alpha
Alpha_pos=Positions(i,:);
end
if fitness>Alpha_score && fitness<Beta_score
Beta_score=fitness; % Update beta
Beta_pos=Positions(i,:);
end
if fitness>Alpha_score && fitness>Beta_score && fitness<Delta_score
Delta_score=fitness; % Update delta
Delta_pos=Positions(i,:);
end
end
a=2-l*((2)/Max_iter); % a decreases linearly fron 2 to 0
% Update the Position of search agents including omegas
for i=1:size(Positions,1)
for j=1:size(Positions,2)
r1=rand(); % r1 is a random number in [0,1]
r2=rand(); % r2 is a random number in [0,1]
A1=2*a*r1-a; % Equation (3.3)
C1=2*r2; % Equation (3.4)
D_alpha=abs(C1*Alpha_pos(j)-Positions(i,j)); % Equation (3.5)-part 1
X1=Alpha_pos(j)-A1*D_alpha; % Equation (3.6)-part 1
r1=rand();
r2=rand();
A2=2*a*r1-a; % Equation (3.3)
C2=2*r2; % Equation (3.4)
D_beta=abs(C2*Beta_pos(j)-Positions(i,j)); % Equation (3.5)-part 2
X2=Beta_pos(j)-A2*D_beta; % Equation (3.6)-part 2
r1=rand();
r2=rand();
A3=2*a*r1-a; % Equation (3.3)
C3=2*r2; % Equation (3.4)
D_delta=abs(C3*Delta_pos(j)-Positions(i,j)); % Equation (3.5)-part 3
X3=Delta_pos(j)-A3*D_delta; % Equation (3.5)-part 3
Positions(i,j)=(X1+X2+X3)/3;% Equation (3.7)
end
end
l=l+1;
Convergence_curve(l)=Alpha_score;
end
Kevin
Kevin 2024 年 1 月 12 日
編集済み: Kevin 2024 年 1 月 12 日
Check my previous comment again, since i edited it after i found my zip folder, i have included the zip there too, and thank you for finding this
Voss
Voss 2024 年 1 月 12 日
The zip contains "Get_Functions_details.m", which defines 23 objective functions (called F1 through F23), so maybe use one of those. You can copy the structure of "main.m" and replace the call to EO there with a call to your PSO function.
It may still be useful to know where the PSO function comes from.
Kevin
Kevin 2024 年 1 月 17 日
Thank you for your help. I had managed to make it work.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

製品

リリース

R2021b

タグ

質問済み:

2024 年 1 月 12 日

コメント済み:

2024 年 1 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by