how to solve this 'error unable to define local function ALO' ?

14 次查看(过去 30 天)
fobj = @ReadGrasshopperFile;
dim = 3;
Max_iteration = 100;
SearchAgents_no = 300;
lb=[1,1,1];
ub=[10,10,10];
[Best_score,Best_pos,cg_curve]=ALO(SearchAgents_no,Max_iteration,lb,ub,dim,fobj);
function [Elite_antlion_fitness,Elite_antlion_position,Convergence_curve]=ALO(N,Max_iter,lb,ub,dim,fobj,handles)
% Initialize the positions of antlions and ants
antlion_position=initialization(N,dim,ub,lb);
ant_position=initialization(N,dim,ub,lb);
% Initialize variables to save the position of elite, sorted antlions,
% convergence curve, antlions fitness, and ants fitness
Sorted_antlions=zeros(N,dim);
Elite_antlion_position=zeros(1,dim);
Elite_antlion_fitness=inf;
Convergence_curve=zeros(1,Max_iter);
antlions_fitness=zeros(1,N);
ants_fitness=zeros(1,N);
% Calculate the fitness of initial antlions and sort them
for i=1:size(antlion_position,1)
antlions_fitness(1,i)=fobj(antlion_position(i,:));
end
[sorted_antlion_fitness,sorted_indexes]=sort(antlions_fitness);
for newindex=1:N
Sorted_antlions(newindex,:)=antlion_position(sorted_indexes(newindex),:);
end
Elite_antlion_position=Sorted_antlions(1,:);
Elite_antlion_fitness=sorted_antlion_fitness(1);
% Main loop start from the second iteration since the first iteration
% was dedicated to calculating the fitness of antlions
Current_iter=2;
while Current_iter<Max_iter+1
% This for loop simulate random walks
for i=1:size(ant_position,1)
% Select ant lions based on their fitness (the better anlion the higher chance of catching ant)
Rolette_index=RouletteWheelSelection(1./sorted_antlion_fitness);
if Rolette_index==-1
Rolette_index=1;
end
% RA is the random walk around the selected antlion by rolette wheel
RA=Random_walk_around_antlion(dim,Max_iter,lb,ub, Sorted_antlions(Rolette_index,:),Current_iter);
% RA is the random walk around the elite (best antlion so far)
[RE]=Random_walk_around_antlion(dim,Max_iter,lb,ub, Elite_antlion_position(1,:),Current_iter);
ant_position(i,:)= (RA(Current_iter,:)+RE(Current_iter,:))/2; % Equation (2.13) in the paper
end
for i=1:size(ant_position,1)
% Boundar checking (bring back the antlions of ants inside search
% space if they go beyoud the boundaries
Flag4ub=ant_position(i,:)>ub;
Flag4lb=ant_position(i,:)<lb;
ant_position(i,:)=(ant_position(i,:).*(~(Flag4ub+Flag4lb)))+ub.*Flag4ub+lb.*Flag4lb;
ants_fitness(1,i)=fobj(ant_position(i,:));
All_fitness(1,i)=ants_fitness(1,i);
end
% Update antlion positions and fitnesses based of the ants (if an ant
% becomes fitter than an antlion we assume it was cought by the antlion
% and the antlion update goes to its position to build the trap)
double_population=[Sorted_antlions;ant_position];
double_fitness=[sorted_antlion_fitness ants_fitness];
[double_fitness_sorted, I]=sort(double_fitness);
double_sorted_population=double_population(I,:);
antlions_fitness=double_fitness_sorted(1:N);
Sorted_antlions=double_sorted_population(1:N,:);
% Update the position of elite if any antlinons becomes fitter than it
if antlions_fitness(1)<Elite_antlion_fitness
Elite_antlion_position=Sorted_antlions(1,:);
Elite_antlion_fitness=antlions_fitness(1);
end
% Keep the elite in the population
Sorted_antlions(1,:)=Elite_antlion_position;
antlions_fitness(1)=Elite_antlion_fitness;
% Update the convergence curve
Convergence_curve(Current_iter)=Elite_antlion_fitness;
if Current_iter>2
line([Current_iter-1 Current_iter], [Convergence_curve(Current_iter-1) Convergence_curve(Current_iter)],'Color',[0 0.4470 0.7410])
xlabel('Iteration');
ylabel('Best score obtained so far');
drawnow
end
results = get(handles.uitable1,'data');
results{2,1}=Current_iter;
results{2,2}=Elite_antlion_fitness;
set(handles.uitable1,'data',results);
Current_iter=Current_iter+1;
end
end
Hello,
I used your valuable optimization (ALO) source codes. Unfortunately, when I run the code, it gives me some error " unable to define local function ALO."
I really appreciate any help you can provide.
  4 个评论
Geoff Hayes
Geoff Hayes 2022-2-21
@bahar vojdani and can you post the full error message please so that we can see which line is generting this error message?
bahar vojdani
bahar vojdani 2022-2-21
>> ReadGrasshopperFile
ans =
4490
>> ALO
Error: File: ALO.m Line: 35 Column: 75
Function 'ALO' has already been declared within this
scope.
>>

请先登录,再进行评论。

回答(1 个)

Cris LaPierre
Cris LaPierre 2022-2-21
编辑:Cris LaPierre 2022-2-21
Local functions can only be accessed within the script they are defined in. You cannot call it from the command window, as your example shows, or from another file.
If you would like to do so, you need to create a function file that just contains your function, and save it with the same name as your function.
If you keep your function as a local function, make sure you do not save your script using the same name as your local function name.
  1 个评论
bahar vojdani
bahar vojdani 2022-2-21
I have changed the name of file, but I have new error:
Not enough input arguments.
Error in OALO>ALO (line 133)
results = get(handles.uitable1,'data');
Error in OALO (line 33)
[Best_score,Best_pos,cg_curve]=ALO(SearchAgents_no,Max_iteration,lb,ub,dim,fobj);
>>

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Get Started with Optimization Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by