Spaces:
Runtime error
Runtime error
苏泓源
commited on
Commit
·
fcdd69c
1
Parent(s):
b51f06d
update
Browse files
app.py
CHANGED
@@ -104,9 +104,91 @@ def demo_plot(city, facility):
|
|
104 |
|
105 |
|
106 |
def solver_plot(data_npy, boost=False):
|
107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
|
109 |
-
data = np.loadtxt(data_npy)
|
110 |
|
111 |
|
112 |
|
@@ -166,4 +248,4 @@ with gr.Blocks() as demo:
|
|
166 |
)
|
167 |
|
168 |
if __name__ == "__main__":
|
169 |
-
demo.launch(
|
|
|
104 |
|
105 |
|
106 |
def solver_plot(data_npy, boost=False):
|
107 |
+
data = data_npy.split('\n')
|
108 |
+
n = len(data)
|
109 |
+
p = int((len(data[0].split(' '))-2) / 2)
|
110 |
+
|
111 |
+
positions = []
|
112 |
+
demands = []
|
113 |
+
actual_facilities = []
|
114 |
+
for row in data:
|
115 |
+
row = row.split(' ')
|
116 |
+
row = [x for x in row if len(x)]
|
117 |
+
print(row)
|
118 |
+
|
119 |
+
positions.append([float(row[0]), float(row[1])])
|
120 |
+
|
121 |
+
demand = []
|
122 |
+
for i in range(2, 2+p):
|
123 |
+
demand.append(float(row[i]))
|
124 |
+
demands.append(demand)
|
125 |
+
|
126 |
+
actual_facility = []
|
127 |
+
for i in range(2+p, 2+2*p):
|
128 |
+
actual_facility.append(bool(int(row[i])))
|
129 |
+
actual_facilities.append(actual_facility)
|
130 |
+
positions = np.array(positions)
|
131 |
+
demands = np.array(demands)
|
132 |
+
actual_facilities = np.array(actual_facilities)
|
133 |
+
solution_facilities = ~actual_facilities
|
134 |
+
print(actual_facilities)
|
135 |
+
|
136 |
+
actual_fig = go.Figure()
|
137 |
+
solution_fig = go.Figure()
|
138 |
+
for i in range(p):
|
139 |
+
actual_fig.add_trace(go.Scattermapbox(
|
140 |
+
lat=positions[actual_facilities[:, i]][:, 0],
|
141 |
+
lon=positions[actual_facilities[:, i]][:, 1],
|
142 |
+
mode='markers',
|
143 |
+
marker=go.scattermapbox.Marker(
|
144 |
+
size=10,
|
145 |
+
color=px.colors.qualitative.Plotly[i]
|
146 |
+
),
|
147 |
+
name=f'Facility {i+1}'
|
148 |
+
))
|
149 |
+
solution_fig.add_trace(go.Scattermapbox(
|
150 |
+
lat=positions[solution_facilities[:, i]][:, 0],
|
151 |
+
lon=positions[solution_facilities[:, i]][:, 1],
|
152 |
+
mode='markers',
|
153 |
+
marker=go.scattermapbox.Marker(
|
154 |
+
size=10,
|
155 |
+
color=px.colors.qualitative.Plotly[i]
|
156 |
+
),
|
157 |
+
name=f'Facility {i+1}'
|
158 |
+
))
|
159 |
+
|
160 |
+
actual_fig.update_layout(
|
161 |
+
mapbox=dict(
|
162 |
+
style='carto-positron',
|
163 |
+
center=dict(lat=np.mean(positions[actual_facilities[:, i]][:, 0]), \
|
164 |
+
lon=np.mean(positions[actual_facilities[:, i]][:, 1])),
|
165 |
+
zoom=11.0
|
166 |
+
),
|
167 |
+
margin=dict(l=0, r=0, b=0, t=0),)
|
168 |
+
|
169 |
+
solution_fig.update_layout(
|
170 |
+
mapbox=dict(
|
171 |
+
style='carto-positron',
|
172 |
+
center=dict(lat=np.mean(positions[solution_facilities[:, i]][:, 0]), \
|
173 |
+
lon=np.mean(positions[solution_facilities[:, i]][:, 1])),
|
174 |
+
zoom=11.0
|
175 |
+
),
|
176 |
+
margin=dict(l=0, r=0, b=0, t=0),)
|
177 |
+
# show legend
|
178 |
+
actual_fig.update_layout(showlegend=True)
|
179 |
+
solution_fig.update_layout(showlegend=True)
|
180 |
+
|
181 |
+
positions = np.deg2rad(positions)
|
182 |
+
dist = pairwise_distances(positions, metric='haversine') * 6371
|
183 |
+
actual_ac = 0
|
184 |
+
solution_ac = 0
|
185 |
+
for i in range(p):
|
186 |
+
ac_matrix = dist * demands[:, i][:, None]
|
187 |
+
actual_ac += ac_matrix[:, actual_facilities[:, i]].min(axis=-1).sum()
|
188 |
+
solution_ac += ac_matrix[:, solution_facilities[:, i]].min(axis=-1).sum()
|
189 |
+
|
190 |
+
return actual_fig, solution_fig, actual_ac, solution_ac
|
191 |
|
|
|
192 |
|
193 |
|
194 |
|
|
|
248 |
)
|
249 |
|
250 |
if __name__ == "__main__":
|
251 |
+
demo.launch()
|