Set a custom stoping criteria for Simulated Annealing
조회 수: 2 (최근 30일)
이전 댓글 표시
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
채택된 답변
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
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:
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Simulated Annealing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!