-
Notifications
You must be signed in to change notification settings - Fork 14
/
GLPKInterfaceLP.jl
400 lines (344 loc) · 10.6 KB
/
GLPKInterfaceLP.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
module GLPKInterfaceLP
using SparseArrays
using LinearAlgebra
import GLPK
import MathProgBase
const MPB = MathProgBase
using ..GLPKInterfaceBase
export GLPKSolverLP
mutable struct GLPKMathProgModelLP <: GLPKMathProgModel
inner::GLPK.Prob
method::Symbol
param::Union{GLPK.SimplexParam, GLPK.InteriorParam}
infeasible_bounds::Bool
end
mutable struct GLPKSolverLP <: MPB.AbstractMathProgSolver
presolve::Bool
method::Symbol
opts
function GLPKSolverLP(;presolve::Bool=false, method::Symbol=:Simplex, opts...)
method in [:Simplex, :Exact, :InteriorPoint] ||
error("""
Unknown method for GLPK LP solver: $method
Allowed methods:
:Simplex
:Exact
:InteriorPoint""")
new(presolve, method, opts)
end
end
function Base.copy(m::GLPKMathProgModelLP)
m2inner = GLPK.Prob()
GLPK.copy_prob(m2inner, m.inner, GLPK.ON)
return GLPKMathProgModelLP(m2inner, m.method, deepcopy(m.param), m.infeasible_bounds)
end
function MPB.LinearQuadraticModel(s::GLPKSolverLP)
if s.method == :Simplex || s.method == :Exact
param = GLPK.SimplexParam()
if s.presolve
param.presolve = GLPK.ON
end
elseif s.method == :InteriorPoint
param = GLPK.InteriorParam()
if s.presolve
@warn "Ignored option: presolve"
end
else
error("This is a bug")
end
param.msg_lev = GLPK.MSG_ERR
for (k,v) in s.opts
i = findfirst(x->x==k, fieldnames(typeof(param)))
if (VERSION < v"0.7-" && i > 0) || (VERSION >= v"0.7-" && i !== nothing)
t = typeof(param).types[i]
setfield!(param, i, convert(t, v))
else
@warn "Ignored option: $(string(k))"
end
end
lpm = GLPKMathProgModelLP(GLPK.Prob(), s.method, param, false)
return lpm
end
function MPB.setparameters!(s::GLPKSolverLP; mpboptions...)
opts = collect(Any, s.opts)
for (optname, optval) in mpboptions
if optname == :TimeLimit
push!(opts, (:tm_lim,round(Int,1000*optval))) # milliseconds
elseif optname == :Silent
if optval == true
push!(opts, (:msg_lev,GLPK.MSG_OFF))
end
else
error("Unrecognized parameter $optname")
end
end
s.opts = opts
nothing
end
function MPB.setparameters!(m::GLPKMathProgModelLP; mpboptions...)
for (optname, optval) in mpboptions
if optname == :TimeLimit
m.param.tm_lim = round(Int,1000*optval)
elseif optname == :Silent
if optval == true
m.param.msg_lev = GLPK.MSG_OFF
m.smplxparam.msg_lev = GLPK.MSG_OFF
end
else
error("Unrecognized parameter $optname")
end
end
end
function MPB.optimize!(lpm::GLPKMathProgModelLP)
lpm.infeasible_bounds = false
lp = lpm.inner
for c in 1:MPB.numvar(lpm)
if GLPK.get_col_lb(lp, c) > GLPK.get_col_ub(lp, c)
lpm.infeasible_bounds = true
break
end
end
if !lpm.infeasible_bounds
for r in 1:MPB.numconstr(lpm)
if GLPK.get_row_lb(lp, r) > GLPK.get_row_ub(lp, r)
lpm.infeasible_bounds = true
break
end
end
end
if !lpm.infeasible_bounds
if lpm.method == :Simplex
solve = GLPK.simplex
elseif lpm.method == :Exact
solve = GLPK.exact
elseif lpm.method == :InteriorPoint
solve = GLPK.interior
else
error("bug")
end
return solve(lpm.inner, lpm.param)
end
end
function MPB.status(lpm::GLPKMathProgModelLP)
if lpm.infeasible_bounds
return :Infeasible
end
if lpm.method == :Simplex || lpm.method == :Exact
get_status = GLPK.get_status
elseif lpm.method == :InteriorPoint
get_status = GLPK.ipt_status
else
error("bug")
end
s = get_status(lpm.inner)
if s == GLPK.OPT
return :Optimal
elseif s == GLPK.INFEAS
return :Infeasible
elseif s == GLPK.UNBND
return :Unbounded
elseif s == GLPK.FEAS
return :Feasible
elseif s == GLPK.NOFEAS
return :Infeasible
elseif s == GLPK.UNDEF
return :Undefined
else
error("Internal library error")
end
end
function MPB.getobjval(lpm::GLPKMathProgModelLP)
if lpm.infeasible_bounds
if GLPK.get_obj_dir(lpm.inner) == GLPK.MAX
return -Inf
else
return Inf
end
end
if lpm.method == :Simplex || lpm.method == :Exact
get_obj_val = GLPK.get_obj_val
elseif lpm.method == :InteriorPoint
get_obj_val = GLPK.ipt_obj_val
else
error("bug")
end
return get_obj_val(lpm.inner)
end
function check_feasible_bounds(lpm::GLPKMathProgModelLP, name::String)
if lpm.infeasible_bounds
error("$name is not available when some constraint bounds are infeasible (lower bound > upper bound)")
end
end
function MPB.getsolution(lpm::GLPKMathProgModelLP)
check_feasible_bounds(lpm, "getsolution")
lp = lpm.inner
n = GLPK.get_num_cols(lp)
if lpm.method == :Simplex || lpm.method == :Exact
get_col_prim = GLPK.get_col_prim
elseif lpm.method == :InteriorPoint
get_col_prim = GLPK.ipt_col_prim
else
error("bug")
end
return [get_col_prim(lp, i) for i in 1:n]
end
function MPB.getconstrsolution(lpm::GLPKMathProgModelLP)
check_feasible_bounds(lpm, "getconstrsolution")
lp = lpm.inner
m = GLPK.get_num_rows(lp)
if lpm.method == :Simplex || lpm.method == :Exact
get_row_prim = GLPK.get_row_prim
elseif lpm.method == :InteriorPoint
get_row_prim = GLPK.ipt_row_prim
else
error("bug")
end
return [get_row_prim(lp, i) for i in 1:m]
end
function MPB.getreducedcosts(lpm::GLPKMathProgModelLP)
check_feasible_bounds(lpm, "getreducedcosts")
lp = lpm.inner
n = GLPK.get_num_cols(lp)
if lpm.method == :Simplex || lpm.method == :Exact
get_col_dual = GLPK.get_col_dual
elseif lpm.method == :InteriorPoint
get_col_dual = GLPK.ipt_col_dual
else
error("bug")
end
return [get_col_dual(lp, i) for i in 1:n]
end
function MPB.getconstrduals(lpm::GLPKMathProgModelLP)
check_feasible_bounds(lpm, "getconstrduals")
lp = lpm.inner
m = GLPK.get_num_rows(lp)
if lpm.method == :Simplex || lpm.method == :Exact
get_row_dual = GLPK.get_row_dual
elseif lpm.method == :InteriorPoint
get_row_dual = GLPK.ipt_row_dual
else
error("bug")
end
return [get_row_dual(lp, i) for i in 1:m]
end
# The functions getinfeasibilityray and getunboundedray are adapted from code
# taken from the LEMON C++ optimization library. This is the copyright notice:
#
### Copyright (C) 2003-2010
### Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
### (Egervary Research Group on Combinatorial Optimization, EGRES).
###
### Permission to use, modify and distribute this software is granted
### provided that this copyright notice appears in all copies. For
### precise terms see the accompanying LICENSE file.
###
### This software is provided "AS IS" with no warranty of any kind,
### express or implied, and with no claim as to its suitability for any
### purpose.
function MPB.getinfeasibilityray(lpm::GLPKMathProgModelLP)
if lpm.infeasible_bounds
# See https://github.com/JuliaOpt/GLPKMathProgInterface.jl/pull/34
return zeros(MPB.numconstr(lpm))
end
lp = lpm.inner
if lpm.method == :Simplex || lpm.method == :Exact
elseif lpm.method == :InteriorPoint
error("getinfeasibilityray is not available when using the InteriorPoint method")
else
error("bug")
end
m = GLPK.get_num_rows(lp)
ray = zeros(m)
ur = GLPK.get_unbnd_ray(lp)
if ur != 0
if ur <= m
k = ur
get_stat = GLPK.get_row_stat
get_bind = GLPK.get_row_bind
get_prim = GLPK.get_row_prim
get_ub = GLPK.get_row_ub
else
k = ur - m
get_stat = GLPK.get_col_stat
get_bind = GLPK.get_col_bind
get_prim = GLPK.get_col_prim
get_ub = GLPK.get_col_ub
end
get_stat(lp, k) == GLPK.BS || error("unbounded ray is primal (use getunboundedray)")
ray[get_bind(lp, k)] = (get_prim(lp, k) > get_ub(lp, k)) ? -1 : 1
GLPK.btran(lp, ray)
else
eps = 1e-7
for i = 1:m
idx = GLPK.get_bhead(lp, i)
if idx <= m
k = idx
get_prim = GLPK.get_row_prim
get_ub = GLPK.get_row_ub
get_lb = GLPK.get_row_lb
else
k = idx - m
get_prim = GLPK.get_col_prim
get_ub = GLPK.get_col_ub
get_lb = GLPK.get_col_lb
end
res = get_prim(lp, k)
if res > get_ub(lp, k) + eps
ray[i] = -1
elseif res < get_lb(lp, k) - eps
ray[i] = 1
else
continue # ray[i] == 0
end
if idx <= m
ray[i] *= GLPK.get_rii(lp, k)
else
ray[i] /= GLPK.get_sjj(lp, k)
end
end
GLPK.btran(lp, ray)
for i = 1:m
ray[i] /= GLPK.get_rii(lp, i)
end
end
return ray
end
function MPB.getunboundedray(lpm::GLPKMathProgModelLP)
check_feasible_bounds(lpm, "getreducedcosts")
lp = lpm.inner
if lpm.method == :Simplex || lpm.method == :Exact
elseif lpm.method == :InteriorPoint
error("getunboundedray is not available when using the InteriorPoint method")
else
error("bug")
end
m = GLPK.get_num_rows(lp)
n = GLPK.get_num_cols(lp)
ray = zeros(n)
ur = GLPK.get_unbnd_ray(lp)
if ur != 0
if ur <= m
k = ur
get_stat = GLPK.get_row_stat
get_dual = GLPK.get_row_dual
else
k = ur - m
get_stat = GLPK.get_col_stat
get_dual = GLPK.get_col_dual
ray[k] = 1
end
get_stat(lp, k) != GLPK.BS || error("unbounded ray is dual (use getinfeasibilityray)")
for (ri, rv) in zip(GLPK.eval_tab_col(lp, ur)...)
ri > m && (ray[ri - m] = rv)
end
if (GLPK.get_obj_dir(lp) == GLPK.MAX) ⊻ (get_dual(lp, k) > 0)
ray .*= -1.0
end
else
for i = 1:n
ray[i] = GLPK.get_col_prim(lp, i)
end
end
return ray
end
end