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
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
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.
bahar vojdani
bahar vojdani 2022-6-8
I wanted to run exactly that cod but in another system.
unfortunately, we got errors

请先登录,再进行评论。

更多回答(3 个)

bahar vojdani
bahar vojdani 2022-3-3
I really appreciate for your help. The edit file that you give me it works well.
  4 个评论
bahar vojdani
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
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
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 个评论
bahar vojdani
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
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
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
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 CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by