Spaces:
Runtime error
Runtime error
苏泓源
commited on
Commit
·
94c9f8b
1
Parent(s):
ba82b7a
update
Browse files- app.py +70 -25
- demo/{NY.npy → New_York.npy} +0 -0
app.py
CHANGED
@@ -1,10 +1,11 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
-
import torch
|
4 |
import plotly.graph_objects as go
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
def plot_from_json(json_data):
|
8 |
fig = go.Figure()
|
9 |
|
10 |
fig.add_trace(go.Scatter(x=[1, 2, 3, 4], y=[10, 11, 12, 13], mode='lines', name='New York'))
|
@@ -17,27 +18,69 @@ def plot_from_json(json_data):
|
|
17 |
solution_fig = fig # Replace this line with your solution_fig
|
18 |
|
19 |
return actual_fig, solution_fig
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
-
return actual_ac, solution_ac
|
41 |
|
42 |
|
43 |
def get_example():
|
@@ -56,8 +99,11 @@ with gr.Blocks() as demo:
|
|
56 |
with gr.Row():
|
57 |
actual_map = gr.Plot(label='Actual Facility Distribution')
|
58 |
solution_map = gr.Plot(label='Relocation Facility Distribution')
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
61 |
|
62 |
gr.Markdown("## FLP&IUMFLP Solver")
|
63 |
with gr.Column():
|
@@ -76,14 +122,13 @@ with gr.Blocks() as demo:
|
|
76 |
with gr.Row():
|
77 |
actual_ac = gr.Textbox(label='Real-world Access Cost')
|
78 |
solution_ac = gr.Textbox(label='Relocation Access Cost')
|
79 |
-
data_file.upload(fn=
|
80 |
-
btn2.click(fn=
|
81 |
-
btn2.click(fn=calculate_ac, inputs=[city, facility, boost], outputs=[actual_ac, solution_ac])
|
82 |
|
83 |
gr.Examples(
|
84 |
examples=get_example(),
|
85 |
inputs=[data_npy],
|
86 |
-
fn=
|
87 |
outputs=[actual_map, solution_map],
|
88 |
)
|
89 |
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
|
|
3 |
import plotly.graph_objects as go
|
4 |
+
import plotly.express as px
|
5 |
+
from sklearn.metrics import pairwise_distances
|
6 |
+
import torch
|
7 |
|
8 |
+
def plot_from_npy(npy_data):
|
|
|
9 |
fig = go.Figure()
|
10 |
|
11 |
fig.add_trace(go.Scatter(x=[1, 2, 3, 4], y=[10, 11, 12, 13], mode='lines', name='New York'))
|
|
|
18 |
solution_fig = fig # Replace this line with your solution_fig
|
19 |
|
20 |
return actual_fig, solution_fig
|
21 |
+
|
22 |
+
def solver_plot(data_npy, boost=False):
|
23 |
+
actual_fig = go.Figure()
|
24 |
+
solution_fig = go.Figure()
|
25 |
+
actual_ac = 0 # Replace this line with your actual_ac
|
26 |
+
solution_ac = 0 # Replace this line with your solution_ac
|
27 |
+
return actual_fig, solution_fig, actual_ac, solution_ac
|
28 |
|
29 |
+
def demo_plot(city, facility):
|
30 |
+
data = np.load(f'data/{city.replace(' ', '_')}.npy')
|
31 |
+
positions = data[:, :2]
|
32 |
+
demands = data[:, 1 + len(facility)]
|
33 |
+
actual_facility = data[:, 1 + 2*len(facility)]
|
34 |
+
solution_facility = data[:, 1 + 3*len(facility)]
|
35 |
|
36 |
+
actual_fig = go.Figure()
|
37 |
+
solution_fig = go.Figure()
|
38 |
+
for i in range(len(facility)):
|
39 |
+
actual_fig.add_trace(go.Scattermapbox(
|
40 |
+
lat=positions[actual_facility[:,i]][:, 0],
|
41 |
+
lon=positions[actual_facility[:,i]][:, 1],
|
42 |
+
mode='markers',
|
43 |
+
marker=go.scattermapbox.Marker(
|
44 |
+
size=10,
|
45 |
+
color=px.colors.qualitative.Plotly[i]
|
46 |
+
),
|
47 |
+
name='Solution'
|
48 |
+
))
|
49 |
|
50 |
+
solution_fig.add_trace(go.Scattermapbox(
|
51 |
+
lat=positions[solution_facility[:,i]][:, 0],
|
52 |
+
lon=positions[solution_facility[:,i]][:, 1],
|
53 |
+
mode='markers',
|
54 |
+
marker=go.scattermapbox.Marker(
|
55 |
+
size=10,
|
56 |
+
color=px.colors.qualitative.Plotly[i]
|
57 |
+
),
|
58 |
+
name='Solution'
|
59 |
+
))
|
60 |
|
61 |
+
actual_fig.update_layout(
|
62 |
+
mapbox=dict(
|
63 |
+
style='carto-positron',
|
64 |
+
center=dict(lat=np.mean(positions[actual_facility[:,i]][:, 0]), \
|
65 |
+
lon=np.mean(positions[actual_facility[:,i]][:, 1])),
|
66 |
+
zoom=11.0
|
67 |
+
),
|
68 |
+
margin=dict(l=0, r=0, b=0, t=0),)
|
69 |
+
solution_fig.update_layout(
|
70 |
+
mapbox=dict(
|
71 |
+
style='carto-positron',
|
72 |
+
center=dict(lat=np.mean(positions[solution_facility[:,i]][:, 0]), \
|
73 |
+
lon=np.mean(positions[solution_facility[:,i]][:, 1])),
|
74 |
+
zoom=11.0
|
75 |
+
),
|
76 |
+
margin=dict(l=0, r=0, b=0, t=0),)
|
77 |
+
|
78 |
+
dist = pairwise_distances(positions, metric='haversine') * 6371
|
79 |
+
ac_matrix = dist * demands[:, None]
|
80 |
+
actual_ac = ac_matrix[:, actual_facility == 1].min(axis=-1).sum()
|
81 |
+
solution_ac = ac_matrix[:, solution_facility == 1].min(axis=-1).sum()
|
82 |
|
83 |
+
return actual_fig, solution_fig, actual_ac, solution_ac
|
84 |
|
85 |
|
86 |
def get_example():
|
|
|
99 |
with gr.Row():
|
100 |
actual_map = gr.Plot(label='Actual Facility Distribution')
|
101 |
solution_map = gr.Plot(label='Relocation Facility Distribution')
|
102 |
+
with gr.Row():
|
103 |
+
actual_ac = gr.Textbox(label='Real-world Access Cost')
|
104 |
+
solution_ac = gr.Textbox(label='Relocation Access Cost')
|
105 |
+
demo.load(fn=demo_plot, inputs=[city, facility], outputs=[actual_map, solution_map, actual_ac, solution_ac])
|
106 |
+
btn.click(fn=demo_plot, inputs=[city, facility], outputs=[actual_map, solution_map, actual_ac, solution_ac])
|
107 |
|
108 |
gr.Markdown("## FLP&IUMFLP Solver")
|
109 |
with gr.Column():
|
|
|
122 |
with gr.Row():
|
123 |
actual_ac = gr.Textbox(label='Real-world Access Cost')
|
124 |
solution_ac = gr.Textbox(label='Relocation Access Cost')
|
125 |
+
data_file.upload(fn=solver_plot, inputs=[data_file, boost], outputs=[actual_map, solution_map, actual_ac, solution_ac])
|
126 |
+
btn2.click(fn=solver_plot, inputs=[data_npy, boost], outputs=[actual_map, solution_map, actual_ac, solution_ac])
|
|
|
127 |
|
128 |
gr.Examples(
|
129 |
examples=get_example(),
|
130 |
inputs=[data_npy],
|
131 |
+
fn=plot_from_npy,
|
132 |
outputs=[actual_map, solution_map],
|
133 |
)
|
134 |
|
demo/{NY.npy → New_York.npy}
RENAMED
The diff for this file is too large to render.
See raw diff
|
|