蚁群算法 C语言程序(已运行)
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
fstream result;
result.open("optimal_results.log", ios::app);
if(!result)
{
cout<<"can't open the <optimal_results.log> file!\n";
exit(0);
}
result<<"*-------------------------------------------------------------------------*"<<endl;
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;
}Biblioteka Baidu
NC++;
} while(NC<NcMax);
//output the calculating results
while(s<N)
{
for(k=0;k<M;k++)
{
int jrand=rand()%3000;
drand=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);
result<<"the shortest length of the path is:"<<BestSolution<<endl;
result<<"the best route is:"<<endl;
for(i=0;i<N;i++)
result<<BestRoute[i]<<" ";
result<<endl;
{
inxyfile>>x[i]>>y[i];
if( ++i >= N ) break;
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/lyp2003ok/archive/2009/01/04/3706252.aspx
result<<"the initialized parameters of ACA are as follows:"<<endl;
result<<"alfa="<<alfa<<", beta="<<beta<<", rou="<<rou<<", Q="<<Q<<endl;
result<<"the maximum iteration number of ACA is:"<<NcMax<<endl;
//Basic Ant Colony Algorithm for TSP
#include <iostream.h>
#include <fstream.h>
#include <math.h>
#include <time.h>
#include <conio.h>
#include <stdlib.h>
#include <iomanip.h>
#define N 31 //city size
#define M 31 //ant number
double inittao=1;
double tao[N][N];
double detatao[N][N];
double distance[N][N];
double yita[N][N];
int tabu[M][N];
result<<"*-------------------------------------------------------------------------*"<<endl<<endl;
result.close();
cout<<"the shortest length of the path is:"<<BestSolution<<endl;
}
for(j=0;j<N;j++)
{
if(tabu[k][j]==0)
pper+=pow(tao[route[k][s-1]][j],alfa)*pow(yita[route[k][s-1]][j],beta)/partsum;
if(pper>drand)
break;
}
tabu[k][j]=1;
route[k][s]=j;
int route[M][N];
double solution[M];
int BestRoute[N];
double BestSolution=10000000000;
double alfa,beta,rou,Q;
int NcMax;
void initparameter(void); // initialize the parameters of basic ACA
for(i=0;i<N;i++)
for(int j=0;j<N;j++)
{
tao[i][j]=inittao;
if(j!=i)
yita[i][j]=100/distance[i][j];
}
for(int k=0;k<M;k++)
for(i=0;i<N;i++)
route[k][i]=-1;
srand(time(NULL));
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];
}
// calculate the heuristic parameters
double EvalueSolution(int *a); // evaluate the solution of TSP, and calculate the length of path
void InCityXY( double x[], double y[], char *infile ); // input the nodes' coordinates of TSP
{
fstream inxyfile( infile, ios::in | ios::nocreate );
if( !inxyfile )
{
cout<<"can't open the <"<<infile<<"> file!\n";
exit(0);
}
int i=0;
while( !inxyfile.eof() )
detatao[route[k][N-1]][route[k][0]]+=Q/solution[k];
}
for(i=0;i<N;i++)
for(int 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;
}
s++;
}
// the pheromone is updated
for(i=0;i<N;i++)
for(int j=0;j<N;j++)
detatao[i][j]=0;
for(k=0;k<M;k++)
{
solution[k]=EvalueSolution(route[k]);
if(solution[k]<BestSolution)
}
double EvalueSolution(int *a)
{
double dist=0;
for(int i=0;i<N-1;i++)
dist+=distance[a[i]][a[i+1]];
dist+=distance[a[i]][a[0]];
return dist;
}
void InCityXY( double x[], double y[], char *infile )
{
BestSolution=solution[k];
for(s=0;s<N;s++)
BestRoute[s]=route[k][s];
}
}
for(k=0;k<M;k++)
{
for(s=0;s<N-1;s++)
detatao[route[k][s]][route[k][s+1]]+=Q/solution[k];
for(k=0;k<M;k++)
{
route[k][0]=k%N;
tabu[k][route[k][0]]=1;
}
//each ant try to find the optiamal path
do {
int s=1;
double partsum;
double pper;
double drand;
//ant choose one whole path
void initparameter(void)
{
alfa=1; beta=5; rou=0.9; Q=100;
NcMax=200;
}
void main(void)
{
int NC=0;
initparameter();
double x[N];
double y[N];
InCityXY( x, y, "city31.tsp" );
result.open("optimal_results.log", ios::app);
if(!result)
{
cout<<"can't open the <optimal_results.log> file!\n";
exit(0);
}
result<<"*-------------------------------------------------------------------------*"<<endl;
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;
}Biblioteka Baidu
NC++;
} while(NC<NcMax);
//output the calculating results
while(s<N)
{
for(k=0;k<M;k++)
{
int jrand=rand()%3000;
drand=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);
result<<"the shortest length of the path is:"<<BestSolution<<endl;
result<<"the best route is:"<<endl;
for(i=0;i<N;i++)
result<<BestRoute[i]<<" ";
result<<endl;
{
inxyfile>>x[i]>>y[i];
if( ++i >= N ) break;
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/lyp2003ok/archive/2009/01/04/3706252.aspx
result<<"the initialized parameters of ACA are as follows:"<<endl;
result<<"alfa="<<alfa<<", beta="<<beta<<", rou="<<rou<<", Q="<<Q<<endl;
result<<"the maximum iteration number of ACA is:"<<NcMax<<endl;
//Basic Ant Colony Algorithm for TSP
#include <iostream.h>
#include <fstream.h>
#include <math.h>
#include <time.h>
#include <conio.h>
#include <stdlib.h>
#include <iomanip.h>
#define N 31 //city size
#define M 31 //ant number
double inittao=1;
double tao[N][N];
double detatao[N][N];
double distance[N][N];
double yita[N][N];
int tabu[M][N];
result<<"*-------------------------------------------------------------------------*"<<endl<<endl;
result.close();
cout<<"the shortest length of the path is:"<<BestSolution<<endl;
}
for(j=0;j<N;j++)
{
if(tabu[k][j]==0)
pper+=pow(tao[route[k][s-1]][j],alfa)*pow(yita[route[k][s-1]][j],beta)/partsum;
if(pper>drand)
break;
}
tabu[k][j]=1;
route[k][s]=j;
int route[M][N];
double solution[M];
int BestRoute[N];
double BestSolution=10000000000;
double alfa,beta,rou,Q;
int NcMax;
void initparameter(void); // initialize the parameters of basic ACA
for(i=0;i<N;i++)
for(int j=0;j<N;j++)
{
tao[i][j]=inittao;
if(j!=i)
yita[i][j]=100/distance[i][j];
}
for(int k=0;k<M;k++)
for(i=0;i<N;i++)
route[k][i]=-1;
srand(time(NULL));
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];
}
// calculate the heuristic parameters
double EvalueSolution(int *a); // evaluate the solution of TSP, and calculate the length of path
void InCityXY( double x[], double y[], char *infile ); // input the nodes' coordinates of TSP
{
fstream inxyfile( infile, ios::in | ios::nocreate );
if( !inxyfile )
{
cout<<"can't open the <"<<infile<<"> file!\n";
exit(0);
}
int i=0;
while( !inxyfile.eof() )
detatao[route[k][N-1]][route[k][0]]+=Q/solution[k];
}
for(i=0;i<N;i++)
for(int 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;
}
s++;
}
// the pheromone is updated
for(i=0;i<N;i++)
for(int j=0;j<N;j++)
detatao[i][j]=0;
for(k=0;k<M;k++)
{
solution[k]=EvalueSolution(route[k]);
if(solution[k]<BestSolution)
}
double EvalueSolution(int *a)
{
double dist=0;
for(int i=0;i<N-1;i++)
dist+=distance[a[i]][a[i+1]];
dist+=distance[a[i]][a[0]];
return dist;
}
void InCityXY( double x[], double y[], char *infile )
{
BestSolution=solution[k];
for(s=0;s<N;s++)
BestRoute[s]=route[k][s];
}
}
for(k=0;k<M;k++)
{
for(s=0;s<N-1;s++)
detatao[route[k][s]][route[k][s+1]]+=Q/solution[k];
for(k=0;k<M;k++)
{
route[k][0]=k%N;
tabu[k][route[k][0]]=1;
}
//each ant try to find the optiamal path
do {
int s=1;
double partsum;
double pper;
double drand;
//ant choose one whole path
void initparameter(void)
{
alfa=1; beta=5; rou=0.9; Q=100;
NcMax=200;
}
void main(void)
{
int NC=0;
initparameter();
double x[N];
double y[N];
InCityXY( x, y, "city31.tsp" );