function has already been declared within this scope.
77 次查看(过去 30 天)
显示 更早的评论
I wanted to use optimization toolbox, but when I run the intiallization.m of each toolbox. Unfortunatly, I have got this error "has already been declared within this scope." even though I save the intialization exactily like the name of function.
dim = 8;
ub = [15 15 23 23 4.0 15 15 14];
lb = [2 2 10 10 2.7 2 2 1 ];
fobj = @CostFunction;
SearchAgents_no=30;
Max_iter=5;
% This function initialize the first population of search agents
function Positions=initialization(SearchAgents_no,dim,ub,lb)
Boundary_no= size(ub,2); % numnber of boundaries
% If the boundaries of all variables are equal and user enter a signle
% number for both ub and lb
if Boundary_no==1
Positions=rand(SearchAgents_no,dim).*(ub-lb)+lb;
end
% If each variable has a different lb and ub
if Boundary_no>1
for i=1:dim
ub_i=ub(i);
lb_i=lb(i);
Positions(:,i)=rand(SearchAgents_no,1).*(ub_i-lb_i)+lb_i;
end
end
end
采纳的回答
Walter Roberson
2022-2-28
function Positions=initialization(SearchAgents_no,dim,ub,lb)
That line cannot appear in a file named initialization.m
That function also is not called within your script, and you do not create a handle to it within your script, so there is no way for the function to be called, and therefore no reason for the function to exist.
22 个评论
Walter Roberson
2022-2-28
MATLAB does not use that structure for optimization. That structure is used by several pieces of third party code including Grey Wolf Optimization, and Whale Optimization.
In each case for those pieces of code, initialization.m is not the place to set the number of optimization agents and so on: those values should be set in the main function or script for the optimizer.
bahar vojdani
2022-2-28
Yes the Grey Wolf Optimization has 3 pieces of third party code, I dont know how can I run them. whould you pleas tell me how can I run this toolbox step by step?
Walter Roberson
2022-2-28
edit main.m starting from SearchAgents_no=30; % Number of search agents to have your lines such as
dim = 8;
ub = [15 15 23 23 4.0 15 15 14];
lb = [2 2 10 10 2.7 2 2 1 ];
fobj = @CostFunction;
SearchAgents_no=30;
Max_iter=5;
Edit your initialization.m to have
% This function initialize the first population of search agents
function Positions=initialization(SearchAgents_no,dim,ub,lb)
Boundary_no= size(ub,2); % numnber of boundaries
% If the boundaries of all variables are equal and user enter a signle
% number for both ub and lb
if Boundary_no==1
Positions=rand(SearchAgents_no,dim).*(ub-lb)+lb;
end
% If each variable has a different lb and ub
if Boundary_no>1
for i=1:dim
ub_i=ub(i);
lb_i=lb(i);
Positions(:,i)=rand(SearchAgents_no,1).*(ub_i-lb_i)+lb_i;
end
end
end
Do not initialize dim and so on in initialization.m : those variables should all be set in your main.m at appropriate lines.
bahar vojdani
2022-3-1
Thanks for your answer. I have changed the main.m file. like below cod:
%
% You can simply define your cost in a seperate file and load its handle to fobj
% The initial parameters that you need are:
%__________________________________________
fobj = @CostFunction;
dim = 8;
Max_iteration = 5
SearchAgents_no = 30
ub = [15 15 23 23 4.0 15 15 14];
lb = [2 2 10 10 2.7 2 2 1 ];
% If all the variables have equal lower bound you can just
% define lb and ub as two single number numbers
;
% To run GWO: [Best_score,Best_pos,GWO_cg_curve]=GWO(SearchAgents_no,Max_iteration,lb,ub,dim,fobj)
%__________________________________________
clear all
clc
SearchAgents_no=30; % Number of search agents
Function_name='F10'; % Name of the test function that can be from F1 to F23 (Table 1,2,3 in the paper)
Max_iteration=500; % Maximum numbef of iterations
% Load details of the selected benchmark function
[lb,ub,dim,fobj]=Get_Functions_details(Function_name);
[Best_score,Best_pos,GWO_cg_curve]=GWO(SearchAgents_no,Max_iteration,lb,ub,dim,fobj);
figure('Position',[500 500 660 290])
%Draw search space
subplot(1,2,1);
func_plot(Function_name);
title('Parameter space')
xlabel('x_1');
ylabel('x_2');
zlabel([Function_name,'( x_1 , x_2 )'])
%Draw objective space
subplot(1,2,2);
semilogy(GWO_cg_curve,'Color','r')
title('Objective space')
xlabel('Iteration');
ylabel('Best score obtained so far');
axis tight
grid on
box on
legend('GWO')
display(['The best solution obtained by GWO is : ', num2str(Best_pos)]);
display(['The best optimal value of the objective funciton found by GWO is : ', num2str(Best_score)]);
bahar vojdani
2022-3-1
Output argument "lb" (and maybe others) not assigned during call to "Get_Functions_details".
Error in main (line 47)
[lb,ub,dim,fobj]=Get_Functions_details(Function_name);
>> this the error
Walter Roberson
2022-3-1
Are you using the Get_Functions_details directly out of the GWO File Exchange Contribution that I linked to earlier?
And could you confirm that the Function_name you are passing in is 'F10' ?
bahar vojdani
2022-3-2
No, I am using main.m file for changing the detile. how should I change the the Get_Functions_details?
I changed the Function_name = CostFunction.
I have gotten an error again.
"Output argument "lb" (and maybe others) not assigned during call to "Get_Functions_details".
Error in main (line 47)
[lb,ub,dim,fobj]=Get_Functions_details(Function_name);"
Walter Roberson
2022-3-2
Please zip up all of the code that you have created, and assume that I will install GWO from the File Exchange.
What currently shows up for
which -all Get_Functions_details
bahar vojdani
2022-3-2
with typing the' which -all Get_Functions_details' I got this:
D:\dr.rahbar\compartive.study\mr.hakim\GWO\GWO\Get_Functions_details.m
C:\Users\asus\AppData\Roaming\MathWorks\MATLAB Add-Ons\Toolboxes\Grey Wolf Optimizer (GWO)\GWO\Get_Functions_details.m % Shadowed
Walter Roberson
2022-3-2
You have two GWO installed. They might potentially be interfering with each other.
bahar vojdani
2022-3-2
function Positions=initialization(SearchAgents_no,dim,ub,lb) appear in a file named initialization.m.
If I want to run this optimization at first I should set the variables in main.m and agter that run the main.m?
Walter Roberson
2022-3-2
Revised versions of functions attached.
This includes cleaning up some of the original functions, which needed better designs.
You really need to fix up CostFunction. The one you are using now is not something you can productively use with that kind of optimization.
Your CostFunction should be using the input values unchanged -- the GWO driver is already providing guided random values for the inputs. You should not be using rand() or randn() or randi() anywhere inside of CostFunction: your CostFunction should always return the same thing given the same inputs.
Your CostFunction appears to be designed for the idea that you write values to a file, and then somehow no more than 30 seconds later, results appear in a different file. If you are expecting an external program to process the set of parameters you are writing out, then your CostFunction should make an explicit call to the external program and wait for results.
Walter Roberson
2022-3-2
If I want to run this optimization at first I should set the variables in main.m and agter that run the main.m?
No. GWO() will initialize the variables.
bahar vojdani
2022-3-2
yeah this point that you said "Your CostFunction appears to be designed for the idea that you write values to a file, and then somehow no more than 30 seconds later, results appear in a different file. If you are expecting an external program to process the set of parameters you are writing out, then your CostFunction should make an explicit call to the external program and wait for results" is exactly true. I want to know it cause a problem?
Walter Roberson
2022-3-2
Your code is always waiting 30 seconds for a result, every iteration, no matter how long the outside program takes.
The outside program is not certain to be running at all.
The outside program is not certain to return an answer with 30 seconds.
The outside program is not certain to be looking in the correct directory to notice the revised input file
Depending on exactly how everything is arranged, the outside program might have problems reading the data file, if CostFunction is busy writing it. Or depending how everything is arranged, if the data file is not locked while it is being written, then the outside program might potentially end up reading the data while it is still being written. (There are ways to reduce these two problems.)
bahar vojdani
2022-3-6
Hello dear,
If I want to calculate the optimization time, I should use tic toc in which file?
I really appreciate any help you can provide.
Walter Roberson
2022-3-6
tic
[Best_score,Best_pos,GWO_cg_curve]=GWO(SearchAgents_no,Max_iteration,lb,ub,dim,fobj);
toc
However the times are nearly meaningless because of your sleep(30) in your objective function.
bahar vojdani
2022-6-5
Hello dear,
I really appreciate for always helping.
I have a question about that file you sent to me " to_post" works on my laptop perfectly, but when I open it with another laptop does work and gives me this error:
Unrecognized function or variable 'initialization'.
Error in GWO (line 35)
Positions=initialization(SearchAgents_no,dim,ub,lb);
Error in main (line 40)
[Best_score,Best_pos,GWO_cg_curve]=GWO(SearchAgents_no,Max_iteration,lb,ub,dim,fobj);
Walter Roberson
2022-6-5
Did you install gwo on the second system? https://www.mathworks.com/matlabcentral/fileexchange/44974-grey-wolf-optimizer-gwo
bahar vojdani
2022-6-8
yes we installed that not from this web site. we installed that file you have sent.
Walter Roberson
2022-6-8
Earlier you had
D:\dr.rahbar\compartive.study\mr.hakim\GWO\GWO\Get_Functions_details.m
C:\Users\asus\AppData\Roaming\MathWorks\MATLAB Add-Ons\Toolboxes\Grey Wolf Optimizer (GWO)\GWO\Get_Functions_details.m % Shadowed
The code I posted is only for use with that second series of code, the one available through the File Exchange or Add-on Explorer. It is not compatible with whatever the mr.hakim code is.
更多回答(3 个)
bahar vojdani
2022-3-3
I really appreciate for your help. The edit file that you give me it works well.
4 个评论
bahar vojdani
2022-4-26
Hi dear,
I am using the optimization algorithm, and the simulation is done in another software, and the output enters the Matlab. I want to write a loop inside MATLAB that does not save until a new file is imported and actually delete this 30-second pause.
Walter Roberson
2022-4-26
How can you tell when the other program has finished writing the file?
Do you have any control over the file name written to?
bahar vojdani
2022-4-26
That is my question how can I say when the simulation is finished save the result in Matlab.
Walter Roberson
2022-4-26
Some software programs do not create the output file under that name until they are finished writing to the file; in such a case, you could monitor the available files in the target directory and stop waiting when the file appears.
However, it is considerably more common for software programs to just go ahead and start writing to the file under the target name, and keep writing to it until they are finished; depending on the software involved they might or might not close the file at that point. If you are dealing with that kind of program (common!) then you cannot rely on the existence of the file to tell you that the software has finished writing to the file, since it might still be writing to the file.
Some people try hack work-arounds such as waiting 30 seconds or other fixed time period, under the assumption that "surely" the program would have finished writing to the file in that time. That is not a good assumption at all.
In some situations, a controlling program (such as MATLAB) can invoke the executable that processes the inputs, and wait until the executable finished before the controlling program continues. But earlier I said that "Your CostFunction appears to be designed for the idea that you write values to a file, and then somehow no more than 30 seconds later, results appear in a different file" and you completely agreed -- but that flow implies that the program processing the parameters is running independently of MATLAB, and is itself watching for input control files to appear and processing them, without the program being invoked from MATLAB. If you were invoking the program from MATLAB and it stopped running afterwards, then there would be possibilities to work with.
If you have an independent program that is watching for control input files to appear and processing them itself, then you can ask the question of whether the independent starts running a new process to process the input file -- because if it did, then there would be the possibility that you could monitor to see whether the task still existed.
Sometimes you cannot do much except loop asking to read the output file, expecting that the fopen() will fail as long as the other process has the output file open, so the success of the read() implicitly tells you that the process is done with the file.
bahar vojdani
2022-6-17
hello dear, In the zip file that you sent to me, there is a file with the name" MATLAB Drive Tag" . what is this function? Is it cause this zip file just works on my laptop?
25 个评论
Walter Roberson
2022-6-17
I happened to store the files on MATLAB Drive, so that I could access them using MATLAB Online. When you store something on MATLAB Drive then MATLAB Drive automatically adds the file you mention; MATLAB Drive uses information in the file to determine which files need to be synchronized between your local computer and the cloud storage. The file should not have any effect at all on the code, and you can delete the file without any effect on the code. It would not stop the code from running on another system.
bahar vojdani
2022-6-19
Hello dear I really appreciate your time, I have a question in GWO optimizer if I want to change the optimization terminated from number of agent to time. How can I change the code
bahar vojdani
2022-6-20
Hello dear I really appreciate your time, I have a question in GWO optimizer if I want to change the optimization terminated from number of agent to time. How can I change the code
another question is when I use the WOA_toolbox again I Got error. matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)WOA_toolbox('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
Walter Roberson
2022-6-21
To change the limit to time, in file GWO change
while l<Max_iter
to
maxtime = TIME_LIMIT_IN_SECONDS_GOES_HERE;
starttime = tic;
while toc(starttime) < maxtime
bahar vojdani
2022-7-1
In the GWO toolbox, I changed while l<Max_iter to maxtime = TIME_LIMIT_IN_SECONDS_GOES_HERE;
starttime = tic;
while toc(starttime) < maxtime
but I get error
%___________________________________________________________________%
% Grey Wolf Optimizer (GWO) source codes version 1.0 %
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
wb = waitbar(0, 'GWO iterations: Please wait');
cleanMe = onCleanup(@() delete(wb));
wb.Position = [47 1280 360 75];
wb2 = waitbar(0, 'Search agent, please wait');
cleanMe2 = onCleanup(@() delete(wb2));
wb2.Position = [47 1180 360 75];
npos = size(Positions,1);
% Main loop
maxtime = TIME_LIMIT_IN_SECONDS_GOES_HERE;
starttime = tic;
while toc(starttime) < maxtime
waitbar(l./Max_iter, wb);
for i=1:npos
waitbar(i./npos, wb2);
% 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
bahar vojdani
2022-7-2
Unrecognized function or variable 'TIME_LIMIT_IN_SECONDS_GOES_HERE'.
Error in GWO (line 35)
maxtime = TIME_LIMIT_IN_SECONDS_GOES_HERE;
Error in main (line 18)
[Best_score,Best_pos,GWO_cg_curve]=GWO(SearchAgents_no,Max_iteration,lb,ub,dim,fobj);
Walter Roberson
2022-7-2
Have you considered replacing TIME_LIMIT_IN_SECONDS_GOES_HERE with the actual time limit that you want to use, in seconds?
bahar vojdani
2022-7-3
Hello dear,
I really appreciate your helps. It works well but if I want to use this method ( maxtime = TIME_LIMIT_IN_SECONDS_GOES_HERE;
starttime = tic;
while toc(starttime) < maxtime) in ALO toolbox I get error
Not enough input arguments.
Error in ALO (line 38)
antlion_position=initialization(N,dim,ub,lb);
Walter Roberson
2022-7-5
Did you try copying main.m from ALO and modifying it to refer to your functions, and then run it? main.m calls ALO with appropriate parameters. When I look at your error message, it looks to me as if you tried to call ALO directly without passing in appropriate parameters to it.
bahar vojdani
2022-7-6
I did, but I have to change "Max_iter" to "maxtime" and "current_it" to "starttime" in all files
bahar vojdani
2022-7-6
%
I have to change "Max_iter" to "maxtime" and "current_it" to "starttime" in all files, and I have sent my main file. unfortunately, I got this error.
Output argument "lb" (and maybe others) not assigned during call to "Get_Functions_details".
Error in main (line 44)
[lb, ub, dim, fobj] = Get_Functions_details(Function_name);
%___________________________________________________________________%
% Ant Lion Optimizer (ALO) source codes demo 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, The Ant Lion Optimizer %
% Advances in Engineering Software , in press,2015 %
% DOI: http://dx.doi.org/10.1016/j.advengsoft.2015.01.010 %
% %
%___________________________________________________________________%
% You can simply define your cost in a seperate file and load its handle to fobj
% The initial parameters that you need are:
%__________________________________________
% fobj = @YourCostFunction
% dim = number of your variables
% Max_iteration = maximum number of generations
% SearchAgents_no = number of search agents
% lb=[lb1,lb2,...,lbn] where lbn is the lower bound of variable n
% ub=[ub1,ub2,...,ubn] where ubn is the upper bound of variable n
% If all the variables have equal lower bound you can just
% define lb and ub as two single number numbers
%__
tic
% Max_iteration = 1;
SearchAgents_no = 50;
Function_name = 'CostFunction';
maxtime = 1200;
dim = 2;
ub = [5.12];
lb = [-5.12 ];
[lb, ub, dim, fobj] = Get_Functions_details(Function_name);
[Best_score,Best_pos,GWO_cg_curve]=ALO(SearchAgents_no,maxtime,lb,ub,dim,fobj);
clear all
clc
SearchAgents_no=40; % Number of search agents
Function_name='F1'; % Name of the test function that can be from F1 to F23 (Table 1,2,3 in the paper)
Max_iteration=500; % Maximum numbef of iterations
% Load details of the selected benchmark function
[lb,ub,dim,fobj]=Get_Functions_details(Function_name);
[Best_score,Best_pos,cg_curve]=ALO(SearchAgents_no,maxtime,lb,ub,dim,fobj);
figure('Position',[500 500 660 290])
%Draw search space
subplot(1,2,1);
func_plot(Function_name);
title('Test function')
xlabel('x_1');
ylabel('x_2');
zlabel([Function_name,'( x_1 , x_2 )'])
grid off
%Draw objective space
subplot(1,2,2);
semilogy(cg_curve,'Color','r')
title('Convergence curve')
xlabel('Iteration');
ylabel('Best score obtained so far');
axis tight
grid off
box on
legend('ALO')
display(['The best solution obtained by ALO is : ', num2str(Best_pos)]);
display(['The best optimal value of the objective funciton found by ALO is : ', num2str(Best_score)]);
bahar vojdani
2022-7-18
I have to change "Max_iter" to "maxtime" and "current_it" to "starttime" in all files, and I have sent my main file. unfortunately, I got this error.
Output argument "lb" (and maybe others) not assigned during call to "Get_Functions_details".
Error in main (line 44)
[lb, ub, dim, fobj] = Get_Functions_details(Function_name);
%___________________________________________________________________%
% Ant Lion Optimizer (ALO) source codes demo 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, The Ant Lion Optimizer %
% Advances in Engineering Software , in press,2015 %
% DOI: http://dx.doi.org/10.1016/j.advengsoft.2015.01.010 %
% %
%___________________________________________________________________%
% You can simply define your cost in a seperate file and load its handle to fobj
% The initial parameters that you need are:
%__________________________________________
% fobj = @YourCostFunction
% dim = number of your variables
% Max_iteration = maximum number of generations
% SearchAgents_no = number of search agents
% lb=[lb1,lb2,...,lbn] where lbn is the lower bound of variable n
% ub=[ub1,ub2,...,ubn] where ubn is the upper bound of variable n
% If all the variables have equal lower bound you can just
% define lb and ub as two single number numbers
%__
tic
% Max_iteration = 1;
SearchAgents_no = 50;
Function_name = 'CostFunction';
maxtime = 1200;
dim = 2;
ub = [5.12];
lb = [-5.12 ];
[lb, ub, dim, fobj] = Get_Functions_details(Function_name);
[Best_score,Best_pos,GWO_cg_curve]=ALO(SearchAgents_no,maxtime,lb,ub,dim,fobj);
clear all
clc
SearchAgents_no=40; % Number of search agents
Function_name='F1'; % Name of the test function that can be from F1 to F23 (Table 1,2,3 in the paper)
Max_iteration=500; % Maximum numbef of iterations
% Load details of the selected benchmark function
[lb,ub,dim,fobj]=Get_Functions_details(Function_name);
[Best_score,Best_pos,cg_curve]=ALO(SearchAgents_no,maxtime,lb,ub,dim,fobj);
figure('Position',[500 500 660 290])
%Draw search space
subplot(1,2,1);
func_plot(Function_name);
title('Test function')
xlabel('x_1');
ylabel('x_2');
zlabel([Function_name,'( x_1 , x_2 )'])
grid off
%Draw objective space
subplot(1,2,2);
semilogy(cg_curve,'Color','r')
title('Convergence curve')
xlabel('Iteration');
ylabel('Best score obtained so far');
axis tight
grid off
box on
legend('ALO')
display(['The best solution obtained by ALO is : ', num2str(Best_pos)]);
display(['The best optimal value of the objective funciton found by ALO is : ', num2str(Best_score)]);
bahar vojdani
2022-7-23
@Walter Roberson Hello Dear,
I have a problem to use the ALO Toolbox.
Should I modified just Manin file?
bahar vojdani
2022-7-23
when I run main file, I got this error
Not enough input arguments.
Error in Get_Functions_details (line 31)
switch F
Error in main (line 48)
[lb, ub, dim, fobj] = Get_Functions_details();
Walter Roberson
2022-7-23
You need to modify Get_Functions_details in order to return:
- lower bound of all variables
- upper bound of all variables
- number of variables
- handle to the objective function
in that order.
bahar vojdani
2022-7-27
@Walter Roberson hello dear
I have modified that. But, I got an error when I run main file:
Error: File: Get_Functions_details.m Line: 36 Column: 29
Function 'Get_Functions_details' has already been declared within this scope.
Error in main (line 48)
[lb, ub, dim, fobj] = Get_Functions_details();
Walter Roberson
2022-7-27
What are the first 35 lines of Get_Functions_details.m ?
MATLAB supports three kinds of .m files:
- files whose first non-comment word is function are function files
- files whose first non-comment word is classdef are class definition files
- all other .m files are script files.
When you create a script file, you cannot declare any functions inside it that have the same name as the script file.
When you create a function file, the only function that can have the same name as the function file, is the first function in the file. The first function in a function file, no matter what the function line says, will always be treated as-if the function name was the same as the name of the .m file; the actual name in the function line is ignored. All other functions declared in the file must have a name that is different than the name of the file.
bahar vojdani
2022-7-29
Hello Dear,
I really appreciate for your help.
In 35 lines of Get_Functions_details.m the function of file has be difined.
I attached all files that I used here. I cant find the solution for these errors in Get_Functions_details. and Main.m files
Walter Roberson
2022-7-29
ub = [5.12];
lb = [-5.12 ];
dim = 2;
Function_name = 'CostFunction';
Those four lines are before the declaration of Get_Functions_details so you made the code into a script rather than a function. Then you try to define a function with the same name as the script, which is not permitted.
bahar vojdani
2022-7-29
You mean, I have to defined these ub = [5.12];
lb = [-5.12 ];
dim = 2;
Function_name = 'CostFunction';
just in main file?
bahar vojdani
2022-7-29
In addition, if I do not defined these hyperprameters in Get_Functions_details I will get these error:
Not enough input arguments.
Error in Get_Functions_details (line 33)
switch F
Error in main (line 48)
[lb, ub, dim, fobj] = Get_Functions_details();
Walter Roberson
2022-7-30
You should have
function [lb, ub, dim, Function_name] = Get_functions_details(varargin)
ub = [5.12];
lb = [-5.12 ];
dim = 2;
Function_name = 'CostFunction';
end
and delete everything else out of Get_functions_details
bahar vojdani
2022-8-2
Hello dear,
I have changed my Get_Functions_detailsas like as you said. But, I have an error. These are:
Array indices must be positive integers or logical values.
Error in ALO (line 49)
antlions_fitness(1,i)=fobj(antlion_position(i,:));
Error in main (line 54)
[Best_score,Best_pos,cg_curve]=ALO(SearchAgents_no,Max_iteration,lb,ub,dim,fobj);
1 个评论
Walter Roberson
2022-8-2
function [lb, ub, dim, Function_name] = Get_functions_details(varargin)
ub = [5.12];
lb = [-5.12 ];
dim = 2;
Function_name = @CostFunction;
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)