Set a custom stoping criteria for Simulated Annealing

2 views (last 30 days)
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)

Accepted Answer

R
R on 4 Apr 2024
Edited: R on 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
ans = 1.0000e-06
fun = @dejong5fcn;
x0 = [0 0];
x = simulannealbnd(fun,x0,[],[],options)
By modifying 'options.FunctionTolerance', you can set your custom stopping criteria. Refer to the following documentation for more information:
  1 Comment
Ductho Le
Ductho Le on 4 Apr 2024
Thank you for your comment. I have read about the 'MaxStallIterations' option but I couldn't understand what they mean in the description. Now, you make it clear. Thanks a lot!
Best,

Sign in to comment.

More Answers (0)

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!