資源簡介
基本蟻群算法C,比較詳細!

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Text;
namespace?ant_C
{
????class?CAnt
????{??????????
????public?int[]?m_nPath;?//螞蟻走的路徑
????????int[]?m_nAllowedCity;//沒去過的城市
????????
????int?m_nCurCityNo;?//當前所在城市編號
????int?m_nMovedCityCount;?//已經去過的城市數量
????????public?double?m_dbPathLength;?//螞蟻走過的路徑長度
????????public?CAnt()
????????{
????????????m_nPath=new?int[Common.N_CITY_COUNT];
????????????m_nAllowedCity=new?int[Common.N_CITY_COUNT];?//沒去過的城市
????????}
????????//初始化函數,螞蟻搜索前調用
????????public?void?Init()
????????{
????????????for?(int?i?=?0;?i? ????????{
????????m_nAllowedCity[i]=1;?//設置全部城市為沒有去過
????????m_nPath[i]=0;?//螞蟻走的路徑全部設置為0
????????}
????????//螞蟻走過的路徑長度設置為0
????????m_dbPathLength=0.0;?
????????//隨機選擇一個出發城市
????????????m_nCurCityNo?=?Common.rnd(0?Common.N_CITY_COUNT);
????????//把出發城市保存入路徑數組中
????????m_nPath[0]=m_nCurCityNo;
????????//標識出發城市為已經去過了
????????m_nAllowedCity[m_nCurCityNo]=0;?
????????//已經去過的城市數量設置為1
????????m_nMovedCityCount=1;?
????????}
????????//選擇下一個城市
????????//返回值?為城市編號
????????public?int?ChooseNextCity()
????????{
????????int?nSelectedCity=-1;?//返回結果,先暫時把其設置為-1
????????//==============================================================================
????????//計算當前城市和沒去過的城市之間的信息素總和
????????
????????double?dbTotal=0.0;
????????double[]?prob=new?double[Common.N_CITY_COUNT];?//保存各個城市被選中的概率
????????for?(int?i=0;i ????????{
????????if?(m_nAllowedCity[i]?==?1)?//城市沒去過
????????{
????????????????????prob[i]?=?System.Math.Pow(Common.g_Trial[m_nCurCityNoi]?Common.ALPHA)?*?System.Math.Pow(1.0?/?Common.g_Distance[m_nCurCityNoi]?Common.BETA);?//該城市和當前城市間的信息素
????????dbTotal=dbTotal+prob[i];?//累加信息素,得到總和
????????}
????????else?//如果城市去過了,則其被選中的概率值為0
????????{
????????prob[i]=0.0;
????????}
????????}
????????//==============================================================================
????????//進行輪盤選擇
????????double?dbTemp=0.0;
????????if?(dbTotal?>?0.0)?//總的信息素值大于0
????????{
????????dbTemp=Common.rnd(0.0dbTotal);?//取一個隨機數
????????for?(int?i=0;i ????????{
????????if?(m_nAllowedCity[i]?==?1)?//城市沒去過
????????{
????????dbTemp=dbTemp-prob[i];?//這個操作相當于轉動輪盤,如果對輪盤選擇不熟悉,仔細考慮一下
????????if?(dbTemp?0.0)?//輪盤停止轉動,記下城市編號,直接跳出循環
????????{
????????nSelectedCity=i;
????????break;
????????}
????????}
????????}
????????}
????????//==============================================================================
????????//如果城市間的信息素非常小?(?小到比double能夠表示的最小的數字還要小?)
????????//那么由于浮點運算的誤差原因,上面計算的概率總和可能為0
????????//會出現經過上述操作,沒有城市被選擇出來
????????//出現這種情況,就把第一個沒去過的城市作為返回結果
????????
????????//題外話:剛開始看的時候,下面這段代碼困惑了我很長時間,想不通為何要有這段代碼,后來才搞清楚。
????????if?(nSelectedCity?==?-1)
????????{
????????????????for?(int?i?=?0;?i? ??
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????5710??2011-06-15?16:11??ant_C#\ant_C\ant.cs
?????文件???????2044??2011-06-15?16:35??ant_C#\ant_C\ant_C.csproj
?????文件????????704??2011-06-15?16:35??ant_C#\ant_C\AO.cs
?????文件???????5632??2005-11-11?22:25??ant_C#\ant_C\bin\Release\ant_C.vshost.exe
?????文件???????2419??2011-06-15?16:35??ant_C#\ant_C\Common.cs
?????文件???????1181??2011-06-11?10:04??ant_C#\ant_C\Properties\AssemblyInfo.cs
?????文件???????5029??2011-06-15?16:56??ant_C#\ant_C\tsp.cs
?????文件????????398??2011-06-11?11:23??ant_C#\ant_C\ve-42C.tmp
?????文件????????904??2011-06-11?10:04??ant_C#\ant_C.sln
????..A..H.?????13312??2011-06-15?17:00??ant_C#\ant_C.suo
?????目錄??????????0??2011-06-15?17:00??ant_C#\ant_C\obj\Debug\TempPE
?????目錄??????????0??2011-06-15?17:00??ant_C#\ant_C\obj\Release\TempPE
?????目錄??????????0??2011-06-15?17:00??ant_C#\ant_C\bin\Debug
?????目錄??????????0??2011-06-15?17:00??ant_C#\ant_C\bin\Release
?????目錄??????????0??2011-06-15?17:00??ant_C#\ant_C\obj\Debug
?????目錄??????????0??2011-06-15?17:00??ant_C#\ant_C\obj\Release
?????目錄??????????0??2011-06-15?17:00??ant_C#\ant_C\bin
?????目錄??????????0??2011-06-15?17:00??ant_C#\ant_C\obj
?????目錄??????????0??2011-06-11?10:04??ant_C#\ant_C\Properties
?????目錄??????????0??2011-06-15?17:00??ant_C#\ant_C
?????目錄??????????0??2011-06-11?10:04??ant_C#
-----------?---------??----------?-----??----
????????????????37333????????????????????21
- 上一篇:c語言編寫的一個基于mysql簡單數據管理系統
- 下一篇:c++操作系統進程管理模擬
評論
共有 條評論