I have found the minimum cost of the production matrix by using PSO,the code is below. Now I want to do the same with ACO. I am highly obliged if anybody help me in doing so
Afficher commentaires plus anciens
clear all;clc;close all;
A=360;
h =10;
l=40;
o=20;
p=1;
%%Production Matrix
D=[60 80 60 100 90 60 90 50 60 50
70 80 70 60 60 70 80 60 80 80
50 60 80 80 70 90 50 80 90 80
80 90 100 70 80 80 70 100 70 60
100 60 50 90 50 100 90 90 50 70];
[p t]=size(D);
iterations=100;
population=50;
xmax=1.25*D;
xmin=0.75.*D;
N = population;
N_GER = iterations;
PHI1 = 1.5;
PHI2 = 1.5;
W = 1;
v=zeros(p,t,N);
X_M AX = xmax;
X_MIN = xmin;
vmin=((xmax)-(xmin))/(N*5);
vmax=((xmax)-(xmin))/(N*5);
gBest = zeros(p,t);
gbestvalue = 10000000000+ zeros(p,t);
gaux = ones(p,t,N);
xBest=zeros(p,t,N);
fitBest=zeros(N,1);
fit = zeros(N,1);
nger=1;
x=initSwarm(D,N,p,t, X_MIN, X_MAX);
for j=1:N
[Cost(j), Icost(j), Scost(j),
Ocost(j)]=Obj_func(D,x(:,:,j),A,h,l,o,p);
fitBest(j)=Cost(j);
end
[a,b]=min(Cost);
gBest=x(:,:,b);
gbestvalue = Cost(b);
gIbest=Icost(b);
gSbest=Scost(b);
gObest=Ocost(b);
xBest = x;
while(nger<=N_GER)
i=1;
for k=1:N
randnum1 = rand ([p,t]);
randnum2 = rand ([p,t]);
v(:,:,k) = W.*v(:,:,k)+ randnum1.*(PHI1.*(xBest(:,:,k)-x(:,:,k)))
+ randnum2.*(PHI2.*(gBest-x(:,:,k)));
for i=1:p
for j=1:t
v(i,j,k) = ( (v(i,j,k) <= vmin(i,j)).*vmin(i,j) ) +
( (v(i,j,k) > vmin(i,j)).*v(i,j,k) );
v(i,j,k) = ( (v(i,j,k) >= vmax(i,j)).*vmax(i,j) ) +
( (v(i,j,k) < vmax(i,j)).*v(i,j,k) );
end
end
x(:,:,k) = ceil(x(:,:,k)+abs(v(:,:,k)));
for i=1:p
for j=1:t
if x(i,j,k) < X_MIN(i,j)
x(i,j,k) = ceil(X_MIN(i,j));
elseif x(i,j,k) > X_MAX(i,j)
x(i,j,k) = floor(X_MAX(i,j));
end
end
end
x(:,:,k)=Repair_Strategy(D,x(:,:,k));
% a(:,:,k)=x(:,:,k)
end
while(i<=N)
if(i==N)
for j=1:N
[Cost(j), Icost(j), Scost(j),
Ocost(j)]=Obj_func(D,x(:,:,j),A,h,l,o,p);
fit(j)=Cost(j);
if fit(j) < fitBest(j)
fitBest(j) = fit(j);
xBest(:,:,j) = x(:,:,j);
Ibest(j)=Icost(j);
Sbest(j)=Scost(j);
Obest(j)=Ocost(j);
end
end
[a,b]=min(fit);
if (fit(b) < gbestvalue)
gBest=x(:,:,b);
gbestvalue = fit(b);
gIbest=Ibest(b);
gSbest=Sbest(b);
gObest=Obest(b);
end
end
i=i+1;
end
GenBest(nger)=gbestvalue;
nger=nger+1;
end
figure
plot(GenBest),shg,grid
xlabel('\bfIteration#')
ylabel('\bfF value')
gbest=gBest
gfunc=gbestvalue
gIcost=gIbest
gScost=gSbest
gOcost=gObest
function [swarm]=initSwarm(D,N, p,t, V_MIN, V_MAX)
swarm = zeros(p,t,N);
for k = 1: N
for i=1:p
for j=1:t
temp(i,j,k) = abs(floor(rand(1,1) * ( V_MAX(i,j)-V_MIN(j) ) + V_MIN(i,j)));
end
end
swarm(:,:,k)=Repair_Strategy(D,temp(:,:,k));
end
function [Cost, Icost, Scost, Ocost]=Obj_func(D,Qr,A,h,l,o,p)
[p t]=size(D);
for i=1:p
I(i,1)=0;
for j=2:t
I(i,j)=I(i,j-1)+Qr(i,j)-D(i,j);
Inv(i,j)=max(I(i,j),0);
Shrt(i,j)=abs(min(I(i,j),0));
end
end
for j=1:t
q(j)=sum(Qr(:,j));
SInv(j)=sum(Inv(:,j));
Sshrt(j)=sum(Shrt(:,j));
Ocst(j)=max(q(j)*p-A);
end
Icost=h.*sum(SInv);
Scost=l.*sum(Sshrt);
Ocost=o.*sum(Ocst);
Cost=Icost+Scost+Ocost;
function Mout=Repair_Strategy(D,Q)
[p t]=size(D);
for i=1:p
SumD(i)=sum(D(i,:));
SumQ(i)=sum(Q(i,:));
end
DiffQD=SumQ-SumD;
Del=floor(DiffQD/t);
for i=1:p
for j=1:t-1
Qd(i,j)=Q(i,j)-Del(i);
end
sumN(i)=sum(Qd(i,:));
Qd(i,t)=SumD(i)-sumN(i);
end
Mout=Qd;
4 commentaires
Thomas
le 28 Mai 2012
what exactly is your question? what errors are u seeing? we cannot test anything with out the complete code, in this case the function initSwarm...
amir saeed
le 30 Mai 2012
Walter Roberson
le 30 Mai 2012
You have not described the goal, the problem that you are trying to solve. All you have said is that you do not know how to proceed.
amir saeed
le 31 Mai 2012
Réponses (1)
Walter Roberson
le 1 Juin 2012
0 votes
Your existing question is still here and still active. Your duplicate question has been deleted. You may wish to edit the above existing question to remove any redundant information.
Please do not open duplicate questions; it just adds to confusion and annoys the volunteers who answer questions.
Catégories
En savoir plus sur Traveling Salesman (TSP) dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!