# include <iostream.h>
# include <fostream.h>
# include <math.h>
# include <conio.h>
# include <stdlib.h>
# include <iomanip.h>
# define N 20 //city size
# define M 10 //ant number
double inittao=1;
double tao[N][N];
double detatao[N][N];
double distance[N][N];
double yita[N][N];
int tabu[M][M];
int route[M][N];
double solution[M];
int bestroute[N];
double Bestsolution=10000000000;
double alfa,beta,rou,Q;
int Ncmax;
void initparameter();
double Evaluesolution(int *a);
void Incityxy(double x[],double y[]);
void main()
{
int NC=0;
initparameter();
double x[];
double y[];
Incityxy(x,y);
for(int i=0;i<N;i++)
for(int j=i+1;j<N;j++)
{
distance[j][i]=sqrt((x[i]-x[j])*(x[i]-x[j])-(y[i]-y[j])*(y[i]-y[j]));
distance[i][j]=distance[j][i];
}
for(i=0;i<N;i++)
for(j=0;j<N;j++)
{
tao[i][j]=inittao;
if(j!=i)
yita[i][j]=100/(distance[i][j]+50-int(distance[i][j])%50)
}
for(int k=0;k<M;k++)
for(i=0;i<N;i++)
route[k][i]=-1;
srand(time(NULL));
for(k=0;k<M;k++)
{
route[k][0]=(k+N)%N;
tabu[k][route[k][0]]=1;
}
do {
int s=1;
double partsum;
double pper;
double drand;
while(s,N)
{
for(int k=0;k<M;k++)
{
int jrand=rand()%3000;
drand=double(jrand)/3001;
partsum=0;
pper=0;
for(int j=0;j<N;j++)
{
if(tabu[k][j]==0)
partsum+=pow(tao[route[k][s-1]][j],alfa)*pow(yita[route[k][s-1]][j],beta);
if(pper>drand)
break;
}
tabu[k][j]=1;
route[k][s]=j;
}
s++;
}
for(i=0;i<N;i++)
for(int j=0;j<N;j++)
detatao[i][j]=0;
for(int k=0;k<M;k++)
solution[k]=Evaluesolution(route[k]);
for(k=1;k<M;k++)
if(solution[k]<Bestsolution)
{
Bestsolution=solution[k];
for(s=0;s<N;s++)
Bestsolution[s]=route[k][s];
}
for(k=0;k<M;k++)
{
for(s=0;s<N-1;s++)
detatao[route[k][s]][route[k][s]]+=Q/solution[k];
detatao[route[k][N-1]][route[k][0]]+=Q/solution[k];
}
for(i=0;i<N;i++)
for(j=0;j<N;j++)
{
tao[i][j]=rou*tao[i][j]+detatao[i][j];
if(tao[i][j]<0.00001)
tao[i][j]=0.00001;
if(tao[i][j]>20)
tao[i][j]=20;
}
for(k=0;k<M;k++)
for(int j=1;j<N;j++)
{
tabu[k][route[k][j]]=0;
route[k][j]=-1;
}
NC++;
} while(NC<Ncmax);
fstream result;
result.open("optimal_results.dat",ios::app);
if(!result)
{
cout<<"can't open the 'optimal_results' file\n";
abort();
}
result<<"************************************"<<endl;
result<<"the initialized parameters of aca are as follows:"<<endl;
result<<"alfa="<<alfa<<",beta="<<beta<<endl;
result<<"Q="<<Q<<", the maximum iteration number of aca is:"<<Ncmax<<endl;
result<<"the evaporation parameter of aca is:"<<rou<<endl;
result<<"**************************************"<<endl;
for(i=0;i<N;i++)
result<<bestroute[i]<<".";
result<<endl;
result.close();
}
double Evaluesolution(int *a)
{
double dist=0;
for(int i=0;i<N;i++)
dist+=distance[*(a+(i+N)%N)][*(a+(i+N+1)%N)];
return dist;
}
void Incityxy(double x[],double y[])
{
fstream inxyfile;
inxyfile.open("city_coordinates.dat",ios::in);
if(!inxyfile)
{
cout<<"can't open the city_coordinates' file\n";
abort();
}
char ch1,ch2;
while(!inxyfile.eof())
{
inxyfile.get(ch1);
if(ch1>='0')
break;
int i=0,j=0;
x[0]=0;y[0]=0;
while(!inxyfile.eof())
{
inxyfile.get(ch1);
if(ch1>='0'&&ch1<='9')
{
ch2=ch1;
while(ch2>='0'&&ch2<='9')
{
switch(i)
{
case 0:break;
case 1:x[j]=x[j]*10+(double(ch2)-48);break;
case 2:y[j]=y[j]*10+(double(ch2)-48);break;
}
inxyfile.get(ch2);
}
i=(++i)%3;
if(i==0&&j<N-1)
{
j++;
x[j]=0;
y[j]=0;
}
}
}
}
}
# include <fostream.h>
# include <math.h>
# include <conio.h>
# include <stdlib.h>
# include <iomanip.h>
# define N 20 //city size
# define M 10 //ant number
double inittao=1;
double tao[N][N];
double detatao[N][N];
double distance[N][N];
double yita[N][N];
int tabu[M][M];
int route[M][N];
double solution[M];
int bestroute[N];
double Bestsolution=10000000000;
double alfa,beta,rou,Q;
int Ncmax;
void initparameter();
double Evaluesolution(int *a);
void Incityxy(double x[],double y[]);
void main()
{
int NC=0;
initparameter();
double x[];
double y[];
Incityxy(x,y);
for(int i=0;i<N;i++)
for(int j=i+1;j<N;j++)
{
distance[j][i]=sqrt((x[i]-x[j])*(x[i]-x[j])-(y[i]-y[j])*(y[i]-y[j]));
distance[i][j]=distance[j][i];
}
for(i=0;i<N;i++)
for(j=0;j<N;j++)
{
tao[i][j]=inittao;
if(j!=i)
yita[i][j]=100/(distance[i][j]+50-int(distance[i][j])%50)
}
for(int k=0;k<M;k++)
for(i=0;i<N;i++)
route[k][i]=-1;
srand(time(NULL));
for(k=0;k<M;k++)
{
route[k][0]=(k+N)%N;
tabu[k][route[k][0]]=1;
}
do {
int s=1;
double partsum;
double pper;
double drand;
while(s,N)
{
for(int k=0;k<M;k++)
{
int jrand=rand()%3000;
drand=double(jrand)/3001;
partsum=0;
pper=0;
for(int j=0;j<N;j++)
{
if(tabu[k][j]==0)
partsum+=pow(tao[route[k][s-1]][j],alfa)*pow(yita[route[k][s-1]][j],beta);
if(pper>drand)
break;
}
tabu[k][j]=1;
route[k][s]=j;
}
s++;
}
for(i=0;i<N;i++)
for(int j=0;j<N;j++)
detatao[i][j]=0;
for(int k=0;k<M;k++)
solution[k]=Evaluesolution(route[k]);
for(k=1;k<M;k++)
if(solution[k]<Bestsolution)
{
Bestsolution=solution[k];
for(s=0;s<N;s++)
Bestsolution[s]=route[k][s];
}
for(k=0;k<M;k++)
{
for(s=0;s<N-1;s++)
detatao[route[k][s]][route[k][s]]+=Q/solution[k];
detatao[route[k][N-1]][route[k][0]]+=Q/solution[k];
}
for(i=0;i<N;i++)
for(j=0;j<N;j++)
{
tao[i][j]=rou*tao[i][j]+detatao[i][j];
if(tao[i][j]<0.00001)
tao[i][j]=0.00001;
if(tao[i][j]>20)
tao[i][j]=20;
}
for(k=0;k<M;k++)
for(int j=1;j<N;j++)
{
tabu[k][route[k][j]]=0;
route[k][j]=-1;
}
NC++;
} while(NC<Ncmax);
fstream result;
result.open("optimal_results.dat",ios::app);
if(!result)
{
cout<<"can't open the 'optimal_results' file\n";
abort();
}
result<<"************************************"<<endl;
result<<"the initialized parameters of aca are as follows:"<<endl;
result<<"alfa="<<alfa<<",beta="<<beta<<endl;
result<<"Q="<<Q<<", the maximum iteration number of aca is:"<<Ncmax<<endl;
result<<"the evaporation parameter of aca is:"<<rou<<endl;
result<<"**************************************"<<endl;
for(i=0;i<N;i++)
result<<bestroute[i]<<".";
result<<endl;
result.close();
}
double Evaluesolution(int *a)
{
double dist=0;
for(int i=0;i<N;i++)
dist+=distance[*(a+(i+N)%N)][*(a+(i+N+1)%N)];
return dist;
}
void Incityxy(double x[],double y[])
{
fstream inxyfile;
inxyfile.open("city_coordinates.dat",ios::in);
if(!inxyfile)
{
cout<<"can't open the city_coordinates' file\n";
abort();
}
char ch1,ch2;
while(!inxyfile.eof())
{
inxyfile.get(ch1);
if(ch1>='0')
break;
int i=0,j=0;
x[0]=0;y[0]=0;
while(!inxyfile.eof())
{
inxyfile.get(ch1);
if(ch1>='0'&&ch1<='9')
{
ch2=ch1;
while(ch2>='0'&&ch2<='9')
{
switch(i)
{
case 0:break;
case 1:x[j]=x[j]*10+(double(ch2)-48);break;
case 2:y[j]=y[j]*10+(double(ch2)-48);break;
}
inxyfile.get(ch2);
}
i=(++i)%3;
if(i==0&&j<N-1)
{
j++;
x[j]=0;
y[j]=0;
}
}
}
}
}