You are on page 1of 4

if nargin == 1,

Q = 100;

% Constant related to the quantity of trail laid by ants.

alpha = 1;
% The relative importance of the trail.
beta = 3;
% The relative importance of the visibility.
rho = 0.1;
% (1-rho) Trail Evaporation.
n = handles.Dim;
% 1<=i<=n; Numbers os Nodes.
m = n;
% Numbers of Ants equal to Numbers of Nodes.
% Algortihm Parameters.
Param = struct('m',m,'Q',Q,'alpha',alpha,'beta',beta,'rho',rho);
Default = {num2str(m); num2str(Q); num2str(alpha); num2str(beta);...
num2str(rho)};
Help = { ...
'm = Total number of ants.'
'Q = Constant related to the quantity of trail laid by ants.';
'alpha = The relative importance of the trail.';
'beta = The relative importance of the visibility.';
'rho = Trail persistence.';
};
if handles.Value >= 2,
elitist = 5;
% Elitist Ant System Strategy.
Param.elitist = elitist;
Default{end+1} = num2str(elitist);
Help{end+1} = 'elitist = Elitist Strategy';
end,
return,

end,

m
= Swarm.m;
alpha = Swarm.alpha;
beta = Swarm.beta;
rho
= Swarm.rho;
Q
= Swarm.Q;
n
= handles.Dim;
PS
= handles.NodeCoord(:,2:end);
if isfield(Swarm,'elistist'),
elitist = Swarm.elitist;
GA
= Swarm.GA;
else
elitist = 0;
GA
= 0;
end,
Symmetric = 1;
tabu = zeros(n,m);

% Para resolver problemas TSP simetricos.


% Cada columna guarda las posiciones del
% recorrido de cada hormiga.
if isempty(handles.NodeWeight)
d = ((PS(:,ones(1,n))'-PS(:,ones(1,n))).^2+ ...
(PS(:,2*ones(1,n))'-PS(:,2*ones(1,n))).^2).^0.5;
else
d = handles.NodeWeight;
end,
% Cada columna identifica el nodo i y los
% valores sobre dicha columna indican la
% distancia del arco que hay hasta el nodo j.
d_ent = round(d);
% La distancia entera entre cada nodo.
AntPos = zeros(n,m);% Matriz que identifica las ciudades visitadas
% por la k-esima hormiga, poniendo un cero.
GAL1 = zeros(1,Iter+1);% Variable para grafica.
GAL2 = GAL1;

%% Heuristica del Vecino mas Cercano (NNH)


[Best,L_Best,L_Best_ent] = NNH(d);
if (elitist~=0)&&(GA==1)
tabu_e = Best'*ones(1,elitist);
L_e = L_Best_ent*ones(1,elitist);
end,
GAL1(1) = L_Best_ent;
GAL2(1) = L_Best_ent;
tao0 = 1e-6;
tao = tao0*ones(n,n);
tao_elitist = zeros(n,n);
delta_tao = zeros(n,n);
d(d==0) = Inf;
d_ent(d_ent==0) = Inf;
eta = 1./d_ent;

% GAL1 es un parametro para la grafica.


%
%
%
%
%
%
%
%

Valor del rastro de feromona inicial.


Intensidad del rastro sobre cada arco.
Actualizacion rastro de "hormigas elitistas".
Actualizacion rastro del tour de k hormigas.
La diagonal de d es igual a cero, para evitar
que haya division por cero se pone Inf;
Distancia entera con correcion de cero.
Visibilidad.

%% Inicializacion de las Figuras


ACO_graph_init(handles,PS,Best);
AUX0 = zeros(1,n);
%% El Algoritmo AS
for t = 1:Iter,
AntPos = AntPos+1;
tabu = 0.*tabu;
s = 1;

% AntPos = 1 inicia las ciudades visitadas.


% Inicializar la lista tabu.
% indice de la lista tabu. 1<=s<=n;

% Primera posicion de las hormigas.


tabu(s,:) = randi(n,1,m);
% tabu guarda la etiqueta de id. de la
% ciudad donde se ubica cada hormiga.
% AntPos se pone con ceros en las posiciones donde se encuentra y se ha
% encontrado la hormiga con el fin de hacer la evaluacion correcta del
% recorrido permitido.
AntPos(tabu(s,:)+(0:(m-1))*n) = 0;
%% Calculo de Probabilidades de recorrido entre ciudades.%%
for s = 1:n-1,
AUX1 = (tao(:,tabu(s,:)).^alpha).*(eta(:,tabu(s,:)).^beta).*AntPos;
AUX2 = sum(AUX1);
pr3 = AUX1./AUX2(ones(n,1),:); % Exploration.
% Estrategia de la Ruleta para pr3.
AUX2 = sum(pr3);
pr3 = cumsum(pr3)./AUX2(ones(n,1),:);
pr = rand(1,m); pr(pr==0) = 0.5;
pr3 = pr3-pr(ones(n,1),:);
pr3(pr3<0) = 1;
[pr,ind] = min(pr3);
tabu(s+1,:) = ind;
AntPos(tabu(s+1,:)+(0:m-1)*n) = 0;
end,

% end for s = 1:n-1,

%% Calculo de la distancia Real y Entera Acumulada.%%


AUX1 = 0*AUX1; AUX1(1:end-1,:) = tabu(2:end,:); AUX1(end,:) = tabu(1,:);
L_real = sum(d(AUX1+(tabu-1)*n));

L_ent

= sum(d_ent(AUX1+(tabu-1)*n));

%% Verificacion de una mejor trayectoria.%%


[minimo1,I1] = min(L_real);
[minimo2,I2] = min(L_ent);
% Verificacion de la mejor trayectoria global.
Index = [];
if (minimo1<L_Best)&&(L_ent(I1)==L_Best_ent),
Index = I1;
elseif minimo2 < L_Best_ent,
Index = I2;
end,
if ~isempty(Index),
Best = tabu(:,Index)';
L_Best = L_real(Index);
L_Best_ent = L_ent(Index);
end,
%% Calculo de las mejores trayectorias para refuerzo elitista %%
if (GA==1)
[AUX2,Index_L] = sort(L_ent);
tabu_e(:,L_e-AUX2(1:elitist)>0) = tabu(:,...
Index_L(L_e-AUX2(1:elitist)>0));
L_e(L_e-AUX2(1:elitist)>0) = AUX2(L_e-AUX2(1:elitist)>0);
end,
%% Regla de Actualizacion del Rastro de Feromona.%%
% Se hace para cada arco (j,i) que pertenece a la trayectoria de cada
% hormiga.
delta_tao = 0*delta_tao;
for k=1:m,
AUX0(1:end-1) = tabu(2:end,k); AUX0(end) = tabu(1,k);
delta_tao(AUX0'+(tabu(:,k)-1)*n) = delta_tao(AUX0'+...
(tabu(:,k)-1)*n) + Q/L_ent(k);
if (Symmetric)
delta_tao(tabu(:,k)+(AUX0'-1)*n) = delta_tao(tabu(:,k)+...
(AUX0'-1)*n) + Q/L_ent(k);
end,
end,
% Se hace para cada arco (j,i) que pertenece a la mejor trayectoria
% hasta ahora encontrada. Estrategia "Hormigas Elitistas".
tao_elitist = 0*tao_elitist;
if (GA==0),
Factor = elitist;
AUX0(1:end-1) = Best(2:end); AUX0(end) = Best(1);
tao_elitist(AUX0+(Best-1)*n) = Q/L_Best_ent;
if (Symmetric),
tao_elitist(Best+(AUX0-1)*n) = Q/L_Best_ent;
end,
else
Factor = 1;
for k = 1:elitist,
AUX0(1:end-1) = tabu_e(2:end,k); AUX0(end) = tabu_e(1,k);
tao_elitist(AUX0'+(tabu_e(:,k)-1)*n) = tao_elitist(AUX0'+(tabu_e(:,k)-1)*n)

+...

Q/L_e(k);
if (Symmetric),
tao_elitist(tabu_e(:,k)+(AUX0'-1)*n) = tao_elitist(tabu_e(:,k)+...
(AUX0'-1)*n)+Q/L_e(k);
end,

end,
end,

%% Actualizacion del rastro de feromona total.


tao = (1-rho)*tao+delta_tao+Factor*tao_elitist;
%% Graficas y Captura de proyecciones.%%
[GAL1,GAL2] = ACO_graph(handles,PS,Best,L_Best,L_Best_ent,L_ent,GAL1,GAL2,t);
end,

You might also like