蚁群算法 Delphi 源程序:
{*Ant algorithm for VRP —Ant cycle, Ant density, Ant quantity*}
const
inf=99999999; eps=1E-8
type
item=integer;
var
FN:string; f:System. Text;
procedure T_VRPANT_RUN;
const maxn=500; ruo=0.7; Q=10;
label loop;
type
item2=real;
Arr1=array of array of item;
Arr2=array of array of item2;
Arr3=array of array of boolean;
Arr4=array of array of item;
Arr5=array of array of item2;
n,i,j,k,l,ii,jj,count,s,maxcount,tweight,index,model,qq,capa,m,
Last,selected,tm,weight:item;tmax,tmin:item2;datatype:byte;
W,route,opt,cycle:arrl;t,dt:arr2;ch:arr3;x,y:arr5;
Len,tlen,nearest,series,demand,kcount,tkcount:arr4;
var
function PValue(i,j,k:item):item2
var 1:item; sum:item2;
begin
Sum:=0;
For 1:=2 to n do
If(capa>demand[1])and(ch[1]and(cycle[k,l]=0)and(1< >i)then
Sum:=sum+t[i,1]/w[i,l];
If(sum>eps)and(cycle[k,j]=0)and(j< >i)then
ahead,i,i1,i2,index,j,j1,j2,last,limit,max,next,s1,s2,t1,t2,maxtemp:item;pt:arr4;
Sun:t[i,j]/ w[i,l]/sum
PValue:=sum;
end;
procedure TwoOpt(p:item);
var
begin
setLength(ph,n+1);
t1:=1; t2:=1; sl:=1; s2:=1;
for I:=1 to p-1 do pt[route[k,i]:=route[k,i+1];
pt[route[k,p]:=route[k,l];
repeat
maxtemp:=0; i1:=1;
for i:=1 to p-2 do
begin
if i=1 then limit:=p-1 else limit:=p;
i2:=pt[i1]; j1:=pt[i2];
for j:=i+2 to limit do
begin
j2:=pt[j1]
max:=w[i1,i2]+w[j1,j2]-(w[i1,j1]=+w[i2,j2]);
if(max>maxtemp)then
begin
s1:=i1; s2:=i2; t1=j1; t2=j2; maxtemp:=max;
end;
j1:=j2;
end;
i1:=i2;
end;
if ( maxtemp>0) then
begin
pt[s1]:=t1; next:=s2; last:= t2;
repeat
ahead:=pt[next];pt[next]:=last;
last:=next;next:=ahead;
until next=t2;
end;
until(maxtemp=0);
index:=1;
for i:=1 to p do
begin
end;
end;
route[k,i]:=index; index:=pt[index];
procedure Antmove;
label lop, select, check,next;
var a,j,k:item;
begin
k:=1; capa:=qq; last:=n-1;
for j:=1 to last do series[j]:=j+1;
for j:=1 to last do ch[j]:=true
for j:=1 to last do kcount[j]:=0;
lop:
select:
nearest[k]:=1;
for j:=1 to n do cycle[k,j]:=0;
a:=nearest[k]; j:=1;
while j< =last do
begin
index:=0;
selected:=random(last)+1;
if (capa> =demand[series[selected]])then
begin
index:=series[selected];
if (random
=1 then goto select;
if last>=1 then;
begin k:=k+1; capa:=qq; goto lop; end;
m:=k;
next:
end;
begin
AssignFile(f,FN); Reset(f);
{$I-}Readln(f,n,datatype,qq,maxcount); {$I+}
If(IOResult< >0)or(n<4)or(n>maxn)or(maxcount<1)or(datatype<1)
or(datatype>2)or(qq<=0) then
begin ShowMessage(‘数据错误’); System.Close(f); exit; end;
SetLength(t,n+1,n+1);
SetLength(dt,n+1,n+1);
SetLength(w,n+1,n+1);
SetLength(opt,n+1,n+1);
SetLength(route,n+1,n+1);
SetLength(cycle,n+1,n+1);
If datatype=1 then
begin
SetLength(x,n+1); SetLength(y,n+1);
for i:=1 to n do
begin
{$I-}Readln(f,ii,x[i],y[i]); {$I+}
If(IOResult< >0)or(ii< >i) then
Begin Show Message(‘数据错误’); System.Close(f); exit; end;
end;
for i:=1 to n-1 do for j:=i+1 to n do
begin
w[i,j]:=trunc(sprt(spr(x[i]-x[j])+spr(y[i]-y[j]))+0.5);
w[j,i]:=w[i,j]; t[i,j]:=1; dt[i,j]:=0;
t[j,i]:=t[i,j]; dt[j,i]:=dt[i,j];
{$I-}Readln(f,ii,jj,w[i,j]); {$I+}
If(IOResult< >0)or(ii< >i) or(jj< >j)or(w[i,j]<1)then
begin Show Message(‘数据错误’); System.Close(f); exit; end;
w[j,i]:=w[i,j]; t[i,j]:=1; dt[i,j]:=0;
t[j,i]:=t[i,j]; dt[j,i]:=dt[i,j];
end;
for i:=1 to n do
begin
w[i,i]:=inf; t[i,i]:=1; dt[i,i]:=0;
end;
SetLength(x,0); SetLength(y,0);
end
else
begin
for i:=1 to n-1 do for j:=i+1 to n do
begin
end;
for i:=1 to n do
begin
w[i,i]:=inf; t[i,i]:=1; dt[i,i]=0;
end;
end;
SetLength(len,n+1);
SetLength(tlen,n+1);
SetLength(series,n+1);
SetLength(nearest,n+1);
SetLength(tkcount,n+1,n+1);
SetLength(demand,n+1);
SetLength(kcount,n+1);
SetLength(ch,n+1);
demand[1]:=0;
for i:=2 to n do
begin
{$I-}Readln(f,ii,demand[i];{$I+}
If(IOResult< >0)or(ii< >i)or(demand[i]>qq)or(demand[i]<0)then
1oop:
AntMove;
weight:=0;
for k:=1 to m do len[k]:=0;
for k:=1 to m do
begin
index:=1;
for i:=1 to kcount[k]+1 do
begin
route[k,i]:=index; index:=cycle[k,index];
end;
TwoOpt(kcount[k]+1);
Len[k]:=w[route[k,kcount[k]+1],route[k,1]];
for i:=1 to kcount[k] do len[k]:=len[k]+w[route[k,i],route[k,i+1]];
weight:=weight+len[k];
begin Show Message(‘数据错误’); System.Close(f); exit; end;
end
System.Close(f);
FN:=Copy(FN,1,Length(FN)-4)+’.OUT’;
Show Message(’输出结果存入文件:’+FN);
AssignFile(f,FN);Rewrite(f);
count:=0;
tweight:=inf;
index:=1;
tm:=inf;
randomize;
model:=random(3)+1;
end;
if m< tm then
begin
tm:=m; tweight:=weight;
for k:=1 to tm do
begin
tkcount[k]:=kcount[k];
for j:=1 to tkcount[k]+1 do opt[k,j]:=route[k,j];
tlen[k]:=len[k];
end;
end;
if m=tm then if tweight>weight then
begin
tweight:=weight;
for k:=1 to tm do
begin
tkcount[k]:=kcount[k];
for j:=1 to tkcount[k]+1 do opt[k,j]:=route[k,j];
tlen[k]:=len[k];
end;
end;
for k:=1 to tm do
begin
case model of
1:
begin
for 1:=1 to kcount[k] do
begin
ii:=route[k,l];jj=route[k,1+1];
dt[ii,jj]:=dt[ii,jj]+q/len[k];
end;
ii:=route[k,kcount[k]+1]; jj=route[k,1];
dt[ii,jj]:=dt[ii,jj]+q/len[k];
end;
begin
for 1:=1 to kcount[k] do
begin
ii:=route[k,l]; jj:=route[k,1+1];
dt[ii,jj]:=dt[ii,jj]+q;
end;
ii:=route[k,kcount[k]+1]; jj:=route[k,1];
dt[ii,jj]:=dt[ii,jj]+q;
for 1:=1 to kcount[k] do
begin
ii:=route[k,1]; jj:=route[k,l+1]
dt[ii,jj]:=dt[ii,jj]+q/w[ii,jj];
end;
ii:=route[k,kcount[k]+1]; jj:=route[k,1];
dt[ii,jj]:=dt[ii,jj]+q/w[ii,jj];
2:
3:
end;
begin
end
end
end;
for i:=1 to n do for j:=1 to n do
begin
t[i,j]:=ruo*t[i,j]+dt[i,j];
tmax:=1/(tweight*(1-ruo); tmin=tmax/5;
if (t[i,j]>tmax) then t[i,j]:=tmax;
if (t[i,j]
for i:=1 to n do for j:= 1 to n do dt[i,j]:=0;
if count