Using system;
Using system. Collections. Generic;
Using system. text;
Namespace antsystem
{
Public Class AA
{
/** // <Summary>
/// Importance to the amount of information
/// </Summary>
Private int Alpha;
/** // <Summary>
/// Importance of heuristic Information
/// </Summary>
Private int Beta;
/** // <Summary>
/// The volatile speed of the information element
/// </Summary>
Private double lo;
/** // <Summary>
/// City distance Matrix
/// </Summary>
Private double [,] city;
/** // <Summary>
/// Information Element Matrix
/// </Summary>
Private double [,] message;
/** // <Summary>
/// Opnelist is used to store the next feasible City
/// </Summary>
Private queue <int> openlist = new queue <int> ();
/** // <Summary>
/// Closedlist is used to store cities that have been accessed
/// </Summary>
Private queue <int> closedlist = new queue <int> ();
/** // <Summary>
/// A good storage path
/// </Summary>
Private queue <int> bestlist = new queue <int> ();
Private int pro_time = 0;
/**///////////////////////////////////// /////////////////////
/// <Summary>
/// Constructor: form a matrix of city distance and information elements
/// </Summary>
/// <Param name = "city"> city distance matrix </param>
/// <Param name = "Lo"> the volatile speed of the information elements </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];
}
}
// Initialize the information element matrix.
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>
/// Change the information element matrix. closed_list is a good path.
/// </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>
/// Enter a linked list to calculate its total path
/// </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>
/// The next collection of cities that can be taken after generation to city I. Add the city number to openlist.
/// The generated city cannot already exist in 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>
/// Select the path that should be followed. After path a is selected, the openlist is cleared and A is added to the openlist.
/// </Summary>
/// <Returns> </returns>
Private int choiceroute ()
{
Int Index = 0; // record the selected City
Random random = new random ();
Double random_value = (double) random. nextdouble (); // probability of random selection
Int [] temp_array = new int [openlist. Count];
Temp_array = openlist. toarray ();
Double sum_message = 0; // total information of all nodes in 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 cycles in total
For (INT I = 0; I <4; I ++)
{
/** // A total of N ants 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); // modify the amount of information
}
Return bestlist;
}
}
}