#1楼 得分:0回复于:2009-10-19 09:47:16
程序太长,接着发:
/**************************************************************/
AntColonySystem::AntColonySystem(double alpha,double beta,double rho,double q0,double alpha1)
{
ALPHA=alpha;
BETA=beta;
RHO=rho;
Q0=q0;
ALPHA1=alpha1;
}
//设定初始的信息量
inline double AntColonySystem::calc_tau0()
{
double best_length=(double)N*max_dist();
for(int n=0;n<N;n++)
{
NNAnt*nnANT=new NNAnt(n);
Tour*tour;
tour=nnANT->search();
double tour_length=calc_length(tour);
if(tour_length<best_length)
best_length=tour_length;
delete nnANT;
}
return 1.0/((double)best_length);
//在蚁群系统中为return 1.0/((double)best_length);
}
//初始化tau0
inline void AntColonySystem::init_tau_by_value(double value)
{
TAU0=value;
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
TAU[i][j]=TAU0;
}
//用矩阵表示的初始信息量
inline void AntColonySystem::init_tau_by_matrix(doubleMatrix matrix)
{
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
TAU[i][j]=matrix[i][j];
}
inline void AntColonySystem::init_uniform()
{
//蚂蚁均匀分布在城市上
for(int k=0;k<M;k++)
ANTS[k]=new ACSAnt(this,(k%N));
}
inline void AntColonySystem::init_random()
{
//随机分布
for(int k=0;k<M;k++)
ANTS[k]=new ACSAnt(this,(int)((double)N*(rand()/(double)RAND_MAX)));
}
//每个城市最多有一只蚂蚁
inline void AntColonySystem::init_randomMOAPC()
{
//randomly distributed with MOPAC(most one ant per city)
bool MOAPCarray[N];
assert(M<=N);
for(int n=0;n<N;n++)
MOAPCarray[n]=false;
for(int k=0;k<N;k++)
{
int c;
do
{
c=(int)((double)N*(rand()/(double)RAND_MAX));
}
while(MOAPCarray[c]);
MOAPCarray[c]=true;
ANTS[k]=new ACSAnt(this,c);
}
}
//n值的确定
inline double AntColonySystem::ETA(int i,int j)
{
return(1.0/D[i][j]);
}
//概率转移规则
inline double AntColonySystem::transition(int i,int j)
{
if(i!=j)
return(pow(TAU[i][j],ALPHA1)*pow(ETA(i,j),BETA));
else
return(0.0);
}
inline double AntColonySystem::sum_transition(int i,int allowed[])
{
double sum=0.0;
for(int j=0;j<N;j++)
sum+=((double)allowed[j]*transition(i,j));
return (sum);
}
//在蚁群系统中执行如下操作
inline void AntColonySystem::local_update_rule(int i,int j)
{
Q0+=TAU[i][j];
//symmetric TSP
TAU[j][i]=TAU[i][j];
}
//在蚂蚁系统中不执行上述操作
//清楚全局更新轨迹量
inline void AntColonySystem::clear_global_update()
{
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
dTAU[i][j]=0.0;
}
//进行全局更新
inline void AntColonySystem::add_global_update(Tour*tour,double length)
{
for(int n=0;n<N;n++)
{
int i=(*tour)[n][0];
int j=(*tour)[n][1];
dTAU[i][j]+=(1.0/length);
//symmetric TSP
dTAU[j][i]+=(1.0/length);
}
}
//全局更新规则
inline void AntColonySystem::global_update_rule()
{
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
TAU[i][j]=(1.0-ALPHA)*TAU[i][j]+ALPHA*dTAU[i][j];
}
//用矩阵表示更新后的轨迹量
inline doubleMatrix*AntColonySystem::get_tau()
{
return &TAU;
}
//找出当前循环的最短路径,并进行全局激素更新
inline Tour*AntColonySystem::search(int T)
{
static Tour best_tour;
Tour*tour;
double best_length=(double)N*max_dist(),tour_length;
clear_global_update();
int a,b;
int t;
for(t=0;t<T;t++)
{
//printf("loop %d:\n",t);
for(int k=0;k<M;k++)
{
tour=ANTS[k]->search();
tour_length=calc_length(tour);
/*printf("\tant %d length: %f:",k,tour_length);
for(int i=0;i<N;i++)
{
printf("%d",(*tour)[i][0]);
}*/
//printf("\n");
if(tour_length<best_length)
{
a=t;
b=k;
for(int i=0;i<N;i++)
{
best_tour[i][0]=(*tour)[i][0];
best_tour[i][1]=(*tour)[i][1];
}
best_length=tour_length;
clear_global_update();
add_global_update(tour,tour_length);
printf("[%d/%d]:%lf\n",t,T,tour_length);
}
}
global_update_rule();
}
//printf("[%d/%d]best tour(length=%f):\n",t,T,best_length);
//printf_tour(best_tour);
//printf('[%d/%d]iterations done \n',t,T);
printf("best_length:%f,%d %d\n",best_length,a,b);
for(int i=0;i<N;i++)
{
printf("%d",best_tour[i][0]);
}
return(&best_tour);
}
//产生随机数
int main(int argc,char*argv[])
{
//PRNG initalisieren
time_t timer,timer1;
time(&timer);
//pid_t pid=getpid()+getppid();
unsigned long seed=timer;
/*if(seed==0)
{
time(&timer);
seed=7*timer*pid;
if(seed==0)seed=pid;
else seed=seed%56000;
}*/
seed=seed%56000;
srand((unsigned int)seed);
calc_dist();
//Ant Colony System
AntColonySystem*acs=new AntColonySystem(0.999,5.0,1.0,0.0,1.0);
double tau0=acs->calc_tau0();
acs->init_tau_by_value(tau0);
acs->init_uniform();
acs->search(10000);
time(&timer1);
int t=timer1-timer;
printf("\n time used: %d s",t);
return (0);
}
系统提示错误如下:
--------------------Configuration: ant - Win32 Debug--------------------
Linking...
ant.obj : error LNK2001: unresolved external symbol "public: int (* __thiscall ACSAnt::search(void))[20][2]" (?search@ACSAnt@@QAEPAY1BE@1HXZ)
Debug/ant.exe : fatal error LNK1120: 1 unresolved externals