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!