/
variable_bounds.jl
467 lines (409 loc) · 18.9 KB
/
variable_bounds.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
"""
init_tight_bound(m::Optimizer)
Initialize internal bound vectors (placeholders) to be used in other places.
In this case, we don't have to mess with the original bound information.
"""
function init_tight_bound(m::Optimizer)
m.l_var_tight = [
m.l_var_orig
fill(-Inf, m.num_var_linear_mip + m.num_var_nonlinear_mip)
]
m.u_var_tight = [
m.u_var_orig
fill(Inf, m.num_var_linear_mip + m.num_var_nonlinear_mip)
]
for i in 1:m.num_var_orig
if m.var_type_orig[i] == :Bin
m.l_var_tight[i] = 0.0
m.u_var_tight[i] = 1.0
# elseif m.var_type_orig[i] == :Int
# m.l_var_tight[i] = floor(m.l_var_tight[i])
# m.u_var_tight[i] = ceil(m.u_var_tight[i])
end
end
return
end
"""
init_disc(m::Optimizer)
This function initialize the dynamic discretization used for any bounding models. By default, it takes (.l_var_orig, .u_var_orig) as the base information. User is allowed to use alternative bounds for initializing the discretization dictionary.
The output is a dictionary with MathProgBase variable indices keys attached to the :Optimizer.discretization.
"""
function init_disc(m::Optimizer)
for var in 1:(m.num_var_orig+m.num_var_linear_mip+m.num_var_nonlinear_mip)
if m.var_type[var] in [:Bin, :Cont]
lb = m.l_var_tight[var]
ub = m.u_var_tight[var]
m.discretization[var] = [lb, ub]
else
error(
"[EXCEPTION] Unexpected variable type when initializing discretization dictionary.",
)
end
end
return
end
"""
_get_discretization_dict(m::Optimizer, lbs::Vector{Float64}, ubs::Vector{Float64})
Utility functions to convert bounds vectors to Dictionary based structures that are more suitable for
partition operations.
"""
function _get_discretization_dict(
m::Optimizer,
lbs::Vector{Float64},
ubs::Vector{Float64},
)
@assert length(lbs) == length(ubs)
var_discretization = Dict()
total_var_cnt = m.num_var_orig + m.num_var_linear_mip + m.num_var_nonlinear_mip
for var in 1:total_var_cnt
if length(lbs) == total_var_cnt
lb = lbs[var]
ub = ubs[var]
else
lb = -Inf
ub = Inf
end
var_discretization[var] = [lb, ub]
end
return var_discretization
end
"""
flatten_discretization(discretization::Dict)
Utility functions to eliminate all partition on discretizing variable and keep the loose bounds.
"""
function flatten_discretization(discretization::Dict; kwargs...)
flatten_discretization = Dict()
for var in keys(discretization)
flatten_discretization[var] = [discretization[var][1], discretization[var][end]]
end
return flatten_discretization
end
"""
detect_bound_from_aff(m::Optimizer)
Detect bounds from parse affine constraint. This function examines the one variable constraints such as
x >= 5, x <= 5 or x == 5 and fetch the information to m.l_var_tight and m.u_var_tight.
"""
function bound_propagation(m::Optimizer)
exhausted = false
infeasible = false
tol = Alp.get_option(m, :tol)
while !exhausted
exhausted = true
for aff in m.bounding_constr_mip
for i in 1:length(aff[:vars])
var_idx = aff[:vars][i].args[2]
var_coef = aff[:coefs][i]
@assert (isa(var_idx, Float64) || isa(var_idx, Int))
if aff[:sense] == :(==) && var_coef > 0.0
eval_l_bound = aff[:rhs] / var_coef
eval_u_bound = aff[:rhs] / var_coef
for j in 1:length(aff[:vars])
if j != i && aff[:coefs][j] * var_coef > 0.0 # same sign
(eval_l_bound != -Inf) && (
eval_l_bound -=
abs(aff[:coefs][j] / var_coef) *
m.u_var_tight[aff[:vars][j].args[2]]
)
(eval_u_bound != Inf) && (
eval_u_bound -=
abs(aff[:coefs][j] / var_coef) *
m.l_var_tight[aff[:vars][j].args[2]]
)
elseif j != i && aff[:coefs][j] * var_coef < 0.0 # different sign
(eval_l_bound != -Inf) && (
eval_l_bound +=
abs(aff[:coefs][j] / var_coef) *
m.l_var_tight[aff[:vars][j].args[2]]
)
(eval_u_bound != Inf) && (
eval_u_bound +=
abs(aff[:coefs][j] / var_coef) *
m.u_var_tight[aff[:vars][j].args[2]]
)
end
end
if eval_l_bound > m.l_var_tight[var_idx] + tol
exhausted = false
m.l_var_tight[var_idx] = eval_l_bound
(Alp.get_option(m, :log_level) > 199) && println(
"[VAR$(var_idx)] LB $(m.l_var_tight[var_idx]) evaluated from constraint",
)
elseif eval_l_bound > m.u_var_tight[var_idx] + tol
(Alp.get_option(m, :log_level) > 199) && println(
"[VAR$(var_idx)] Infeasibility detection during bound propagation",
)
infeasible = true
break
end
if eval_u_bound < m.u_var_tight[var_idx] - tol
exhausted = false
m.u_var_tight[var_idx] = eval_u_bound
(Alp.get_option(m, :log_level) > 199) && println(
"[VAR$(var_idx)] UB $(m.u_var_tight[var_idx]) evaluated from constraints",
)
elseif eval_u_bound < m.l_var_tight[var_idx] - tol
(Alp.get_option(m, :log_level) > 199) && println(
"[VAR$(var_idx)] Infeasibility detection during bound propagation",
)
infeasible = true
break
end
elseif aff[:sense] == :(>=) && var_coef > 0.0 # a($) - by + cz >= 100, y∈[1,10], z∈[2,50], a,b,c > 0
eval_bound = aff[:rhs] / var_coef
for j in 1:length(aff[:vars])
if j != i && aff[:coefs][j] > 0.0
eval_bound -=
abs(aff[:coefs][j] / var_coef) *
m.u_var_tight[aff[:vars][j].args[2]]
elseif j != i
aff[:coefs][j] < 0.0
eval_bound +=
abs(aff[:coefs][j] / var_coef) *
m.l_var_tight[aff[:vars][j].args[2]]
end
(eval_bound == -Inf) && break
end
if eval_bound > m.l_var_tight[var_idx] + tol
exhausted = false
m.l_var_tight[var_idx] = eval_bound
(Alp.get_option(m, :log_level) > 199) && println(
"[VAR$(var_idx)] LB $(m.l_var_tight[var_idx]) evaluated from constraints",
)
elseif eval_bound > m.u_var_tight[var_idx] + tol
(Alp.get_option(m, :log_level) > 199) && println(
"[VAR$(var_idx)] Infeasibility detection during bound propagation",
)
infeasible = true
break
end
elseif aff[:sense] == :(>=) && var_coef < 0.0 # -a($) - by + cz >= 100, y∈[1,10], z∈[2,50], a,b,c > 0
eval_bound = aff[:rhs] / var_coef
for j in 1:length(aff[:vars])
if j != i && aff[:coefs][j] > 0.0
eval_bound +=
abs(aff[:coefs][j] / var_coef) *
m.u_var_tight[aff[:vars][j].args[2]]
elseif j != i && aff[:coefs][j] < 0.0
eval_bound -=
abs(aff[:coefs][j] / var_coef) *
m.l_var_tight[aff[:vars][j].args[2]]
end
(eval_bound == Inf) && break
end
if eval_bound < m.u_var_tight[var_idx] - tol
exhausted = false
m.u_var_tight[var_idx] = eval_bound
(Alp.get_option(m, :log_level) > 199) && println(
"[VAR$(var_idx)] UB $(m.u_var_tight[var_idx]) evaluated from constraints",
)
elseif eval_bound < m.l_var_tight[var_idx] - tol
(Alp.get_option(m, :log_level) > 199) && println(
"[VAR$(var_idx)] Infeasibility detection during bound propagation",
)
infeasible = true
break
end
elseif (aff[:sense] == :(<=) && aff[:coefs][i] > 0.0) # a($) - by + cz <= 100, y∈[1,10], z∈[2,50], a,b,c > 0
eval_bound = aff[:rhs] / var_coef
for j in 1:length(aff[:vars])
if j != i && aff[:coefs][j] > 0.0
eval_bound -=
abs(aff[:coefs][j] / var_coef) *
m.l_var_tight[aff[:vars][j].args[2]]
elseif j != i && aff[:coefs][j] < 0.0
eval_bound +=
abs(aff[:coefs][j] / var_coef) *
m.u_var_tight[aff[:vars][j].args[2]]
end
(eval_bound == Inf) && break
end
if eval_bound < m.u_var_tight[var_idx] - tol
exhausted = false
m.u_var_tight[var_idx] = eval_bound
(Alp.get_option(m, :log_level) > 199) && println(
"[VAR$(var_idx)] UB $(m.u_var_tight[var_idx]) evaluated from constraints",
)
elseif eval_bound < m.l_var_tight[var_idx] - tol
(Alp.get_option(m, :log_level) > 199) && println(
"[VAR$(var_idx)] Infeasibility detection during bound propagation",
)
infeasible = true
break
end
elseif (aff[:sense] == :(<=) && aff[:coefs][i] < 0.0) # -a($) - by + cz <= 100, y∈[1,10], z∈[2,50], a,b,c > 0
eval_bound = aff[:rhs] / var_coef
for j in 1:length(aff[:vars])
if j != i && aff[:coefs][j] > 0.0
eval_bound +=
abs(aff[:coefs][j] / var_coef) *
m.l_var_tight[aff[:vars][j].args[2]]
elseif j != i && aff[:coefs][j] < 0.0
eval_bound -=
abs(aff[:coefs][j] / var_coef) *
m.u_var_tight[aff[:vars][j].args[2]]
end
(eval_bound == -Inf) && break
end
if eval_bound > m.l_var_tight[var_idx] + tol
exhausted = false
m.l_var_tight[var_idx] = eval_bound
(Alp.get_option(m, :log_level) > 199) && println(
"[VAR$(var_idx)] LB $(m.l_var_tight[var_idx]) evaluated from constraints",
)
elseif eval_bound > m.u_var_tight[var_idx] + tol
(Alp.get_option(m, :log_level) > 199) && println(
"[VAR$(var_idx)] Infeasibility detection during bound propagation",
)
infeasible = true
break
end
end
end
end
(exhausted == true && Alp.get_option(m, :log_level) > 99) &&
println("Initial constraint-based bound evaluation exhausted...")
end
if infeasible
m.status[:bounding_solve] = MOI.INFEASIBLE
@warn "[INFEASIBLE] Infeasibility detected via bound propagation"
end
return infeasible
end
"""
Recategorize :Int variables to :Bin variables if variable bounds are [0,1]
"""
function recategorize_var(m::Optimizer)
for i in 1:m.num_var_orig
if m.var_type_orig[i] == :Int && m.l_var_orig[i] == 0.0 && m.u_var_orig[i] == 1.0
m.var_type_orig[i] = :Bin
m.var_type[i] = :Bin
end
end
return
end
"""
resolve_var_bounds(m::Optimizer)
Resolve the bounds of the lifted variable using the information in l_var_tight and u_var_tight. This method only takes
in known or trivial bounds information to deduce lifted variable bounds and to potentially avoid the cases of infinity bounds.
"""
function resolve_var_bounds(m::Optimizer)
# Basic Bound propagation
if Alp.get_option(m, :presolve_bp)
setproperty!(m, :presolve_infeasible, Alp.bound_propagation(m)) # Fetch bounds from constraints
end
# Resolve unbounded variables in the original formulation
Alp.resolve_inf_bounds(m)
# Added sequential bound resolving process base on DFS process, which ensures all bounds are secured.
# Increased complexity from linear to square but a reasonable amount
# Potentially, additional mapping can be applied to reduce the complexity
for i in 1:length(m.term_seq)
k = m.term_seq[i]
if haskey(m.nonconvex_terms, k)
m.nonconvex_terms[k][:bound_resolver](m, k)
elseif haskey(m.linear_terms, k)
Alp.basic_linear_bounds(m, k)
else
error("Found homeless term key $(k) during bound resolution.")
end
end
return
end
"""
Resolving Inf variable bounds since Alpine relies on finite bounds to construct relaxations
"""
function resolve_inf_bounds(m::Optimizer)
warnuser = false
infcount_l = 0
infcount_u = 0
# Only specify necessary bounds
for i in 1:length(m.l_var_orig)
if m.l_var_tight[i] == -Inf
warnuser = true
m.l_var_tight[i] = -Alp.get_option(m, :large_bound)
infcount_l += 1
end
if m.u_var_tight[i] == Inf
warnuser = true
m.u_var_tight[i] = Alp.get_option(m, :large_bound)
infcount_u += 1
end
end
infcount = min(infcount_l, infcount_u)
if infcount == 1
warnuser && println(
"Warning: -/+Inf bounds detected on at least $infcount variable. Initializing with values -/+$(Alp.get_option(m, :large_bound)). This may affect global optimal values and run times.",
)
elseif infcount > 1
warnuser && println(
"Warning: -/+Inf bounds detected on at least $infcount variables. Initializing with values -/+$(Alp.get_option(m, :large_bound)). This may affect global optimal values and run times.",
)
end
return
end
"""
resolve_var_bounds(nonconvex_terms::Dict, discretization::Dict)
For discretization to be performed, we do not allow a variable being discretized to have infinite bounds.
The lifted/auxiliary variables may have infinite bounds and the function infers bounds on these variables. This process
can help speed up the subsequent solve times.
Only used in presolve bound tightening
"""
function resolve_var_bounds(m::Optimizer, d::Dict; kwargs...)
# Added sequential bound resolving process base on DFS process, which ensures all bounds are accurate.
# Potentially, additional mapping can be applied to reduce the complexity
for i in 1:length(m.term_seq)
k = m.term_seq[i]
if haskey(m.nonconvex_terms, k)
nlk = k
if m.nonconvex_terms[nlk][:nonlinear_type] in ALPINE_C_MONOMIAL
d = Alp.basic_monomial_bounds(m, nlk, d)
elseif m.nonconvex_terms[nlk][:nonlinear_type] in [:BINPROD]
d = Alp.basic_binprod_bounds(m, nlk, d)
elseif m.nonconvex_terms[nlk][:nonlinear_type] in [:BINLIN]
d = Alp.basic_binlin_bounds(m, nlk, d)
else
error("EXPECTED ERROR : NEED IMPLEMENTATION")
end
elseif haskey(m.linear_terms, k)
d = Alp.basic_linear_bounds(m, k, d)
else
error(
"Found a homeless term key $(k) during bound resolution im resolve_var_bounds.",
)
end
end
return d
end
"""
update_var_bounds(m::Optimizer, discretization::Dict; len::Float64=length(keys(discretization)))
This function takes in a dictionary-based discretization information and convert them into two bounds vectors (l_var, u_var) by picking the smallest and largest numbers. User can specify a certain length that may contains variables that is out of the scope of discretization.
Output::
l_var::Vector{Float64}, u_var::Vector{Float64}
"""
function update_var_bounds(discretization::Dict{Any,Any}; kwargs...)
options = Dict(kwargs)
haskey(options, :len) ? len = options[:len] : len = length(keys(discretization))
l_var = fill(-Inf, len)
u_var = fill(Inf, len)
for var_idx in keys(discretization)
l_var[var_idx] = discretization[var_idx][1]
u_var[var_idx] = discretization[var_idx][end]
end
return l_var, u_var
end
# ================================================================= unused, but needs testing
# """
# resolve_closed_var_bounds(m::Optimizer)
# This function seeks variable with tight bounds (by presolve_bt_width_tol) by checking .l_var_tight and .u_var_tight.
# If a variable is found to be within a sufficiently small interval then no discretization will be performed on this variable
# and the .discretization will be cleared with the tight bounds for basic McCormick operation if necessary.
# """
# function resolve_closed_var_bounds(m::Optimizer; kwargs...)
# for var in m.candidate_disc_vars
# if abs(m.l_var_tight[var] - m.u_var_tight[var]) <
# Alp.get_option(m, :presolve_bt_width_tol) # Closed Bound Criteria
# deleteat!(m.disc_vars, findfirst(m.disc_vars, var)) # Clean nonconvex_terms by deleting the info
# m.discretization[var] = [m.l_var_tight[var], m.u_var_tight[var]] # Clean up the discretization for basic McCormick if necessary
# end
# end
# return
# end