-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulatedAnnealing.java
More file actions
202 lines (185 loc) · 5.49 KB
/
SimulatedAnnealing.java
File metadata and controls
202 lines (185 loc) · 5.49 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import java.util.ArrayList;
import java.util.Random;
public class SimulatedAnnealing {
Random rand = new Random();
ArrayList<int[]> cities;
ArrayList<int[]> routes = new ArrayList<int[]>();
ArrayList<Integer> routeDistances = new ArrayList<Integer>();
int numCities;
double startingTemp;
int numIterations;
double coolingRate;
SimulatedAnnealing(){
this.numCities = 10;
}
SimulatedAnnealing(ArrayList<int[]> cities, double startingTemp, int numIterations, double coolingRate){
this.cities = cities;
this.numCities = cities.size();
this.startingTemp = startingTemp;
this.numIterations = numIterations;
this.coolingRate = coolingRate;
}
public void startSimulatedAnnealing() {
System.out.println("Starting routes:");
printRoutes();
System.out.println("Starting Distances:");
rankRoutes();
printRouteDistances();
int bestRoute = routeDistances.get(0);
int iteration = 1;
for(int i = 0; i < numIterations; i++) {
System.out.println("Running iteration " + iteration + ":");
if(startingTemp <= 0.1) {
break;
}
for(int[] j : routes) {
int a = rand.nextInt(j.length);
int b = rand.nextInt(j.length);
while(b == a) {
b = rand.nextInt(j.length);
}
swapCities(j, a, b);
int newRank = rankSingleRoute(routes.indexOf(j));
if (Math.exp((routeDistances.get(routes.indexOf(j)) - newRank) / startingTemp) < Math.random()) {// using Boltzmann function of probability distribution
swapCities(j, a, b);
}
else {
routeDistances.set(routes.indexOf(j), newRank);
}
}
int r = routeDistances.get(0);
int a = routeDistances.indexOf(r);
for(int d : routeDistances) {
if(d < r) {
a = routeDistances.indexOf(d);
r = routeDistances.get(a);
}
}
System.out.println("Current best route is " + (a) + " with a distance of " + r);
startingTemp *= coolingRate;
printRoutes();
printRouteDistances();
bestRoute = getBestRoute();
iteration++;
}
System.out.println("The best route is route " + (bestRoute+1));
}
//make all routes
public void makeRoutes(int numRoutes) {
int[] newRoute = new int[numCities];
int index = 0;
for(int city = 1; index < numCities; city++,index++) { //initialize first route
newRoute[index] = city;
}
routes.add(newRoute);
while(numRoutes > 1) { //accounts for the route that is already made and when the condition is checked after the last route
newRoute = new int[numCities];
populateRoute(newRoute);
shuffleRoute(newRoute);
routes.add(newRoute);
numRoutes--;
}
}
//populate the given route
public void populateRoute(int[] array) {
int index = 0;
for(int city = 1; index < numCities; city++,index++) { //initialize first route
array[index] = city;
}
}
//randomly shuffle routes
public void shuffleRoute(int[] array) {
for (int i = 0; i < array.length; i++) {
int randomIndexToSwap = rand.nextInt(array.length);
int temp = array[randomIndexToSwap];
array[randomIndexToSwap] = array[i];
array[i] = temp;
}
}
//swap two cities
public void swapCities(int[] array, int indexA, int indexB) {
int temp = array[indexA];
array[indexA] = array[indexB];
array[indexB] = temp;
}
//defines a total distance traveled for each route
public void rankRoutes() {
int cityA;
int cityB;
for(int[] i : routes) { //navigates routes
int routeDistance = 0;
for(int j = 0; j < i.length; j++) {// navigates the cities in each route
cityA = i[j];
if((j + 1) == i.length) { //goes back to the beginning of the route if j runs over array
cityB = i[0];
}
else {
cityB = i[j+1];
}
routeDistance += getDistance(cities.get(cityA - 1), cities.get(cityB - 1)); //add distance between all cities in the route
}
routeDistances.add(routeDistance);
}
}
//return distance of route to see if it is better
public int rankSingleRoute(int route) {
int cityA;
int cityB;
int routeDistance = 0;
int[] currentRoute = routes.get(route);
for(int j = 0; j < currentRoute.length; j++) {// navigates the cities in each route
cityA = currentRoute[j];
if((j + 1) == currentRoute.length) { //goes back to the beginning of the route if j runs over array
cityB = currentRoute[0];
}
else {
cityB = currentRoute[j+1];
}
routeDistance += getDistance(cities.get(cityA - 1), cities.get(cityB - 1)); //add distance between all cities in the route
}
return routeDistance;
}
//return distance between cities
public int getDistance(int[] cityA, int[] cityB) {
int distance = (int)Math.sqrt(Math.pow(Math.abs(cityA[0] - cityB[0]), 2) + //using Pythagorean theorem to find distance
Math.pow(Math.abs(cityA[1] - cityB[1]), 2));
return distance;
}
public int getBestRoute() {
int bestRoute = 0;
for(int i : routeDistances) {
if(i < routeDistances.get(bestRoute)) {
bestRoute = routeDistances.indexOf(i);
}
}
return bestRoute;
}
//PRINT FUNCTIONS
//print all routes
public void printRoutes() {
int i = 1;
for(int[] route : routes){
System.out.print("Route number " + i + " is: [");
for(int r = 0; r < route.length; r++) {
if(r < route.length - 1) {
System.out.print(route[r] + ", ");
}
else {
System.out.print(route[r]);
}
}
System.out.println("]");
i++;
}
System.out.println();
}
//print all route distances
public void printRouteDistances() {
int r = 1;
for(int i : routeDistances) {
System.out.println("Total distance of route " + r + " is: " + i);
r++;
}
System.out.println();
}
}