-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoute.java
More file actions
142 lines (96 loc) · 3.58 KB
/
Copy pathRoute.java
File metadata and controls
142 lines (96 loc) · 3.58 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
public class Route {
private LinkedLocation head;
private LinkedLocation tail;
public int size;
private int capacity;
public Route(Location head) {
LinkedLocation startLocation = new LinkedLocation(head);
this.capacity = 20;
this.head = startLocation;
this.tail = startLocation;
size++;
}
public void addDestination(Location newObject) throws IllegalArgumentException {
LinkedLocation newNode = new LinkedLocation(newObject);
LinkedLocation currLocation = this.head;
if(this.size == this.capacity) {
throw new IllegalArgumentException("Cannot add another destination");
}
else {
if(size > 1) {
for(int i = 1; i < size; i++) {
currLocation = currLocation.getNext();
}
}
currLocation.setNext(newNode);
newNode.setPrevious(currLocation);
newNode.setNext(this.head);
this.tail = newNode;
this.head.setPrevious(this.tail);
size++;
}
}
public boolean isEmpty() {
return size == 0;
}
public int routeDistance() {
int totalDist = 0;
LinkedLocation currLocation = this.head;
for(int i = 0; i < size; i++) {
totalDist += Tsp.calcDistance(currLocation.getLocation(),
currLocation.getNext().getLocation());
currLocation = currLocation.getNext();
}
return totalDist;
}
public LinkedLocation getNode(int j) {
LinkedLocation currLocation = this.head;
for(int i = 0; i < j; i++ ) {
currLocation = currLocation.getNext();
}
return currLocation;
}
public Route swap(int i, int j) {
Route newRoute = new Route(this.head.getLocation());
LinkedLocation currLocation = this.head;
//step one
for(int k = 1; k < i ; ++k) {
currLocation = this.head;
for(int m = 0; m < k; m++) {
currLocation = currLocation.getNext();
}
newRoute.addDestination(currLocation.getLocation());
}
//step 2
for(int k = j; k > i - 1; k--) {
currLocation = this.head;
for(int m = 0; m < k ; m++) {
currLocation = currLocation.getNext();
}
newRoute.addDestination(currLocation.getLocation());
}
//step 3
for(int k = j + 1; k < this.size; k++) {
currLocation = this.head;
for(int m = 0; m < k; m++) {
currLocation = currLocation.getNext();
}
newRoute.addDestination(currLocation.getLocation());
}
return newRoute;
}
public String toString() {
String string = "This route contains " + this.size + " destinations \n";
if (size == 0) {
return string;
}
int i = 0;
LinkedLocation currLocation = this.head;
while(i < size) {
string += currLocation.getLocation().getName() + ", ";
currLocation = currLocation.getNext();
i++;
}
return string;
}
}