how to Plot convergence of Simulated annealing optimization problem?

7 visualizaciones (últimos 30 días)
Aqsa Ghafar
Aqsa Ghafar el 8 de Abr. de 2022
Comentada: Aqsa Ghafar el 8 de Abr. de 2022
I want to pot convergence of best costs obtained from simulated annealing optimization algorithm. i want a vector of best costs for plotting. would you help me out in this regard?
here is my code for main file.
function [xnew, newcost, ]=simu_my2(l,u,xsol,Maxiter,CostFunction)
%%%%%%%%%%for first test function sphere%%%%%%%%
%nd= 5;
%l=-5.12*ones(1,nd);
%u=5.12*ones(1,nd);
%xsol=[0.01 0.01]; % it can be random(1e-10)*(l+rand(1,nd).*(u-l));
To=1;%0.5
%Maxiter=100;
% CostFunction = @cost1;% Cost Function
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% for camel hump
%% Initialization
alpha=0.1;
T=To;
cost11 = CostFunction(xsol);%%%%%%%%%%%%first cost
%% SA Main Loop
for k=1: Maxiter
% Create and Evaluate New Solution
dx=0.01*(l+(rand(size(l)).*(u-l)));% step size * neighbour
xnew=xsol+dx;
newcost = CostFunction(xnew);%%%%%%%%%%%second cost
diff=newcost-cost11;
if diff<0 % If NEWSOL is better than SOL
xnew;
newcost;
elseif rand <exp(-diff/T)%=P %%%%%%%%%%%%% nature inspired optmization algorithm
xnew;
newcost;
end
if newcost<cost11
xnew;
end
T=(1-0.1).^(k);
end
newcost
xnew
end
%% Results
% function f=cost1(x)
% f=sum(x.^2);
% end
% function f= cost2(x)
% f=(4-2.1*x(1).^2+x(1).^4/3).*x(1).^2+x(1).*x(2)+4*(x(2).^2-1).*x(2).^2;
% end
% function f = ackley(xx, a, b, c)
%

Respuestas (1)

Alan Weiss
Alan Weiss el 8 de Abr. de 2022
Perhaps you want to collect the cost as a vector that you can then plot. Something like this:
% Put this line immediately after %% SA Main Loop
costs = zeros(Maxiter,1);
cost(1) = cost11;
% Just below for k=1: Maxiter put
if k > 1
cost(k) = cost(k-1);
end
end
% Where you have
if diff<0 % If NEWSOL is better than SOL
xnew;
newcost;
% Instead put
if diff<0 % If NEWSOL is better than SOL
xsol = xnew;
costs(k) = newcost;
% Similarly, where you have
elseif rand <exp(-diff/T)%=P %%%%%%%%%%%%% nature inspired optmization algorithm
xnew;
% Instead put
elseif rand <exp(-diff/T)%=P %%%%%%%%%%%%% nature inspired optmization algorithm
xsol = xnew;
costs(k) = newcost;
This should leave you with an array costs that you can then plot.
Alan Weiss
MATLAB mathematical toolbox documentation
  2 comentarios
Aqsa Ghafar
Aqsa Ghafar el 8 de Abr. de 2022
do i have to change the output names in existing function?
Aqsa Ghafar
Aqsa Ghafar el 8 de Abr. de 2022
after replacing these commands. it gives an error

Iniciar sesión para comentar.

Categorías

Más información sobre Simulated Annealing en Help Center y File Exchange.

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by