dancealgorithm 0.1.2__tar.gz → 0.1.4__tar.gz
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-0.1.2/src/dancealgorithm.egg-info → dancealgorithm-0.1.4}/PKG-INFO +1 -1
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/pyproject.toml +1 -1
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/__init__.py +1 -1
- dancealgorithm-0.1.4/src/dancealgorithm/assignments/LC - Delivery/Solution.java +180 -0
- dancealgorithm-0.1.4/src/dancealgorithm/assignments/LC - Drilling/Solution.java +197 -0
- dancealgorithm-0.1.4/src/dancealgorithm/assignments/LC - Flight/Solution.java +180 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/downloader.py +1 -1
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4/src/dancealgorithm.egg-info}/PKG-INFO +1 -1
- dancealgorithm-0.1.2/src/dancealgorithm/assignments/LC - Delivery/Solution.java +0 -261
- dancealgorithm-0.1.2/src/dancealgorithm/assignments/LC - Drilling/Solution.java +0 -275
- dancealgorithm-0.1.2/src/dancealgorithm/assignments/LC - Flight/Solution.java +0 -261
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/LICENSE +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/README.md +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/setup.cfg +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/__main__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/DA - Campus/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/DA - Campus/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/DA - Flight/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/DA - Flight/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/DA - Smart/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/DA - Smart/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/DA.txt +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/FK - Advertisement/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/FK - Advertisement/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/FK - Electric/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/FK - Electric/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/FK - Emergency/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/FK - Emergency/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/FK.txt +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/GC - Classroom/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/GC - Classroom/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/GC - Interference/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/GC - Interference/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/GC - University/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/GC - University/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/GC.txt +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/K - Cloud/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/K - Cloud/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/K - Disaster/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/K - Disaster/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/K - Hospital/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/K - Hospital/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/K.txt +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/LC - Delivery/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/LC - Drilling/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/LC - Flight/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/LC.txt +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/MS - Commerce/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/MS - Commerce/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/MS - Commerce/dataset.csv +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/MS - Customer/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/MS - Customer/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/MS - Customer/dataset.csv +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/MS - Library/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/MS - Library/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/MS - Library/dataset.csv +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/MS.txt +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/MSG - Emergency/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/MSG - Emergency/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/MSG - Logistics/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/MSG - Logistics/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/MSG - Skyconnect/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/MSG - Skyconnect/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/MSG.txt +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/QS - Commerce/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/QS - Commerce/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/QS - Commerce/dataset.csv +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/QS - Movie/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/QS - Movie/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/QS - Movie/dataset.csv +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/QS - Online/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/QS - Online/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/QS - Online/dataset.csv +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/QS.txt +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/assignments/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/cli.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm/config.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm.egg-info/SOURCES.txt +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm.egg-info/dependency_links.txt +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm.egg-info/entry_points.txt +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.4}/src/dancealgorithm.egg-info/top_level.txt +0 -0
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "dancealgorithm"
|
|
7
|
-
version = "0.1.
|
|
7
|
+
version = "0.1.4"
|
|
8
8
|
description = "DanceAlgorithm: Instant DAA Algorithms, Lab Problem Sheets, Datasets, and Code Vault."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.9"
|
|
@@ -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
|
|
@@ -0,0 +1,180 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
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
|
+
static class Hole {
|
|
10
|
+
double x, y;
|
|
11
|
+
String label;
|
|
12
|
+
|
|
13
|
+
Hole(double x, double y, String label) {
|
|
14
|
+
this.x = x;
|
|
15
|
+
this.y = y;
|
|
16
|
+
this.label = label;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Search node
|
|
21
|
+
static class Node implements Comparable<Node> {
|
|
22
|
+
double[][] matrix;
|
|
23
|
+
int[] path;
|
|
24
|
+
int level;
|
|
25
|
+
double cost;
|
|
26
|
+
int city;
|
|
27
|
+
|
|
28
|
+
Node(double[][] mat, int[] path, int level, double cost, int city) {
|
|
29
|
+
int n = mat.length;
|
|
30
|
+
this.matrix = new double[n][n];
|
|
31
|
+
for (int i = 0; i < n; i++) {
|
|
32
|
+
System.arraycopy(mat[i], 0, this.matrix[i], 0, n);
|
|
33
|
+
}
|
|
34
|
+
this.path = Arrays.copyOf(path, path.length);
|
|
35
|
+
this.level = level;
|
|
36
|
+
this.cost = cost;
|
|
37
|
+
this.city = city;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
@Override
|
|
41
|
+
public int compareTo(Node other) {
|
|
42
|
+
return Double.compare(this.cost, other.cost);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Matrix reduction
|
|
47
|
+
public static double reduceMatrix(double[][] mat, int n) {
|
|
48
|
+
double reduction = 0;
|
|
49
|
+
|
|
50
|
+
for (int i = 0; i < n; i++) {
|
|
51
|
+
double rmin = INF;
|
|
52
|
+
for (int j = 0; j < n; j++) {
|
|
53
|
+
if (mat[i][j] < rmin) rmin = mat[i][j];
|
|
54
|
+
}
|
|
55
|
+
if (rmin > 0 && rmin < INF) {
|
|
56
|
+
reduction += rmin;
|
|
57
|
+
for (int j = 0; j < n; j++) {
|
|
58
|
+
if (mat[i][j] < INF) mat[i][j] -= rmin;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
for (int j = 0; j < n; j++) {
|
|
64
|
+
double cmin = INF;
|
|
65
|
+
for (int i = 0; i < n; i++) {
|
|
66
|
+
if (mat[i][j] < cmin) cmin = mat[i][j];
|
|
67
|
+
}
|
|
68
|
+
if (cmin > 0 && cmin < INF) {
|
|
69
|
+
reduction += cmin;
|
|
70
|
+
for (int i = 0; i < n; i++) {
|
|
71
|
+
if (mat[i][j] < INF) mat[i][j] -= cmin;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return reduction;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Branch and bound TSP
|
|
80
|
+
public static void solveLCBB_TSP(double[][] costMatrix, Hole[] holes) {
|
|
81
|
+
int n = costMatrix.length;
|
|
82
|
+
|
|
83
|
+
double[][] initialMat = new double[n][n];
|
|
84
|
+
for (int i = 0; i < n; i++) {
|
|
85
|
+
System.arraycopy(costMatrix[i], 0, initialMat[i], 0, n);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
double rootCost = reduceMatrix(initialMat, n);
|
|
89
|
+
|
|
90
|
+
PriorityQueue<Node> pq = new PriorityQueue<>();
|
|
91
|
+
int[] rootPath = {0};
|
|
92
|
+
pq.add(new Node(initialMat, rootPath, 1, rootCost, 0));
|
|
93
|
+
|
|
94
|
+
double bestCost = INF;
|
|
95
|
+
int[] bestTour = null;
|
|
96
|
+
|
|
97
|
+
while (!pq.isEmpty()) {
|
|
98
|
+
Node curr = pq.poll();
|
|
99
|
+
|
|
100
|
+
if (curr.cost >= bestCost) {
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (curr.level == n) {
|
|
105
|
+
int lastCity = curr.city;
|
|
106
|
+
if (costMatrix[lastCity][0] < INF) {
|
|
107
|
+
double tourCost = 0;
|
|
108
|
+
for (int i = 0; i < n - 1; i++) {
|
|
109
|
+
tourCost += costMatrix[curr.path[i]][curr.path[i + 1]];
|
|
110
|
+
}
|
|
111
|
+
tourCost += costMatrix[lastCity][0];
|
|
112
|
+
|
|
113
|
+
if (tourCost < bestCost) {
|
|
114
|
+
bestCost = tourCost;
|
|
115
|
+
bestTour = new int[n + 1];
|
|
116
|
+
System.arraycopy(curr.path, 0, bestTour, 0, n);
|
|
117
|
+
bestTour[n] = 0;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
int u = curr.city;
|
|
124
|
+
|
|
125
|
+
for (int v = 0; v < n; v++) {
|
|
126
|
+
boolean visited = false;
|
|
127
|
+
for (int p : curr.path) {
|
|
128
|
+
if (p == v) { visited = true; break; }
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (!visited && curr.matrix[u][v] < INF) {
|
|
132
|
+
double[][] childMat = new double[n][n];
|
|
133
|
+
for (int i = 0; i < n; i++) {
|
|
134
|
+
System.arraycopy(curr.matrix[i], 0, childMat[i], 0, n);
|
|
135
|
+
}
|
|
136
|
+
double edgeVal = curr.matrix[u][v];
|
|
137
|
+
|
|
138
|
+
for (int j = 0; j < n; j++) childMat[u][j] = INF;
|
|
139
|
+
for (int i = 0; i < n; i++) childMat[i][v] = INF;
|
|
140
|
+
if (curr.level < n - 1) {
|
|
141
|
+
childMat[v][0] = INF;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
double reduction = reduceMatrix(childMat, n);
|
|
145
|
+
double childCost = curr.cost + edgeVal + reduction;
|
|
146
|
+
|
|
147
|
+
if (childCost < bestCost) {
|
|
148
|
+
int[] childPath = Arrays.copyOf(curr.path, curr.path.length + 1);
|
|
149
|
+
childPath[childPath.length - 1] = v;
|
|
150
|
+
pq.add(new Node(childMat, childPath, curr.level + 1, childCost, v));
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
System.out.printf("%nTotal Drill Head Travel Distance : %.2f mm%n", bestCost);
|
|
157
|
+
System.out.print("Optimal Drilling Toolpath Sequence : ");
|
|
158
|
+
if (bestTour != null) {
|
|
159
|
+
for (int i = 0; i <= n; i++) {
|
|
160
|
+
System.out.print(holes[bestTour[i]].label);
|
|
161
|
+
if (i < n) System.out.print(" -> ");
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
System.out.println();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Driver code
|
|
168
|
+
public static void main(String args[]) {
|
|
169
|
+
System.out.println("=======================================================================");
|
|
170
|
+
System.out.println("PS 24: CircuitPro PCB CNC Drilling - LC Branch & Bound (Java)");
|
|
171
|
+
System.out.println("=======================================================================");
|
|
172
|
+
|
|
173
|
+
Hole holes[] = {
|
|
174
|
+
new Hole(0.0, 0.0, "Home (0,0)"),
|
|
175
|
+
new Hole(10.0, 45.0, "Hole-1 (10,45)"),
|
|
176
|
+
new Hole(25.0, 80.0, "Hole-2 (25,80)"),
|
|
177
|
+
new Hole(60.0, 75.0, "Hole-3 (60,75)"),
|
|
178
|
+
new Hole(85.0, 30.0, "Hole-4 (85,30)"),
|
|
179
|
+
new Hole(40.0, 10.0, "Hole-5 (40,10)")
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
int n = holes.length;
|
|
183
|
+
double costMatrix[][] = new double[n][n];
|
|
184
|
+
|
|
185
|
+
for (int i = 0; i < n; i++) {
|
|
186
|
+
for (int j = 0; j < n; j++) {
|
|
187
|
+
if (i == j) {
|
|
188
|
+
costMatrix[i][j] = INF;
|
|
189
|
+
} else {
|
|
190
|
+
costMatrix[i][j] = Math.hypot(holes[i].x - holes[j].x, holes[i].y - holes[j].y);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
solveLCBB_TSP(costMatrix, holes);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
@@ -0,0 +1,180 @@
|
|
|
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[] airports) {
|
|
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("%nTotal Operational Aircraft Fuel & Gate Cost: Rs. %.2f (Lakhs)%n", bestCost);
|
|
146
|
+
System.out.print("Optimal Aircraft Tour Circuit : ");
|
|
147
|
+
if (bestTour != null) {
|
|
148
|
+
for (int i = 0; i <= n; i++) {
|
|
149
|
+
System.out.print(airports[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 23: AeroConnect Aircraft Circuit Tour - LC Branch & Bound (Java)");
|
|
160
|
+
System.out.println("=======================================================================");
|
|
161
|
+
|
|
162
|
+
String airports[] = {
|
|
163
|
+
"Delhi (DL)",
|
|
164
|
+
"Mumbai (MH)",
|
|
165
|
+
"Pune (MH)",
|
|
166
|
+
"Ahmedabad (GJ)",
|
|
167
|
+
"Bengaluru (KA)"
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
double costMatrix[][] = {
|
|
171
|
+
{INF, 12, 29, 45, 38},
|
|
172
|
+
{14, INF, 19, 36, 42},
|
|
173
|
+
{30, 21, INF, 18, 55},
|
|
174
|
+
{48, 38, 17, INF, 50},
|
|
175
|
+
{39, 40, 52, 49, INF}
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
solveLCBB_TSP(costMatrix, airports);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
@@ -15,7 +15,7 @@ import zipfile
|
|
|
15
15
|
from pathlib import Path
|
|
16
16
|
from typing import Dict, List, Optional, Tuple
|
|
17
17
|
|
|
18
|
-
USER_AGENT = "DanceAlgorithm-CLI/0.1.
|
|
18
|
+
USER_AGENT = "DanceAlgorithm-CLI/0.1.4"
|
|
19
19
|
MAX_ARCHIVE_SIZE_BYTES = 300 * 1024 * 1024 # 300 MB limit
|
|
20
20
|
MAX_FILE_COUNT = 10000 # 10,000 files limit
|
|
21
21
|
|