function has already been declared within this scope.
Show older comments
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
Accepted Answer
More Answers (3)
bahar vojdani
on 3 Mar 2022
0 votes
4 Comments
bahar vojdani
on 26 Apr 2022
Walter Roberson
on 26 Apr 2022
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
on 26 Apr 2022
Walter Roberson
on 26 Apr 2022
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
on 17 Jun 2022
0 votes
25 Comments
Walter Roberson
on 17 Jun 2022
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
on 19 Jun 2022
bahar vojdani
on 20 Jun 2022
Walter Roberson
on 21 Jun 2022
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
Walter Roberson
on 21 Jun 2022
I would need the full error message for WOA_toolbox .
bahar vojdani
on 1 Jul 2022
Walter Roberson
on 1 Jul 2022
What is the error message?
bahar vojdani
on 2 Jul 2022
Walter Roberson
on 2 Jul 2022
Have you considered replacing TIME_LIMIT_IN_SECONDS_GOES_HERE with the actual time limit that you want to use, in seconds?
bahar vojdani
on 3 Jul 2022
Walter Roberson
on 5 Jul 2022
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
on 6 Jul 2022
Walter Roberson
on 6 Jul 2022
Well, go ahead and do that then.
bahar vojdani
on 6 Jul 2022
bahar vojdani
on 18 Jul 2022
bahar vojdani
on 23 Jul 2022
bahar vojdani
on 23 Jul 2022
Walter Roberson
on 23 Jul 2022
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
on 27 Jul 2022
Walter Roberson
on 27 Jul 2022
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
on 29 Jul 2022
Walter Roberson
on 29 Jul 2022
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
on 29 Jul 2022
bahar vojdani
on 29 Jul 2022
Walter Roberson
on 30 Jul 2022
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
on 2 Aug 2022
0 votes
1 Comment
Walter Roberson
on 2 Aug 2022
function [lb, ub, dim, Function_name] = Get_functions_details(varargin)
ub = [5.12];
lb = [-5.12 ];
dim = 2;
Function_name = @CostFunction;
end
Categories
Find more on Surrogate Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!