-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP4SA.cpp
More file actions
executable file
·169 lines (164 loc) · 4.89 KB
/
P4SA.cpp
File metadata and controls
executable file
·169 lines (164 loc) · 4.89 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <cmath>
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <fstream>
#include <algorithm>
using std::cout;
using std::cin;
using std::endl;
using std::vector;
using std::string;
using std::ifstream;
using std::ofstream;
struct city
{
int cityNum;
int lat;
int lon;
};
class annealing
{
private:
int numCities, solCount;
double tourLength, bestLength;
string outFileName;
vector<int> tour;
vector<int> bestTour; //the order of the route
vector<city> cities; //cities and the latitude and longitude
public:
void randTourSolution()
{
std::random_shuffle(tour.begin(), tour.end());
for (int i = 0; i < numCities; i++)
{
bestTour[i] = tour[i];
}
}
void swapNeighbors(vector<int> &tempTour)
{
int a = 0, b = 0;
while(a == b)
{
a = rand() % numCities; //randomly choosing two cities
b = rand() % numCities;
}
int temp = tempTour[a]; //swapping those two cites to change
tempTour[a] = tempTour[b]; //order of tour
tempTour[b] = temp;
}
void calcTourLength(vector<int> &tempTour, double &length)
{
length = 0;
double a, b;
for (int i = 0; i < numCities - 1; i++)
{
a = (double)cities[tempTour[i + 1]].lat - (double)cities[tempTour[i]].lat; //calculating distace between cities
b = (double)cities[tempTour[i + 1]].lon - (double)cities[tempTour[i]].lon;
length += sqrt(a*a + b*b);
}
a = (double)cities[tempTour[0]].lat - (double)cities[tempTour[numCities - 1]].lat; //calculating distance from last city to starting city
b = (double)cities[tempTour[0]].lon - (double)cities[tempTour[numCities - 1]].lon;
length += sqrt(a*a + b*b);
}
void anneal()
{
double temperature = 100000000000.0, deltaT = 0.0, tMin = 1.0; //temperature and probability calculations
double alpha = .999997, tempLength = 0;
vector<int> tempTour(numCities); //create vector to test new route
solCount = 0; //calculate how many new tours have been accepted
randTourSolution(); //start with random solution
calcTourLength(tour, tourLength); //length of random solution
bestLength = tourLength;
solCount++; //solution added so solCount incremented
while (temperature > tMin)
{
for (int i = 0; i < numCities; i++)
tempTour[i] = tour[i];
// for (int i = 0; i < numCities; i++)
// {
swapNeighbors(tempTour);
calcTourLength(tempTour, tempLength);
deltaT = tempLength - tourLength;
if (deltaT < 0.0 || exp(-(deltaT / temperature)) > ((double)rand() / RAND_MAX)) //if new length is less than old or meets probability
{
for (int i = 0; i < numCities; i++)
{
tour[i] = tempTour[i];
}
tourLength = tempLength;
solCount++;
if (solCount % numCities == 0)
{
/*for (int i = 0; i < numCities; i++)
{
cout << tour[i] << ", ";
}
cout <<"\nTour Length: " << tourLength << endl;*/
}
if (tourLength < bestLength)
{
bestLength = tourLength;
for (int i = 0; i < numCities; i++)
{
bestTour[i] = tour[i];
}
}
// }
}
temperature *= alpha;
}
cout << "\nFinal tour = ";
for (int i = 0; i < numCities; i++)
{
cout << bestTour[i] << ", ";
}
cout << "\nFinal tour length: " << bestLength << endl;
ofstream outFile(outFileName.c_str());
for (int i = 0; i < numCities; i++)
{
outFile << bestTour[i] << " ";
}
outFile << "\nFinal tour length: " << bestLength << "\n";
outFile.close();
}
annealing(const string fileName)
{
int c, x, y;
city temp;
ifstream file(fileName.c_str());
if (file.is_open())
{
while(!file.eof())
{
file >> c >> x >> y;
temp.cityNum = c;
temp.lat = x;
temp.lon = y;
cities.push_back(temp);
tour.push_back(c);
bestTour.push_back(c);
}
}
outFileName = fileName + ".tour";
tour.pop_back();
cities.pop_back();
numCities = tour.size();
solCount = 0;
tourLength = 0;
}
};
int main(int argc, char *argv[])
{
srand(unsigned(time(NULL)));
if (argc != 2){
cout << "You must specify a file in the command line." << endl;
return 1;
}
const string fileName(argv[1]);
annealing TSP(fileName);
TSP.anneal();
return 0;
}