/*************************************************************** * Program Name: Artificial ant colony algorithm for TSP combinatorial optimization problem (ACA_TSP) * Compilation environment: Visual c+ + 6.0 * Mode of communication: Email (junh_cs@126.com) ***************************************************************/#include <st dio.h> #include <stdlib.h> #include <math.h> #include <time.h> #include <string.h>/******** /#define CITY_NUM 30//indicates number of cities #define ANT_NUM 10//indicates number of ants #d Efine Iteritive_num 400//indicates the number of iterations #define POSITION_DIM 2//Represents the dimension of city coordinates #define FCITY_INDEX 0//is used to denote the initial city subscript (currently only equals 0, late Improved)//The following parameter selection has a significant effect on the result #define AILFA 5//is used to indicate the importance of pheromone in calculating the transfer probability #define BETA 4//is used to indicate the importance of visibility (distance information) in calculating the transfer probability #define VOL Atie_factor 0.08//volatile factor between 0 to 1 #define INIT_PHEROMONE 0.1//initial pheromone content per side//////on DATA30.dat parameter #define CITY_NUM 30/ /indicates the number of cities #define ANT_NUM 10//Indicates the number of ants #define ITERITIVE_NUM 400//indicates the number of iterations #define POSITION_DIM 2//Represents the dimension of city coordinates #define Fcity_index 0//used to indicate the bottom of the initial city(currently only equals 0, post-improvement)//The following parameter selection has a significant effect on the result #define AILFA 4//is used to indicate the importance of pheromone in calculating the transfer probability #define BETA 3//used to indicate the importance of visibility (distance information) in calculating the transfer probability #define Volatie_factor 0.08//volatile factor between 0 and 1 #define INIT_PHEROMONE 0.1//initial pheromone content per side//0->17->1->9->13->27 ->6->16->21->11->24->3->7->8->28->15->14->22->20->12->10->4- >29->23->5->25->18->26->19->2->0//total_distance = 420.794632 *//********************* ///define a global variable double City_pos[city_num][position_dim]; Store the city's coordinate information double pheromone[city_num][city_num]; Stores Citya to cityb side of the pheromone double city_dis[city_num][city_num]; Storage Citya to cityb distance, i.e.: visibility int local_road_info[city_num+1]; Store this best (shortest) path (default is the number of files read-in starts) int global_road_info[city_num+1]; Store this best (shortest) path (the default is to start the file read-in number) double Local_road_dis = 9999999999; Storage best (shortest) path length double Global_road_dis = 9999999999; Storage best (shortest) path length int tag_old[ant_num][city_num]; Storage every ant has walkedWhich roads, traversed with 1, did not pass with 0/**************************************************************///function predefined area double Calculate_pij (int ant, int citya, int cityb); Calculate ant transfer cityb probability void Initialize () from Citya; Initialize the Environment void Init_tag_old (); Initialize the Tag_old variable double odistance (double *a, double *b, int vector_len); Calculates the distance between two cities of Void Findonetimefood (); The process of simulating an artificial ant colony for food at a time void Update_pheromone_matrix (); According to Global_road information with the new pheromone double rand_select_value (double under, double upper); From [under, Upper] produces a random number int is_ij_int_global_road (int i, int j); Determine that IJ is not in the global optimal path void search_short_road (); Find the global optimal path void Print_cmd (); Output shortest path and path length at command line end void Print_file (double seconds, char* File_path);
Output shortest path and path length in file/**************************************************************/void Main () {int k;
int i;
FILE *FP;
float x;
Float y;
clock_t start;
clock_t end; Srand (Time (NULL)); Initialize random seed for (k = 0; k <; k++) {//Read city position (x, y) FP=fopen ("DATA30.dat", "R");
for (i = 0; i < City_num; i++) {fscanf (FP, "%f%f", &x, &y);
printf ("%f,%f\n", X, y);
City_pos[i][0]= x;
City_pos[i][1]= y;
} fclose (FP); Start=clock ();
The time that the logger started running initialize ();
Search_short_road (); End=clock ();
Print_file (1.0* (End-start)/1000, "experimental results. txt") for recording the end of the program run; }}/************************************************************** * @function: Initializes the variable to build the initialization state *********************
/void Initialize () {int i;
Int J; Average the pheromone value to per edge//calculate the distance of each edge for (i = 0; i < City_num; i++) {p
Heromone[i][i] = Init_pheromone;
for (j = 0; J < i; J + +) {//i-i is marked in tag_old city_dis[i][j] = odistance (City_pos[i], city_pos[j], Position_dim); City_dis[j][i] = City_dis[i][j]; The distance here is equal to pheromone[i][j] = pheromone[j][i] = Init_pheromone;
Approximately equals 0}}//Initialize the Tag_old array init_tag_old (); GloBal_road_dis = 9999999999;
Local_road_dis = 9999999999; }/************************************************************** * @function: Initialize tag_old variable ***********************
/void Init_tag_old () {int i;
Int J;
for (i = 0; i < Ant_num; i++) {tag_old[i][0] = 1;
for (j = 1; j < City_num; J + +) {Tag_old[i][j] = 0; }}}/************************************************************** * @function: Find the best path **************************
/void Search_short_road () {int i;
int times = 0;
while (Times < Iteritive_num) {//Find the current global best path Findonetimefood ();
if (Local_road_dis < Global_road_dis) {Global_road_dis = Local_road_dis;
for (i = 0; i < city_num+1; i++) {global_road_info[i] = Local_road_info[i];
}}//end_if printf ("The%d iteritive: \ n", times);
Print_cmd ();
Update the pheromone matrix Update_pheromone_matrix ();
times++; }//end_while}/***************** @function: Update pheromone matrix **********************************************
/void Update_pheromone_matrix () {int i;
Int J; for (i = 0; i < City_num; i++) {for (j = 0; J < City_num; J + +) {if (1 = = Is_ij_int_global_road (i, J)) {//in Global best path
Diameter Pheromone[i][j] = (1-volatie_factor) *pheromone[i][j] + Volatie_factor/global_road_dis;
}else{//Not in the global optimal path pheromone[i][j] = (1-volatie_factor) *pheromone[i][j]; }}}}/************************************************************** * @function: Judging the ij edge is not in the Global_road *********
/int Is_ij_int_global_road (int i, int j) {int k;
int ans = 0;
for (k = 0; k < city_num; k++) {if (i = = Global_road_info[k] && j = = Global_road_info[k+1]) {ans = 1;
Break
}} return ans; }/************************************************************** * @function: Simulates the artificial ant colony the process of finding food once (or need to modify) *********/void Findonetimefood () {int i;
Int J;
int k; int next_city; Can represent the current ant selection of the next city subscript double Next_city_pro[city_num]; The probability of storing every ant going to the next city int now_road[city_num+1];
Store each ant find the path double Now_road_dis; Init_tag_old (); The historical information will initialize Local_road_dis = 9999999999 Each time it is dispatched;
Initialize Local_road_diss for (i = 0; i < Ant_num; i++) {//Calculate the path of the first ant to walk now_road[0] = 0;
Now_road_dis = 0.0; for (j = 1; j < City_num; J + +) {//Decision CITY_NUM-1 times//Calculate the probability of the first ant to go to each city//and store it in the form of a histogram accumulation in Next_city_pro next_city_p
Ro[0] = 0.0;
for (k = 1; k < city_num; k++) {Double tmp = Calculate_pij (i, now_road[j-1], k);
NEXT_CITY_PRO[K] = next_city_pro[k-1] + tmp; }//Generate a random number to see which interval it falls in double rd = rand_select_value (0.0, 1.0-0.000001); -0.000001? Make sure every city can walk to for (next_city = 1; next_city < city_num; next_city++) {//next_city=1? Because the No. 0 city has gone through if (Rd < Next_
City_pro[next_city]) {break;//k inside stores the first ant's Choice next city target }} Tag_old[i][next_city] = 1;
NOW_ROAD[J] = next_city;
Now_road_dis + = city_dis[now_road[j-1]][next_city];
} Now_road[city_num] = 0;
Now_road_dis + = city_dis[now_road[next_city]][0];
if (Now_road_dis < Local_road_dis) {Local_road_dis = Now_road_dis;
for (k = 0; k < city_num+1; k++) {local_road_info[k] = now_road[k]; }}}}/************************************************************** * @function: Calculates the probability of ant transferring cityb from Citya **********
/double Calculate_pij (int ant, int now_city, int aim_city) {
Double ans = 0.0;
Double sum = 0.0;
int i; if (0 = = Tag_old[ant][aim_city]) {for (i = 0; i < City_num; i++) {if (0 = = Tag_old[ant][i]) {sum + = (POW (phero
Mone[now_city][i], Ailfa) *pow (1/city_dis[now_city][i], BETA));
}} Double tmp = POW (pheromone[now_city][aim_city], Ailfa) *pow (1/city_dis[now_city][aim_city], BETA);
ans = tmp/sum;
} return ans; }
/************* @function: Calculates the distance between two vectors * @notice: Two vectors A and B must be Vector_len length *******
/double odistance (double *a, double *b, int vector_len) {
Double ans = 0;
int i;
for (i = 0; i < Vector_len; i++) {ans + = (* (a+i)-* (b+i)) * (* (a+i)-* (b+i));
} ans = sqrt (ans);
return ans; }/************************************************************** * @function: This function is random in the [under, Upper] interval * Select a value * *
/Double Rand_select_value (double under, double upper) {
if (under>upper) {printf ("\nerror:\t[%d,%d] is invalid region!", under, Upper);
Exit (1);
} Double Lamta = (double) rand ()/(Rand_max);
Return (under+ (upper-under) *LAMTA); }/************************************************************** * @function: This function is mainly to judge the two equal-length a vector and b vector is not the same * if the same return 1, otherwise return 0 **************************************************************/int Is_Same_vector (double* A, double* b, int len) {int ans = 1, i;
for (i = 0; i < len; i++) {if (A[i]! = B[i]) {ans = 0;
Break
}} return ans; }/************************************************************** * @function: Output pseudo-suboptimal path at command line, and total path length *****************
/void Print_cmd () {int i;
for (i = 0; i < City_num; i++) {printf ("%d->", Global_road_info[i]);
} printf ("%d\ntotal_distance =%.6f\n", Global_road_info[city_num], Global_road_dis); }/************************************************************** * @function: The pseudo suboptimal path in the file, and the total length of the path ****************** /void Print_file (double seconds, char* file_path) {File *FP = fopen (file_
Path, "a");
int i;
for (i = 0; i < City_num; i++) {fprintf (FP, "%d->", Global_road_info[i]);
} fprintf (FP, "%d\n Total Distance:%.6f\n", Global_road_info[city_num], Global_road_dis);
fprintf (FP, "%d iterations total:%.2f sec \ n", iteritive_num, seconds); FclosE (FP); }
Give the DATA30.DAT test data
(x, y) represents the coordinates of the city
54 62
58 69
71 71
45 21
25 62
37 84
83 46
41 26
44 35
64 60
22 60
62 32
18 54
68 58
18 40
24 42
91 38
54 67
74 78
83 69
4 50
82 7
13 40
2 99
58 35
41 94
87 76
71 44
25 38
7 64