-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03-test-mip.jl
196 lines (163 loc) · 5.67 KB
/
03-test-mip.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
# using Infiltrator
# using Revise
using OPAC
using Graphs
using Serialization
using Printf
using MetaGraphs
using JuMP
# using Cthulhu
const air_cost=1_000
const air_cap=10_000
const L=4000
const inst_ind = 1
const inst_list = sort(filter(map(readdir("instances")) do x
n,m = parse.(Int, split(x[1:end-4], "-")[2:end])
(n=n, m=m, fn="instances/"*x)
end) do x
x.n <= 51 && x.m <= 1000
end, by=x->(x.n, x.m))
function load_inst(i, s, P)
inst = load_instance(i.fn)
ag, a2l, l2a = mk_air_graph(i.n, i.n, s)
mg = combgraph(inst.lg, ag, a2l, P; download_cost=inst.download_cost, upload_cost=inst.upload_cost)
inst, P, mg, ag, l2a
end
obj_if(model) = if termination_status(model) == MOI.OPTIMAL
objective_value(model), 0.0
else
if has_values(model)
objective_value(model), relative_gap(model)
else
-1, relative_gap(model)
end
end
function fun_benders_gen(inst, P, mg, ag, l2a, m)
model = master_colgen(inst.D, P, mg, inst.lg, ag, l2a;
air_cost=air_cost, air_cap=air_cap, L=L,
model=m, download_cost=inst.download_cost, upload_cost=inst.upload_cost)
obj_if(model)
end
function fun_benders(inst, P, mg, ag, l2a, m, im)
model = master_problem(inst.D, P, inst.lg, ag, l2a;
air_cost=air_cost, air_cap=air_cap, L=L,
model=m, download_cost=inst.download_cost, upload_cost=inst.upload_cost, inner_model=im)
obj_if(model)
end
function fun_native(inst, P, mg, ag, l2a, m)
model = solve(inst.D, P, inst.lg, ag, l2a, air_cap=air_cap, L=L,
air_cost=air_cost, model=m(), upload_cost=inst.upload_cost, download_cost=inst.download_cost)
obj_if(model)
end
function show_table(res; show_loss=false)
k = ["BD", "BGG", "BGC", "Gurobi", "CPLEX"]
join([string(hk.n) * " & " * string(hk.m) * " & " * string(hk.s) * " & " * string(hk.p) * " & " * join([
begin
j = (mode=i, hk...)
if j ∈ keys(res)
l = res[j]
if show_loss
if l.val[2] == -1.0 # optimal
""
else
@sprintf " %.3e " l.val[1]
end
else
if l.val[2] == 0.0 # optimal
@sprintf " %.1f " l.time
elseif l.val[1] == -1.0 || l.val[2] >= 1 # infeasible
""
else
@sprintf " %.1f\\,\\si{\\percent} " (l.val[2] * 100)
end
end
else
""
end
end
for i in k], " & ") * " \\\\ \n"
for hk in sort(unique([(n=k.n, m=k.m, s=k.s, p=k.p)
for k in keys(res)]), by=x->(x.n, x.m, -x.s, x.p))])
end
# warmup
@time let (inst, P, mg, ag, l2a) = load_inst(inst_list[inst_ind], 5, 3)
fun_benders_gen(inst, 3, mg, ag, l2a, multiflow_gurobi())
for im in [multiflow_gurobi, multiflow_cplex]
fun_benders(inst, 3, mg, ag, l2a, multiflow_gurobi(), im)
fun_native(inst, 3, mg, ag, l2a, im)
end
end
function main(inst_list, fn, res)
for i in inst_list,
s in [5, 10],
p in [1, 5, 10]
let kn = (mode="BD", n=i.n, m=i.m, s=s, p=p)
if kn ∉ keys(res)
try
t = @elapsed val = let (inst, P, mg, ag, l2a) = load_inst(i, 5, p)
fun_benders_gen(inst, P, mg, ag, l2a, multiflow_gurobi())
end
@show kn, t
res[kn] = (time=t, val=val)
fn !== nothing && serialize(fn, res)
catch e
@show e
end
end
end
for (m, ms) in [(multiflow_gurobi, "G")],
(im, ims) in zip([multiflow_gurobi, multiflow_cplex], ["G", "C"])
let kn = (mode="B"*ms*ims, n=i.n, m=i.m, s=s, p=p)
if kn ∉ keys(res)
try
inst = load_instance(i.fn)
t = @elapsed val = let (inst, P, mg, ag, l2a) = load_inst(i, 5, p)
fun_benders(inst, P, mg, ag, l2a, m(), im)
end
@show kn, t
res[kn] = (time=t, val=val)
fn !== nothing && serialize(fn, res)
catch e
@show e
end
end
end
end
for (m, ms) in zip([multiflow_gurobi, multiflow_cplex], ["Gurobi", "CPLEX"])
let kn = (mode=ms, n=i.n, m=i.m, s=s, p=p)
if kn ∉ keys(res)
try
inst = load_instance(i.fn)
t = @elapsed val = let (inst, P, mg, ag, l2a) = load_inst(i, 5, p)
fun_native(inst, P, mg, ag, l2a, m)
end
@show kn, t
res[kn] = (time=t, val=val)
fn !== nothing && serialize(fn, res)
catch e
@show e
end
end
end
end
w1 = show_table(deepcopy(res); show_loss=false)
println(w1)
w2 = show_table(deepcopy(res); show_loss=true)
if fn !== nothing
open(fn *"-t.txt", "w") do fd
println(fd, w1)
end
open(fn *"-l.txt", "w") do fd
println(fd, w2)
end
end
end
res
end
main(inst_list[[1]], nothing, Dict())
main(inst_list, "runs.jld", try
deserialize("runs.jld")
catch e
println("ignoring error ", e)
Dict()
end)