瀏覽代碼

Add graph solving and plotting script

Toni Fadjukoff 8 年之前
父節點
當前提交
f53dd55e14
共有 2 個文件被更改,包括 106 次插入10 次删除
  1. 61 0
      satellites.py
  2. 45 10
      src/main/kotlin/name/lamperi/orbital/main.kt

+ 61 - 0
satellites.py 查看文件

@@ -0,0 +1,61 @@
1
+import numpy as np
2
+import matplotlib.pyplot as plt
3
+from mpl_toolkits.mplot3d import Axes3D
4
+from mpl_toolkits.mplot3d import proj3d
5
+
6
+data = [
7
+	["SAT0", 1346.496594546033, -6631.509574249269, -1784.401979591288],
8
+	["SAT1", 3986.6137971140965, -4309.794821125442, 3652.552861361319],
9
+	["SAT2", 6066.873731024742, 1768.4246674363346, -2645.6457201477174],
10
+	["SAT3", -531.4244577628424, -5725.907186159696, -3873.2462304175233],
11
+	["SAT4", -3029.294804348662, -6060.24715150741, -1163.1126511352572],
12
+	["SAT5", 4235.879179136093, -5048.655863634309, -1330.5861403372983],
13
+	["SAT6", -3213.176589077265, -4782.048496222451, -3740.7862955786086],
14
+	["SAT7", 176.0460873300058, 488.53751332601894, -6717.752685099294],
15
+	["SAT8", 286.49182959607276, 19.39475455789748, -6993.120858082854],
16
+	["SAT9", 6399.451382476649, -2113.9916687975474, -2002.7547869095122],
17
+	["SAT10", 3257.7488038015017, 1981.1452195906668, -5821.828870417848],
18
+	["SAT11", -581.1213836443779, 3374.6766339195256, 5964.093711437812],
19
+	["SAT12", 217.1789623719815, 178.85016316224656, -6843.305647372251],
20
+	["SAT13", 1176.6725366453472, -4957.9575328468, 4380.506198321206],
21
+	["SAT14", -1068.982383556387, 526.9728758308576, 6575.590264183411],
22
+	["SAT15", 1165.7614133253296, 584.8807748235947, 6545.811084796057],
23
+	["SAT16", -5993.097263023728, 2437.0166813209858, 2726.5808375640754],
24
+	["SAT17", -219.2118047031717, -105.032581318871, -6728.36026711676],
25
+	["SAT18", 3868.5717603149724, 4556.803440191685, -3214.801047688104],
26
+	["SAT19", 55.609142839530044, -1439.9935335382188, -6839.854023068437],
27
+	["Start", -2633.5917604717556, -5669.215907008842, -1230.3765435398993],
28
+	["End", -5854.876300459916, -190.38043902511365, -2504.759428517919]
29
+]
30
+x = [a[1] for a in data]
31
+y = [a[2] for a in data]
32
+z = [a[3] for a in data]
33
+names = [a[0] for a in data]
34
+
35
+fig = plt.figure()
36
+ax = fig.add_subplot(111, projection='3d')
37
+
38
+ax.scatter(x,y,z)
39
+
40
+plotlabels = []
41
+for name,xi,yi,zi in zip(names,x,y,z):
42
+	x2, y2, _ = proj3d.proj_transform(xi,yi,zi, ax.get_proj())
43
+	h = plt.annotate(name, xy = (x2,y2), xytext=(-20,20), textcoords='offset points', ha = 'right', va = 'bottom',
44
+			bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.5),
45
+			arrowprops = dict(arrowstyle = '-', connectionstyle = 'arc3,rad=0'))
46
+	plotlabels.append(h)
47
+
48
+def update_position(e):
49
+	for h,xi,yi,zi in zip(plotlabels, x, y, z):
50
+		x2, y2, _ = proj3d.proj_transform(xi,yi,zi, ax.get_proj())
51
+		h.xy = x2,y2
52
+		h.update_positions(fig.canvas.renderer)
53
+	fig.canvas.draw()
54
+
55
+fig.canvas.mpl_connect('motion_notify_event', update_position)
56
+
57
+ax.set_xlabel('X')
58
+ax.set_ylabel('Y')
59
+ax.set_zlabel('Z')
60
+
61
+plt.show()

+ 45 - 10
src/main/kotlin/name/lamperi/orbital/main.kt 查看文件

@@ -1,6 +1,8 @@
1
-package name.lamperi.orbital;
1
+package name.lamperi.orbital
2
+
3
+import java.io.File
4
+import java.util.*
2 5
 
3
-import java.io.File;
4 6
 
5 7
 // Latitude = South - North (-90...90)
6 8
 // Longitude = East West (-180...180)
@@ -162,6 +164,39 @@ fun buildGraph(problem : Problem) : Graph {
162 164
     return Graph(nodeMap)
163 165
 }
164 166
 
167
+data class Routing (val node : String, var parent : String?, var distance : Int = Integer.MAX_VALUE)
168
+
169
+fun solveGraph(start : String, end : String, graph : Graph) {
170
+    val routes : Map<String,Routing> = graph.nodes.map { Routing(node = it.key, parent = null) }.associateBy { it.node }
171
+
172
+    val queue = LinkedList<String>()
173
+
174
+    val route = routes[start]!!
175
+    route.distance = 0
176
+
177
+    queue.add(start)
178
+
179
+    while (!queue.isEmpty()) {
180
+        val current = queue.remove()
181
+        val currentRoute = routes[current]!!
182
+        val route = routes[current]!!
183
+
184
+        val node = graph.nodes[current]!!
185
+        for (neighbor in node.neighbors) {
186
+            val neighborRoute = routes[neighbor]!!
187
+            if (neighborRoute.distance == Integer.MAX_VALUE) {
188
+                neighborRoute.distance = currentRoute.distance + 1
189
+                neighborRoute.parent = current
190
+                queue.add(neighbor)
191
+            }
192
+        }
193
+    }
194
+
195
+    val endRoute = routes[end]
196
+    if (endRoute != null) {
197
+        println("Distance was ${endRoute.distance}")
198
+    }
199
+}
165 200
 
166 201
 fun main(args : Array<String>) {
167 202
     val file = File("src/main/resources/satellites.txt")
@@ -171,14 +206,14 @@ fun main(args : Array<String>) {
171 206
     val graph = buildGraph(problem)
172 207
     println(graph)
173 208
 
174
-    val startNode = graph.nodes["Start"]
175
-    if (startNode != null) {
176
-        startNode.neighbors.forEach { n ->
177
-            val neighbor = graph.nodes[n]
178
-            if (neighbor != null) {
179
-                println("${startNode.info.point} vs ${neighbor.info.point}")
180
-            }
181
-        }
209
+    solveGraph("Start", "End", graph)
210
+
211
+    /*
212
+    graph.nodes.values.forEach {
213
+        println("[\"${it.info.name}\", ${it.info.point.x}, ${it.info.point.y}, ${it.info.point.z}]")
182 214
     }
215
+    */
216
+
217
+
183 218
 
184 219
 }