File size: 12,148 Bytes
ef007f9
 
9036b2e
94c9f8b
 
 
a257639
 
9036b2e
c43ff17
94c9f8b
 
a257639
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94c9f8b
 
a257639
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94c9f8b
467801b
94c9f8b
c8cf824
a8c9d0c
 
 
 
 
 
 
2b7d085
8df3023
94c9f8b
a8c9d0c
 
 
9a6fd28
94c9f8b
 
a8c9d0c
 
 
 
94c9f8b
2b7d085
 
94c9f8b
 
 
 
 
c8cf824
94c9f8b
 
2b7d085
 
94c9f8b
 
 
 
 
c8cf824
94c9f8b
c43ff17
2b7d085
 
 
 
 
 
 
 
b51f06d
2b7d085
 
 
 
 
 
 
 
 
 
 
94c9f8b
2b7d085
94c9f8b
2b7d085
 
a8c9d0c
 
 
2b7d085
 
 
c43ff17
94c9f8b
c43ff17
 
a257639
fcdd69c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c8cf824
fcdd69c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b51f06d
 
c43ff17
 
a257639
 
b51f06d
c8cf824
 
 
a257639
c8cf824
 
b51f06d
c43ff17
62202b1
c43ff17
62202b1
2b7d085
 
c43ff17
 
 
c8cf824
94c9f8b
 
c8cf824
94c9f8b
 
c43ff17
c8cf824
c43ff17
 
ba82b7a
c43ff17
8df3023
c8cf824
8df3023
c43ff17
c8cf824
 
 
a257639
 
c8cf824
 
 
c43ff17
 
c8cf824
 
c43ff17
c8cf824
 
 
94c9f8b
c43ff17
ef007f9
c8cf824
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import gradio as gr
import numpy as np
import plotly.graph_objects as go
import plotly.express as px
from sklearn.metrics import pairwise_distances
import torch
from facility_location import multi_eval
import pickle



def solver_plot(data_npy, boost=False):
    multi_eval.main(data_npy, boost)
    all_solutions = pickle.loads(open('./facility_location/solutions.pkl', 'rb').read())
    
    data = data_npy.split('\n')
    n = len(data)
    p = int((len(data[0].split(' '))-2) / 2)
    
    positions = []
    demands = []
    actual_facilities = []
    for row in data:
        row = row.split(' ')
        row = [x for x in row if len(x)]
        
        positions.append([float(row[0]), float(row[1])])
        
        demand = []
        for i in range(2, 2+p):
            demand.append(float(row[i]))
        demands.append(demand)
        
        actual_facility = []
        for i in range(2+p, 2+2*p):
            actual_facility.append(bool(int(float(row[i]))))
        actual_facilities.append(actual_facility)
    positions = np.array(positions)
    demands = np.array(demands)
    actual_facilities = np.array(actual_facilities)
    solution_facilities =  np.array(all_solutions).T
    # print(solution_facilities)
    # print(actual_facilities)
        
    actual_fig = go.Figure()
    solution_fig = go.Figure()
    for i in range(p):
        actual_fig.add_trace(go.Scattermapbox(
            lat=positions[actual_facilities[:, i]][:, 0],
            lon=positions[actual_facilities[:, i]][:, 1],
            mode='markers',
            marker=go.scattermapbox.Marker(
                size=10,
                color=px.colors.qualitative.Plotly[i]
            ),
            name=f'Facility {i+1}'
        ))
        solution_fig.add_trace(go.Scattermapbox(
            lat=positions[solution_facilities[:, i]][:, 0],
            lon=positions[solution_facilities[:, i]][:, 1],
            mode='markers',
            marker=go.scattermapbox.Marker(
                size=10,
                color=px.colors.qualitative.Plotly[i]
            ),
            name=f'Facility {i+1}'
        ))
    
        actual_fig.update_layout(
        mapbox=dict(
            style='carto-positron',
            center=dict(lat=np.mean(positions[actual_facilities[:, i]][:, 0]), \
                lon=np.mean(positions[actual_facilities[:, i]][:, 1])),
            zoom=11.0
        ),
        margin=dict(l=0, r=0, b=0, t=0),)

        solution_fig.update_layout(
        mapbox=dict(
            style='carto-positron',
            center=dict(lat=np.mean(positions[solution_facilities[:, i]][:, 0]), \
                lon=np.mean(positions[solution_facilities[:, i]][:, 1])),
            zoom=11.0
        ),
        margin=dict(l=0, r=0, b=0, t=0),)
        # show legend
        actual_fig.update_layout(showlegend=True)
        solution_fig.update_layout(showlegend=True)
    
    positions = np.deg2rad(positions)
    dist = pairwise_distances(positions, metric='haversine') * 6371
    actual_ac = 0
    solution_ac = 0
    for i in range(p):
        ac_matrix = dist * demands[:, i][:, None]
        actual_ac += ac_matrix[:, actual_facilities[:, i]].min(axis=-1).sum()
        solution_ac += ac_matrix[:, solution_facilities[:, i]].min(axis=-1).sum()
        
    return actual_fig, solution_fig, actual_ac, solution_ac
    
def demo_plot(city, facility):
    facility_name = ["🏫 School", "πŸ₯ Hospital", "🌳 Park"]
    all_facility = ["🏫 School", "πŸ₯ Hospital", "🌳 Park"]
    for i in range(len(all_facility)):
        if all_facility[i] in facility:
            all_facility[i] = True
        else:
            all_facility[i] = False
    
    city_name = city.replace(' ', '_')
    data = np.loadtxt(f'demo/{city_name}.txt')
    positions = data[:, :2]
    demands = data[:, 2:5]
    actual_facility = data[:, 5:8]
    solution_facility = data[:, 8:11]
    
    actual_fig = go.Figure()
    solution_fig = go.Figure()
    for i in range(len(all_facility)):
        if not all_facility[i]:
            continue
        
        actual_fig.add_trace(go.Scattermapbox(
            lat=positions[actual_facility[:, i] == 1][:, 0],
            lon=positions[actual_facility[:, i] == 1][:, 1],
            mode='markers',
            marker=go.scattermapbox.Marker(
                size=10,
                color=px.colors.qualitative.Plotly[i]
            ),
            name=facility_name[i],
        ))
        solution_fig.add_trace(go.Scattermapbox(
            lat=positions[solution_facility[:, i] == 1][:, 0],
            lon=positions[solution_facility[:, i] == 1][:, 1],
            mode='markers',
            marker=go.scattermapbox.Marker(
                size=10,
                color=px.colors.qualitative.Plotly[i]
            ),
            name=facility_name[i],
        ))
    
        actual_fig.update_layout(
        mapbox=dict(
            style='carto-positron',
            center=dict(lat=np.mean(positions[actual_facility[:, i] == 1][:, 0]), \
                lon=np.mean(positions[actual_facility[:, i] == 1][:, 1])),
            zoom=11.0
        ),
        margin=dict(l=0, r=0, b=0, t=0),)

        solution_fig.update_layout(
        mapbox=dict(
            style='carto-positron',
            center=dict(lat=np.mean(positions[solution_facility[:, i] == 1][:, 0]), \
                lon=np.mean(positions[solution_facility[:, i] == 1][:, 1])),
            zoom=11.0
        ),
        margin=dict(l=0, r=0, b=0, t=0),)
        # show legend
        actual_fig.update_layout(showlegend=True)
        solution_fig.update_layout(showlegend=True)
    
    positions = np.deg2rad(positions)
    dist = pairwise_distances(positions, metric='haversine') * 6371
    actual_ac = 0
    solution_ac = 0
    for i in range(len(all_facility)):
        if not all_facility[i]:
            continue
        ac_matrix = dist * demands[:, i][:, None]
        actual_ac += ac_matrix[:, actual_facility[:, i] == 1].min(axis=-1).sum()
        solution_ac += ac_matrix[:, solution_facility[:, i] == 1].min(axis=-1).sum()
    
    return actual_fig, solution_fig, actual_ac, solution_ac


def solver_plot1(data_npy, boost=False):
    data = data_npy.split('\n')
    n = len(data)
    p = int((len(data[0].split(' '))-2) / 2)
    
    positions = []
    demands = []
    actual_facilities = []
    for row in data:
        row = row.split(' ')
        row = [x for x in row if len(x)]
        
        positions.append([float(row[0]), float(row[1])])
        
        demand = []
        for i in range(2, 2+p):
            demand.append(float(row[i]))
        demands.append(demand)
        
        actual_facility = []
        for i in range(2+p, 2+2*p):
            actual_facility.append(bool(int(float(row[i]))))
        actual_facilities.append(actual_facility)
    positions = np.array(positions)
    demands = np.array(demands)
    actual_facilities = np.array(actual_facilities)
    solution_facilities =  ~actual_facilities
        
    actual_fig = go.Figure()
    solution_fig = go.Figure()
    for i in range(p):
        actual_fig.add_trace(go.Scattermapbox(
            lat=positions[actual_facilities[:, i]][:, 0],
            lon=positions[actual_facilities[:, i]][:, 1],
            mode='markers',
            marker=go.scattermapbox.Marker(
                size=10,
                color=px.colors.qualitative.Plotly[i]
            ),
            name=f'Facility {i+1}'
        ))
        solution_fig.add_trace(go.Scattermapbox(
            lat=positions[solution_facilities[:, i]][:, 0],
            lon=positions[solution_facilities[:, i]][:, 1],
            mode='markers',
            marker=go.scattermapbox.Marker(
                size=10,
                color=px.colors.qualitative.Plotly[i]
            ),
            name=f'Facility {i+1}'
        ))
    
        actual_fig.update_layout(
        mapbox=dict(
            style='carto-positron',
            center=dict(lat=np.mean(positions[actual_facilities[:, i]][:, 0]), \
                lon=np.mean(positions[actual_facilities[:, i]][:, 1])),
            zoom=11.0
        ),
        margin=dict(l=0, r=0, b=0, t=0),)

        solution_fig.update_layout(
        mapbox=dict(
            style='carto-positron',
            center=dict(lat=np.mean(positions[solution_facilities[:, i]][:, 0]), \
                lon=np.mean(positions[solution_facilities[:, i]][:, 1])),
            zoom=11.0
        ),
        margin=dict(l=0, r=0, b=0, t=0),)
        # show legend
        actual_fig.update_layout(showlegend=True)
        solution_fig.update_layout(showlegend=True)
    
    positions = np.deg2rad(positions)
    dist = pairwise_distances(positions, metric='haversine') * 6371
    actual_ac = 0
    solution_ac = 0
    for i in range(p):
        ac_matrix = dist * demands[:, i][:, None]
        actual_ac += ac_matrix[:, actual_facilities[:, i]].min(axis=-1).sum()
        solution_ac += ac_matrix[:, solution_facilities[:, i]].min(axis=-1).sum()
        
    return actual_fig, solution_fig, actual_ac, solution_ac
    

def get_example():
    return [
        ('40.71 -73.93 213 1\n40.72 -73.99 15 1\n40.65 -73.88 365 1\n40.57 -73.96 629 0\n40.70 -73.97 106 0\n40.61 -73.95 189 1'),
        ("40.71 -73.93 213 124 0 1\n40.72 -73.99 15 43 1 0\n40.65 -73.88 365 214 1 0\n40.57 -73.96 629 431 0 1\n40.70 -73.97 106 241 0 1\n40.60 -73.92 129 214 1 0\n40.61 -73.95 189 264 0 1\n40.63 -73.94 124 164 1 0"),
        ]


def load_npy_file(file_obj):
    data = np.loadtxt(file_obj.name)
    string_array = '\n'.join([' '.join(map(str, row)) for row in data])
    return string_array
    

with gr.Blocks() as demo:
    gr.Markdown("## Demo")
    with gr.Column():
        city = gr.Radio(choices=["New York", "Boston", "Los Angeles", "Chicago"], value="New York", label="Select City:")
        facility = gr.CheckboxGroup(choices=["🏫 School", "πŸ₯ Hospital", "🌳 Park"], value=["πŸ₯ Hospital"], label="Select Facility:")
        btn = gr.Button(value="πŸš€ Generate")
        with gr.Row():
            actual_map = gr.Plot(label='Actual Facility Distribution')
            solution_map = gr.Plot(label='Relocated Facility Distribution')
        with gr.Row():
            actual_ac = gr.Textbox(label='Real-world Access Cost')
            solution_ac = gr.Textbox(label='Relocated Access Cost')
    demo.load(fn=demo_plot, inputs=[city, facility], outputs=[actual_map, solution_map, actual_ac, solution_ac])
    btn.click(fn=demo_plot, inputs=[city, facility], outputs=[actual_map, solution_map, actual_ac, solution_ac])
    
    gr.Markdown("## FLP & IUMFLP Solver")
    with gr.Column():
        with gr.Row():
            data_npy = gr.Textbox(label="Input")
            data_file = gr.UploadButton(
            label="πŸ“ Upload a txt file",
            file_count="single", 
            file_types=[".txt"])
        with gr.Row():
            gr.Examples(
                examples=get_example(),
                inputs=[data_npy],
                fn=solver_plot1,
                outputs=[actual_map, solution_map, actual_ac, solution_ac],
            )
        with gr.Row():
            boost = gr.Checkbox(label="Turbo Boost (accelerate solution generation with fewer SWAP steps)", value=False)
            btn2 = gr.Button(value="πŸš€ Generate")
        with gr.Row():
            actual_map = gr.Plot(label='Initial Solution')
            solution_map = gr.Plot(label='Final Solution')
        with gr.Row():
            actual_ac = gr.Textbox(label='Initial Access Cost')
            solution_ac = gr.Textbox(label='Final Access Cost')
    data_file.upload(fn=load_npy_file, inputs=[data_file], outputs=[data_npy])
    btn2.click(fn=solver_plot, inputs=[data_npy, boost], outputs=[actual_map, solution_map, actual_ac, solution_ac])
    
if __name__ == "__main__":
    demo.launch()