# Include <iostream>
# Include <math. h>
# Include <fstream>
# Include <time. h>
Using namespace std;
Const int iAntCount = 34; // Number of ants
Const int iCityCount = 51; // number of cities
Const int iItCount = 2000; // The maximum number of times of downgrading
Const double q= 100;
Const double alpha = 1;
Const double beta = 5;
Const double rou = 0.5;
Int besttour [iCityCount]; // The list of most available paths
Double rnd (int low, double uper) // obtain a random number
{
Double p = (rand ()/(double) RAND_MAX) * (uper)-(low) + (low );
Return (p );
}
Int rnd (int uper)
{
Return (rand () % uper );
}
Class GInfo // tsp map information, including the information elements of the information elements, city distance, and information Element Matrix
{
Public:
Double m_dDeltTrial [iCityCount] [iCityCount];
Double m_dTrial [iCityCount] [iCityCount];
Double distance [iCityCount] [iCityCount];
};
GInfo Map;
Class ant
{
Private:
Int ChooseNextCity (); // select a city
Double prob [iCityCount];
Int m_iCityCount;
Int AllowedCity [iCityCount]; // cities that have not passed
Public:
Void addcity (int city );
Int tabu [iCityCount];
Void Clear ();
Void UpdateResult ();
Double m_dLength;
Double m_dShortest;
Void move ();
Ant ();
Void move2last ();
};
Void ant: move2last ()
{
Int I;
For (I = 0; I <iCityCount; I ++)
If (AllowedCity [I] = 1 ){
Addcity (I );
Break;
}
}
Void ant: Clear ()
{
M_dLength = 0;
Int I;
For (I = 0; I <iCityCount; I ++ ){
Prob [I] = 0;
AllowedCity [I] = 1;
}
I = tabu [iCityCount-1];
M_iCityCount = 0;
Addcity (I );
}
Ant: ant ()
{
M_dLength = m_dShortest = 0;
M_iCityCount = 0;
Int I;
For (I = 0; I <iCityCount; I ++ ){
AllowedCity [I] = 1;
Prob [I] = 0;
}
}
Void ant: addcity (int city)
{
// Add city to tabu;
Tabu [m_iCityCount] = city;
M_iCityCount ++;
AllowedCity [city] = 0;
}
Int ant: ChooseNextCity ()
{
// Update the probability of path selection
// Select a path from tabu [m_iCityCount-1] to next
Int I;
Int j = 10000;
Double temp = 0;
Int curCity = tabu [m_iCityCount-1];
For (I = 0; I <iCityCount; I ++ ){
If (AllowedCity [I] = 1 ))
Temp + = pow (1.0/Map. distance [curCity] [I]), beta )*
Pow (Map. m_dTrial [curCity] [I]), alpha );
}
Double sel = 0;
For (I = 0; I <iCityCount; I ++ ){
If (AllowedCity [I] = 1 )){
Prob [I] = pow (1.0/Map. distance [curCity] [I]), beta )*
Pow (Map. m_dTrial [curCity] [I]), alpha)/temp;
Sel + = prob [I];
}
Else
Prob [I] = 0;
}
Double mRate = rnd (0, sel );
Double mSelect = 0;
For (I = 0; I <iCityCount; I ++ ){
If (AllowedCity [I] = 1 ))
MSelect + = prob [I];
If (mSelect> = mRate ){
J = I;
Break;
}
}
If (j = 10000 ){
Temp =-1;
For (I = 0; I <icitycount; I ++ ){
If (allowedcity [I] = 1 ))
If (temp ){
Temp = POW (1.0/map. distance [curcity] [I]), beta)
* POW (Map. m_dtrial [curcity] [I]), alpha );
J = I;
}
}
}
Return J;
}
Void ant: updateresult ()
{
// Update the length of tour
Int I;
For (I = 0; I <iCityCount-1; I ++ ){
M_dlength + = map. distance [Tabu [I] [Tabu [I + 1];
M_dlength + = map. distance [Tabu [iCityCount-1] [Tabu [0];
}
}
Void ant: Move ()
{
// The ant move to next town and add town ID to TABU.
Int J;
J = choosenextcity ();
Addcity (j );
}
Class project
{
Public:
Void updatetrial ();
Double m_dlength;
Void initmap ();
Ant ants [iantcount];
Void getant ();
Void startsearch ();
Project ();
};
Void project: updatetrial ()
{
// Calculate the changes of trial information
Int I;
Int J;
For (I = 0; I <iAntCount; I ++ ){
For (j = 0; j <iCityCount-1; j ++ ){
Map. m_dDeltTrial [ants [I]. tabu [j] [ants [I]. tabu [j + 1] + = Q/ants [I]. m_dLength;
Map. m_dDeltTrial [ants [I]. tabu [j + 1] [ants [I]. tabu [j] + = Q/ants [I]. m_dLength;
Map. m_dDeltTrial [ants [I]. tabu [iCityCount-1] [ants [I]. tabu [0] + = Q/ants [I]. m_dLength;
Map. m_dDeltTrial [ants [I]. tabu [0] [ants [I]. tabu [iCityCount-1] + = Q/ants [I]. m_dLength;
}
For (I = 0; I <iCityCount; I ++)
For (j = 0; j <iCityCount; j ++ ){
Map. m_dTrial [I] [j] = (rou * Map. m_dTrial [I] [j] + Map. m_dDeltTrial [I] [j]);
Map. m_dDeltTrial [I] [j] = 0;
}
}
}
Void project: initmap ()
{
Int I, j;
For (I = 0; I <iCityCount; I ++)
For (j = 0; j <iCityCount; j ++ ){
Map. m_dTrial [I] [j] = 1;
Map. m_dDeltTrial [I] [j] = 0;
}
}
Project: project ()
{
// Initial map, read map infomation from file. et.
Initmap ();
M_dLength = 10e9;
Ifstream in ("eil51.tsp ");
Struct city
{
Int num;
Int x;
Int y;
} Cc [iCityCount];
For (int I = 0; I <iCityCount; I ++)
{
In> CC [I]. Num> CC [I]. x> CC [I]. Y;
Besttour [I] = 0;
}
Int J;
For (I = 0; I <icitycount; I ++)
For (j = 0; j <icitycount; j ++)
Map. distance [I] [J] = SQRT (POW (CC [I]. X-CC [J]. X), 2) +
Pow (CC [I]. Y-CC [J]. Y), 2 ));
}
Void project: getant ()
{
// Randomly put ant into map
Int I = 0;
Int city;
Srand (unsigned) Time (null) + rand ());
For (I = 0; I <iantcount; I ++ ){
City = RND (icitycount );
Ants [I]. addcity (city );
}
}
Void project: startsearch ()
{
// Begin to find best solution
Int max = 0; // every ant tours times
Int I;
Int j;
Double temp;
Int temptour [iCityCount];
While (max <iItCount ){
For (j = 0; j <iAntCount; j ++)
For (I = 0; I <iCityCount-1; I ++)
Ants [j]. move ();
For (j = 0; j <iAntCount; j ++ ){
Ants [j]. move2last ();
Ants [j]. UpdateResult ();
}
// Find out the best solution of the step and put it into temp
Int t;
Temp = ants [0]. m_dLength;
For (t = 0; t <iCityCount; t ++)
Temptour [t] = ants [0]. tabu [t];
For (j = 0; j <iAntCount; j ++ ){
If (temp> ants [j]. m_dLength ){
Temp = ants [j]. m_dLength;
For (t = 0; t <iCityCount; t ++)
Temptour [t] = ants [j]. tabu [t];
}
}
If (temp <m_dLength ){
M_dLength = temp;
For (t = 0; t <iCityCount; t ++)
Besttour [t] = temptour [t];
}
Printf ("% d: % f/n", max, m_dLength );
UpdateTrial ();
For (j = 0; j <iAntCount; j ++)
Ants [j]. Clear ();
Max ++;
}
Printf ("The shortest toure is: % f/n", m_dLength );
For (int t = 0; t <iCityCount; t ++)
Printf ("% d", besttour [t]);
}
Int main ()
{
Project TSP;
TSP. GetAnt ();
TSP. StartSearch ();
Return 0;
}