-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmax_study.jl
executable file
·541 lines (448 loc) · 15.5 KB
/
dmax_study.jl
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
using JuMP
using GLPK
using Gurobi
"""
enum_solutions(U,i,y)
Recursive function that enumerates all the possible solutions of the robust problem
- `U` : uncertainty set
- `i` : current vertex under consideration
- `y` : solution being recursively built
"""
function enum_solutions(U,i,y)
if (i == length(U))
res = Array{Int,2}(undef,length(U),0);
for k in U[i]
res = [res [y;k]];
end
return res;
else
res = Array{Int,2}(undef,length(U),0);
for k in U[i]
res = [res enum_solutions(U,i+1,[y;k])];
end
return res;
end
end
"""
min_ratio_exact(n,F)
Search for the uncertainty set maximizing the approximation factor for a given edge list.
The uncertainty set of each vertex contains as many points as its degree, because the worst ratio can always be obtained for such instance. With this choice, we can arbitrarily set which points of the uncertainty sets achieve maximum distance between the extremities of each eadge. This gives rise to a continuous model.
- `n` : number of vertices of the graph
- `F` : edge list
"""
function min_ratio_exact(n,F)
degree
# initialize the adjacency list
A = [Int[] for i in 1:n]
ranks = Dict();
for (i,j) in F
push!(A[i],j);
push!(A[j],i);
ranks[(i,j)] = (length(A[i]),length(A[j]));
end
@info "Adjacency list: $A"
# initialize the uncertainty sets with one point per adjacent vertex and an extra point for the optimum
U = [Int[] for i in 1:n];
nU = 0; # total number of points in U
for i in 1:n
for j in A[i]
nU += 1;
push!(U[i], nU);
end
#nU += 1;
#push!(U[i],nU);
end
@info "Uncertainty sets: $U"
indU = 1:nU; # indices of the points in U
E = [(k,l) for (i,j) in F for k in U[i] for l in U[j]];
##
model = Model(Gurobi.Optimizer);
#
@variable(model, dintra[i in 1:n] >= 0);
@variable(model, d[k in indU, l in indU] >= 0);
@variable(model, dmax[i in 1:n, j in 1:n ; (i,j) in F] >= 0);
@variable(model, ω >= 0);
#
@objective(model, Min, ω);
# we restrict the search to uncertainty sets where the points are equidistant
for i in 1:n
for k in U[i], l in U[i]
if k < l
@constraint(model, d[k,l] == dintra[i])
end
end
end
# Distance constraints
# identity
@constraint(model, ct_identity[k in indU], d[k,k] == 0);
# symmetry
@constraint(model, ct_symmetry[k in indU, l in indU ; k <l], d[k,l] == d[l,k]);
# transitivity
for i in indU, j in indU, k in indU
if i < j & j < k
@constraint(model, d[i,j] + d[j,k] >= d[i,k]);
@constraint(model, d[i,j] + d[i,k] >= d[j,k]);
@constraint(model, d[i,k] + d[j,k] >= d[i,j]);
end
end
# compute the pairwise maximum distances
@constraint(model, ctsumdmax, sum(dmax[i,j] for (i,j) in F) == length(F));
for (i,j) in F
if i < j
@constraint(model, dmax[i,j] == d[U[i][ranks[(i,j)][1]],U[j][ranks[(i,j)][2]]]);
for k in U[i], l in U[j]
@constraint(model, dmax[i,j] >= d[k,l]);
end
end
end
#Enumerate all the possible solutions of the robust problem
Y = enum_solutions(U, 1, Array{Int,1}());
for y in eachcol(Y)
edges = [];
for (i,j) in F
edges = [edges ; (y[i],y[j])];
end
@constraint(model, ω >= sum(d[k,l] for (k,l) in edges));
end
optimize!(model);
return objective_value(model)/(length(F)), value.(d), value.(dmax);
end
"""
min_ratio_dual(n,F)
Search for the uncertainty set maximizing the approximation factor for a given edge list.
Here, we consider a relaxation of the robust problem to dualize it and avoid the enumeration of an exponential number of constraints in the epigraphic formulation of the objective.
The function returns an upper bound on the worst-case ratio
- `n` : number of vertices of the graph
- `F` : edge list
"""
function min_ratio_dual(n,F)
# initialize the adjacency list
A = [Int[] for i in 1:n]
ranks = Dict();
for (i,j) in F
push!(A[i],j);
push!(A[j],i);
ranks[(i,j)] = (length(A[i]),length(A[j]));
end
@debug "Adjacency list: $A"
# initialize the uncertainty sets with one point per adjacent vertex and an extra point for the optimum
U = [Int[] for i in 1:n];
nU = 0; # total number of points in U
ii = [] # table i[k]
for i in 1:n
for j in A[i]
nU += 1;
push!(U[i], nU);
push!(ii, i)
end
# nU += 1;
# push!(U[i],nU);
# push!(ii, i)
end
@debug "Uncertainty sets: $U"
indU = 1:nU; # indices of the points in U
E = [(k,l) for (i,j) in F for k in U[i] for l in U[j]];
##
model = Model(Gurobi.Optimizer);
#
#
@variable(model, dintra[i in 1:n] >= 0);
@variable(model, d[k in indU, l in indU] >= 0);
@variable(model, dmax[i in 1:n, j in 1:n ; (i,j) in F] >= 0);
@variable(model, ω >= 0);
#
@objective(model, Min, ω);
# we restrict the search to uncertainty sets where the points are equidistant
for i in 1:n
for k in U[i], l in U[i]
if k < l
@constraint(model, d[k,l] == dintra[i])
end
end
end
# Distance constraints
# identity
@constraint(model, ct_identity[k in indU], d[k,k] == 0);
# symmetry
@constraint(model, ct_symmetry[k in indU, l in indU ; k <l], d[k,l] == d[l,k]);
# transitivity
for i in indU, j in indU, k in indU
if i < j & j < k
@constraint(model, d[i,j] + d[j,k] >= d[i,k]);
@constraint(model, d[i,j] + d[i,k] >= d[j,k]);
@constraint(model, d[i,k] + d[j,k] >= d[i,j]);
end
end
# compute the pairwise maximum distances
@constraint(model, ctsumdmax, sum(dmax[i,j] for (i,j) in F) == length(F));
for (i,j) in F
if i < j
@constraint(model, dmax[i,j] == d[U[i][ranks[(i,j)][1]],U[j][ranks[(i,j)][2]]]);
for k in U[i], l in U[j]
@constraint(model, dmax[i,j] >= d[k,l]);
end
end
end
#add the dual constraints
@variable(model, α[i in 1:n])
@variable(model, β[k in indU, j in 1:n] ≥ 0)
@variable(model, γ[k in indU, j in 1:n] ≥ 0)
@constraint(model, [i in 1:n, k in U[i]], α[i] - sum(β[k,j] for (i,j) in F if i==ii[k]) - sum(γ[k,j] for (j,i) in F if i==ii[k]) ≥ 0)
@constraint(model, [(k,l) in E], β[k,ii[l]] + γ[l,ii[k]] ≥ d[k,l])
@objective(model, Min, sum(α[i] for i in 1:n))
#
optimize!(model);
@info "Dual optimal value is $(objective_value(model)/length(F))"
# verification
@debug begin
"---- running verification"
check = Model(Gurobi.Optimizer)
@variable(check, δ[k in indU], Bin)
@variable(check, Δ[(k,l) in E], Bin)
@constraint(check, [i in 1:n], sum(δ[k] for k in U[i]) == 1)
@constraint(check, [(i,j) in F, k in U[i]], sum(Δ[(k,l)] for (k′,l) in E if k′ == k && in(l,U[j])) ≤ δ[k])
@constraint(check, [(j,i) in F, k in U[i]], sum(Δ[(l,k)] for (l,k′) in E if k′ == k && in(l,U[j])) ≤ δ[k])
@objective(check, Max, sum(value(d[k,l])*Δ[(k,l)] for (k,l) in E))
optimize!(check);
"Check optimal value is $(objective_value(check)/length(F))"
end
return objective_value(model)/(length(F)), value.(d), value.(dmax);
end
######################################
# 3-PATH
######################################
# V = [1 ; 2 ; 3];
# F = [(1,2) ; (2,3)];
# U = [1 2 ; 3 4 ; 5 6]
# rho_3path = maxratio(F,U);
# println("maximum factor for the 3-path: ", 1.0/rho_3path);
######################################
# TRIANGLE
######################################
# V = [1 ; 2 ; 3];
# F = [(1,2) ; (1,3) ; (2,3)];
# U = [1 2 3 4 ; 5 6 7 8 ; 9 10 11 12]
# rho_triangle = maxratio(F,U);
# println("maximum factor for the triangle: ", 1.0/rho_triangle);
######################################
# 4-CYCLE
######################################
# V = [1 ; 2 ; 3 ; 4];
# F = [(1,2) ; (2,3) ; (3,4) ; (1,4)];
# U = [1 2 ; 3 4 ; 5 6 ; 7 8]
# rho_4cycle = maxratio(F,U);
# println("maximum factor for the 4-cycle: ", 1.0/rho_4cycle);
######################################
# 5-CYCLE
######################################
# F = [(1,2) ; (2,3) ; (3,4) ; (4,5) ; (1,5)];
# U = [1 2 ; 3 4 ; 5 6 ; 7 8 ; 9 10]
# rho_5cycle = maxratio(F,U);
# println("maximum factor for the 5-cycle: ", 1.0/rho_4cycle);
# ######################################
# # 4-CLIQUE
# ######################################
# V = [1 ; 2 ; 3 ; 4];
# F = [(1,2) ; (1,3) ; (1,4) ; (2,3) ; (2,4) ; (3,4)];
# U = [1 2 ; 3 4 ; 5 6 ; 7 8]
# rho_4clique = maxratio(F,U);
# println("maximum factor for the 4-clique: ", 1.0/rho_4cycle);
# ######################################
# # 4-STAR
# ######################################
# V = [1 ; 2 ; 3 ; 4];
# F = [(1,2) ; (1,3) ; (1,4)];
# U = [1 2 ; 3 4 ; 5 6 ; 7 8]
# rho_4star = maxratio(F,U);
# println("maximum factor for the 4-star: ", 1.0/rho_4star);
######################################
# 7-TREE
######################################
#F = [(1,2) ; (1,3) ; (1,4) ; (2,5) ; (3,6) ; (4,7)];
#U = [1 2 3; 4 5 6 ; 7 8 9 ; 10 11 12 ; 13 14 15 ; 16 17 18 ; 19 20 21];
#
#rho_7bintree = maxratio(F,U);
#
#println("maximum factor for the 7-binary tree: ", 1.0/rho_7bintree);
######################################
# 7-BINARY-TREE
######################################
#F = [(1,2) ; (1,3) ; (2,4) ; (2,5) ; (3,6) ; (3,7)];
#U = [1 2 3; 4 5 6 ; 7 8 9 ; 10 11 12 ; 13 14 15 ; 16 17 18 ; 19 20 21];
#
#rho_7bintree = maxratio(F,U);
#
#println("maximum factor for the 7-binary tree: ", 1.0/rho_7bintree);
######################################
# 11-TREE
######################################
#F = [(1,2) ; (1,3) ; (2,4) ; (2,5) ; (3,6) ; (3,7) ; (4,8) ; (5,9) ; (6,10) ; (7,11)];
#U = [1 2 ; 3 4 ; 5 6 ; 7 8 ; 9 10 ; 11 12 ; 13 14 ; 15 16 ; 17 18 ; 19 20 ; 21 22];
#
#rho_11tree = maxratio(F,U);
#println("maximum factor for the 7-binary tree: ", 1.0/rho_11tree);
"""
test_trees()
Search the worst-case ratio for a set of (small) tree graphs
"""
function test_trees()
@info "FOUR LEVEL BINARY TREE"
F = [(1,2) ; (1,3) ; (2,4) ; (2,5) ; (3,6) ; (3,7) ; (4,8) ; (4,9) ; (5,10) ; (5,11) ; (6,12) ; (6,13) ; (7,14) ; (7,15)];
@info "Exact method"
rho_4lv_bintree = min_ratio_exact(15,F);
@info "maximum factor for the 4-level binary tree: $(1.0/rho_4lv_bintree)"
@info "Approximation with dualized model";
rho_4lv_bintree_bound = min_ratio_dual(15,F);
@info "dual bound maximum factor for the 4-level ternary tree: $(1.0/rho_4lv_bintree_bound)"
@info "TREE LEVEL TERNARY TREE"
F = Any[(1, 2), (1, 3), (1, 4), (2, 5), (2, 6), (2, 7), (3, 8), (3, 9), (3, 10), (4, 11), (4, 12), (4, 13)];
@info "Exact method"
rho_3lv_tertree = min_ratio_exact(13,F);
@info "maximum factor for the 3-level ternary tree: $(1.0/rho_3lv_tertree)"
@info "Approximation with dualized model";
rho_3lv_tertree_bound = min_ratio_dual(13,F);
@info "dual bound maximum factor for the 4-level ternary tree: $(1.0/rho_3lv_tertree_bound)"
@info "FOUR LEVEL TERNARY TREE"
for i in 5:13
for j in 1:3
global F = [F;(i, 13 + 3*(i-5) + j)]
end
end
@info "Exact method"
rho_4lv_tertree = min_ratio_exact(40,F);
@info "maximum factor for the 4-level ternary tree: $(1.0/rho_4lv_tertree)"
@info "Approximation with dualized model";
rho_4lv_tertree_bound = min_ratio_dual(40,F);
@info "dual bound maximum factor for the 4-level ternary tree: $(1.0/rho_4lv_tertree_bound)"
end
"""
test_clique(K)
Search the worst-case ratio of a K-clique
`K` : size of the largest clique that will be tested
"""
function test_clique(K)
@info "Worst case ratio of a $(K)-CLIQUE"
F = [];
for i in 1:K
for j in i+1:K
F = [F ; (i,j)];
end
end
@debug begin
"Exact method"
rho_exact, d, dmax = min_ratio_exact(K,F);
"maximum factor for the $(K)-clique: $(1.0/rho_exact)"
"\n"
"internal distances"
for i in 1:K
"- in vertex $i"
for k in (i-1)*(K-1)+1:i*(K-1)
for l in (i-1)*(K-1)+1:i*(K-1)
if k < l
@info "d[$k,$l]=$(d[k,l])"
end
end
end
end
"\n"
"pairwise distances"
for (i,j) in F
"- edge ($i,$j)"
for k in (i-1)*(K-1)+1:i*(K-1)
for l in (j-1)*(K-1)+1:j*(K-1)
"d[$k,$l]=$(d[k,l])"
end
end
end
end
@info "Approximation with dualized model";
rho_dual,d,dmax = min_ratio_dual(K,F);
@info "dual bound maximum factor for the $(K)-clique: $(1.0/rho_dual)"
end
"""
test_quasiclique(K)
Search the worst-case ratio of a K-quasi-clique, i.e., a clique minus an edge
`K` : size of the largest clique that will be tested
"""
function test_quasiclique(K)
@info "Worst case ratio of a $(K)-QUASI-CLIQUE"
F = [(1,j) for j in 2:K-1];
for i in 2:K
for j in i+1:K
F = [F ; (i,j)];
end
end
@info "Approximation with dualized model";
rho_dual,d,dmax = min_ratio_dual(K,F);
@info "dual bound maximum factor for the $(K)-clique: $(1.0/rho_dual)"
end
######################################
# ENUMERATE ALL GRAPHS WITH N VERTICES
######################################
"""
enum_connected_graphs(n,(i,j),F, cur_deg, prev_deg)
Recursive function that enumerate the edge lists of all connected graphs with n vertices
Break symmetries by imposing non-increasing degrees of the vertices
"""
function enum_connected_graphs(n,(i,j), F)
if (i==n-1)
res = Set();
G = Graph(n);
for (k,l) in F
add_edge!(G, k, l);
end
degrees = Graphs.degree(G)[end:-1:1];
if !issorted(degrees) return res; end
if is_connected(G)
push!(res, F);
if degrees[1] < degrees[2] push!(res, [F;(n-1,n)]); end
else
if degrees[1] >= degrees[2] return res; end
add_edge!(G, n-1, n);
if is_connected(G)
push!(res, [F;(n-1,n)]);
end
end
return res;
else
res = Set();
if j == n
union!(res, enum_connected_graphs(n,(i+1,i+2), F));
union!(res, enum_connected_graphs(n, (i+1,i+2), [F;(i,j)]));
else
union!(res, enum_connected_graphs(n,(i,j+1), F));
union!(res, enum_connected_graphs(n, (i,j+1), [F;(i,j)]));
end
return res;
end
end
"""
get_all_ratios(n,k)
Get the ratios for all connected graphs with n vertices and at most k distinct points per uncertainty set
"""
function get_all_ratios(n)
all_graphs = enum_connected_graphs(n,(1,2),[]);
ratios = [];
for F in all_graphs
println("\n---------------------------");
println("Get the ratio of graph:");
println("\t F = ", F);
rho = min_ratio_exact(n,F);
push!(ratios, 1.0/rho);
println("maximum factor : ", 1.0/rho);
end
return ratios;
end
function get_all_ratios_adjlist(n)
all_graphs = enum_connected_graphs(n,(1,2),[]);
ratios = [];
for F in all_graphs
println("\n---------------------------");
println("Get the ratio of graph:");
println("\t F = ", F);
rho = min_ratio_exact(n,F);
push!(ratios, 1.0/rho);
println("maximum factor : ", 1.0/rho);
end
return ratios;
end