資源簡介
蟻群優化算法解決旅行商問題(TSP),基于c++編程平臺經過調試,能夠達到預期效果

代碼片段和文件信息
//?Ant_System_Alogrithm.cpp:?implementation?of?the?CAnt_System_Alogrithm?class.
//
//////////////////////////////////////////////////////////////////////
#include?“stdafx.h“
#include?“ASA.h“
#include?“stdlib.h“
#include?“stdio.h“
#include?“math.h“
#include?“Ant_System_Alogrithm.h“
#include?“GamblingPad.h“
#ifdef?_DEBUG
#undef?THIS_FILE
static?char?THIS_FILE[]=__FILE__;
#define?new?DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
//?Construction/Destruction
//////////////////////////////////////////////////////////////////////
CAnt_System_Alogrithm::CAnt_System_Alogrithm()
{
}
//功能:初始化環境和蟻群信息
void?CAnt_System_Alogrithm::init()
{
???FILE??*fp;
???if((fp=fopen(“el51.txt““r“))==NULL)
???{
::AfxMessageBox(“cannot?open?kroa100?file“);
????exit(0);
???}
???int?ijxy;
???for(j=0;?j ???{
??????fscanf(fp“%d%d%d“&i&x&y);
??????citypos[i-1].x=x;
??????citypos[i-1].y=y;
???}
???fclose(fp);
????
???//初始化城市距離dis_city[i][j],η[i][j]г[i][j]
???for(i=0;?i ???{
??dis_city[i][i]?=?0;
??yita[i][i]=1;
??tao[i][i]=C+0.5;
??????for(j=i+1;?j ??{?
???? dis_city[i][j]??=(int)(??sqrt(????pow(citypos[i].x-citypos[j].x2?)?
?????????????????????????????+pow(citypos[i].y-citypos[j].y?2)??)? +0.5?
???);
????????dis_city[j][i]??=dis_city[i][j];
yita[i][j]=yita[j][i]=1.0/dis_city[i][j];
tao?[i][j]=tao?[j][i]=C+0.5;
??}
???}
???
???//初始化每只螞蟻信息
???::srand(?(unsigned)time(?NULL?)?);
???for?(i=0;?i ???{
???Ant[i].len=0;??????//當前累積長度為0
???Ant[i].tailpos=0;??//路徑末尾指示器為0
???Ant[i].curpos?=?rand()%CITY_NUM;???//當前位置隨機設定
???//路徑信息為空,所有頂點都未訪問
???for?(j=0;?j ???{
???Ant[i].path[j]=-1;
???Ant[i].flag[j]=FALSE;
???}
???//將當前位置加入路徑信息
???Ant[i].path[Ant[i].tailpos++]=Ant[i].curpos;
???Ant[i].flag[Ant[i].curpos]=TRUE;
???}
??? bestresult.len=999999;?//初始化最優解長度
}
//功能:執行蟻群算法
void?CAnt_System_Alogrithm::run()
{
int?ik;
int?nc;
int?minlen=10000000;
int????NOofbestAnt?=0;
//反復執行NCMAX次訓練
for?(nc=0;?nc {
//每只螞蟻都遍歷城市一遍
for?(k=0;?k {
????? findPath(k);
if?(Ant[k].len {
minlen=Ant[k].len;
????????????????NOofbestAnt=k;
}
}
//與最優解比較,更新最優解
if?(Ant[NOofbestAnt].len
//更新信息素
????????updateTao();
//初始化每只螞蟻的信息,準備下一次循環
for?(k=0;?k {
????????Ant[k].len=0;???????????????//當前累積長度為0
????????Ant[k].tailpos=0;???????????//路徑末尾指示器為0
????//Ant[k].curpos?=?rand()%CITY_NUM;???//當前位置隨機設定
for?(i=0;?i Ant[k].flag[i]=FALSE;
//將當前位置加入路徑信息
Ant[k].path[Ant[k].tailpos++]=Ant[k].curpos;
????????Ant[k].flag[Ant[k].curpos]=TRUE;
}
}//end?of?for?(nc=0;?nc }
void?CAnt_System_Alogrithm::updateTao()
{
int?ijk;
double?temp;
dou
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????7300??2005-03-13?16:48??蟻群\ASA算法\Ant_System_Alogrithm.cpp
?????文件???????3569??2010-04-06?17:45??蟻群\ASA算法\Ant_System_Alogrithm.dsp
?????文件????????548??2010-04-06?17:46??蟻群\ASA算法\Ant_System_Alogrithm.dsw
?????文件???????1497??2005-03-13?16:24??蟻群\ASA算法\Ant_System_Alogrithm.h
?????文件??????33792??2010-04-06?17:46??蟻群\ASA算法\Ant_System_Alogrithm.ncb
?????文件??????48640??2010-04-06?17:46??蟻群\ASA算法\Ant_System_Alogrithm.opt
?????文件???????1616??2010-04-06?17:45??蟻群\ASA算法\Ant_System_Alogrithm.plg
?????文件??????24908??2005-03-09?13:58??蟻群\ASA算法\ASA.APS
?????文件???????1668??2005-03-09?17:12??蟻群\ASA算法\ASA.clw
?????文件???????3636??2005-03-09?13:57??蟻群\ASA算法\ASA.cpp
?????文件???????4591??2005-03-09?17:12??蟻群\ASA算法\ASA.dsp
?????文件????????531??2005-03-09?13:57??蟻群\ASA算法\ASA.dsw
?????文件???????1334??2005-03-09?13:57??蟻群\ASA算法\ASA.h
?????文件??????82944??2005-03-13?16:52??蟻群\ASA算法\ASA.ncb
?????文件??????53760??2005-03-13?16:52??蟻群\ASA算法\ASA.opt
?????文件???????1187??2005-03-13?16:48??蟻群\ASA算法\ASA.plg
?????文件???????8321??2005-03-09?13:57??蟻群\ASA算法\ASA.rc
?????文件???????1392??2005-03-11?16:54??蟻群\ASA算法\ChildView.cpp
?????文件???????1267??2005-03-09?13:57??蟻群\ASA算法\ChildView.h
?????文件??????30145??2010-04-06?17:45??蟻群\ASA算法\Debug\Ant_System_Alogrithm.obj
?????文件????5099840??2010-04-06?17:45??蟻群\ASA算法\Debug\Ant_System_Alogrithm.pch
?????文件??????25600??2010-04-06?17:45??蟻群\ASA算法\Debug\Ant_System_Alogrithm.pdb
?????文件?????189440??2010-04-06?17:45??蟻群\ASA算法\Debug\vc60.idb
?????文件??????69632??2010-04-06?17:45??蟻群\ASA算法\Debug\vc60.pdb
?????目錄??????????0??2010-04-06?17:45??蟻群\ASA算法\Debug
?????文件????????612??2002-10-22?19:48??蟻群\ASA算法\el51.txt
?????文件????????954??2005-03-13?16:23??蟻群\ASA算法\GamblingPad.cpp
?????文件????????840??2005-03-13?16:24??蟻群\ASA算法\GamblingPad.h
?????文件???????3280??2005-03-09?13:57??蟻群\ASA算法\MainFrm.cpp
?????文件???????1744??2005-03-09?13:57??蟻群\ASA算法\MainFrm.h
............此處省略15個文件信息
評論
共有 條評論