Set a custom stoping criteria for Simulated Annealing

조회 수: 2 (최근 30일)
Ductho Le
Ductho Le 2024년 4월 3일
댓글: Ductho Le 2024년 4월 4일
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)

채택된 답변

R
R 2024년 4월 4일
편집: R 2024년 4월 4일
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
Ductho Le
Ductho Le 2024년 4월 4일
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,

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Simulated Annealing에 대해 자세히 알아보기

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by