using System;
using System.Collections.Generic;
using System.Text;
namespace AntSystem
{
public class AA
{
/**//// <summary>
/// 對資訊量的重視程度
/// </summary>
private int alpha;
/**//// <summary>
/// 啟發學習法資訊的受重視程度
/// </summary>
private int beta;
/**//// <summary>
/// 資訊素的揮發速度
/// </summary>
private double lo;
/**//// <summary>
/// 城市距離矩陣
/// </summary>
private double[,] City;
/**//// <summary>
/// 資訊素矩陣
/// </summary>
private double[,] Message;
/**//// <summary>
/// opneList用於存放下一步可行城市
/// </summary>
private Queue<int> openList=new Queue<int> ();
/**//// <summary>
/// closedList用於存放已經訪問過的城市
/// </summary>
private Queue<int> closedList=new Queue<int> ();
/**//// <summary>
/// 儲存較好的路徑
/// </summary>
private Queue <int> BestList=new Queue<int> ();
private int Pro_time = 0;
/**//////////////////////////////////////////////////////////
/// <summary>
/// 建構函式:形成城市距離和資訊素矩陣
/// </summary>
/// <param name="city">城市距離矩陣</param>
/// <param name="Lo"> 資訊素的揮發速度</param>
public AA(double[,] city,double Lo,int Alpha,int Beta)
{
alpha = Alpha;
beta = Beta;
lo=Lo;
int temp = Convert.ToInt32( Math.Sqrt(city.Length));
City=new double [temp,temp];
Message=new double [temp,temp];
for (int i = 0; i < temp; i++)
{
for (int j = 0; j < temp; j++)
{
City[i, j] = city[i, j];
}
}
//初始化資訊素矩陣
for (int i = 0; i < temp; i++)
{
for (int j = 0; j < temp; j++)
{
if (i != j)
{
Message[i, j] = (double)1 / (temp * temp - temp);
}
}
}
}
/**/////////////////////////////////////////////////////////////
/// <summary>
/// 改變資訊素矩陣,closed_list為較好的路徑
/// </summary>
/// <param name="closed_list"></param>
private void Change_Message(Queue<int> closed_list)
{
lock (this)
{
int[] temp_Array = new int[closed_list.Count];
temp_Array = closed_list.ToArray();
for (int i = 0; i < closed_list.Count - 1; i++)
{
Message[temp_Array[i], temp_Array[i + 1]] = Message[temp_Array[i], temp_Array[i + 1]] + lo / ((1 - lo) *Convert.ToInt32(Get_Weight(closed_list)+1));
}
Message[temp_Array[temp_Array.Length - 1], temp_Array[0]] = Message[temp_Array[temp_Array.Length - 1], temp_Array[0]] + lo / ((1 - lo) *Convert.ToInt32(Get_Weight(closed_list)));
for (int i = 0; i < closed_list.Count; i++)
{
for (int j = 0; j < closed_list.Count; j++)
{
Message[i, j] = (1 - lo) * Message[i, j];
}
}
}
}
/**////////////////////////////////////////////////////////////////
/// <summary>
/// 輸入一個鏈表,計算出其對應的總路徑
/// </summary>
/// <param name="closed_list"></param>
/// <returns></returns>
public double Get_Weight(Queue <int> closed_list)
{
lock (this)
{
double sum = 0;
int[] temp_Array = new int[closed_list.Count];
temp_Array = closed_list.ToArray();
for (int i = 0; i < Convert.ToInt32(temp_Array.Length) - 1; i++)
{
sum = sum + City[temp_Array[i], temp_Array[i + 1]];
}
sum = sum + City[temp_Array[temp_Array.Length - 1], temp_Array[0]];
return sum;
}
}
/**///////////////////////////////////////////////////////////////
/// <summary>
/// 產生到i城市後,下一個可走城市的集合。並將城市編號加入到openList中。
/// 產生的城市不可以已經存在closedList中
/// </summary>
/// <param name="i"></param>
private void NextCity()
{
openList.Clear();
int temp_int=Convert.ToInt32(Math.Sqrt(City.Length));
for (int i = 0; i < temp_int; i++)
{
if (closedList.Contains(i) ==false)
{
openList.Enqueue(i);
}
}
}
/**///////////////////////////////////////////////////////////////
/// <summary>
/// 選擇應該走那條路,選擇完路A後,清空openList,再把A加入到openList中
/// </summary>
/// <returns></returns>
private int choiceRoute()
{
int index = 0;//記錄選擇的城市
Random random = new Random();
double random_value =(double) random.NextDouble();//隨機播放的機率
int[] temp_Array=new int [openList.Count];
temp_Array=openList.ToArray();
double sum_Message = 0;//openList所有節點的總資訊量
for (int i = 0; i < openList.Count; i++)
{
double eta = 1 / City[Pro_time, temp_Array[i]];
sum_Message = sum_Message +Math.Pow(Message[Pro_time, temp_Array[i]],alpha)*Math.Pow (eta,beta);
}
double temp=0;
for (int j = 0; j < openList.Count; j++)
{
double eta = 1 / City[Pro_time, temp_Array[j]];
temp=temp+Math.Pow(Message[Pro_time,temp_Array[j]],alpha)*Math.Pow(eta,beta)/sum_Message;
if (temp > random_value)
{
index = temp_Array [j];
break;
}
}
openList.Clear();
openList.Enqueue(index);
return index;
}
/**//////////////////////////////////////////////////////////////
public Queue<int> Main_DW()
{
BestList.Clear();
/**////共迴圈20次
for (int i = 0; i < 4; i++)
{
/**////共有n只螞蟻n=City'number Convert.ToInt32(Math.Sqrt(City.Length))
for (int j = 0; j < Convert.ToInt32(Math.Sqrt(City.Length)); j++)
{
openList.Enqueue(0);
closedList.Clear();
while (openList.Count != 0 && closedList.Count != Convert.ToInt32(Math.Sqrt(City.Length)))
{
int temp = openList.Dequeue();
Pro_time = temp;
closedList.Enqueue(temp);
if (openList.Count == 0 && closedList.Count == Convert.ToInt32(Math.Sqrt(City.Length)))
{
if (BestList.Count == 0)
{
int[] temp_Array = new int[Convert.ToInt32(Math.Sqrt(City.Length))];
temp_Array = closedList.ToArray();
for (int k = 0; k < Convert.ToInt32(Math.Sqrt(City.Length)); k++)
{
BestList.Enqueue(temp_Array[k]);
}
}
if (Get_Weight(BestList) > Get_Weight(closedList))
{
BestList.Clear();
int[] temp_Array = new int[Convert.ToInt32(Math.Sqrt(City.Length))];
temp_Array = closedList.ToArray();
for (int k = 0; k < Convert.ToInt32(Math.Sqrt(City.Length)); k++)
{
BestList.Enqueue(temp_Array[k]);
}
}
}
NextCity();
choiceRoute();
}
}
Change_Message(BestList);//修改資訊量
}
return BestList;
}
}
}