fudii0921 commited on
Commit
5784d50
·
verified ·
1 Parent(s): 90030f4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -0
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import networkx as nx
2
+ import gradio as gr
3
+
4
+ def find_shortest_path(graph, start, end):
5
+ """
6
+ Find the shortest path in a graph using Dijkstra's algorithm.
7
+
8
+ Parameters:
9
+ graph (dict): A dictionary representing the graph where keys are node names and values are dictionaries of neighboring nodes and their distances.
10
+ start (str): The starting node.
11
+ end (str): The destination node.
12
+
13
+ Returns:
14
+ list: The shortest path from start to end.
15
+ float: The total distance of the shortest path.
16
+ """
17
+ G = nx.DiGraph()
18
+
19
+ # Add edges to the graph
20
+ for node, neighbors in graph.items():
21
+ for neighbor, distance in neighbors.items():
22
+ G.add_edge(node, neighbor, weight=distance)
23
+
24
+ # Find the shortest path using Dijkstra's algorithm
25
+ shortest_path = nx.dijkstra_path(G, start, end)
26
+ total_distance = nx.dijkstra_path_length(G, start, end)
27
+
28
+ return shortest_path, total_distance
29
+
30
+ def combine_maps(city_map, jam_map):
31
+ """
32
+ Combine city map and jam map by adding their weights.
33
+
34
+ Parameters:
35
+ city_map (dict): A dictionary representing the city map where keys are node names and values are dictionaries of neighboring nodes and their distances.
36
+ jam_map (dict): A dictionary representing the jam map where keys are node names and values are dictionaries of neighboring nodes and their jam factors.
37
+
38
+ Returns:
39
+ dict: A combined map with summed weights.
40
+ """
41
+ combined_map = {}
42
+
43
+ for node in city_map:
44
+ combined_map[node] = {}
45
+ for neighbor in city_map[node]:
46
+ combined_map[node][neighbor] = city_map[node][neighbor] + jam_map[node][neighbor]
47
+
48
+ return combined_map
49
+
50
+ # Example graph representing the city map
51
+ city_map = {
52
+ '重症外傷センター': {'池袋': 2, '中目黒': 5},
53
+ '池袋': {'重症外傷センター': 2, '中目黒': 3, '六本木': 4},
54
+ '中目黒': {'重症外傷センター': 5, '池袋': 3, '六本木': 2, '銀座': 6},
55
+ '六本木': {'池袋': 4, '中目黒': 2, '銀座': 1},
56
+ '銀座': {'中目黒': 6, '六本木': 1}
57
+ }
58
+
59
+ # Example graph representing the jam map
60
+ jam_map = {
61
+ '重症外傷センター': {'池袋': 5, '中目黒': 2},
62
+ '池袋': {'重症外傷センター': 5, '中目黒': 4, '六本木': 3},
63
+ '中目黒': {'重症外傷センター': 5, '池袋': 6, '六本木': 3, '銀座': 2},
64
+ '六本木': {'池袋': 1, '中目黒': 3, '銀座': 5},
65
+ '銀座': {'中目黒': 3, '六本木': 4}
66
+ }
67
+
68
+ # Combine city map and jam map
69
+ combined_map = combine_maps(city_map, jam_map)
70
+
71
+ def shortest_path_interface(start_point, destination):
72
+ shortest_path, total_distance = find_shortest_path(combined_map, start_point, destination)
73
+ return f"一番短い{start_point}から{destination}までの経路は: {shortest_path}\nトータルの距離は{total_distance}ユニットです。"
74
+
75
+ # Create Gradio interface
76
+ iface = gr.Interface(
77
+ fn=shortest_path_interface,
78
+ inputs=[gr.Textbox(label="出発地",value="銀座"),gr.Textbox(label="到着地",value="重症外傷センター")],
79
+ outputs="text",
80
+ title="救急搬送ルートの最適化",
81
+ description="出発地点と目的地を入力して、最短経路を計算します。"
82
+ )
83
+
84
+ # Launch the interface
85
+ iface.launch()