dancealgorithm 0.1.2__py3-none-any.whl → 0.1.4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dancealgorithm/__init__.py +1 -1
- dancealgorithm/assignments/LC - Delivery/Solution.java +180 -261
- dancealgorithm/assignments/LC - Drilling/Solution.java +197 -275
- dancealgorithm/assignments/LC - Flight/Solution.java +180 -261
- dancealgorithm/downloader.py +1 -1
- {dancealgorithm-0.1.2.dist-info → dancealgorithm-0.1.4.dist-info}/METADATA +1 -1
- {dancealgorithm-0.1.2.dist-info → dancealgorithm-0.1.4.dist-info}/RECORD +11 -11
- {dancealgorithm-0.1.2.dist-info → dancealgorithm-0.1.4.dist-info}/WHEEL +0 -0
- {dancealgorithm-0.1.2.dist-info → dancealgorithm-0.1.4.dist-info}/entry_points.txt +0 -0
- {dancealgorithm-0.1.2.dist-info → dancealgorithm-0.1.4.dist-info}/licenses/LICENSE +0 -0
- {dancealgorithm-0.1.2.dist-info → dancealgorithm-0.1.4.dist-info}/top_level.txt +0 -0
dancealgorithm/__init__.py
CHANGED
|
@@ -9,7 +9,7 @@ import sys
|
|
|
9
9
|
from pathlib import Path
|
|
10
10
|
from typing import Any, Dict, List, Optional, Union
|
|
11
11
|
|
|
12
|
-
__version__ = "0.1.
|
|
12
|
+
__version__ = "0.1.4"
|
|
13
13
|
|
|
14
14
|
# Import bundled assignments registry and sheets
|
|
15
15
|
from dancealgorithm.assignments import ASSIGNMENTS_DIR, REGISTRY, SHEETS
|
|
@@ -1,261 +1,180 @@
|
|
|
1
|
-
|
|
2
|
-
import java.util
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
int
|
|
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
|
-
for (int
|
|
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
|
-
continue;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
int iU = curr.iCity;
|
|
187
|
-
|
|
188
|
-
// 9. Generate Child Nodes
|
|
189
|
-
for (int iV = 0; iV < iSize; iV++) {
|
|
190
|
-
boolean bVisited = false;
|
|
191
|
-
|
|
192
|
-
for (int iCnt = 0; iCnt < curr.iPath.length; iCnt++) {
|
|
193
|
-
if (curr.iPath[iCnt] == iV) {
|
|
194
|
-
bVisited = true;
|
|
195
|
-
break;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
if (!bVisited && curr.dMatrix[iU][iV] < INF) {
|
|
200
|
-
double dChild[][] = new double[iSize][iSize];
|
|
201
|
-
|
|
202
|
-
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
203
|
-
System.arraycopy(curr.dMatrix[iCnt], 0,
|
|
204
|
-
dChild[iCnt], 0, iSize);
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
double dEdge = curr.dMatrix[iU][iV];
|
|
208
|
-
|
|
209
|
-
for (int jCnt = 0; jCnt < iSize; jCnt++) {
|
|
210
|
-
dChild[iU][jCnt] = INF;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
214
|
-
dChild[iCnt][iV] = INF;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
if (curr.iLevel < iSize - 1) {
|
|
218
|
-
dChild[iV][0] = INF;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
double dReduction = ReduceMatrix(dChild, iSize);
|
|
222
|
-
|
|
223
|
-
double dChildCost = curr.dCost +
|
|
224
|
-
dEdge + dReduction;
|
|
225
|
-
|
|
226
|
-
if (dChildCost < dBestCost) {
|
|
227
|
-
int iChildPath[] = Arrays.copyOf(
|
|
228
|
-
curr.iPath, curr.iPath.length + 1);
|
|
229
|
-
|
|
230
|
-
iChildPath[iChildPath.length - 1] = iV;
|
|
231
|
-
|
|
232
|
-
pq.add(new Node(dChild, iChildPath,
|
|
233
|
-
curr.iLevel + 1,
|
|
234
|
-
dChildCost, iV));
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
// 10. Display Optimal Tour
|
|
241
|
-
if (iBestTour == null) {
|
|
242
|
-
System.out.println("\nNo complete round-trip route exists.");
|
|
243
|
-
return;
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
System.out.printf("\nOptimal Round-Trip Delivery Cost: Rs. %.2f%n",
|
|
247
|
-
dBestCost);
|
|
248
|
-
|
|
249
|
-
System.out.print("Optimal Delivery Tour: ");
|
|
250
|
-
|
|
251
|
-
for (int iCnt = 0; iCnt <= iSize; iCnt++) {
|
|
252
|
-
System.out.print(sLocations[iBestTour[iCnt]]);
|
|
253
|
-
|
|
254
|
-
if (iCnt < iSize) {
|
|
255
|
-
System.out.print(" -> ");
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
System.out.println();
|
|
260
|
-
}
|
|
261
|
-
}
|
|
1
|
+
import java.util.Arrays;
|
|
2
|
+
import java.util.PriorityQueue;
|
|
3
|
+
|
|
4
|
+
// Main solution
|
|
5
|
+
public class Solution {
|
|
6
|
+
// O(n! * n^2)
|
|
7
|
+
static final double INF = 1e9;
|
|
8
|
+
|
|
9
|
+
// Search node
|
|
10
|
+
static class Node implements Comparable<Node> {
|
|
11
|
+
double[][] matrix;
|
|
12
|
+
int[] path;
|
|
13
|
+
int level;
|
|
14
|
+
double cost;
|
|
15
|
+
int city;
|
|
16
|
+
|
|
17
|
+
Node(double[][] mat, int[] path, int level, double cost, int city) {
|
|
18
|
+
int n = mat.length;
|
|
19
|
+
this.matrix = new double[n][n];
|
|
20
|
+
for (int i = 0; i < n; i++) {
|
|
21
|
+
System.arraycopy(mat[i], 0, this.matrix[i], 0, n);
|
|
22
|
+
}
|
|
23
|
+
this.path = Arrays.copyOf(path, path.length);
|
|
24
|
+
this.level = level;
|
|
25
|
+
this.cost = cost;
|
|
26
|
+
this.city = city;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
@Override
|
|
30
|
+
public int compareTo(Node other) {
|
|
31
|
+
return Double.compare(this.cost, other.cost);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Matrix reduction
|
|
36
|
+
public static double reduceMatrix(double[][] mat, int n) {
|
|
37
|
+
double reduction = 0;
|
|
38
|
+
|
|
39
|
+
for (int i = 0; i < n; i++) {
|
|
40
|
+
double rmin = INF;
|
|
41
|
+
for (int j = 0; j < n; j++) {
|
|
42
|
+
if (mat[i][j] < rmin) rmin = mat[i][j];
|
|
43
|
+
}
|
|
44
|
+
if (rmin > 0 && rmin < INF) {
|
|
45
|
+
reduction += rmin;
|
|
46
|
+
for (int j = 0; j < n; j++) {
|
|
47
|
+
if (mat[i][j] < INF) mat[i][j] -= rmin;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
for (int j = 0; j < n; j++) {
|
|
53
|
+
double cmin = INF;
|
|
54
|
+
for (int i = 0; i < n; i++) {
|
|
55
|
+
if (mat[i][j] < cmin) cmin = mat[i][j];
|
|
56
|
+
}
|
|
57
|
+
if (cmin > 0 && cmin < INF) {
|
|
58
|
+
reduction += cmin;
|
|
59
|
+
for (int i = 0; i < n; i++) {
|
|
60
|
+
if (mat[i][j] < INF) mat[i][j] -= cmin;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return reduction;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Branch and bound TSP
|
|
69
|
+
public static void solveLCBB_TSP(double[][] costMatrix, String[] locations) {
|
|
70
|
+
int n = costMatrix.length;
|
|
71
|
+
|
|
72
|
+
double[][] initialMat = new double[n][n];
|
|
73
|
+
for (int i = 0; i < n; i++) {
|
|
74
|
+
System.arraycopy(costMatrix[i], 0, initialMat[i], 0, n);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
double rootCost = reduceMatrix(initialMat, n);
|
|
78
|
+
|
|
79
|
+
PriorityQueue<Node> pq = new PriorityQueue<>();
|
|
80
|
+
int[] rootPath = {0};
|
|
81
|
+
pq.add(new Node(initialMat, rootPath, 1, rootCost, 0));
|
|
82
|
+
|
|
83
|
+
double bestCost = INF;
|
|
84
|
+
int[] bestTour = null;
|
|
85
|
+
|
|
86
|
+
while (!pq.isEmpty()) {
|
|
87
|
+
Node curr = pq.poll();
|
|
88
|
+
|
|
89
|
+
if (curr.cost >= bestCost) {
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (curr.level == n) {
|
|
94
|
+
int lastCity = curr.city;
|
|
95
|
+
if (costMatrix[lastCity][0] < INF) {
|
|
96
|
+
double tourCost = 0;
|
|
97
|
+
for (int i = 0; i < n - 1; i++) {
|
|
98
|
+
tourCost += costMatrix[curr.path[i]][curr.path[i + 1]];
|
|
99
|
+
}
|
|
100
|
+
tourCost += costMatrix[lastCity][0];
|
|
101
|
+
|
|
102
|
+
if (tourCost < bestCost) {
|
|
103
|
+
bestCost = tourCost;
|
|
104
|
+
bestTour = new int[n + 1];
|
|
105
|
+
System.arraycopy(curr.path, 0, bestTour, 0, n);
|
|
106
|
+
bestTour[n] = 0;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
int u = curr.city;
|
|
113
|
+
|
|
114
|
+
for (int v = 0; v < n; v++) {
|
|
115
|
+
boolean visited = false;
|
|
116
|
+
for (int p : curr.path) {
|
|
117
|
+
if (p == v) { visited = true; break; }
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (!visited && curr.matrix[u][v] < INF) {
|
|
121
|
+
double[][] childMat = new double[n][n];
|
|
122
|
+
for (int i = 0; i < n; i++) {
|
|
123
|
+
System.arraycopy(curr.matrix[i], 0, childMat[i], 0, n);
|
|
124
|
+
}
|
|
125
|
+
double edgeVal = curr.matrix[u][v];
|
|
126
|
+
|
|
127
|
+
for (int j = 0; j < n; j++) childMat[u][j] = INF;
|
|
128
|
+
for (int i = 0; i < n; i++) childMat[i][v] = INF;
|
|
129
|
+
if (curr.level < n - 1) {
|
|
130
|
+
childMat[v][0] = INF;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
double reduction = reduceMatrix(childMat, n);
|
|
134
|
+
double childCost = curr.cost + edgeVal + reduction;
|
|
135
|
+
|
|
136
|
+
if (childCost < bestCost) {
|
|
137
|
+
int[] childPath = Arrays.copyOf(curr.path, curr.path.length + 1);
|
|
138
|
+
childPath[childPath.length - 1] = v;
|
|
139
|
+
pq.add(new Node(childMat, childPath, curr.level + 1, childCost, v));
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
System.out.printf("%nOptimal Round-Trip Delivery Cost: Rs. %.2f%n", bestCost);
|
|
146
|
+
System.out.print("Optimal Delivery Tour Sequence : ");
|
|
147
|
+
if (bestTour != null) {
|
|
148
|
+
for (int i = 0; i <= n; i++) {
|
|
149
|
+
System.out.print(locations[bestTour[i]]);
|
|
150
|
+
if (i < n) System.out.print(" -> ");
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
System.out.println();
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Driver code
|
|
157
|
+
public static void main(String args[]) {
|
|
158
|
+
System.out.println("=======================================================================");
|
|
159
|
+
System.out.println("PS 22: SwiftShip Delivery Route TSP - LC Branch & Bound (Java)");
|
|
160
|
+
System.out.println("=======================================================================");
|
|
161
|
+
|
|
162
|
+
String locations[] = {
|
|
163
|
+
"Central Depot (Connaught Place)",
|
|
164
|
+
"East Hub (Anand Vihar)",
|
|
165
|
+
"North Hub (Rohini)",
|
|
166
|
+
"West Hub (Dwarka)",
|
|
167
|
+
"South Hub (Saket)"
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
double costMatrix[][] = {
|
|
171
|
+
{INF, 20, 30, 10, 11},
|
|
172
|
+
{15, INF, 16, 4, 2},
|
|
173
|
+
{3, 5, INF, 2, 4},
|
|
174
|
+
{19, 6, 18, INF, 3},
|
|
175
|
+
{16, 4, 7, 16, INF}
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
solveLCBB_TSP(costMatrix, locations);
|
|
179
|
+
}
|
|
180
|
+
}
|