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.
@@ -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.2"
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
- public class Solution {
5
- static final double INF = 1e9;
6
-
7
- // 1. Main Function
8
- public static void main(String args[]) {
9
- Scanner sc = new Scanner(System.in);
10
-
11
- System.out.print("Enter number of locations: ");
12
- int iSize = sc.nextInt();
13
-
14
- String sLocations[] = new String[iSize];
15
-
16
- // 2. Get Location Names
17
- sc.nextLine();
18
-
19
- for (int iCnt = 0; iCnt < iSize; iCnt++) {
20
- System.out.print("Enter location " + (iCnt + 1) + ": ");
21
- sLocations[iCnt] = sc.nextLine();
22
- }
23
-
24
- // 3. Get Cost Matrix
25
- double dGraph[][] = new double[iSize][iSize];
26
-
27
- System.out.println("\nEnter travel cost matrix:");
28
- System.out.println("Enter -1 if there is no direct route.");
29
-
30
- for (int iCnt = 0; iCnt < iSize; iCnt++) {
31
- for (int jCnt = 0; jCnt < iSize; jCnt++) {
32
- if (iCnt == jCnt) {
33
- dGraph[iCnt][jCnt] = INF;
34
- } else {
35
- System.out.print("Cost from " + sLocations[iCnt] +
36
- " to " + sLocations[jCnt] + ": ");
37
-
38
- double dValue = sc.nextDouble();
39
-
40
- if (dValue == -1) {
41
- dGraph[iCnt][jCnt] = INF;
42
- } else {
43
- dGraph[iCnt][jCnt] = dValue;
44
- }
45
- }
46
- }
47
- }
48
-
49
- // 4. Apply LC Branch and Bound
50
- SolveTSP(dGraph, sLocations);
51
-
52
- sc.close();
53
- }
54
-
55
- // 5. Search Node
56
- static class Node implements Comparable<Node> {
57
- double dMatrix[][];
58
- int iPath[];
59
- int iLevel;
60
- double dCost;
61
- int iCity;
62
-
63
- Node(double dGraph[][], int iPath[],
64
- int iLevel, double dCost, int iCity) {
65
- int iSize = dGraph.length;
66
-
67
- dMatrix = new double[iSize][iSize];
68
-
69
- for (int iCnt = 0; iCnt < iSize; iCnt++) {
70
- System.arraycopy(dGraph[iCnt], 0,
71
- dMatrix[iCnt], 0, iSize);
72
- }
73
-
74
- this.iPath = Arrays.copyOf(iPath, iPath.length);
75
- this.iLevel = iLevel;
76
- this.dCost = dCost;
77
- this.iCity = iCity;
78
- }
79
-
80
- public int compareTo(Node other) {
81
- return Double.compare(this.dCost, other.dCost);
82
- }
83
- }
84
-
85
- // 6. Reduce Cost Matrix
86
- static double ReduceMatrix(double dGraph[][], int iSize) {
87
- double dReduction = 0;
88
-
89
- // Row Reduction
90
- for (int iCnt = 0; iCnt < iSize; iCnt++) {
91
- double dMin = INF;
92
-
93
- for (int jCnt = 0; jCnt < iSize; jCnt++) {
94
- if (dGraph[iCnt][jCnt] < dMin) {
95
- dMin = dGraph[iCnt][jCnt];
96
- }
97
- }
98
-
99
- if (dMin > 0 && dMin < INF) {
100
- dReduction += dMin;
101
-
102
- for (int jCnt = 0; jCnt < iSize; jCnt++) {
103
- if (dGraph[iCnt][jCnt] < INF) {
104
- dGraph[iCnt][jCnt] -= dMin;
105
- }
106
- }
107
- }
108
- }
109
-
110
- // Column Reduction
111
- for (int jCnt = 0; jCnt < iSize; jCnt++) {
112
- double dMin = INF;
113
-
114
- for (int iCnt = 0; iCnt < iSize; iCnt++) {
115
- if (dGraph[iCnt][jCnt] < dMin) {
116
- dMin = dGraph[iCnt][jCnt];
117
- }
118
- }
119
-
120
- if (dMin > 0 && dMin < INF) {
121
- dReduction += dMin;
122
-
123
- for (int iCnt = 0; iCnt < iSize; iCnt++) {
124
- if (dGraph[iCnt][jCnt] < INF) {
125
- dGraph[iCnt][jCnt] -= dMin;
126
- }
127
- }
128
- }
129
- }
130
-
131
- return dReduction;
132
- }
133
-
134
- // 7. LC Branch and Bound TSP
135
- static void SolveTSP(double dGraph[][], String sLocations[]) {
136
- int iSize = dGraph.length;
137
-
138
- double dInitial[][] = new double[iSize][iSize];
139
-
140
- for (int iCnt = 0; iCnt < iSize; iCnt++) {
141
- System.arraycopy(dGraph[iCnt], 0,
142
- dInitial[iCnt], 0, iSize);
143
- }
144
-
145
- double dRootCost = ReduceMatrix(dInitial, iSize);
146
-
147
- PriorityQueue<Node> pq = new PriorityQueue<>();
148
-
149
- int iPath[] = { 0 };
150
-
151
- pq.add(new Node(dInitial, iPath, 1, dRootCost, 0));
152
-
153
- double dBestCost = INF;
154
- int iBestTour[] = null;
155
-
156
- // 8. Search for Minimum Cost Tour
157
- while (!pq.isEmpty()) {
158
- Node curr = pq.poll();
159
-
160
- if (curr.dCost >= dBestCost) {
161
- continue;
162
- }
163
-
164
- if (curr.iLevel == iSize) {
165
- int iLast = curr.iCity;
166
-
167
- if (dGraph[iLast][0] < INF) {
168
- double dTourCost = 0;
169
-
170
- for (int iCnt = 0; iCnt < iSize - 1; iCnt++) {
171
- dTourCost += dGraph[curr.iPath[iCnt]][curr.iPath[iCnt + 1]];
172
- }
173
-
174
- dTourCost += dGraph[iLast][0];
175
-
176
- if (dTourCost < dBestCost) {
177
- dBestCost = dTourCost;
178
- iBestTour = Arrays.copyOf(curr.iPath, iSize + 1);
179
- iBestTour[iSize] = 0;
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
+ }