展會(huì)信息港展會(huì)大全

請(qǐng)教大家一個(gè)用蟻群算法解決TSP問(wèn)題的程序
來(lái)源:互聯(lián)網(wǎng)   發(fā)布日期:2011-08-18 14:39:05   瀏覽:5572次  

導(dǎo)讀:本人在做一個(gè)調(diào)度 課題 我把問(wèn)題抽象成了TSP問(wèn)題 從資料中找了點(diǎn)程序 經(jīng)過(guò)調(diào)試 語(yǔ)法錯(cuò)誤已排除 就是還有邏輯錯(cuò)誤 連接成EXE文件 時(shí)候出了問(wèn)題 請(qǐng)大家指點(diǎn) 程序如...

#1樓 得分:0回復(fù)于:2009-10-19 09:47:16

程序太長(zhǎng),接著發(fā):
/**************************************************************/
AntColonySystem::AntColonySystem(double alpha,double beta,double rho,double q0,double alpha1)
{
ALPHA=alpha;
BETA=beta;
RHO=rho;
Q0=q0;
ALPHA1=alpha1;
}
//設(shè)定初始的信息量
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);
//在蟻群系統(tǒng)中為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()
{
//隨機(jī)分布
for(int k=0;k<M;k++)
ANTS[k]=new ACSAnt(this,(int)((double)N*(rand()/(double)RAND_MAX)));
}
//每個(gè)城市最多有一只螞蟻
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]);
}
//概率轉(zhuǎn)移規(guī)則
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);
}
//在蟻群系統(tǒng)中執(zhí)行如下操作
inline void AntColonySystem::local_update_rule(int i,int j)
{
Q0+=TAU[i][j];
//symmetric TSP
TAU[j][i]=TAU[i][j];
}
//在螞蟻系統(tǒng)中不執(zhí)行上述操作
//清楚全局更新軌跡量
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;
}
//進(jìn)行全局更新
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);
}
}
//全局更新規(guī)則
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;
}
//找出當(dāng)前循環(huán)的最短路徑,并進(jìn)行全局激素更新
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);
}
//產(chǎn)生隨機(jī)數(shù)
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);
}
系統(tǒng)提示錯(cuò)誤如下:
--------------------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

贊助本站

相關(guān)內(nèi)容
AiLab云推薦
展開(kāi)

熱門(mén)欄目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能實(shí)驗(yàn)室 版權(quán)所有    關(guān)于我們 | 聯(lián)系我們 | 廣告服務(wù) | 公司動(dòng)態(tài) | 免責(zé)聲明 | 隱私條款 | 工作機(jī)會(huì) | 展會(huì)港