-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel_latest.py
1705 lines (1420 loc) · 78.9 KB
/
model_latest.py
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import gurobipy as gp
from gurobipy import GRB
from itertools import product
import os
import pandas as pd
import numpy as np
from docplex.cp.model import *
import copy
from map_utils import map_back_allocate, map_back_assign
import matplotlib.pyplot as plt
amenity_weights_dict = { "grocery": [3],
"restaurants": [.75, .45, .25, .25, .225, .225, .225, .225, .2, .2],
"shopping": [.5, .45, .4, .35, .3],
"coffee": [1.25, .75],
"banks": [1],
"parks": [1], "schools": [1], "books": [1], "entertainment": [1]}
choice_weights_raw = np.array([.75, .45, .25, .25, .225, .225, .225, .225, .2, .2]) # for restaurant
restaurant_sum = np.sum(choice_weights_raw)
choice_weights = choice_weights_raw / restaurant_sum # for restaurant
L_a=[0,400,1800,2400,5000000]
L_f_a=[100,95,10,0,0]
weights_array = np.array([3,restaurant_sum,1]) / (restaurant_sum+3+1) # grocery, restaurant, school (temp)
weights_array_multi = np.array([3, .75, .45, .25, .25, .225, .225, .225, .225, .2, .2, 1]) / (restaurant_sum+3+1)
w_choice_multi_amenity = choice_weights_raw / (restaurant_sum+3+1)
time_limit=5*60*60 # 5h time limit
def opt_single(df_from,df_to,amenity_df, SP_matrix,k,threads,results_sava_path,bp, focus,EPS=0.5):
'''single amenity case, no depth of choice'''
if len(df_from)>0:
df_from = df_from[['geometry', 'node_ids']]
if len(df_to)>0:
df_to = df_to[['geometry', 'node_ids']]
m = gp.Model('max_walk_score')
# grouping
groups_to=df_to.groupby('node_ids').groups # keys are node id, values are indices
group_values_to=list(groups_to.values())
num_allocation = len(group_values_to)
capacity = [len(item) for item in group_values_to]
groups_from = df_from.groupby('node_ids').groups
group_values_from = list(groups_from.values())
num_residents = len(group_values_from)
num_cur = len(amenity_df)
cartesian_prod_assign = list(product(range(num_residents), range(num_allocation + num_cur))) # a list of tuples
cartesian_prod_allocate = list(product(range(num_residents), range(num_allocation)))
# retrieve distances
d = {(i, j): SP_matrix[df_from.iloc[group_values_from[i][0]]["node_ids"], df_to.iloc[group_values_to[j][0]]["node_ids"]] for i, j in cartesian_prod_allocate}
for i in range(num_residents):
for l in range(num_cur):
cur_id = num_allocation+l
d[(i, cur_id)] = SP_matrix[df_from.iloc[group_values_from[i][0]]["node_ids"], amenity_df.iloc[l]["node_ids"]]
# Variables
x = m.addVars(cartesian_prod_assign, vtype=GRB.BINARY, name='assign')
y = m.addVars(num_allocation, vtype=GRB.BINARY, name='activate')
a = m.addVars(num_residents, vtype=GRB.CONTINUOUS, name='dist')
f = m.addVars(num_residents, vtype=GRB.CONTINUOUS, ub=100,name='score')
if bp:
print("branch priority set")
m.update()
# branching priority
# if BranchPriority:
for j in range(num_allocation):
y[j].setAttr("BranchPriority", 100)
m.update()
# for (n,m) in cartesian_prod_assign:
# x[(n,m)].setAttr("BranchPriority",0)
# Constraints
## WalkScore
m.addConstrs((a[n] ==
(gp.quicksum(d[(n, m)] * x[(n, m)] for m in range(num_allocation + num_cur)))) for n in
range(num_residents))
for n in range(num_residents):
m.addGenConstrPWL(a[n], f[n], L_a, L_f_a)
## assgined nodes satisfy demand
m.addConstrs(
(gp.quicksum(x[(n, m)] for m in range(num_allocation + num_cur)) == 1 for n in range(num_residents)),
name='Demand')
## resource constraint
m.addConstr(gp.quicksum(y[m] for m in range(num_allocation)) <= k, name='resource')
## activation
m.addConstrs((x[(n, m)] <= y[m] for n, m in cartesian_prod_allocate), name='setup')
m.addConstrs(y[j] <= capacity[j] for j in range(num_allocation))
# objective
m.Params.Threads = threads
m.setObjective(gp.quicksum(f[n] for n in range(num_residents))/num_residents, GRB.MAXIMIZE)
m.setParam("LogFile", results_sava_path)
m.Params.TimeLimit = time_limit
m.Params.MIPFocus = focus
m.Params.NodefileStart = 0.5
m.optimize()
assignments = [(i, j) for (i, j) in x.keys() if (x[i, j].x > EPS)]
allocations = [j for j in y.keys() if (y[j].x > EPS)]
# save allocation solutions
allocate_var_id = allocations
allocate_row_id = []
allocate_node_id = []
for j in allocate_var_id:
for l in range(int(np.round(y[j].x))):
allocate_row_id.append(group_values_to[j][l])
allocate_node_id.append(df_to.iloc[group_values_to[j][l]]["node_ids"])
allocated_D = {
"allocate_var_id": allocate_var_id,
"allocate_node_id": allocate_node_id,
"allocate_row_id": allocate_row_id
}
assign_from_var_id = [i for (i, j) in assignments]
assign_to_var_id = [j for (i, j) in assignments]
assign_from_node_id = [df_from.iloc[group_values_from[i][0]]["node_ids"] for (i, j) in assignments]
assign_to_node_id = []
assign_type = []
dist=[]
for (i,j) in assignments:
if j < num_allocation:
assign_to_node_id.append(df_to.iloc[group_values_to[j][0]]["node_ids"])
assign_type.append('allocated')
dist.append(SP_matrix[df_from.iloc[group_values_from[i][0]]["node_ids"], df_to.iloc[group_values_to[j][0]]["node_ids"]])
else:
assign_to_node_id.append(amenity_df.iloc[j-num_allocation]["node_ids"])
assign_type.append('existing')
dist.append(SP_matrix[df_from.iloc[group_values_from[i][0]]["node_ids"],amenity_df.iloc[j-num_allocation]["node_ids"]])
assigned_D = {
"assign_from_var_id": assign_from_var_id,
"assign_to_var_id": assign_to_var_id,
"assign_from_node_id": assign_from_node_id,
"assign_to_node_id": assign_to_node_id,
"assign_type": assign_type,
"dist": dist}
obj = m.getObjective()
obj_value = obj.getValue()
dist_obj = np.mean(dist)
return obj_value, dist_obj, m.Runtime, m, allocated_D, assigned_D, num_residents, num_allocation, num_cur, m.status
def opt_single_depth(df_from,df_to,amenity_df, SP_matrix,k,threads,results_sava_path,bp, focus,EPS=0.5):
'''single amenity case, with consideration of depth of choice. For amenity=restaurant specifically'''
if len(df_from)>0:
df_from = df_from[['geometry', 'node_ids']]
if len(df_to)>0:
df_to = df_to[['geometry', 'node_ids']]
m = gp.Model('max_walk_score')
# grouping
groups_to=df_to.groupby('node_ids').groups # keys are node id, values are indices
group_values_to=list(groups_to.values())
num_allocation = len(group_values_to)
capacity = [len(item) for item in group_values_to]
groups_from = df_from.groupby('node_ids').groups
group_values_from = list(groups_from.values())
num_residents = len(group_values_from)
num_cur = len(amenity_df)
tot_choices = min(k + num_cur, len(choice_weights))
no_choices = list(range(tot_choices, len(choice_weights)))
cartesian_prod_assign = list(product(range(num_residents), range(num_allocation + num_cur), range(tot_choices))) # last index is for depth of choice
cartesian_prod_allocate = list(product(range(num_residents), range(num_allocation)))
# retrieve distances
d = {(i, j): SP_matrix[df_from.iloc[group_values_from[i][0]]["node_ids"], df_to.iloc[group_values_to[j][0]]["node_ids"]] for i, j in cartesian_prod_allocate}
for i in range(num_residents):
for l in range(num_cur):
cur_id = num_allocation+l
d[(i, cur_id)] = SP_matrix[df_from.iloc[group_values_from[i][0]]["node_ids"], amenity_df.iloc[l]["node_ids"]]
# Variables
x = m.addVars(cartesian_prod_assign, vtype=GRB.BINARY, name='assign')
y = m.addVars(num_allocation, vtype=GRB.INTEGER, name='activate')
a = m.addVars(num_residents, vtype=GRB.CONTINUOUS, name='dist')
f = m.addVars(num_residents, vtype=GRB.CONTINUOUS, ub=100,name='score')
if bp:
print("branch priority set")
m.update()
# branching priority
# if BranchPriority:
for j in range(num_allocation):
y[j].setAttr("BranchPriority", 100)
m.update()
# for (n,m) in cartesian_prod_assign:
# x[(n,m)].setAttr("BranchPriority",0)
# Constraints
## WalkScore
no_choice_sum =sum([choice_weights[c]*L_a[-2] for c in no_choices])
m.addConstrs((
a[n] ==
(gp.quicksum(choice_weights[c]*(gp.quicksum(d[(n, m)] * x[(n, m, c)] for m in range(num_allocation + num_cur))) for c in range(tot_choices)) + no_choice_sum)
)
for n in range(num_residents))
for n in range(num_residents):
m.addGenConstrPWL(a[n], f[n], L_a, L_f_a)
## assign choices
m.addConstrs((
(gp.quicksum(x[(n, m, c)] for m in range(num_allocation + num_cur)) == 1) for c in range(tot_choices) for n in range(num_residents)),
name='choices')
## resource constraint
m.addConstr(gp.quicksum(y[m] for m in range(num_allocation)) <= k, name='resource')
## activation
m.addConstrs((x[(n, m, c)] <= y[m] for (n, m, c) in list(product(range(num_residents), range(num_allocation), range(tot_choices)))), name='setup')
## node capacity
m.addConstrs(y[j] <= capacity[j] for j in range(num_allocation))
# choices can not be the same place
## newly allocated
m.addConstrs(((gp.quicksum(x[(n, m, c)] for c in range(tot_choices)) <= y[m]) for m in range(num_allocation) for n in range(num_residents)), name='choices')
## currently existing
m.addConstrs(((gp.quicksum(x[(n, m, c)] for c in range(tot_choices)) <= 1) for m in range(num_allocation,num_allocation+num_cur) for n in range(num_residents)), name='choices')
# # symmetry
# ## choice id=2 and 3
# if tot_choices >= 4:
# m.addConstrs(gp.quicksum(d[(n, m)] * x[(n, m, 2)] for m in range(num_allocation + num_cur)) <= gp.quicksum(d[(n, m)] * x[(n, m, 3)] for m in range(num_allocation + num_cur))
# for n in range(num_residents))
# ## choice id = 8 and 9
# if tot_choices >= 10:
# m.addConstrs(gp.quicksum(d[(n, m)] * x[(n, m, 8)] for m in range(num_allocation + num_cur)) <= gp.quicksum(
# d[(n, m)] * x[(n, m, 9)] for m in range(num_allocation + num_cur))
# for n in range(num_residents))
# ## choice id=4,5,6,7
# for id in [4,5,6]:
# if tot_choices >= (id+2):
# m.addConstrs(gp.quicksum(d[(n, m)] * x[(n, m, id)] for m in range(num_allocation + num_cur)) <= gp.quicksum(
# d[(n, m)] * x[(n, m, id+1)] for m in range(num_allocation + num_cur))
# for n in range(num_residents))
# objective
m.Params.Threads = threads
m.setObjective(gp.quicksum(f[n] for n in range(num_residents))/num_residents, GRB.MAXIMIZE)
m.setParam("LogFile", results_sava_path)
m.Params.TimeLimit = time_limit
m.Params.MIPFocus = focus
m.Params.NodefileStart = 0.5
m.optimize()
allocations = [j for j in y.keys() if (y[j].x > EPS)]
# save allocation solutions
allocate_var_id = []
allocate_var_id_ = allocations
allocate_row_id = []
allocate_node_id = []
for j in allocate_var_id_:
for l in range(int(np.round(y[j].x))):
allocate_var_id.append(j)
allocate_row_id.append(group_values_to[j][l])
allocate_node_id.append(df_to.iloc[group_values_to[j][l]]["node_ids"])
allocated_D = {
"allocate_var_id": allocate_var_id,
"allocate_node_id": allocate_node_id,
"allocate_row_id": allocate_row_id
}
assigned_D={}
for choice in range(tot_choices):
all = [(i, j, c) for (i, j, c) in x.keys() if (x[(i, j, c)].x > EPS)]
assignments = [(i,j,c) for (i,j,c) in all if (c==choice)]
assign_from_var_id = [i for (i, j, c) in assignments]
assign_to_var_id = [j for (i, j, c) in assignments]
assign_from_node_id = [df_from.iloc[group_values_from[i][0]]["node_ids"] for (i, j, c) in assignments]
assign_to_node_id = []
assign_type = []
dist=[]
for (i, j, c) in assignments:
if j < num_allocation:
assign_to_node_id.append(df_to.iloc[group_values_to[j][0]]["node_ids"])
assign_type.append('allocated')
dist.append(SP_matrix[df_from.iloc[group_values_from[i][0]]["node_ids"], df_to.iloc[group_values_to[j][0]]["node_ids"]])
else:
assign_to_node_id.append(amenity_df.iloc[j-num_allocation]["node_ids"])
assign_type.append('existing')
dist.append(SP_matrix[df_from.iloc[group_values_from[i][0]]["node_ids"],amenity_df.iloc[j-num_allocation]["node_ids"]])
assigned_D[str(choice)+"_assign_from_var_id"]=assign_from_var_id
assigned_D[str(choice)+"_assign_to_var_id"]=assign_to_var_id
assigned_D[str(choice)+"_assign_from_node_id"]=assign_from_node_id
assigned_D[str(choice)+"_assign_to_node_id"]=assign_to_node_id
assigned_D[str(choice)+"_assign_type"]=assign_type
assigned_D[str(choice)+"_dist"]=dist
obj = m.getObjective()
obj_value = obj.getValue()
dist_obj = [np.mean(assigned_D[str(c)+"_dist"]) if (str(c)+"_dist") in assigned_D.keys() else 0 for c in range(len(choice_weights))]
return obj_value, dist_obj, m.Runtime, m, allocated_D, assigned_D, num_residents, num_allocation, num_cur, m.status
def opt_multiple(df_from,df_to,grocery_df, restaurant_df, school_df, SP_matrix, k_array, threads,results_sava_path,bp, focus,EPS=0.5):
'''multiple amenity case, no depth of choice'''
if len(df_from)>0:
df_from = df_from[['geometry', 'node_ids']]
if len(df_to)>0:
df_to = df_to[['geometry', 'node_ids']]
m = gp.Model('max_walk_score')
# grouping
groups_to=df_to.groupby('node_ids').groups # keys are node id, values are indices
group_values_to=list(groups_to.values())
num_allocation = len(group_values_to)
capacity = [len(item) for item in group_values_to]
groups_from = df_from.groupby('node_ids').groups
group_values_from = list(groups_from.values())
num_residents = len(group_values_from)
num_cur_grocery = len(grocery_df)
num_cur_restaurant = len(restaurant_df)
num_cur_school = len(school_df)
cur_index=num_allocation
range_grocery_dest_list = list(range(num_allocation)) + list(range(cur_index, cur_index + num_cur_grocery))
cur_index+=num_cur_grocery
range_restaurant_dest_list = list(range(num_allocation)) + list(range(cur_index, cur_index + num_cur_restaurant))
cur_index+=num_cur_restaurant
range_school_dest_list = list(range(num_allocation)) + list(range(cur_index, cur_index + num_cur_school))
cartesian_prod_assign_grocery = list(product(range(num_residents), range_grocery_dest_list, [0]))
cartesian_prod_assign_restaurant = list(product(range(num_residents),range_restaurant_dest_list, [1]))
cartesian_prod_assign_school = list(product(range(num_residents), range_school_dest_list, [2]))
cartesian_prod_allocate = list(product(range(num_residents), list(range(num_allocation)), [0,1,2]))
# retrieve distances
d = {(i, j): SP_matrix[df_from.iloc[group_values_from[i][0]]["node_ids"], df_to.iloc[group_values_to[j][0]]["node_ids"]] for i, j in list(product(range(num_residents), range(num_allocation)))}
for i in range(num_residents):
start_id = num_allocation
for amenity_df in [grocery_df, restaurant_df, school_df]:
for inst_row in range(len(amenity_df)):
cur_id = start_id + inst_row
d[(i, cur_id)] = SP_matrix[df_from.iloc[group_values_from[i][0]]["node_ids"], amenity_df.iloc[inst_row]["node_ids"]]
start_id += len(amenity_df)
x = m.addVars(cartesian_prod_assign_grocery + cartesian_prod_assign_restaurant + cartesian_prod_assign_school, vtype=GRB.BINARY, name='assign')
y = m.addVars(list(product(range(num_allocation), range(len(k_array)))), vtype=GRB.BINARY, name='activate')
l = m.addVars(num_residents, vtype=GRB.CONTINUOUS, name='dist')
f = m.addVars(num_residents, vtype=GRB.CONTINUOUS, ub=100, name='score')
# branching priority
if bp:
for t in list(product(range(num_allocation), range(len(k_array)))):
y[t].setAttr("BranchPriority", 100)
# for (n,m) in cartesian_prod_assign:
# x[(n,m)].setAttr("BranchPriority",4)
# Constraints
## weighted distance
m.addConstrs(l[i] == (
(weights_array[0] * gp.quicksum(x[(i, j, 0)] * d[(i, j)] for j in range_grocery_dest_list))
+ (weights_array[1] * gp.quicksum(x[(i, j, 1)] * d[(i, j)] for j in range_restaurant_dest_list))
+ (weights_array[2] * gp.quicksum(x[(i, j, 2)] * d[(i, j)] for j in range_school_dest_list))
)
for i in range(num_residents))
# PWL score
for i in range(num_residents):
m.addGenConstrPWL(l[i], f[i], L_a, L_f_a)
## assgined to one instance of amenity
m.addConstrs((gp.quicksum(x[(i, j, 0)] for j in range_grocery_dest_list) == 1 for i in range(num_residents)), name='grocery demand')
m.addConstrs((gp.quicksum(x[(i, j, 1)] for j in range_restaurant_dest_list) == 1 for i in range(num_residents)), name='restaurant demand')
m.addConstrs((gp.quicksum(x[(i, j, 2)] for j in range_school_dest_list) == 1 for i in range(num_residents)), name='school demand')
## resource constraint
m.addConstrs(((gp.quicksum(y[(j,a)] for a in range(len(weights_array))) <= capacity[j]) for j in range(num_allocation)), name='capacity')
# activation
m.addConstrs((x[(i,j,a)] <= y[(j,a)] for (i, j ,a) in cartesian_prod_allocate), name='activation')
# resource constraint
m.addConstrs(((gp.quicksum(y[(j, a)] for j in range(num_allocation)) <= k_array[a]) for a in range(len(k_array))), name='resource')
# objective
m.Params.Threads = threads
m.setObjective(gp.quicksum(f[n] for n in range(num_residents))/num_residents, GRB.MAXIMIZE)
m.setParam("LogFile", results_sava_path)
m.Params.TimeLimit = time_limit
m.Params.MIPFocus = focus
m.Params.NodefileStart = 0.5
m.optimize()
allocations = [(j, a) for (j, a) in y.keys() if (y[(j, a)].x) > EPS]
# save allocation solutions
allocate_var_id_grocery = [(j, a) for (j, a) in allocations if a==0]
allocate_row_id_grocery = []
allocate_node_id_grocery = []
for (j, a) in allocate_var_id_grocery:
for l in range(int(y[(j, a)].x)):
allocate_row_id_grocery.append(group_values_to[j][l])
allocate_node_id_grocery.append(df_to.iloc[group_values_to[j][l]]["node_ids"])
allocate_var_id_restaurant = [(j, a) for (j, a) in allocations if a==1]
allocate_row_id_restaurant = []
allocate_node_id_restaurant = []
for (j, a) in allocate_var_id_restaurant:
for l in range(int(y[(j, a)].x)):
allocate_row_id_restaurant.append(group_values_to[j][l])
allocate_node_id_restaurant.append(df_to.iloc[group_values_to[j][l]]["node_ids"])
allocate_var_id_school = [(j, a) for (j, a) in allocations if a==2]
allocate_row_id_school = []
allocate_node_id_school = []
for (j, a) in allocate_var_id_school:
for l in range(int(y[(j, a)].x)):
allocate_row_id_school.append(group_values_to[j][l])
allocate_node_id_school.append(df_to.iloc[group_values_to[j][l]]["node_ids"])
allocated_D = {
"allocate_var_id_grocery": allocate_var_id_grocery,
"allocate_node_id_grocery": allocate_node_id_grocery,
"allocate_row_id_grocery": allocate_row_id_grocery,
"allocate_var_id_restaurant": allocate_var_id_restaurant,
"allocate_node_id_restaurant": allocate_node_id_restaurant,
"allocate_row_id_restaurant": allocate_row_id_restaurant,
"allocate_var_id_school": allocate_var_id_school,
"allocate_row_id_school": allocate_row_id_school,
"allocate_node_id_school": allocate_node_id_school
}
assignments = [(i, j, a) for (i, j, a) in x.keys() if (x[(i, j, a)].x > EPS)]
assign_from_var_id_grocery = [i for (i, j, a) in assignments if a==0]
assign_to_var_id_grocery = [j for (i, j, a) in assignments if a==0]
assign_from_node_id_grocery = [df_from.iloc[group_values_from[i][0]]["node_ids"] for (i, j, a) in assignments if a==0]
assign_to_node_id_grocery = []
assign_type_grocery = []
dist_grocery =[]
assign_from_var_id_restaurant = [i for (i, j, a) in assignments if a == 0]
assign_to_var_id_restaurant = [j for (i, j, a) in assignments if a == 0]
assign_from_node_id_restaurant = [df_from.iloc[group_values_from[i][0]]["node_ids"] for (i, j, a) in assignments if a == 1]
assign_to_node_id_restaurant = []
assign_type_restaurant = []
dist_restaurant = []
assign_from_var_id_school = [i for (i, j, a) in assignments if a == 0]
assign_to_var_id_school = [j for (i, j, a) in assignments if a == 0]
assign_from_node_id_school = [df_from.iloc[group_values_from[i][0]]["node_ids"] for (i, j, a) in assignments if a == 2]
assign_to_node_id_school = []
assign_type_school = []
dist_school = []
for (i, j, a) in assignments:
if a==0:
if j < num_allocation:
assign_to_node_id_grocery.append(df_to.iloc[group_values_to[j][0]]["node_ids"])
assign_type_grocery.append('allocated')
dist_grocery.append(SP_matrix[df_from.iloc[group_values_from[i][0]]["node_ids"], df_to.iloc[group_values_to[j][0]]["node_ids"]])
else:
assign_to_node_id_grocery.append(grocery_df.iloc[j-num_allocation]["node_ids"])
assign_type_grocery.append('existing')
dist_grocery.append(SP_matrix[df_from.iloc[group_values_from[i][0]]["node_ids"],grocery_df.iloc[j-num_allocation]["node_ids"]])
elif a==1:
if j < num_allocation:
assign_to_node_id_restaurant.append(df_to.iloc[group_values_to[j][0]]["node_ids"])
assign_type_restaurant.append('allocated')
dist_restaurant.append(SP_matrix[df_from.iloc[group_values_from[i][0]]["node_ids"], df_to.iloc[group_values_to[j][0]]["node_ids"]])
else:
assign_to_node_id_restaurant.append(restaurant_df.iloc[j-num_allocation-num_cur_grocery]["node_ids"])
assign_type_restaurant.append('existing')
dist_restaurant.append(SP_matrix[df_from.iloc[group_values_from[i][0]]["node_ids"],restaurant_df.iloc[j-num_allocation-num_cur_grocery]["node_ids"]])
elif a==2:
if j < num_allocation:
assign_to_node_id_school.append(df_to.iloc[group_values_to[j][0]]["node_ids"])
assign_type_school.append('allocated')
dist_school.append(SP_matrix[df_from.iloc[group_values_from[i][0]]["node_ids"], df_to.iloc[group_values_to[j][0]]["node_ids"]])
else:
assign_to_node_id_school.append(school_df.iloc[j-num_allocation-num_cur_restaurant-num_cur_grocery]["node_ids"])
assign_type_school.append('existing')
dist_school.append(SP_matrix[df_from.iloc[group_values_from[i][0]]["node_ids"],school_df.iloc[j-num_allocation-num_cur_restaurant-num_cur_grocery]["node_ids"]])
assigned_D = {
"assign_from_var_id_grocery": assign_from_var_id_grocery,
"assign_to_var_id_grocery": assign_to_var_id_grocery,
"assign_from_node_id_grocery": assign_from_node_id_grocery,
"assign_to_node_id_grocery": assign_to_node_id_grocery,
"assign_type_grocery": assign_type_grocery,
"dist_grocery": dist_grocery,
"assign_from_var_id_restaurant": assign_from_var_id_restaurant,
"assign_to_var_id_restaurant": assign_to_var_id_restaurant,
"assign_from_node_id_restaurant": assign_from_node_id_restaurant,
"assign_to_node_id_restaurant": assign_to_node_id_restaurant,
"assign_type_restaurant": assign_type_restaurant,
"dist_restaurant": dist_restaurant,
"assign_from_var_id_school": assign_from_var_id_school,
"assign_to_var_id_school": assign_to_var_id_school,
"assign_from_node_id_school": assign_from_node_id_school,
"assign_to_node_id_school": assign_to_node_id_school,
"assign_type_school": assign_type_school,
"dist_school": dist_school
}
obj = m.getObjective()
obj_value = obj.getValue()
return obj_value, [np.mean(dist_grocery), np.mean(dist_restaurant), np.mean(dist_school)], m.Runtime, m, allocated_D, assigned_D, num_residents, num_allocation, [num_cur_grocery, num_cur_restaurant, num_cur_school], m.status
def opt_multiple_depth(df_from,df_to,grocery_df, restaurant_df, school_df, SP_matrix, k_array, threads, results_sava_path, bp, focus,EPS=0.5):
'''multiple amenity case, with depth of choice'''
if len(df_from)>0:
df_from = df_from[['geometry', 'node_ids']]
if len(df_to)>0:
df_to = df_to[['geometry', 'node_ids']]
m = gp.Model('max_walk_score')
# grouping
groups_to=df_to.groupby('node_ids').groups # keys are node id, values are indices
group_values_to=list(groups_to.values())
num_allocation = len(group_values_to)
capacity = [len(item) for item in group_values_to]
groups_from = df_from.groupby('node_ids').groups
group_values_from = list(groups_from.values())
num_residents = len(group_values_from)
num_cur_grocery = len(grocery_df)
num_cur_restaurant = len(restaurant_df)
num_cur_school = len(school_df)
cur_index=num_allocation
range_grocery_existing = list(range(cur_index, cur_index + num_cur_grocery))
range_grocery_dest_list = list(range(num_allocation)) + range_grocery_existing
cur_index+=num_cur_grocery
range_restaurant_existing = list(range(cur_index, cur_index + num_cur_restaurant))
range_restaurant_dest_list = list(range(num_allocation)) + range_restaurant_existing
cur_index+=num_cur_restaurant
range_school_existing = list(range(cur_index, cur_index + num_cur_school))
range_school_dest_list = list(range(num_allocation)) + range_school_existing
tot_choices = min(k_array[1] + num_cur_restaurant, len(w_choice_multi_amenity))
no_choices = list(range(tot_choices, len(w_choice_multi_amenity)))
cartesian_prod_assign_grocery = list(product(range(num_residents), range_grocery_dest_list, [0]))
cartesian_prod_assign_restaurant = list(product(range(num_residents),range_restaurant_dest_list, [1], range(tot_choices)))
cartesian_prod_assign_school = list(product(range(num_residents), range_school_dest_list, [2]))
#cartesian_prod_allocate = list(product(range(num_residents), list(range(num_allocation)), [0,1,2]))
# retrieve distances
d = {(i, j): SP_matrix[df_from.iloc[group_values_from[i][0]]["node_ids"], df_to.iloc[group_values_to[j][0]]["node_ids"]] for i, j in list(product(range(num_residents), range(num_allocation)))}
for i in range(num_residents):
start_id = num_allocation
for amenity_df in [grocery_df, restaurant_df, school_df]:
for inst_row in range(len(amenity_df)):
cur_id = start_id + inst_row
d[(i, cur_id)] = SP_matrix[df_from.iloc[group_values_from[i][0]]["node_ids"], amenity_df.iloc[inst_row]["node_ids"]]
start_id += len(amenity_df)
x = m.addVars(cartesian_prod_assign_grocery + cartesian_prod_assign_school, vtype=GRB.BINARY, name='assign')
x_choice = m.addVars(cartesian_prod_assign_restaurant, vtype=GRB.BINARY, name='assign')
y = m.addVars(list(product(range(num_allocation), range(len(k_array)))), vtype=GRB.INTEGER, name='activate')
l = m.addVars(num_residents, vtype=GRB.CONTINUOUS, name='dist')
f = m.addVars(num_residents, vtype=GRB.CONTINUOUS, ub=100, name='score')
# branching priority
if bp:
for t in list(product(range(num_allocation), range(len(k_array)))):
y[t].setAttr("BranchPriority", 100)
# for (n,m) in cartesian_prod_assign:
# x[(n,m)].setAttr("BranchPriority",4)
# Constraints
## weighted distance
no_choice_sum = sum([w_choice_multi_amenity[c] * L_a[-2] for c in no_choices])
m.addConstrs(l[i] == (
(weights_array[0] * gp.quicksum(x[(i, j, 0)] * d[(i, j)] for j in range_grocery_dest_list))
+ (gp.quicksum(w_choice_multi_amenity[c] * (gp.quicksum(x_choice[(i, j, 1, c)] * d[(i, j)] for j in range_restaurant_dest_list)) for c in range(tot_choices)) + no_choice_sum)
+ (weights_array[2] * gp.quicksum(x[(i, j, 2)] * d[(i, j)] for j in range_school_dest_list))
)
for i in range(num_residents))
# PWL score
for i in range(num_residents):
m.addGenConstrPWL(l[i], f[i], L_a, L_f_a)
## assgined to one instance of amenity
m.addConstrs((gp.quicksum(x[(i, j, 0)] for j in range_grocery_dest_list) == 1 for i in range(num_residents)), name='grocery demand')
#m.addConstrs((gp.quicksum(x[(i, j, 1)] for j in range_restaurant_dest_list) == 1 for i in range(num_residents)), name='restaurant demand')
m.addConstrs((gp.quicksum(x[(i, j, 2)] for j in range_school_dest_list) == 1 for i in range(num_residents)), name='school demand')
## assign choices
m.addConstrs(((gp.quicksum(x_choice[(i, j, 1, c)] for j in range_restaurant_dest_list) == 1) for c in range(tot_choices) for i in range(num_residents)), name='choices')
## resource constraint
m.addConstrs(((gp.quicksum(y[(j,a)] for a in range(len(weights_array))) <= capacity[j]) for j in range(num_allocation)), name='capacity')
# activation
m.addConstrs((x[(i,j,a)] <= y[(j,a)] for (i, j ,a) in list(product(range(num_residents), list(range(num_allocation)), [0,2]))), name='activation1')
m.addConstrs((x_choice[(i, j, a, c)] <= y[(j, a)] for (i, j, a, c) in list(product(range(num_residents), list(range(num_allocation)),[1], range(tot_choices)))), name='activation2')
# resource constraint
m.addConstrs(((gp.quicksum(y[(j, a)] for j in range(num_allocation)) <= k_array[a]) for a in range(len(k_array))), name='resource')
# choices can not be the same place
## newly allocated
m.addConstrs(((gp.quicksum(x_choice[(i, j, 1, c)] for c in range(tot_choices)) <= y[(j, 1)]) for j in range(num_allocation) for i in range(num_residents)), name='choices')
## currently existing
m.addConstrs(((gp.quicksum(x_choice[(i, j, 1, c)] for c in range(tot_choices)) <= 1) for j in range_restaurant_existing for i in range(num_residents)), name='choices')
# objective
m.Params.Threads = threads
m.setObjective(gp.quicksum(f[n] for n in range(num_residents))/num_residents, GRB.MAXIMIZE)
m.setParam("LogFile", results_sava_path)
m.Params.TimeLimit = time_limit
m.Params.MIPFocus = focus
m.Params.NodefileStart = 0.5
m.optimize()
allocations = [(j, a) for (j, a) in y.keys() if (y[(j, a)].x) > EPS]
# save allocation solutions
# grocery
allocate_var_id_grocery_ = [(j, a) for (j, a) in allocations if a==0]
allocate_var_id_grocery = []
allocate_row_id_grocery = []
allocate_node_id_grocery = []
for (j, a) in allocate_var_id_grocery_:
for l in range(int(y[(j, a)].x)):
allocate_var_id_grocery.append(j)
allocate_row_id_grocery.append(group_values_to[j][l])
allocate_node_id_grocery.append(df_to.iloc[group_values_to[j][l]]["node_ids"])
# restaurant
allocate_var_id_restaurant_ = [(j, a) for (j, a) in allocations if a==1]
allocate_var_id_restaurant = []
allocate_row_id_restaurant = []
allocate_node_id_restaurant = []
for (j, a) in allocate_var_id_restaurant_:
for l in range(int(y[(j, a)].x)):
allocate_var_id_restaurant.append(j)
allocate_row_id_restaurant.append(group_values_to[j][l])
allocate_node_id_restaurant.append(df_to.iloc[group_values_to[j][l]]["node_ids"])
# school
allocate_var_id_school_ = [(j, a) for (j, a) in allocations if a==2]
allocate_var_id_school = []
allocate_row_id_school = []
allocate_node_id_school = []
for (j, a) in allocate_var_id_school_:
for l in range(int(y[(j, a)].x)):
allocate_var_id_school.append(j)
allocate_row_id_school.append(group_values_to[j][l])
allocate_node_id_school.append(df_to.iloc[group_values_to[j][l]]["node_ids"])
allocated_D = {
"allocate_var_id_grocery": allocate_var_id_grocery,
"allocate_node_id_grocery": allocate_node_id_grocery,
"allocate_row_id_grocery": allocate_row_id_grocery,
"allocate_var_id_restaurant": allocate_var_id_restaurant,
"allocate_node_id_restaurant": allocate_node_id_restaurant,
"allocate_row_id_restaurant": allocate_row_id_restaurant,
"allocate_var_id_school": allocate_var_id_school,
"allocate_row_id_school": allocate_row_id_school,
"allocate_node_id_school": allocate_node_id_school
}
# assignments
assignments = [(i, j, a) for (i, j, a) in x.keys() if (x[(i, j, a)].x > EPS)]
choice_assignments = [(i,j,a,c) for (i,j,a,c) in x_choice.keys() if (x_choice[(i,j,a,c)].x > EPS)]
# grocery
assign_from_var_id_grocery = [i for (i, j, a) in assignments if a==0]
assign_to_var_id_grocery = [j for (i, j, a) in assignments if a==0]
assign_from_node_id_grocery = [df_from.iloc[group_values_from[i][0]]["node_ids"] for (i, j, a) in assignments if a==0]
assign_to_node_id_grocery = []
assign_type_grocery = []
dist_grocery =[]
# school
assign_from_var_id_school = [i for (i, j, a) in assignments if a == 0]
assign_to_var_id_school = [j for (i, j, a) in assignments if a == 0]
assign_from_node_id_school = [df_from.iloc[group_values_from[i][0]]["node_ids"] for (i, j, a) in assignments if a == 2]
assign_to_node_id_school = []
assign_type_school = []
dist_school = []
for (i, j, a) in assignments:
if a==0:
if j < num_allocation:
assign_to_node_id_grocery.append(df_to.iloc[group_values_to[j][0]]["node_ids"])
assign_type_grocery.append('allocated')
dist_grocery.append(SP_matrix[df_from.iloc[group_values_from[i][0]]["node_ids"], df_to.iloc[group_values_to[j][0]]["node_ids"]])
else:
assign_to_node_id_grocery.append(grocery_df.iloc[j-num_allocation]["node_ids"])
assign_type_grocery.append('existing')
dist_grocery.append(SP_matrix[df_from.iloc[group_values_from[i][0]]["node_ids"],grocery_df.iloc[j-num_allocation]["node_ids"]])
elif a==2:
if j < num_allocation:
assign_to_node_id_school.append(df_to.iloc[group_values_to[j][0]]["node_ids"])
assign_type_school.append('allocated')
dist_school.append(SP_matrix[df_from.iloc[group_values_from[i][0]]["node_ids"], df_to.iloc[group_values_to[j][0]]["node_ids"]])
else:
assign_to_node_id_school.append(school_df.iloc[j-num_allocation-num_cur_restaurant-num_cur_grocery]["node_ids"])
assign_type_school.append('existing')
dist_school.append(SP_matrix[df_from.iloc[group_values_from[i][0]]["node_ids"],school_df.iloc[j-num_allocation-num_cur_restaurant-num_cur_grocery]["node_ids"]])
assigned_D = {
"assign_from_var_id_grocery": assign_from_var_id_grocery,
"assign_to_var_id_grocery": assign_to_var_id_grocery,
"assign_from_node_id_grocery": assign_from_node_id_grocery,
"assign_to_node_id_grocery": assign_to_node_id_grocery,
"assign_type_grocery": assign_type_grocery,
"dist_grocery": dist_grocery,
"assign_from_var_id_school": assign_from_var_id_school,
"assign_to_var_id_school": assign_to_var_id_school,
"assign_from_node_id_school": assign_from_node_id_school,
"assign_to_node_id_school": assign_to_node_id_school,
"assign_type_school": assign_type_school,
"dist_school": dist_school
}
# restaurant
for choice in range(tot_choices):
assignments = [(i, j, a, c) for (i, j, a, c) in choice_assignments if (c == choice)]
assign_from_var_id = [i for (i, j, a, c) in assignments]
assign_to_var_id = [j for (i, j, a, c) in assignments]
assign_from_node_id = [df_from.iloc[group_values_from[i][0]]["node_ids"] for (i, j, a, c) in assignments]
assign_to_node_id = []
assign_type = []
dist=[]
for (i, j, a, c) in assignments:
if j < num_allocation:
assign_to_node_id.append(df_to.iloc[group_values_to[j][0]]["node_ids"])
assign_type.append('allocated')
dist.append(SP_matrix[df_from.iloc[group_values_from[i][0]]["node_ids"], df_to.iloc[group_values_to[j][0]]["node_ids"]])
else:
assign_to_node_id.append(restaurant_df.iloc[j-num_allocation-num_cur_grocery]["node_ids"])
assign_type.append('existing')
dist.append(SP_matrix[df_from.iloc[group_values_from[i][0]]["node_ids"],restaurant_df.iloc[j-num_allocation-num_cur_grocery]["node_ids"]])
assigned_D[str(choice)+"_assign_from_var_id_restaurant"]=assign_from_var_id
assigned_D[str(choice)+"_assign_to_var_id_restaurant"]=assign_to_var_id
assigned_D[str(choice)+"_assign_from_node_id_restaurant"]=assign_from_node_id
assigned_D[str(choice)+"_assign_to_node_id_restaurant"]=assign_to_node_id
assigned_D[str(choice)+"_assign_type_restaurant"]=assign_type
assigned_D[str(choice)+"_dist_restaurant"]=dist
obj = m.getObjective()
obj_value = obj.getValue()
restaurant_dist_obj = [np.mean(assigned_D[str(c) + "_dist_restaurant"]) if (str(c) + "_dist_restaurant") in assigned_D.keys() else 0 for c in range(len(choice_weights))]
return obj_value, [np.mean(dist_grocery), restaurant_dist_obj, np.mean(dist_school)], m.Runtime, m, allocated_D, assigned_D, num_residents, num_allocation, [num_cur_grocery, num_cur_restaurant, num_cur_school], m.status
def cur_assignment_single_depth(df_from,amenity_df, SP_matrix,bp, focus,EPS=1.e-6):
''' get assignment for the case with no allocation, considering depth of choice'''
m = gp.Model('cur_assignment')
groups_from = df_from.groupby('node_ids').groups
group_values_from = list(groups_from.values())
num_residents = len(group_values_from)
num_amenity = len(amenity_df)
cartesian_prod = list(product(range(num_residents), range(num_amenity))) # a list of tuples
cartesian_prod_assign = list(product(range(num_residents), range(num_amenity), range(len(choice_weights))))
distances = {(i, j): SP_matrix[df_from.iloc[group_values_from[i][0]]["node_ids"], amenity_df.iloc[j]["node_ids"]] for i, j in cartesian_prod}
x = m.addVars(cartesian_prod_assign, vtype=GRB.BINARY, name='Assign')
## assign choices
tot_choices = min(num_amenity, len(choice_weights))
no_choices = list(range(tot_choices, len(choice_weights)))
m.addConstrs(
(gp.quicksum(x[(n, m, c)] for m in range(num_amenity)) == 1 for c in range(tot_choices) for n in range(num_residents)),
name='choices')
m.addConstrs((x[(n, m, c)] == 0 for c in no_choices for m in range(num_amenity) for n in range(num_residents)),
name='no choices')
# choices can not be the same place
## currently existing
m.addConstrs(((gp.quicksum(x[(n, m, c)] for c in range(tot_choices)) <= 1) for m in range(num_amenity) for n in range(num_residents)), name='choices')
# objective
no_choice_sum =sum([choice_weights[c]*L_a[-2] for c in no_choices])
m.setObjective((gp.quicksum(
(gp.quicksum(
choice_weights[c]*(gp.quicksum(distances[(n, m)] * x[(n, m, c)] for m in range(num_amenity)))
for c in range(tot_choices))+no_choice_sum)
for n in range(num_residents))/num_residents), GRB.MINIMIZE)
m.optimize()
obj = m.getObjective()
obj_value = obj.getValue() # min total dist
assignments = [(i, j, c) for (i, j, c) in x.keys() if (x[i, j, c].x > EPS)]
assigned_D={}
choices_dist = []
for choice in range(tot_choices):
assign_from_var_id = [i for (i, j, c) in assignments if c==choice]
assign_to_var_id = [j for (i, j, c) in assignments if c==choice]
assign_from_node_id = [df_from.iloc[group_values_from[i][0]]["node_ids"] for (i, j, c) in assignments if c==choice]
assign_to_node_id = [amenity_df.iloc[j]["node_ids"] for (i, j, c) in assignments if c==choice]
dist = [SP_matrix[df_from.iloc[group_values_from[i][0]]["node_ids"], amenity_df.iloc[j]["node_ids"]] for (i, j, c) in assignments if c==choice]
choices_dist.append(dist)
assigned_D[str(choice)+"_assign_from_var_id"]=assign_from_var_id
assigned_D[str(choice)+"_assign_to_var_id"]=assign_to_var_id
assigned_D[str(choice)+"_assign_from_node_id"]=assign_from_node_id
assigned_D[str(choice)+"_assign_to_node_id"]=assign_to_node_id
assigned_D[str(choice)+"_dist"]=dist
for choice in range(tot_choices, len(choice_weights)):
choices_dist.append([L_a[-2]]*num_residents)
obj = m.getObjective()
obj_value = obj.getValue()
dist_obj = [np.mean(assigned_D[str(c)+"_dist"]) if (str(c) + "_dist") in assigned_D.keys() else 0 for c in
range(len(choice_weights))]
choices_dist = np.array(choices_dist)
weighted_choices = np.dot(np.array(choice_weights), choices_dist)
scores = dist_to_score(np.array(weighted_choices), L_a, L_f_a)
score_obj = np.mean(scores)
return score_obj, dist_obj, m.Runtime, m, assigned_D, num_residents, num_amenity, m.status
def cur_assignment_single(df_from,amenity_df, SP_matrix,bp, focus,EPS=1.e-6):
''' get assignment for the case with no allocation, no depth of choice'''
m = gp.Model('cur_assignment')
groups_from = df_from.groupby('node_ids').groups
group_values_from = list(groups_from.values())
num_residents = len(group_values_from)
num_amenity = len(amenity_df)
cartesian_prod = list(product(range(num_residents), range(num_amenity), )) # a list of tuples
distances = {(i, j): SP_matrix[df_from.iloc[group_values_from[i][0]]["node_ids"], amenity_df.iloc[j]["node_ids"]] for i, j in cartesian_prod}
if len(amenity_df) == 0:
print("no existing amenities!")
return 0, None, None, None, None, num_residents, 0, None # score_obj, obj_value, m.Runtime, m, assigned_D, num_residents, num_amenity, m.status
assign = m.addVars(cartesian_prod, vtype=GRB.BINARY, name='Assign')
# demand
m.addConstrs((gp.quicksum(assign[(i, j)] for j in range(num_amenity)) == 1 for i in range(num_residents)),
name='Demand')
# objective
m.setObjective(assign.prod(distances)/num_residents, GRB.MINIMIZE)
m.optimize()
obj = m.getObjective()
obj_value = obj.getValue() # min total dist
assignments = [(i, j) for (i, j) in assign.keys() if (assign[i, j].x > EPS)]
assign_from_var_id = [i for (i, j) in assignments]
assign_to_var_id = [j for (i, j) in assignments]
assign_from_node_id = [df_from.iloc[group_values_from[i][0]]["node_ids"] for (i, j) in assignments]
assign_to_node_id = [amenity_df.iloc[j]["node_ids"] for (i, j) in assignments]
dist=[SP_matrix[df_from.iloc[group_values_from[i][0]]["node_ids"], amenity_df.iloc[j]["node_ids"]] for (i, j) in assignments]
scores = dist_to_score(np.array(dist), L_a, L_f_a)
score_obj = np.mean(scores)
assigned_D = {
"assign_from_var_id": assign_from_var_id,
"assign_to_var_id": assign_to_var_id,
"assign_from_node_id": assign_from_node_id,
"assign_to_node_id": assign_to_node_id,
"dist": dist}
return score_obj, obj_value, m.Runtime, m, assigned_D, num_residents, num_amenity, m.status
def dist_to_score(d,L_a,L_f_a):
a = copy.deepcopy(L_a[:-1])
f_a = copy.deepcopy(L_f_a[:-1])
L_m = []
L_c = []
for i in range(len(a) - 1):
x = np.array(a[i:i + 2])
y = np.array(f_a[i:i + 2])
A = np.vstack([x, np.ones(len(x))]).T
m, c = np.linalg.lstsq(A, y, rcond=None)[0]
L_m.append(m)
L_c.append(c)
scores = np.piecewise(d, [d<a[1], (a[1]<=d) & (d<a[2]), (a[2]<=d) & (d<a[3]), d>=a[3]],
[lambda d: L_m[0]*d+L_c[0], lambda d: L_m[1]*d+L_c[1], lambda d: L_m[2]*d+L_c[2], lambda d:0])
return scores
def opt_single_CP(df_from,df_to,amenity_df, SP_matrix,k,threads,results_sava_path,solver_path, EPS=0.5):
'''single amenity case, no depth of choice'''
if len(df_from)>0:
df_from = df_from[['geometry', 'node_ids']]
if len(df_to)>0:
df_to = df_to[['geometry', 'node_ids']]
model = CpoModel(name="max_score")
# grouping
groups_to=df_to.groupby('node_ids').groups # keys are node id, values are indices
group_values_to=list(groups_to.values())
num_allocation = len(group_values_to)
capacity = [len(item) for item in group_values_to]
groups_from = df_from.groupby('node_ids').groups
group_values_from = list(groups_from.values())
num_residents = len(group_values_from)
num_cur = len(amenity_df)
cartesian_prod_allocate = list(product(range(num_residents), range(num_allocation)))
# retrieve distances
d = {(i, j): SP_matrix[df_from.iloc[group_values_from[i][0]]["node_ids"], df_to.iloc[group_values_to[j][0]]["node_ids"]] for i, j in cartesian_prod_allocate}
for i in range(num_residents):
# dummy node: inf distance
d[(i, num_allocation)] = L_a[-1]
# distance to existing ones
for l in range(num_cur):
cur_id = num_allocation + 1 + l
d[(i, cur_id)] = SP_matrix[df_from.iloc[group_values_from[i][0]]["node_ids"], amenity_df.iloc[l]["node_ids"]]
# variables
y = {}
for k_ in range(k):
y[k_] = model.integer_var(min=0, max=num_allocation,name=f'y[{k_}]') #include dummy node
f = {}
for i in range(num_residents):
f[i] = model.float_var(min=0, max=100, name=f'f[{i}]')
dist = {}
for i in range(num_residents):
for k_ in range(k):
dist[(i, k_)] = model.float_var(min=0, max=L_a[-1], name=f'dist[{i},{k_}]')
l = {}
for i in range(num_residents):
l[i] = model.float_var(min=0, max=L_a[-1], name=f'z[{i}]')
# Constraints
## WalkScore
# allocated
for i in range(num_residents):
for k_ in range(k):
model.add(dist[(i,k_)] == (model.element([d[(i, m)] for m in range(num_allocation+1)], y[k_])))
# existing
for i in range(num_residents):
model.add(l[i] == model.min([dist[(i,k_)] for k_ in range(k)] + [d[(i,j)] for j in range(num_allocation + 1, num_allocation + 1 + num_cur)]) )
# # PWL
for i in range(num_residents):
#model.add(f[i] == model.slope_piecewise_linear(l[i], [400, 1800, 2400], [-0.0125, -0.0607, -0.0167, 0], 0, 100))
model.add(f[i] == model.coordinate_piecewise_linear(l[i], -0.0125, [0, 400, 1800, 2400, 5000000], [100, 95, 10, 1, 1], 0))
for j in range(num_allocation):
model.add(model.count(list(y.values()),j)<=capacity[j])
# symmetry breaking
if k>1:
for k_ in range(k-1):
model.add(y[(k_)]<=y[(k_+1)])
model.add(model.all_diff([y[k_] for k_ in range(k)]))
# # objective
model.add(model.maximize(model.sum(f[i] for i in range(num_residents))/num_residents))