![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1659516/image.png)
Set a custom stoping criteria for Simulated Annealing
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi all,
I am using the "simulannealbnd" function to solve my optimization problem. I want to set a custom stoping criteria for my code which is the program will terminate if there is no improvement in ojective function after 1000 iterations. I asked ChatGPT and it provided me this code (see below). However it failed to run. Do you have any idea on this problem? Thanks in advance!
options = optimoptions('simulannealbnd','OutputFcn', @myOutputFunction)
function stop = myOutputFunction(optimValues, state)
persistent bestValue counter maxIterationsWithoutImprovement;
if isempty(bestValue)
bestValue = Inf; % Assume minimization
counter = 0;
maxIterationsWithoutImprovement = 1000;
end
if strcmp(state, 'iter') % Check only during iterations
if optimValues.bestfval < bestValue
bestValue = optimValues.bestfval;
counter = 0;
else
counter = counter + 1;
end
if counter >= maxIterationsWithoutImprovement
disp(['No improvement in the objective function value after ', ...
num2str(maxIterationsWithoutImprovement), ' iterations. Stopping...']);
stop = true;
else
stop = false;
end
else
stop = false;
end
end
The command window show errors like this:
Error using Inversion>myOutputFunction
Too many input arguments.
Error in globaloptim.simulannealbnd.saoutput (line 39)
[stop ,optnew , changed ] = feval(OutputFcns{i},optold,optimValues, ...
Error in globaloptim.simulannealbnd.saengine/callOutputPlotFunctions (line 60)
globaloptim.simulannealbnd.saoutput(options,optimvalues,state,problem);
Error in globaloptim.simulannealbnd.saengine (line 17)
callOutputPlotFunctions('init');
Error in globaloptim.simulannealbnd.driver (line 28)
solverData = globaloptim.simulannealbnd.saengine(solverData,problem,options);
Error in simulannealbnd (line 197)
globaloptim.simulannealbnd.driver(FUN, x0, [], [], [], [], lb, ub, options, defaultopt);
Error in Inversion (line 20)
[x_SA,fval_SA,exitFlag_SA,output_SA] = simulannealbnd(fun,x0,lb,ub,options)
0 Kommentare
Akzeptierte Antwort
R
am 4 Apr. 2024
Bearbeitet: R
am 4 Apr. 2024
To set a stopping criteria for the "simulannealbnd" function, you can use the "MaxStallIterations" option. By setting it to 1000, the iterations will terminate if there is no improvement in the objective function after 1000 iterations. Here's an example of how to set this option:
options = optimoptions('simulannealbnd','MaxStallIterations',1000);
options.FunctionTolerance
fun = @dejong5fcn;
x0 = [0 0];
x = simulannealbnd(fun,x0,[],[],options)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1659516/image.png)
By modifying 'options.FunctionTolerance', you can set your custom stopping criteria. Refer to the following documentation for more information:
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Simulated Annealing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!