-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIteratedlocalsearch.java
More file actions
227 lines (210 loc) · 7.71 KB
/
Iteratedlocalsearch.java
File metadata and controls
227 lines (210 loc) · 7.71 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
/**
* Iterated Local Search algorithm
*/
public class Iteratedlocalsearch extends Algorithm {
private int nums;
private int[] finalPath;
private double finalCost;
private int[] currentPath;
private double currentCost;
private City c;
private Random rand;
private List<List<Double>> output;
/**
* Constructor for the Iterated Local Search algorithm
* @param city the city to implement the algorithm
*/
public Iteratedlocalsearch(City city) {
super();
this.nums = city.getNum();
this.finalPath = new int[city.getNum()];
this.finalCost = Double.MAX_VALUE;
this.currentPath = new int[city.getNum()];
this.currentCost = Double.MAX_VALUE;
this.c = city;
this.rand = new Random(0);
this.output = new ArrayList<>();
}
/**
* Randomly change positions of two locations
* @param path path with randomly changed positions of locations
*/
public void changePosition(int[] path) {
for(int index=path.length-1; index>=0; index--) {
swap(path,this.rand.nextInt(index+1), index);
}
}
/**
* Generate initial random path
* @param graph distances between locations
* @return randomly generated path
*/
private int[] generateRandomPath(double[][] graph){
int len = graph.length;
int[] randomPath = new int[len];
for(int i=0; i<len; i++){
randomPath[i] = i;
}
changePosition(randomPath);
return randomPath;
}
/**
* Get the total distance
* @param graph distances between locations
* @param path current path
* @return the total distance
*/
private double getCost(double[][] graph, int[] path){
double distance = 0;
for(int i = 0; i < path.length-1; i++){
distance += graph[path[i]][path[i+1]];
}
distance += graph[path[path.length-1]][path[0]];
return distance;
}
/**
* Exchange parts of current path
*/
private void addPerturtation(){
int len = this.finalPath.length;
int end1 = 1 + Math.abs(this.rand.nextInt()) % (len/3);
int end2 = end1 + Math.abs(this.rand.nextInt()) % (len/3);
LinkedList<Integer> list = new LinkedList<>();
for(int i = 0;i < end1;i++){
list.add(this.finalPath[i]);
}
for(int i = end2;i < len;i++){
list.add(this.finalPath[i]);
}
for (int i = end1;i < end2;i++){
list.add(this.finalPath[i]);
}
for(int i = 0;i < len;i++){
this.currentPath[i] = list.pollFirst();
}
}
/**
* Swap locations in the path
* @param currPath current path
* @param i location i
* @param j location j
* @return the updated path
*/
private int[] swap(int[] currPath, int i, int j){
int temp = currPath[i];
currPath[i] = currPath[j];
currPath[j] = temp;
return currPath;
}
/**
* Search for optimum locally
* @param path current path
* @param cost the total distance
* @param graph distances between locations
* @param maxImprove maximum number of improvements
* @return final cost
*/
private double localSearch(int[] path,double cost, double[][] graph, int maxImprove){
int count = 0;
int len = path.length;
int[] currPath = new int[len];
double temp = 0.0;
while (count < maxImprove){
for(int i = 0;i < len-1;i++){
for(int j = i+1;j < len;j++){
for(int k = 0;k < len;k++){
currPath[k] = path[k];
}
//// swap two elements of current path
currPath = swap(currPath,i,j);
double currentCost = getCost(graph,currPath);
if(currentCost < cost){
count = 0;
for (int k = 0; k < len ; k++) {
path[k] = currPath[k];
}
cost = currentCost;
temp = currentCost;
}
}
}
count++;
}
return temp == 0.0?cost:temp;
}
/**
* Perturate current path
* @param graph current path
*/
private void perturbation(double[][] graph){
addPerturtation();
this.currentCost = getCost(graph,currentPath);
}
/**
* Method for implementing the Iterated Local Search algorithm on the city and outputing trace file and solution file
* @param fileName the tsp file that is going to implement the algorithm on
* @param cutOffTime the cut-off time set by the user
* @throws IOException
*/
@Override
public void programStarts(String fileName, int cutOffTime) throws IOException {
int maxSearchTimes = 1000000000;
int maxImprove = 50;
String traceFile = fileName.split("\\.")[0] + "_" + "ILS" + "_" + cutOffTime + "_trace.txt";
String solutionFile = fileName.split("\\.")[0] + "_" + "ILS" + "_" + cutOffTime + "_solution.txt";
long start = System.currentTimeMillis();
double[][] graph = this.c.getDistances();
this.finalPath = generateRandomPath(graph);
this.finalCost = getCost(graph, this.finalPath);
int len = this.finalPath.length;
this.finalCost = localSearch(this.finalPath,this.finalCost,graph,maxImprove);
for (int i = 0; i < maxSearchTimes; i++) {
perturbation(graph);
this.currentCost = localSearch(this.currentPath, this.currentCost, graph, maxImprove);
if ((double)(System.currentTimeMillis()- start) / 1000 > cutOffTime) {
break;
}
if (this.currentCost < this.finalCost) {
for (int j = 0; j < len; j++) {
this.finalPath[j] = this.currentPath[j];
}
this.finalCost = this.currentCost;
List<Double> temp = new ArrayList<>();
temp.add((double) (System.currentTimeMillis()- start) / 1000);
temp.add(this.finalCost);
output.add(temp);
}
}
PrintWriter output1 = new PrintWriter(traceFile);
output1.println("|------------------------------------TRACES------------------------------------|");
for (List<Double> doubles : output) {
output1.printf("%.3f seconds, total distance = %d\n", doubles.get(0), Math.round(doubles.get(1)));
}
output1.close();
PrintWriter output2 = new PrintWriter(solutionFile);
int[] path = this.finalPath;
output2.println("|------------------------------------RESULT------------------------------------|");
System.out.println("|------------------------------------RESULT------------------------------------|");
output2.println("Total distance: " + (int) this.finalCost);
System.out.println("Total distance: " + (int) this.finalCost);
for (int i = path.length - 1; i >= 0; i--) {
if (i == 0) {
output2.printf("Location[%02d]", path[i]);
System.out.printf("Location[%02d]\n", path[i]);
}
else if (i % 5 == 0) {
output2.printf("Location[%02d] -> \n", path[i]);
System.out.printf("Location[%02d] -> \n", path[i]);
}
else {
output2.printf("Location[%02d] -> ", path[i]);
System.out.printf("Location[%02d] -> ", path[i]);
}
}
output2.close();
programEnds();
}
}