-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgs.py
707 lines (581 loc) · 23.1 KB
/
algs.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
import pandas as pd
import numpy as np
import random
import torch
import gurobipy
import cvxpy as cp
import matplotlib.pyplot as plt
import pickle as pkl
import utils
import algs
from sodapy import Socrata
from datetime import datetime, timedelta
## ALGORITHM FUNCTIONS ##
EPS = 1e-5
VERBOSE=False
"""
Generate suggested matching given the first stage graph and a set of possible second-stage graphs.
Assumes the uniform distribution over the second-stage graphs.
Arguments:
edges1 (list of edges): The first-stage graph
edges2_list (list of lists of edges): List of the possible second-stage graphs
weights: Vertex weights on the offline vertices.
verbose (bool): True if the solver should print detailed messages.
Output:
1. List of vertices that are suggested to match to in the first stage,
assuming the second-stage graph is generated from the uniform distribution
over the possible second stages.
2. List of edges in the suggested matching.
"""
def suggested_matching(edges1, edges2_list, weights, verbose=False):
if verbose:
print("***Generating suggested matching***")
num_scenarios = len(edges2_list)
if verbose:
print("Creating variables and constraints...")
# Define variables
x = {(i,j): cp.Variable(integer=True) for i,j in edges1}
y = [{(i,j): cp.Variable() for i,j in edges2} for edges2 in edges2_list]
D1 = set([e[0] for e in edges1])
D2_list = [set([e[0] for e in edges2]) for edges2 in edges2_list]
S = set([e[1] for e in edges1] + flatten([[e[1] for e in edges2] for edges2 in edges2_list]))
nbrs1_S = {j: [] for j in S}
nbrs1_D = {i: [] for i in D1}
nbrs2_S_list = [{j: [] for j in S} for case in range(num_scenarios)]
nbrs2_D_list = [{i: [] for i in D2_list[case]} for case in range(num_scenarios)]
for i,j in edges1:
nbrs1_S[j].append(i)
nbrs1_D[i].append(j)
for case in range(num_scenarios):
edges2 = edges2_list[case]
nbrs2_S = nbrs2_S_list[case]
nbrs2_D = nbrs2_D_list[case]
for i,j in edges2:
nbrs2_S[j].append(i)
nbrs2_D[i].append(j)
# print(nbrs1_S)
# print(nbrs1_D)
# print(nbrs2_S_list)
# print(nbrs2_D_list)
# Define constraints
constraints = []
# Non-negativity constraints
for i,j in edges1:
constraints += [x[i,j] >= 0]
for case in range(num_scenarios):
edges2 = edges2_list[case]
for i,j in edges2:
constraints += [y[case][i,j] >= 0]
# Degree constraints
for j in S:
for case in range(num_scenarios):
edges2 = edges2_list[case]
v1 = cp.sum([x[i,j] for i in nbrs1_S[j]])
v2 = cp.sum([y[case][i,j] for i in nbrs2_S_list[case][j]])
if len(nbrs1_S[j]) + len(nbrs2_S_list[case][j]) > 0:
constraints += [v1 + v2 <= 1]
for i in D1:
if len(nbrs1_D[i]) > 0:
constraints += [cp.sum([x[i,j] for j in nbrs1_D[i]]) <= 1]
for case in range(num_scenarios):
D2 = D2_list[case]
for i in D2:
if len(nbrs2_D_list[case][i]) > 0:
constraints += [cp.sum([y[case][i,j] for j in nbrs2_D_list[case][i]]) <= 1]
# Objective
# first-stage value
value1 = cp.sum([weights[j] * cp.sum([x[i,j] for i in nbrs1_S[j]]) for j in S])
# expected second-stage value
value2 = 0
# print("num_scenarios: ", num_scenarios)
for case in range(num_scenarios):
value2 += (1/num_scenarios) * cp.sum([weights[j] * cp.sum([y[case][i,j] for i in nbrs2_S_list[case][j]]) for j in S])
obj = cp.Maximize(value1 + value2)
if verbose:
print("Solving integer program...")
prob = cp.Problem(obj, constraints)
prob.solve(verbose=verbose, solver=cp.MOSEK)
# Return suggested offline vertices j
sugg = []
for j in S:
if len(nbrs1_S[j]) > 0 and cp.sum([x[i,j] for i in nbrs1_S[j]]).value == 1:
sugg.append(j)
if verbose:
print("Finished generating suggested matching.")
print("Expected value is: ", prob.value)
suggested_matching = []
for i,j in edges1:
if x[i,j].value == 1:
suggested_matching.append((i,j))
return sugg, suggested_matching
"""
Generate the best matching in the first stage graph given a second-stage graph.
Arguments:
edges1 (list of edges): The first-stage graph
edges2_list (list of edges): The second-stage graph
weights: Vertex weights on the offline vertices.
verbose (bool): True if the solver should print detailed messages.
Output:
1. List of vertices that are suggested to match to in the first stage.
2. List of edges in the suggested matching.
"""
def best_suggested_matching(edges1, edges2, weights, verbose=False):
if verbose:
print("***Solving for optimal hindsight matching***")
print("Creating variables and constraints...")
D1 = set(e[0] for e in edges1)
D2 = set(e[0] for e in edges2)
S = set([e[1] for e in edges1] + [e[1] for e in edges2])
nbrs1_D = {i: [] for i in D1}
nbrs2_D = {i: [] for i in D2}
nbrs1_S = {j: [] for j in S}
nbrs2_S = {j: [] for j in S}
for i,j in edges1:
nbrs1_D[i].append(j)
nbrs1_S[j].append(i)
for i,j in edges2:
nbrs2_D[i].append(j)
nbrs2_S[j].append(i)
x = {(i,j): cp.Variable(integer=True) for i,j in edges1}
y = {(i,j): cp.Variable() for i,j in edges2}
constraints = []
for i,j in edges1:
constraints += [x[i,j] >= 0]
for i,j in edges2:
constraints += [y[i,j] >= 0]
for i in D1:
if len(nbrs1_D[i]) > 0:
constraints += [cp.sum([x[i,j] for j in nbrs1_D[i]]) <= 1]
for i in D2:
if len(nbrs2_D[i]) > 0:
constraints += [cp.sum([y[i,j] for j in nbrs2_D[i]]) <= 1]
for j in S:
deg1 = cp.sum([x[i,j] for i in nbrs1_S[j]])
deg2 = cp.sum([y[i,j] for i in nbrs2_S[j]])
if len(nbrs1_S[j]) + len(nbrs2_S[j]) > 0:
constraints += [deg1 + deg2 <= 1]
val1 = cp.sum([weights[j]*cp.sum([x[i,j] for i in nbrs1_S[j]]) for j in S])
val2 = cp.sum([weights[j]*cp.sum([y[i,j] for i in nbrs2_S[j]]) for j in S])
obj = cp.Maximize(val1 + val2)
prob = cp.Problem(obj, constraints)
if verbose:
print("Solving IP...")
prob.solve(verbose=verbose, solver=cp.GUROBI)
# Return suggested offline vertices j
sugg = []
for j in S:
if len(nbrs1_S[j]) > 0 and cp.sum([x[i,j] for i in nbrs1_S[j]]).value == 1:
sugg.append(j)
suggested_matching = []
for i,j in edges1:
if x[i,j].value == 1:
suggested_matching.append((i,j))
return sugg, suggested_matching
"""
Implements our algorithm.
Arguments:
edges1 (list of edges): First stage graph.
edges2 (list of edges): Second stage graph.
sugg: List of vertices that are suggested to match to in the first stage.
weights: Vertex weights on the offline vertices.
R: Target minimum robustness level.
verbose (bool): True if the solver should print detailed messages.
Output:
Value of the matching returned by our algorithm.
"""
def our_alg(edges1, edges2, sugg, weights, R, verbose=False):
# Solve the first-stage fractional matching using the convex optimization problem
# maximize sum_j ( w_j * (x_j - F_j(x_j))), s.t. x is a fractional matching
# Here,
# F_L(x) = 0 if x <= 1-R and x - (1-R)(1 + ln(x/(1-R))) if x > 1-R
# F_U(x) = -(1-R)ln(1-x) if x <= R and -(1-R)ln(1-R) + (x-R) if x > R
# Define variables
D1 = set(e[0] for e in edges1)
S = set([e[1] for e in edges1] + [e[1] for e in edges2])
D2 = set(e[0] for e in edges2)
not_sugg = [j for j in S if j not in sugg]
nbrs_i = {i: [] for i in D1}
nbrs_j = {j: [] for j in S}
for i,j in edges1:
nbrs_i[i].append(j)
nbrs_j[j].append(i)
xs = {(i,j): cp.Variable() for i,j in edges1}
ys = {j: cp.Variable() for j in S}
zs = {j: cp.Variable() for j in S}
# print(len(S))
# print(len(weights))
# print(len(zs))
# print(len(not_sugg))
if verbose:
print("Running our algorithm. The problem has \
\n\t |D1| = {D1_size}, \
\n\t |D2| = {D2_size}, \
\n\t |S| = {S_size}, \
\n\t {E1_size} first-stage edges, \
\n\t {E2_size} second-stage edges.".format(D1_size=len(D1), D2_size=len(D2), S_size=len(S),
E1_size=len(edges1), E2_size=len(edges2)))
print("Creating constraints...")
# Define constraints
constraints = []
for i,j in edges1:
constraints += [xs[i,j] >= 0]
for i in D1:
if len(nbrs_i[i]) > 0:
constraints += [cp.sum([xs[i,j] for j in nbrs_i[i]]) <= 1]
for j in S:
constraints += [ys[j] == cp.sum([xs[i,j] for i in nbrs_j[j]])]
constraints += [ys[j] <= 1]
# if len(nbrs_j[j]) > 0:
# constraints += [cp.sum([xs[i,j] for i in nbrs_j[j]]) <= 1]
# constraints += [ys[j] == cp.sum([xs[i,j] for i in nbrs_j[j]])]
# else:
# constraints += [ys[j] == 0]
if j in sugg:
constraints += [zs[j] >= ys[j]]
constraints += [zs[j] >= 1-R]
elif j in not_sugg:
constraints += [zs[j] >= 0]
constraints += [zs[j] <= ys[j]]
constraints += [zs[j] <= R]
# Define objective
def hl(x):
return x - (1 - R)*(1 + cp.log(x/(1-R)))
def hu(x):
return (1-R)*cp.log(1-R) - (x - R) - (1-R)*cp.log(1-x)
objective1 = cp.Maximize( cp.sum([weights[j] * (ys[j] - hl(zs[j])) for j in sugg])
+ cp.sum([weights[j] * (-hu(zs[j])) for j in not_sugg])
+ EPS * cp.sum([ys[j] for j in S]))
prob1 = cp.Problem(objective1, constraints)
# print(prob1)
if verbose:
print("Solving the first-stage problem...")
prob1.solve(verbose=verbose, solver=cp.MOSEK)
# print({j: ys[j].value for j in ys})
# print(objective1.value)
if verbose:
print("Finished solving the first-stage convex program.")
print("Creating second-stage constraints...")
# Solve for the best second-stage matching subject to first-stage decisions
# Variables
x2 = {(i,j): cp.Variable() for i,j in edges2}
nbrs2_i = {i: [] for i in D2}
nbrs2_j = {j: [] for j in S}
# Constraints
constraints2 = []
for i,j in edges2:
nbrs2_i[i].append(j)
nbrs2_j[j].append(i)
constraints2 += [x2[i,j] >= 0]
# Due to numerical issues, y may slightly exceed 1 or slightly be less than 0.
# Therefore we project y onto the interval [0, 1]
for j in S:
if ys[j].value > 1:
ys[j].value = 1.0
elif ys[j].value < 0:
ys[j].value = 0.0
for j in S:
if len(nbrs2_j[j]) > 0:
if ys[j].value > 1:
print('!!!!! Vertex ', j, '!!!!!')
constraints2 += [cp.sum([x2[i,j] for i in nbrs2_j[j]]) <= 1 - ys[j].value]
for i in D2:
if len(nbrs2_i[i]) > 0:
constraints2 += [cp.sum([x2[i,j] for j in nbrs2_i[i]]) <= 1]
# Objective
objective2 = cp.Maximize(cp.sum([weights[j]*x2[i,j] for i,j in edges2]))
prob2 = cp.Problem(objective2, constraints2)
# print(prob2)
if verbose:
print("Solving the second-stage problem...")
prob2.solve(verbose=verbose, solver=cp.MOSEK)
val1 = cp.sum([weights[j] * ys[j] for j in S]).value
val2 = 0
val2 = prob2.value
if verbose:
print("first stage value", val1)
print("second stage value", val2)
return val1 + val2
"""
Solves for optimal matching in a bipartite graph.
Arguments:
edges (list of edges): The graph
weights (list of floats): Vertex weights on the offline vertices
verbose (bool): True if the solver should print detailed messages.
Output:
value of the maximum-weight matching in the graph.
"""
def opt(edges, weights, verbose=False):
D = set(e[0] for e in edges)
S = set(e[1] for e in edges)
nbrs_i = {i: [] for i in D}
nbrs_j = {j: [] for j in S}
for i,j in edges:
nbrs_i[i].append(j)
nbrs_j[j].append(i)
x = {(i,j): cp.Variable() for i,j in edges}
# y = {j: cp.Variable() for j in S}
constraints = []
for i,j in edges:
constraints += [x[i,j] >= 0]
for i in D:
constraints += [cp.sum([x[i,j] for j in nbrs_i[i]]) <= 1]
for j in S:
constraints += [cp.sum([x[i,j] for i in nbrs_j[j]]) <= 1]
# constraints += [y[j] <= 1]
obj = cp.Maximize(cp.sum([weights[j]*cp.sum([x[i,j] for i in nbrs_j[j]]) for j in S]))
prob = cp.Problem(obj, constraints)
prob.solve(verbose=verbose, solver=cp.MOSEK)
return prob.value
"""
Solves for optimal matching in a bipartite graph.
Arguments:
edges1 (list of edges): The first stage graph
edges2 (list of edges): The second stage graph
weights (list of floats): Vertex weights on the offline vertices
verbose (bool): True if the solver should print detailed messages.
Output:
value of the maximum-weight matching in the graph.
"""
def opt(edges1, edges2, weights, verbose=False):
if verbose:
print("***Solving for optimal hindsight matching***")
print("Creating variables and constraints...")
D1 = set(e[0] for e in edges1)
D2 = set(e[0] for e in edges2)
S = set([e[1] for e in edges1] + [e[1] for e in edges2])
nbrs1_D = {i: [] for i in D1}
nbrs2_D = {i: [] for i in D2}
nbrs1_S = {j: [] for j in S}
nbrs2_S = {j: [] for j in S}
for i,j in edges1:
nbrs1_D[i].append(j)
nbrs1_S[j].append(i)
for i,j in edges2:
nbrs2_D[i].append(j)
nbrs2_S[j].append(i)
x = {(i,j): cp.Variable() for i,j in edges1}
y = {(i,j): cp.Variable() for i,j in edges2}
constraints = []
for i,j in edges1:
constraints += [x[i,j] >= 0]
for i,j in edges2:
constraints += [y[i,j] >= 0]
for i in D1:
if len(nbrs1_D[i]) > 0:
constraints += [cp.sum([x[i,j] for j in nbrs1_D[i]]) <= 1]
for i in D2:
if len(nbrs2_D[i]) > 0:
constraints += [cp.sum([y[i,j] for j in nbrs2_D[i]]) <= 1]
for j in S:
deg1 = cp.sum([x[i,j] for i in nbrs1_S[j]])
deg2 = cp.sum([y[i,j] for i in nbrs2_S[j]])
if len(nbrs1_S[j]) + len(nbrs2_S[j]) > 0:
constraints += [deg1 + deg2 <= 1]
val1 = cp.sum([weights[j]*cp.sum([x[i,j] for i in nbrs1_S[j]]) for j in S])
val2 = cp.sum([weights[j]*cp.sum([y[i,j] for i in nbrs2_S[j]]) for j in S])
obj = cp.Maximize(val1 + val2)
prob = cp.Problem(obj, constraints)
if verbose:
print("Solving LP...")
prob.solve(verbose=verbose, solver=cp.MOSEK)
if verbose:
print("Optimal hindsight value is: ", prob.value)
return prob.value
"""
Solves for optimal matching, ASSUMING suggested matching is followed exactly in the first stage.
Arguments:
edges1 (list of edges): First stage graph.
edges2 (list of edges): Second stage graph.
weights (list of floats): Weights on offline vertices.
sugg (list of ints): List of vertices that are suggested to match to in the first stage.
verbose (bool): True if the solver should print detailed messages.
Output:
value of the maximum-weight matching in the graph if the first-stage matching matches exactly the vertices in sugg.
"""
def advice(edges1, edges2, sugg, weights, verbose=False):
# Define variables
D2 = set(e[0] for e in edges2)
S = set([e[1] for e in edges1] + [e[1] for e in edges2])
nbrs2_D = {i: [] for i in D2}
nbrs2_S = {j: [] for j in S}
for i,j in edges2:
nbrs2_D[i].append(j)
nbrs2_S[j].append(i)
x = {(i,j): cp.Variable() for i,j in edges2}
# y = {j: cp.Variable() for j in S}
constraints = []
for i,j in edges2:
constraints += [x[i,j] >= 0]
for j in sugg:
if len(nbrs2_S[j]) > 0:
constraints += [cp.sum([x[i,j] for i in nbrs2_S[j]]) == 0]
for i in D2:
constraints += [cp.sum([x[i,j] for j in nbrs2_D[i]]) <= 1]
for j in S:
if len(nbrs2_S[j]) > 0:
constraints += [cp.sum([x[i,j] for i in nbrs2_S[j]]) <= 1]
# constraints += [y[j] <= 1]
obj = cp.Maximize(cp.sum([weights[j]*cp.sum([x[i,j] for i in nbrs2_S[j]]) for j in S]))
prob = cp.Problem(obj, constraints)
# print(prob)
prob.solve(verbose=verbose, solver=cp.MOSEK)
return np.sum([weights[j] for j in sugg]) + prob.value
"""
Implements the algorithm that greedily selects the max-weight first-stage matching.
Arguments:
edges1 (list of edges): First stage graph.
edges2 (list of edges): Second stage graph.
weights (list of floats): Weights on offline vertices.
verbose (bool): True if the solver should print detailed messages.
Output:
value of the matching returned by the greedy algorithm.
"""
def greedy(edges1, edges2, weights, verbose=False):
if verbose:
print("***Running GREEDY algorithm***")
print("Creating variables and constraints...")
D1 = set(e[0] for e in edges1)
D2 = set(e[0] for e in edges2)
S = set([e[1] for e in edges1] + [e[1] for e in edges2])
nbrs1_D = {i: [] for i in D1}
nbrs2_D = {i: [] for i in D2}
nbrs1_S = {j: [] for j in S}
nbrs2_S = {j: [] for j in S}
for i,j in edges1:
nbrs1_D[i].append(j)
nbrs1_S[j].append(i)
for i,j in edges2:
nbrs2_D[i].append(j)
nbrs2_S[j].append(i)
x = {(i,j): cp.Variable() for i,j in edges1}
y = {(i,j): cp.Variable() for i,j in edges2}
constraints1 = []
for i,j in edges1:
constraints1 += [x[i,j] >= 0]
for i in D1:
if len(nbrs1_D[i]) > 0:
constraints1 += [cp.sum([x[i,j] for j in nbrs1_D[i]]) <= 1]
for j in S:
if len(nbrs1_S[j]) > 0:
constraints1 += [cp.sum([x[i,j] for i in nbrs1_S[j]]) <= 1]
obj1 = cp.Maximize(cp.sum([weights[j]*cp.sum([x[i,j] for i in nbrs1_S[j]]) for j in S]))
prob1 = cp.Problem(obj1, constraints1)
prob1.solve(verbose=verbose, solver=cp.MOSEK)
constraints2 = []
for i,j in edges2:
constraints2 += [y[i,j] >= 0]
for i in D2:
if len(nbrs2_D[i]) > 0:
constraints2 += [cp.sum([y[i,j] for j in nbrs2_D[i]]) <= 1]
for j in S:
amt_prev_filled = 0
if len(nbrs1_S[j]) > 0:
amt_prev_filled = cp.sum([x[i,j] for i in nbrs1_S[j]]).value
if amt_prev_filled > 1:
amt_prev_filled = 1.0
elif amt_prev_filled < 0:
amt_prev_filled = 0.0
if len(nbrs2_S[j]) > 0:
constraints2 += [cp.sum([y[i,j] for i in nbrs2_S[j]]) <= 1 - amt_prev_filled]
obj2 = cp.Maximize(cp.sum([weights[j]*cp.sum([y[i,j] for i in nbrs2_S[j]]) for j in S]))
prob2 = cp.Problem(obj2, constraints2)
prob2.solve(verbose=verbose, solver=cp.MOSEK)
return prob1.value + prob2.value
"""
Implements the fully-robust algorithm of Feng et al.
Arguments:
edges1 (list of edges): First stage graph.
edges2 (list of edges): Second stage graph.
weights: Vertex weights on the offline vertices.
verbose (bool): True if the solver should print detailed messages.
Output:
Value of the matching returned by the fully robust algorithm.
"""
def fully_robust(edges1, edges2, weights, verbose=False):
# Solve the first-stage fractional matching using the convex optimization problem
# maximize sum_j ( w_j * (x_j - F(x_j))), s.t. x is a fractional matching
# Here,
# F(x) = x^2/2
# Define variables
D1 = set(e[0] for e in edges1)
S = set([e[1] for e in edges1] + [e[1] for e in edges2])
D2 = set(e[0] for e in edges2)
nbrs_i = {i: [] for i in D1}
nbrs_j = {j: [] for j in S}
for i,j in edges1:
nbrs_i[i].append(j)
nbrs_j[j].append(i)
xs = {(i,j): cp.Variable() for i,j in edges1}
ys = {j: cp.Variable() for j in S}
zs = {j: cp.Variable() for j in S}
if verbose:
print("Running the fully robust algorithm. The problem has \
\n\t |D1| = {D1_size}, \
\n\t |D2| = {D2_size}, \
\n\t |S| = {S_size}, \
\n\t {E1_size} first-stage edges, \
\n\t {E2_size} second-stage edges.".format(D1_size=len(D1), D2_size=len(D2), S_size=len(S),
E1_size=len(edges1), E2_size=len(edges2)))
print("Creating constraints...")
# Define constraints
constraints = []
for i,j in edges1:
constraints += [xs[i,j] >= 0]
for i in D1:
if len(nbrs_i[i]) > 0:
constraints += [cp.sum([xs[i,j] for j in nbrs_i[i]]) <= 1]
for j in S:
constraints += [ys[j] == cp.sum([xs[i,j] for i in nbrs_j[j]])]
constraints += [ys[j] <= 1]
# Define objective
objective1 = cp.Maximize(cp.sum([weights[j] * (ys[j] - ys[j]**2/2) for j in S]))
prob1 = cp.Problem(objective1, constraints)
# print(prob1)
if verbose:
print("Solving the first-stage problem...")
prob1.solve(verbose=verbose, solver=cp.MOSEK)
# print({j: ys[j].value for j in ys})
# print(objective1.value)
if verbose:
print("Finished solving the first-stage convex program.")
print("Creating second-stage constraints...")
# Solve for the best second-stage matching subject to first-stage decisions
# Variables
x2 = {(i,j): cp.Variable() for i,j in edges2}
nbrs2_i = {i: [] for i in D2}
nbrs2_j = {j: [] for j in S}
# Constraints
constraints2 = []
for i,j in edges2:
nbrs2_i[i].append(j)
nbrs2_j[j].append(i)
constraints2 += [x2[i,j] >= 0]
# Due to numerical issues, y may slightly exceed 1 or slightly be less than 0.
# Therefore we project y onto the interval [0, 1]
for j in S:
if ys[j].value > 1:
ys[j].value = 1.0
elif ys[j].value < 0:
ys[j].value = 0.0
for j in S:
if len(nbrs2_j[j]) > 0:
# if ys[j].value > 1:
# print('!!!!! Vertex ', j, '!!!!!')
constraints2 += [cp.sum([x2[i,j] for i in nbrs2_j[j]]) <= 1 - ys[j].value]
for i in D2:
if len(nbrs2_i[i]) > 0:
constraints2 += [cp.sum([x2[i,j] for j in nbrs2_i[i]]) <= 1]
# Objective
objective2 = cp.Maximize(cp.sum([weights[j]*x2[i,j] for i,j in edges2]))
prob2 = cp.Problem(objective2, constraints2)
# print(prob2)
if verbose:
print("Solving the second-stage problem...")
prob2.solve(verbose=verbose, solver=cp.MOSEK)
val1 = cp.sum([weights[j] * ys[j] for j in S]).value
val2 = 0
val2 = prob2.value
if verbose:
print("first stage value", val1)
print("second stage value", val2)
return val1 + val2