1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
| #include <iostream> #include <cstdlib> #include <ctime> #include <cmath>
using namespace std;
#define T0 50000.0 #define T_end (1e-8) #define q 0.98 #define L 1000 #define N 31 int city_list[N]; double city_pos[N][2] = {{1304,2312},{3639,1315},{4177,2244},{3712,1399},{3488,1535},{3326,1556},{3238,1229},{4196,1004},{4312,790}, {4386,570},{3007,1970},{2562,1756},{2788,1491},{2381,1676},{1332,695},{3715,1678},{3918,2179},{4061,2370},{3780,2212},{3676,2578},{4029,2838}, {4263,2931},{3429,1908},{3507,2367},{3394,2643},{3439,3201},{2935,3240},{3140,3550},{2545,2357},{2778,2826},{2370,2975}};
double distance(double *, double *);
double path_len(int *);
void init();
void create_new();
double distance(double *city1, double *city2) { double x1 = *city1; double y1 = *(city1 + 1); double x2 = *(city2); double y2 = *(city2 + 1);
double dis = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); return dis; }
double path_len(int *arr) { double path = 0; int index = *arr; for (int i = 0; i < N - 1; i++) { int index1 = *(arr + i); int index2 = *(arr + 1 + i); double dis = distance(city_pos[index1 - 1], city_pos[index2 - 1]); path += dis; } int last_index = *(arr + N - 1); int first_index = *arr; double last_dis = distance(city_pos[last_index - 1], city_pos[first_index - 1]); path += last_dis; return path; }
void init() { for (int i = 0; i < N; i++) { city_list[i] = i+1; } }
void create_new() { double r1 = ((double)rand()) / (RAND_MAX + 1.0); double r2 = ((double)rand()) / (RAND_MAX + 1.0); int pos1 = (int)(N * r1); int pos2 = (int)(N * r2); int temp = city_list[pos1]; city_list[pos1] = city_list[pos2]; city_list[pos2] = temp; }
int main() { srand((unsigned)time(NULL)); time_t start, finish; start = clock(); double T; int count = 0; T = T0; init(); int city_list_copy[N]; double f1, f2, df; double r;
while (T > T_end) { for (int i = 0; i < L; i++) { memcpy(city_list_copy, city_list, N * sizeof(int)); create_new(); f1 = path_len(city_list_copy); f2 = path_len(city_list); df = f2 - f1;
if (df >= 0) { r = ((double)rand()) / (RAND_MAX); if (exp(-df / T) <= r) { memcpy(city_list, city_list_copy, N * sizeof(int)); } } } T *= q; count++; }
finish = clock(); double duration = ((double)(finish - start)) / CLOCKS_PER_SEC; cout << "Simulated Annealing: " << endl; cout << "Initial Temperature T0 = " << T0 << ", Cooling Coefficient q = " << q << ", Loops per Temperature: " << L << ", Total Cooling: " << count << "times" << endl;
cout << "Best Path: " << endl; for (int i = 0; i < N - 1; i++) { cout << city_list[i] << "-->"; } cout << city_list[N - 1];
cout << "\nBest Path Length: " << path_len(city_list) << endl;
cout << "Time for Algorithm: " << duration << "sec" << endl; return 0; }
|