-
Notifications
You must be signed in to change notification settings - Fork 37
/
MOIWrapper.jl
843 lines (754 loc) · 28.7 KB
/
MOIWrapper.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
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
using LinQuadOptInterface
const LQOI = LinQuadOptInterface
const MOI = LQOI.MOI
const SUPPORTED_OBJECTIVES = [
LQOI.SinVar,
LQOI.Linear
]
const SUPPORTED_CONSTRAINTS = [
(LQOI.Linear, LQOI.EQ),
(LQOI.Linear, LQOI.LE),
(LQOI.Linear, LQOI.GE),
(LQOI.Linear, LQOI.IV),
(LQOI.SinVar, LQOI.EQ),
(LQOI.SinVar, LQOI.LE),
(LQOI.SinVar, LQOI.GE),
(LQOI.SinVar, LQOI.IV),
(LQOI.VecVar, MOI.Nonnegatives),
(LQOI.VecVar, MOI.Nonpositives),
(LQOI.VecVar, MOI.Zeros),
(LQOI.VecLin, MOI.Nonnegatives),
(LQOI.VecLin, MOI.Nonpositives),
(LQOI.VecLin, MOI.Zeros),
(LQOI.SinVar, MOI.ZeroOne),
(LQOI.SinVar, MOI.Integer)
]
"""
AbstractCallbackData
An abstract type to prevent recursive type definition of Optimizer and
CallbackData, each of which need the other type in a field.
"""
abstract type AbstractCallbackData end
mutable struct Optimizer <: LQOI.LinQuadOptimizer
LQOI.@LinQuadOptimizerBase
presolve::Bool
method::Symbol
interior::GLPK.InteriorParam
intopt::GLPK.IntoptParam
simplex::SimplexParam
solver_status::Int32
last_solved_by_mip::Bool
# See note associated with abstractcallbackdata. When using, make sure to
# add a type assertion, since this will always be the concrete type
# CallbackData.
callback_data::AbstractCallbackData
objective_bound::Float64
callback_function::Function
# See https://github.com/JuliaOpt/GLPKMathProgInterface.jl/pull/15
# for why this is necesary. GLPK interacts weirdly with binary variables and
# bound modification. So lets set binary variables as "Integer" with [0,1]
# bounds that we enforce just before solve.
binaries::Vector{Int}
Optimizer(::Nothing) = new()
end
"""
CallbackData
"""
mutable struct CallbackData <: AbstractCallbackData
model::Optimizer
tree::Ptr{Cvoid}
CallbackData(model::Optimizer) = new(model, C_NULL)
end
"""
__internal_callback__(tree::Ptr{Cvoid}, info::Ptr{Cvoid})
Dummy callback function for internal use only. Responsible for updating the
objective bound.
"""
function __internal_callback__(tree::Ptr{Cvoid}, info::Ptr{Cvoid})
callback_data = unsafe_pointer_to_objref(info)::CallbackData
model = callback_data.model
callback_data.tree = tree
node = GLPK.ios_best_node(tree)
if node != 0
model.objective_bound = GLPK.ios_node_bound(tree, node)
end
model.callback_function(callback_data)
return nothing
end
function Optimizer(;presolve=false, method=:Simplex, kwargs...)
optimizer = Optimizer(nothing)
MOI.empty!(optimizer)
optimizer.presolve = presolve
optimizer.method = method
optimizer.interior = GLPK.InteriorParam()
optimizer.intopt = GLPK.IntoptParam()
optimizer.simplex = GLPK.SimplexParam()
solver_status = Int32(0)
optimizer.last_solved_by_mip = false
optimizer.callback_data = CallbackData(optimizer)
# if VERSION >= v"0.7-"
# optimizer.intopt.cb_func = @cfunction(__internal_callback__, Cvoid, Tuple{Ptr{Cvoid}, Ptr{Cvoid}})
# else
optimizer.intopt.cb_func = @cfunction(__internal_callback__, Cvoid, (Ptr{Cvoid}, Ptr{Cvoid}))
# end
optimizer.intopt.cb_info = pointer_from_objref(optimizer.callback_data)
optimizer.objective_bound = NaN
optimizer.callback_function = (cb_data::CallbackData) -> nothing
optimizer.binaries = Int[]
# Parameters
if presolve
optimizer.simplex.presolve = GLPK.ON
optimizer.intopt.presolve = GLPK.ON
end
optimizer.interior.msg_lev = GLPK.MSG_ERR
optimizer.intopt.msg_lev = GLPK.MSG_ERR
optimizer.simplex.msg_lev = GLPK.MSG_ERR
for (key, value) in kwargs
set_interior = set_parameter(optimizer.interior, key, value)
set_intopt = set_parameter(optimizer.intopt, key, value)
set_simplex = set_parameter(optimizer.simplex, key, value)
if !set_interior && !set_intopt && !set_simplex
warn("Ignoring option: $(key) => $(value)")
end
end
return optimizer
end
function LQOI.get_objective_bound(model::Optimizer)
if !model.last_solved_by_mip
return LQOI.get_objectivesense(model) == MOI.MinSense ? -Inf : Inf
end
constant = LQOI.get_constant_objective(model)
# @mlubin and @ccoey observed some cases where mip_status == OPT and objval
# and objbound didn't match. In that case, they return mip_obj_val, but
# objbound may still be incorrect in cases where GLPK terminates early.
if GLPK.mip_status(model.inner) == GLPK.OPT
return GLPK.mip_obj_val(model.inner) + constant
else
return model.objective_bound + constant
end
end
MOI.canget(::Optimizer, ::MOI.RelativeGap) = false
# Only available from log, not programatically?
MOI.canget(::Optimizer, ::MOI.SimplexIterations) = false
MOI.canget(::Optimizer, ::MOI.BarrierIterations) = false
MOI.canget(::Optimizer, ::MOI.NodeCount) = false
"""
set_parameter(param_store, key::Symbol, value)
Set the field name `key` in a `param_store` type (that is one of `InteriorParam`,
`IntoptParam`, or `SimplexParam`) to `value`.
"""
function set_parameter(param_store, key::Symbol, value)
if key in [:cb_func, :cb_info]
Compat.@warn("Ignored option: $(string(k)). Use the MOI attribute " *
"`GLPK.CallbackFunction` instead.")
return true
end
if key in fieldnames(typeof(param_store))
field_type = typeof(getfield(param_store, key))
setfield!(param_store, key, convert(field_type, value))
return true
else
return false
end
end
LQOI.LinearQuadraticModel(::Type{Optimizer}, env) = GLPK.Prob()
LQOI.supported_objectives(model::Optimizer) = SUPPORTED_OBJECTIVES
LQOI.supported_constraints(model::Optimizer) = SUPPORTED_CONSTRAINTS
function LQOI.set_constant_objective!(model::Optimizer, value)
GLPK.set_obj_coef(model.inner, 0, value)
end
LQOI.get_constant_objective(model::Optimizer) = GLPK.get_obj_coef(model.inner, 0)
"""
get_col_bound_type(lower::Float64, upper::Float64)
Return the GLPK type of the variable bound given a lower bound of `lower` and an
upper bound of `upper`.
"""
function get_col_bound_type(lower::Float64, upper::Float64)
GLPK_INFINITY = VERSION >= v"0.7-" ? floatmax(Float64) : realmax(Float64)
if lower == upper
return GLPK.FX
elseif lower <= -GLPK_INFINITY
return upper >= GLPK_INFINITY ? GLPK.FR : GLPK.UP
else
return upper >= GLPK_INFINITY ? GLPK.LO : GLPK.DB
end
end
"""
set_variable_bound(model::Optimizer, column::Int, lower::Float64, upper::Float64)
Set the bounds of the variable in column `column` to `[lower, upper]`.
"""
function set_variable_bound(model::Optimizer, column::Int, lower::Float64,
upper::Float64)
bound_type = get_col_bound_type(lower, upper)
GLPK.set_col_bnds(model.inner, column, bound_type, lower, upper)
end
function LQOI.change_variable_bounds!(model::Optimizer,
columns::Vector{Int}, new_bounds::Vector{Float64},
senses::Vector{Cchar})
for (column, bound, sense) in zip(columns, new_bounds, senses)
if sense == Cchar('L')
lower_bound = bound
upper_bound = GLPK.get_col_ub(model.inner, column)
elseif sense == Cchar('U')
lower_bound = GLPK.get_col_lb(model.inner, column)
upper_bound = bound
else
error("Invalid variable bound sense: $(sense)")
end
set_variable_bound(model, column, lower_bound, upper_bound)
end
end
function LQOI.get_variable_lowerbound(model::Optimizer, col)
GLPK.get_col_lb(model.inner, col)
end
function LQOI.get_variable_upperbound(model::Optimizer, col)
GLPK.get_col_ub(model.inner, col)
end
function LQOI.get_number_linear_constraints(model::Optimizer)
GLPK.get_num_rows(model.inner)
end
function LQOI.add_linear_constraints!(model::Optimizer,
A::LQOI.CSRMatrix{Float64}, senses::Vector{Cchar}, rhs::Vector{Float64})
nrows = length(rhs)
if nrows <= 0
error("Number of rows must be more than zero.")
elseif nrows == 1
add_row!(model.inner, A.columns, A.coefficients, senses[1], rhs[1])
else
push!(A.row_pointers, length(A.columns)+1)
for i in 1:nrows
indices = A.row_pointers[i]:A.row_pointers[i+1]-1
add_row!(model.inner, A.columns[indices], A.coefficients[indices],
senses[i], rhs[i])
end
pop!(A.row_pointers)
end
end
function LQOI.add_ranged_constraints!(model::Optimizer,
A::LQOI.CSRMatrix{Float64}, lowerbound::Vector{Float64},
upperbound::Vector{Float64})
row1 = GLPK.get_num_rows(model.inner)
LQOI.add_linear_constraints!(model, A,
fill(Cchar('R'), length(lowerbound)),
lowerbound)
row2 = GLPK.get_num_rows(model.inner)
for (row, lower, upper) in zip(row1+1:row2, lowerbound, upperbound)
GLPK.set_row_bnds(model.inner, row, GLPK.DB, lower, upper)
end
end
function LQOI.modify_ranged_constraints!(model::Optimizer,
rows::Vector{Int}, lowerbounds::Vector{Float64},
upperbounds::Vector{Float64})
for (row, lower, upper) in zip(rows, lowerbounds, upperbounds)
LQOI.change_rhs_coefficient!(model, row, lower)
GLPK.set_row_bnds(model.inner, row, GLPK.DB, lower, upper)
end
end
function LQOI.get_range(model::Optimizer, row::Int)
GLPK.get_row_lb(model.inner, row), GLPK.get_row_ub(model.inner, row)
end
"""
add_row!(problem::GLPK.Prob, columns::Vector{Int},
coefficients::Vector{Float64}, sense::Cchar, rhs::Real)
Helper function to add a row to the problem. Sense must be one of `'E'` (ax == b),
`'G'` (ax >= b), `'L'` (ax <= b) , or `'R'` (b <= ax).
If the sense is `'R'` the `rhs` should be the lower bound, and the bounds should
be set in a new API call to enforce the upper bound.
"""
function add_row!(problem::GLPK.Prob, columns::Vector{Int},
coefficients::Vector{Float64}, sense::Cchar, rhs::Real)
if length(columns) != length(coefficients)
error("columns and coefficients have different lengths.")
end
GLPK.add_rows(problem, 1)
num_rows = GLPK.get_num_rows(problem)
GLPK.set_mat_row(problem, num_rows, columns, coefficients)
# According to http://most.ccib.rutgers.edu/glpk.pdf page 22,
# the `lb` argument is ignored for constraint types with no
# lower bound (GLPK.UP) and the `ub` argument is ignored for
# constraint types with no upper bound (GLPK.LO). We pass
# ±DBL_MAX for those unused bounds since (a) we have to pass
# something, and (b) it is consistent with the other usages of
# ±DBL_MAX to represent infinite bounds in the rest of the
# GLPK interface.
if sense == Cchar('E')
GLPK.set_row_bnds(problem, num_rows, GLPK.FX, rhs, rhs)
elseif sense == Cchar('G')
GLPK.set_row_bnds(problem, num_rows, GLPK.LO, rhs, GLPK.DBL_MAX)
elseif sense == Cchar('L')
GLPK.set_row_bnds(problem, num_rows, GLPK.UP, -GLPK.DBL_MAX, rhs)
elseif sense == Cchar('R')
GLPK.set_row_bnds(problem, num_rows, GLPK.DB, rhs, GLPK.DBL_MAX)
else
error("Invalid row sense: $(sense)")
end
end
function LQOI.get_rhs(model::Optimizer, row)
sense = GLPK.get_row_type(model.inner, row)
if sense == GLPK.LO || sense == GLPK.FX || sense == GLPK.DB
return GLPK.get_row_lb(model.inner, row)
else
return GLPK.get_row_ub(model.inner, row)
end
end
function LQOI.get_linear_constraint(model::Optimizer, row::Int)
# note: we return 1-indexed columns here
return GLPK.get_mat_row(model.inner, row)
end
function LQOI.change_rhs_coefficient!(model::Optimizer, row::Int,
rhs::Real)
current_lower = GLPK.get_row_lb(model.inner, row)
current_upper = GLPK.get_row_ub(model.inner, row)
# `get_row_lb` and `get_row_ub` return ±DBL_MAX for rows with no
# lower or upper bound. See page 30 of the GLPK user manual
# http://most.ccib.rutgers.edu/glpk.pdf
if current_lower == current_upper
GLPK.set_row_bnds(model.inner, row, GLPK.FX, rhs, rhs)
elseif current_lower > -GLPK.DBL_MAX && current_upper < GLPK.DBL_MAX
GLPK.set_row_bnds(model.inner, row, GLPK.FX, rhs, rhs)
elseif current_lower > -GLPK.DBL_MAX
GLPK.set_row_bnds(model.inner, row, GLPK.LO, rhs, GLPK.DBL_MAX)
elseif current_upper < GLPK.DBL_MAX
GLPK.set_row_bnds(model.inner, row, GLPK.UP, -GLPK.DBL_MAX, rhs)
else
error("Cannot set right-hand side of a free constraint.")
end
end
function LQOI.change_objective_coefficient!(model::Optimizer, col, coef)
GLPK.set_obj_coef(model.inner, col, coef)
end
function LQOI.change_matrix_coefficient!(model::Optimizer, row, col, coef)
columns, coefficients = GLPK.get_mat_row(model.inner, row)
index = something(findfirst(isequal(col), columns), 0)
if index > 0
coefficients[index] = coef
else
push!(columns, col)
push!(coefficients, coef)
end
GLPK.set_mat_row(model.inner, row, columns, coefficients)
end
function LQOI.delete_linear_constraints!(model::Optimizer, start_row, stop_row)
GLPK.std_basis(model.inner)
indices = collect(start_row:stop_row)
GLPK.del_rows(model.inner, length(indices), indices)
end
function LQOI.change_variable_types!(model::Optimizer,
columns::Vector{Int}, variable_types::Vector)
model.binaries = Int[]
for (column, variable_type) in zip(columns, variable_types)
if variable_type == Cint('I')
GLPK.set_col_kind(model.inner, column, GLPK.IV)
elseif variable_type == Cint('C')
GLPK.set_col_kind(model.inner, column, GLPK.CV)
elseif variable_type == Cint('B')
# note: we lie to GLPK here and set it as an integer variable. See
# the comment in the definition of Optimizer.
GLPK.set_col_kind(model.inner, column, GLPK.IV)
push!(model.binaries, column)
else
error("Invalid variable type: $(vtype).")
end
end
end
function LQOI.change_linear_constraint_sense!(model::Optimizer, rows, senses)
for (row, sense) in zip(rows, senses)
change_row_sense!(model, row, sense)
end
end
"""
change_row_sense!(model::Optimizer, row, sense)
Convert a linear constraint into another type of linear constraint by changing
the comparison sense.
Constraint types supported are 'E' (equality), 'L' (less-than), and 'G'
(greater-than).
For example, `ax <= b` can become `ax >= b` or `ax == b`.
"""
function change_row_sense!(model::Optimizer, row::Int, sense)
old_sense = GLPK.get_row_type(model.inner, row)
if old_sense == GLPK.DB
error("Cannot transform sense of an interval constraint.")
elseif old_sense == GLPK.FR
error("Cannot transform sense of a free constraint.")
end
new_sense = ROW_SENSE_MAP[sense]
if old_sense == new_sense
error("Cannot transform constraint with same sense.")
elseif new_sense == GLPK.DB
error("Cannot transform constraint to ranged constraint.")
end
if old_sense == GLPK.FX || old_sense == GLPK.LO
right_hand_side = GLPK.get_row_lb(model.inner, row)
else
right_hand_side = GLPK.get_row_ub(model.inner, row)
end
# According to http://most.ccib.rutgers.edu/glpk.pdf page 22,
# the `lb` argument is ignored for constraint types with no
# lower bound (GLPK.UP) and the `ub` argument is ignored for
# constraint types with no upper bound (GLPK.LO). We pass
# ±DBL_MAX for those unused bounds since (a) we have to pass
# something, and (b) it is consistent with the other usages of
# ±DBL_MAX to represent infinite bounds in the rest of the
# GLPK interface.
if new_sense == GLPK.FX
GLPK.set_row_bnds(model.inner, row, new_sense, right_hand_side, right_hand_side)
elseif new_sense == GLPK.LO
GLPK.set_row_bnds(model.inner, row, new_sense, right_hand_side, GLPK.DBL_MAX)
elseif new_sense == GLPK.UP
GLPK.set_row_bnds(model.inner, row, new_sense, -GLPK.DBL_MAX, right_hand_side)
end
end
const ROW_SENSE_MAP = Dict(
Cchar('E') => GLPK.FX,
Cchar('R') => GLPK.DB,
Cchar('L') => GLPK.UP,
Cchar('G') => GLPK.LO
)
function LQOI.add_sos_constraint!(model::Optimizer, columns, weights, sos_type)
GLPK.add_sos!(instance.inner, sos_type, columns, weights)
end
function LQOI.set_linear_objective!(model::Optimizer, columns, coefficients)
ncols = GLPK.get_num_cols(model.inner)
new_coefficients = zeros(ncols)
for (col, coef) in zip(columns, coefficients)
new_coefficients[col] += coef
end
for (col, coef) in zip(1:ncols, new_coefficients)
GLPK.set_obj_coef(model.inner, col, coef)
end
end
function LQOI.change_objective_sense!(model::Optimizer, sense)
if sense == :min
GLPK.set_obj_dir(model.inner, GLPK.MIN)
elseif sense == :max
GLPK.set_obj_dir(model.inner, GLPK.MAX)
else
error("Invalid objective sense: $(sense)")
end
end
function LQOI.get_linear_objective!(model::Optimizer, x::Vector{Float64})
@assert length(x) == GLPK.get_num_cols(model.inner)
for col in 1:length(x)
x[col] = GLPK.get_obj_coef(model.inner, col)
end
end
function LQOI.get_objectivesense(model::Optimizer)
sense = GLPK.get_obj_dir(model.inner)
if sense == GLPK.MIN
return MOI.MinSense
elseif sense == GLPK.MAX
return MOI.MaxSense
else
error("Invalid objective sense: $(sense)")
end
end
function LQOI.get_number_variables(model::Optimizer)
GLPK.get_num_cols(model.inner)
end
function LQOI.add_variables!(model::Optimizer, number_to_add::Int)
num_variables = GLPK.get_num_cols(model.inner)
GLPK.add_cols(model.inner, number_to_add)
for i in 1:number_to_add
GLPK.set_col_bnds(model.inner, num_variables+i, GLPK.FR, 0.0, 0.0)
end
end
function LQOI.delete_variables!(model::Optimizer, col, col2)
GLPK.std_basis(model.inner)
columns = collect(col:col2)
GLPK.del_cols(model.inner, length(columns), columns)
end
"""
_certificates_potentially_available(model::Optimizer)
Return true if an infeasiblity certificate or an unbounded ray is potentially
available (i.e., the model has been solved using either the Simplex or Exact
methods).
"""
function _certificates_potentially_available(model::Optimizer)
!model.last_solved_by_mip && (model.method == :Simplex || model.method == :Exact)
end
function LQOI.get_termination_status(model::Optimizer)
if model.last_solved_by_mip
if model.solver_status in [GLPK.EMIPGAP, GLPK.ETMLIM, GLPK.ESTOP]
return MOI.OtherLimit
end
end
status = get_status(model)
if status == GLPK.OPT
return MOI.Success
elseif status == GLPK.INFEAS
if _certificates_potentially_available(model)
return MOI.Success
else
return MOI.InfeasibleNoResult
end
elseif status == GLPK.UNBND
if _certificates_potentially_available(model)
return MOI.Success
else
return MOI.UnboundedNoResult
end
elseif status == GLPK.FEAS
return MOI.SlowProgress
elseif status == GLPK.NOFEAS
return MOI.InfeasibleOrUnbounded
elseif status == GLPK.UNDEF
return MOI.OtherError
else
error("Invalid termination status: $(status)")
end
end
"""
get_status(model::Optimizer)
Get the status from GLPK depending on which method was used to solve the model.
"""
function get_status(model::Optimizer)
if model.last_solved_by_mip
return GLPK.mip_status(model.inner)
else
if model.method == :Simplex || model.method == :Exact
return GLPK.get_status(model.inner)
elseif model.method == :InteriorPoint
return GLPK.ipt_status(model.inner)
end
_throw_invalid_method(model)
end
end
function LQOI.get_primal_status(model::Optimizer)
status = get_status(model)
if status == GLPK.OPT
return MOI.FeasiblePoint
elseif status == GLPK.UNBND && _certificates_potentially_available(model)
return MOI.InfeasibilityCertificate
end
return MOI.UnknownResultStatus
end
function LQOI.get_dual_status(model::Optimizer)
if !model.last_solved_by_mip
status = get_status(model.inner)
if status == GLPK.OPT
return MOI.FeasiblePoint
elseif status == GLPK.INFEAS && _certificates_potentially_available(model)
return MOI.InfeasibilityCertificate
end
end
return MOI.UnknownResultStatus
end
"""
copy_function_result!(dest::Vector, foo, model::GLPK.Prob)
A helper function that loops through the indices in `dest` and stores the result
of `foo(model, i)` for the `i`th index.
"""
function copy_function_result!(dest::Vector, foo, model::GLPK.Prob)
for i in eachindex(dest)
dest[i] = foo(model, i)
end
end
function copy_function_result!(dest::Vector, foo, model::Optimizer)
copy_function_result!(dest, foo, model.inner)
end
"""
_throw_invalid_method(instance::Optimizer)
A helper function to throw an error when the method is set incorrectly. Mainly
used to enforce type-stability in functions that have a run-time switch on the
method.
"""
function _throw_invalid_method(instance::Optimizer)
error("Method is $(instance.method), but it must be one of :Simplex, " *
":Exact, or :InteriorPoint.")
end
function LQOI.get_variable_primal_solution!(model::Optimizer, place)
if model.last_solved_by_mip
copy_function_result!(place, GLPK.mip_col_val, model)
else
if model.method == :Simplex || model.method == :Exact
copy_function_result!(place, GLPK.get_col_prim, model)
elseif model.method == :InteriorPoint
copy_function_result!(place, GLPK.ipt_col_prim, model)
else
_throw_invalid_method(model)
end
end
end
function LQOI.get_linear_primal_solution!(model::Optimizer, place)
if model.last_solved_by_mip
copy_function_result!(place, GLPK.mip_row_val, model)
else
if model.method == :Simplex || model.method == :Exact
copy_function_result!(place, GLPK.get_row_prim, model)
elseif model.method == :InteriorPoint
copy_function_result!(place, GLPK.ipt_row_prim, model)
else
_throw_invalid_method(model)
end
end
end
function LQOI.get_variable_dual_solution!(model::Optimizer, place)
@assert !model.last_solved_by_mip
if model.method == :Simplex || model.method == :Exact
copy_function_result!(place, GLPK.get_col_dual, model)
elseif model.method == :InteriorPoint
copy_function_result!(place, GLPK.ipt_col_dual, model)
else
_throw_invalid_method(model)
end
end
function LQOI.get_linear_dual_solution!(model::Optimizer, place)
@assert !model.last_solved_by_mip
if model.method == :Simplex || model.method == :Exact
copy_function_result!(place, GLPK.get_row_dual, model)
elseif model.method == :InteriorPoint
copy_function_result!(place, GLPK.ipt_row_dual, model)
else
_throw_invalid_method(model)
end
end
function LQOI.get_objective_value(model::Optimizer)
if model.last_solved_by_mip
return GLPK.mip_obj_val(model.inner)
else
if model.method == :Simplex || model.method == :Exact
return GLPK.get_obj_val(model.inner)
elseif model.method == :InteriorPoint
return GLPK.ipt_obj_val(model.inner)
end
_throw_invalid_method(model)
end
end
function LQOI.solve_linear_problem!(model::Optimizer)
model.last_solved_by_mip = false
if model.method == :Simplex
model.solver_status = GLPK.simplex(model.inner, model.simplex)
elseif model.method == :Exact
model.solver_status = GLPK.exact(model.inner, model.simplex)
elseif model.method == :InteriorPoint
model.solver_status = GLPK.interior(model.inner, model.interior)
else
_throw_invalid_method(model)
end
end
"""
round_bounds_to_integer(model)::Tuple{Bool, Vector{Float64}, Vector{Float64}}
GLPK does not allow integer variables with fractional bounds. Therefore, we
round the bounds of binary and integer variables to integer values prior to
solving.
Returns a tuple of the original bounds, along with a Boolean flag indicating if
they need to be reset after solve.
"""
function round_bounds_to_integer(model::Optimizer)
num_variables = GLPK.get_num_cols(model.inner)
lower_bounds = map(i->GLPK.get_col_lb(model.inner, i), 1:num_variables)
upper_bounds = map(i->GLPK.get_col_ub(model.inner, i), 1:num_variables)
variable_types = get_variable_types(model)
bounds_modified = false
for (col, variable_type) in enumerate(variable_types)
new_lower = ceil(lower_bounds[col])
new_upper = floor(upper_bounds[col])
if variable_type == :Bin
new_lower = max(0.0, ceil(lower_bounds[col]))
new_upper = min(1.0, floor(upper_bounds[col]))
elseif variable_type != :Int
continue
end
if lower_bounds[col] != new_lower || upper_bounds[col] != new_upper
set_variable_bound(model, col, new_lower, new_upper)
bounds_modified = true
end
end
return bounds_modified, lower_bounds, upper_bounds
end
function LQOI.solve_mip_problem!(model::Optimizer)
bounds_modified, lower_bounds, upper_bounds = round_bounds_to_integer(model)
try
if model.intopt.presolve == GLPK.OFF
status = GLPK.simplex(model.inner, model.simplex)
if status != 0
model.last_solved_by_mip = false
model.solver_status = status
return
end
end
model.solver_status = GLPK.intopt(model.inner, model.intopt)
model.last_solved_by_mip = true
finally
if bounds_modified
for (col, (lower, upper)) in enumerate(zip(lower_bounds, upper_bounds))
set_variable_bound(model, col, lower, upper)
end
end
end
end
const VARIABLE_TYPE_MAP = Dict(
GLPK.CV => :Cont,
GLPK.IV => :Int,
GLPK.BV => :Bin
)
"""
get_variable_types(model::Optimizer)
Return a vector of symbols (one element for each variable) of the variable type.
The symbols are given by the key-value mapping in `GLPK.VARIABLE_TYPE_MAP`.
"""
function get_variable_types(model::Optimizer)
ncol = GLPK.get_num_cols(model.inner)
col_types = fill(:Cont, ncol)
for i in 1:ncol
col_type = GLPK.get_col_kind(model.inner, i)
col_types[i] = VARIABLE_TYPE_MAP[col_type]
if i in model.binaries
# See the note in the definition of Optimizer about this.
col_types[i] = :Bin
elseif col_types[i] == :Bin
# We never set a variable as binary. GLPK must have made a mistake
# and inferred so based on bounds?
col_types[i] = :Int
end
end
return col_types
end
include("infeasibility_certificates.jl")
function LQOI.get_farkas_dual!(model::Optimizer, place)
get_infeasibility_ray(model, place)
end
function LQOI.get_unbounded_ray!(model::Optimizer, place)
get_unbounded_ray(model, place)
end
# ==============================================================================
# Callbacks in GLPK
# ==============================================================================
"""
CallbackFunction
The attribute to set the callback function in GLPK. The function takes a single
argument of type `CallbackData`.
"""
struct CallbackFunction <: MOI.AbstractOptimizerAttribute end
function MOI.set!(model::Optimizer, ::CallbackFunction, foo::Function)
model.callback_function = foo
end
"""
load_variable_primal!(cb_data::CallbackData)
Load the variable primal solution in a callback.
This can only be called in a callback from `GLPK.IROWGEN`. After it is called,
you can access the `VariablePrimal` attribute as usual.
"""
function load_variable_primal!(cb_data::CallbackData)
model = cb_data.model
if GLPK.ios_reason(cb_data.tree) != GLPK.IROWGEN
error("load_variable_primal! can only be called when reason is GLPK.IROWGEN.")
end
subproblem = GLPK.ios_get_prob(cb_data.tree)
copy_function_result!(model.variable_primal_solution, GLPK.get_col_prim, subproblem)
end
"""
add_lazy_constraint!(cb_data::GLPK.CallbackData, func::LQOI.Linear, set::S) where S <: Union{LQOI.LE, LQOI.GE, LQOI.EQ}
Add a lazy constraint to the model `cb_data.model`. This can only be called in a
callback from `GLPK.IROWGEN`.
"""
function add_lazy_constraint!(cb_data::CallbackData, func::LQOI.Linear, set::S) where S <: Union{LQOI.LE, LQOI.GE, LQOI.EQ}
model = cb_data.model
add_row!(
GLPK.ios_get_prob(cb_data.tree),
[LQOI.get_column(model, term.variable_index) for term in func.terms],
[term.coefficient for term in func.terms],
LQOI.backend_type(model, set),
MOI.Utilities.getconstant(set)
)
end