⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 蚁群c代码1.cpp

📁 蚁群算法 C语言源程序 在Windows XP下通过编译visualC++ 6
💻 CPP
字号:
# include <iostream.h> 
# include <fstream.h> 
# include <math.h> 
# include <time.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[N];
	double y[N];
	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(int 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)); //(unsigned int)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++) 
					bestroute[s]=route[k][s];// Bestsolution[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(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; 
				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(); 
} 

void initparameter(void){
	alfa=1; beta=5; rou=0.9; Q=100;
	Ncmax=200; 
} 

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; 
				}
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -