-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathlinprog.jl
479 lines (412 loc) · 15.4 KB
/
linprog.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
##
## Linear Programming and Mixed Integer Programming interfaces
## for optimization and constraint satisfaction problems
##
require("glpk.jl")
# General notes: the interface is provided as a collection of
# high-level functions which use the glpk library.
# Most functions have almost same interface
# as matlab's linprog, e.g.:
#
# (z, x, flag) = linprog(f, A, b, Aeq, beq, params)
#
# There's a function for each solving method:
# linprog_interior
# linprog_simplex
# linprog_exact
# mixintprog
#
# The function linprog is an alias to linprog_interior
#
# These functions all seek to solve the problem
#
# z = min_{x} (f' * x)
#
# where the vector x is subject to these constraints:
#
# A * x <= b
# Aeq * x == b
# lb <= x <= ub
#
# The return flag is 0 in case of success, and follows
# the glpk library convention otherwise.
# In case of failure, z and x are set to nothing, otherwise
# they will hold the solution found
#
# The parameters Aeq, beq, lb, ub and params are optional.
# This mean they can be either passed as the constant 'nothing'
# or as an empty vector [] or not provided at all.
#
# Some methods have slightly different function calls, see
# individual notes for additional information
typealias SparseOrFullMat{T} Union(Matrix{T}, SparseMatrixCSC{T})
typealias MatOrNothing Union(Matrix, SparseMatrixCSC, Vector{None}, Nothing)
# Linear Programming, Interior point method (default)
#{{{
function linprog_interior{T<:Real,P<:Union(GLPInteriorParam,Nothing)}(f::Vector{T}, A::SparseOrFullMat{T}, b::Vector{T},
Aeq::SparseOrFullMat{T}, beq::Vector{T}, lb::Vector{T}, ub::Vector{T}, params::P)
lp, n = _jl_linprog__setup_prob(f, A, b, Aeq, beq, lb, ub, params)
ret = glp_interior(lp, params)
#println("ret=$ret")
if ret == 0
z = glp_ipt_obj_val(lp)
x = zeros(Float64, n)
for c = 1 : n
x[c] = glp_ipt_col_prim(lp, c)
end
return (z, x, ret)
else
# throw exception here ?
return (nothing, nothing, ret)
end
end
function linprog_interior{T<:Real,P<:Union(GLPInteriorParam,Nothing)}(f::Vector{T}, A::MatOrNothing, b::VecOrNothing,
Aeq::MatOrNothing, beq::VecOrNothing, lb::VecOrNothing, ub::VecOrNothing, params::P)
cA = _jl_linprog__convert_matornothing(T, A)
cb = _jl_linprog__convert_vecornothing(T, b)
cAeq = _jl_linprog__convert_matornothing(T, Aeq)
cbeq = _jl_linprog__convert_vecornothing(T, beq)
clb = _jl_linprog__convert_vecornothing(T, lb)
cub = _jl_linprog__convert_vecornothing(T, ub)
return linprog_interior(f, cA, cb, cAeq, cbeq, clb, cub, params)
end
linprog_interior{T<:Real}(f::Vector{T}, A::MatOrNothing, b::VecOrNothing) =
linprog_interior(f, A, b, nothing, nothing, nothing, nothing, nothing)
linprog_interior{T<:Real}(f::Vector{T}, A::MatOrNothing, b::VecOrNothing,
Aeq::MatOrNothing, beq::VecOrNothing) =
linprog_interior(f, A, b, Aeq, beq, nothing, nothing, nothing)
linprog_interior{T<:Real}(f::Vector{T}, A::MatOrNothing, b::VecOrNothing,
Aeq::MatOrNothing, beq::VecOrNothing, lb::VecOrNothing,
ub::VecOrNothing) =
linprog_interior(f, A, b, Aeq, beq, lb, ub, nothing)
linprog = linprog_interior
#}}}
# Linear Programming, Simplex Method
#{{{
function linprog_simplex{T<:Real,P<:Union(GLPSimplexParam,Nothing)}(f::Vector{T}, A::SparseOrFullMat{T}, b::Vector{T},
Aeq::SparseOrFullMat{T}, beq::Vector{T}, lb::Vector{T}, ub::Vector{T}, params::P)
lp, n = _jl_linprog__setup_prob(f, A, b, Aeq, beq, lb, ub, params)
ret = glp_simplex(lp, params)
#println("ret=$ret")
if ret == 0
z = glp_get_obj_val(lp)
x = zeros(Float64, n)
for c = 1 : n
x[c] = glp_get_col_prim(lp, c)
end
return (z, x, ret)
else
# throw exception here ?
return (nothing, nothing, ret)
end
end
function linprog_simplex{T<:Real,P<:Union(GLPSimplexParam,Nothing)}(f::Vector{T}, A::MatOrNothing, b::VecOrNothing,
Aeq::MatOrNothing, beq::VecOrNothing, lb::VecOrNothing, ub::VecOrNothing, params::P)
cA = _jl_linprog__convert_matornothing(T, A)
cb = _jl_linprog__convert_vecornothing(T, b)
cAeq = _jl_linprog__convert_matornothing(T, Aeq)
cbeq = _jl_linprog__convert_vecornothing(T, beq)
clb = _jl_linprog__convert_vecornothing(T, lb)
cub = _jl_linprog__convert_vecornothing(T, ub)
return linprog_simplex(f, cA, cb, cAeq, cbeq, clb, cub, params)
end
linprog_simplex{T<:Real}(f::Vector{T}, A::MatOrNothing, b::VecOrNothing) =
linprog_simplex(f, A, b, nothing, nothing, nothing, nothing, nothing)
linprog_simplex{T<:Real}(f::Vector{T}, A::MatOrNothing, b::VecOrNothing,
Aeq::MatOrNothing, beq::VecOrNothing) =
linprog_simplex(f, A, b, Aeq, beq, nothing, nothing, nothing)
linprog_simplex{T<:Real}(f::Vector{T}, A::MatOrNothing, b::VecOrNothing,
Aeq::MatOrNothing, beq::VecOrNothing, lb::VecOrNothing,
ub::VecOrNothing) =
linprog_simplex(f, A, b, Aeq, beq, lb, ub, nothing)
#}}}
# Linear Programming, Simplex-exact Method
#{{{
# Notes:
# * uses glp_simplex as a preliminary step
# * the exact step only accepts the "it_lim" and "tm_lim" options,
# which means no message suppression is possible
function linprog_exact{T<:Real,P<:Union(GLPSimplexParam,Nothing)}(f::Vector{T}, A::SparseOrFullMat{T}, b::Vector{T},
Aeq::SparseOrFullMat{T}, beq::Vector{T}, lb::Vector{T}, ub::Vector{T}, params::P)
lp, n = _jl_linprog__setup_prob(f, A, b, Aeq, beq, lb, ub, params)
ret = glp_simplex(lp, params)
if ret != 0
# throw exception here ?
return (nothing, nothing, ret)
end
ret = glp_exact(lp, params)
if ret == 0
z = glp_get_obj_val(lp)
x = zeros(Float64, n)
for c = 1 : n
x[c] = glp_get_col_prim(lp, c)
end
return (z, x, ret)
else
# throw exception here ?
return (nothing, nothing, ret)
end
end
function linprog_exact{T<:Real,P<:Union(GLPSimplexParam,Nothing)}(f::Vector{T}, A::MatOrNothing, b::VecOrNothing,
Aeq::MatOrNothing, beq::VecOrNothing, lb::VecOrNothing, ub::VecOrNothing, params::P)
cA = _jl_linprog__convert_matornothing(T, A)
cb = _jl_linprog__convert_vecornothing(T, b)
cAeq = _jl_linprog__convert_matornothing(T, Aeq)
cbeq = _jl_linprog__convert_vecornothing(T, beq)
clb = _jl_linprog__convert_vecornothing(T, lb)
cub = _jl_linprog__convert_vecornothing(T, ub)
return linprog_exact(f, cA, cb, cAeq, cbeq, clb, cub, params)
end
linprog_exact{T<:Real}(f::Vector{T}, A::MatOrNothing, b::VecOrNothing) =
linprog_exact(f, A, b, nothing, nothing, nothing, nothing, nothing)
linprog_exact{T<:Real}(f::Vector{T}, A::MatOrNothing, b::VecOrNothing,
Aeq::MatOrNothing, beq::VecOrNothing) =
linprog_exact(f, A, b, Aeq, beq, nothing, nothing, nothing)
linprog_exact{T<:Real}(f::Vector{T}, A::MatOrNothing, b::VecOrNothing,
Aeq::MatOrNothing, beq::VecOrNothing, lb::VecOrNothing,
ub::VecOrNothing) =
linprog_exact(f, A, b, Aeq, beq, lb, ub, nothing)
#}}}
# Mixed Integer Programming
#{{{
# Notes:
# * same syntax as linprog algorithms, with an additional col_kind vector and
# an additional set of parameters for the presolve step; and an additional
# return flag for the presolve step:
#
# (z, x, flag, ps_flag) = mixintprog(f, A, b, Aeq, beq, lb, ub, col_kind, param, ps_param)
#
# * if the col_kind vector is not provided, all variables default to integer
# * if the "presolve" options is set to GLP_OFF, then it uses linear programming
# for presolving, via the simplex point method with parameters ps_param (if ps_param is nothing
# or not given, uses the defaults)
function mixintprog{T<:Real,Ti<:Integer,P<:Union(GLPIntoptParam,Nothing),Px<:Union(GLPSimplexParam,Nothing)}(
f::Vector{T}, A::SparseOrFullMat{T}, b::Vector{T}, Aeq::SparseOrFullMat{T}, beq::Vector{T},
lb::Vector{T}, ub::Vector{T}, col_kind::Vector{Ti}, params::P, params_presolve::Px)
lp, n = _jl_linprog__setup_prob(f, A, b, Aeq, beq, lb, ub, params)
_jl_mixintprog_set_col_kind(lp, n, col_kind)
if params == nothing || pointer(params) == C_NULL || params["presolve"] != GLP_ON
ret_ps = glp_simplex(lp, params_presolve)
if ret_ps != 0
# throw exception here ?
# XXX GLP_ESTOP ??
return (nothing, nothing, GLP_ESTOP, ret_ps)
end
else
ret_ps = 0
end
ret = glp_intopt(lp, params)
if ret == 0
z = glp_mip_obj_val(lp)
x = zeros(Float64, n)
for c = 1 : n
x[c] = glp_mip_col_val(lp, c)
end
return (z, x, ret, ret_ps)
else
# throw exception here ?
return (nothing, nothing, ret, ret_ps)
end
end
function mixintprog{T<:Real,P<:Union(GLPIntoptParam,Nothing),Px<:Union(GLPSimplexParam,Nothing)}(
f::Vector{T}, A::MatOrNothing, b::VecOrNothing, Aeq::MatOrNothing, beq::VecOrNothing,
lb::VecOrNothing, ub::VecOrNothing, col_kind::VecOrNothing, params::P, params_presolve::Px)
cA = _jl_linprog__convert_matornothing(T, A)
cb = _jl_linprog__convert_vecornothing(T, b)
cAeq = _jl_linprog__convert_matornothing(T, Aeq)
cbeq = _jl_linprog__convert_vecornothing(T, beq)
clb = _jl_linprog__convert_vecornothing(T, lb)
cub = _jl_linprog__convert_vecornothing(T, ub)
ccol_kind = _jl_linprog__convert_vecornothing(Int32, col_kind)
return mixintprog(f, cA, cb, cAeq, cbeq, clb, cub, ccol_kind, params, params_presolve)
end
mixintprog{T<:Real}(f::Vector{T}, A::MatOrNothing, b::VecOrNothing) =
mixintprog(f, A, b, nothing, nothing, nothing, nothing, nothing, nothing, nothing)
mixintprog{T<:Real}(f::Vector{T}, A::MatOrNothing, b::VecOrNothing,
Aeq::MatOrNothing, beq::VecOrNothing) =
mixintprog(f, A, b, Aeq, beq, nothing, nothing, nothing, nothing, nothing)
mixintprog{T<:Real}(f::Vector{T}, A::MatOrNothing, b::VecOrNothing,
Aeq::MatOrNothing, beq::VecOrNothing, lb::VecOrNothing,
ub::VecOrNothing) =
mixintprog(f, A, b, Aeq, beq, lb, ub, nothing, nothing, nothing)
mixintprog{T<:Real}(f::Vector{T}, A::MatOrNothing, b::VecOrNothing,
Aeq::MatOrNothing, beq::VecOrNothing, lb::VecOrNothing,
ub::VecOrNothing, col_kind::VecOrNothing) =
mixintprog(f, A, b, Aeq, beq, lb, ub, col_kind, nothing, nothing)
mixintprog{T<:Real,P<:Union(GLPIntoptParam,Nothing)}(f::Vector{T}, A::MatOrNothing, b::VecOrNothing,
Aeq::MatOrNothing, beq::VecOrNothing, lb::VecOrNothing,
ub::VecOrNothing, col_kind::VecOrNothing, params::P) =
mixintprog(f, A, b, Aeq, beq, lb, ub, col_kind, params, nothing)
#}}}
## Common auxiliary functions
#{{{
function _jl_linprog__convert_vecornothing{T}(::Type{T}, a::VecOrNothing)
if isequal(a, nothing) || isa(a, Array{None})
return T[]
elseif T <: Integer
if !(eltype(a) <: Integer)
error("integer-valued array required, or [] or nothing")
end
elseif T <: Real
if !(eltype(a) <: Real)
error("real-valued array required, or [] or nothing")
end
end
return convert(Array{T}, a)
end
function _jl_linprog__convert_matornothing{T}(::Type{T}, a::MatOrNothing)
if isequal(a, nothing) || isa(a, Array{None})
return Array(T, 0, 0)
elseif T <: Integer
if !(eltype(a) <: Integer)
error("integer-valued array required, or [] or nothing")
end
elseif T <: Real
if !(eltype(a) <: Real)
error("real-valued array required, or [] or nothing")
end
end
if issparse(a)
return convert(SparseMatrixCSC{T}, a)
else
return convert(Array{T}, a)
end
end
function _jl_linprog__setup_prob{T<:Real, P<:Union(GLPParam, Nothing)}(f::Vector{T}, A::SparseOrFullMat{T}, b::Vector{T},
Aeq::SparseOrFullMat{T}, beq::Vector{T}, lb::Vector{T}, ub::Vector{T}, params::P)
lp = GLPProb()
glp_set_obj_dir(lp, GLP_MIN)
n = size(f, 1)
m = _jl_linprog__check_A_b(A, b, n)
meq = _jl_linprog__check_A_b(Aeq, beq, n)
has_lb, has_ub = _jl_linprog__check_lb_ub(lb, ub, n)
#println("n=$n m=$m meq=$meq has_lb=$has_lb ub=$has_ub")
if m > 0
glp_add_rows(lp, m)
for r = 1 : m
#println(" r=$r b=$(b[r])")
glp_set_row_bnds(lp, r, GLP_UP, 0.0, b[r])
end
end
if meq > 0
glp_add_rows(lp, meq)
for r = 1 : meq
r0 = r + m
#println(" r=$r r0=$r0 beq=$(beq[r])")
glp_set_row_bnds(lp, r0, GLP_FX, beq[r], beq[r])
end
end
glp_add_cols(lp, n)
for c = 1 : n
glp_set_obj_coef(lp, c, f[c])
#println(" c=$c f=$(f[c])")
end
if has_lb && has_ub
for c = 1 : n
#println(" c=$c lb=$(lb[c]) ub=$(ub[c])")
bounds_type = (lb[c] != ub[c] ? GLP_DB : GLP_FX)
glp_set_col_bnds(lp, c, bounds_type, lb[c], ub[c])
end
elseif has_lb
for c = 1 : n
#println(" c=$c lb=$(lb[c])")
glp_set_col_bnds(lp, c, GLP_LO, lb[c], 0.0)
end
elseif has_ub
for c = 1 : n
#println(" c=$c ub=$(ub[c])")
glp_set_col_bnds(lp, c, GLP_UP, 0.0, ub[c])
end
end
if (m > 0 && issparse(A)) && (meq > 0 && issparse(Aeq))
(ia, ja, ar) = findn_nzs([A; Aeq])
elseif (m > 0 && issparse(A)) && (meq == 0)
(ia, ja, ar) = findn_nzs(A)
elseif (m == 0) && (meq > 0 && issparse(Aeq))
(ia, ja, ar) = findn_nzs(Aeq)
else
(ia, ja, ar) = _jl_linprog__dense_matrices_to_glp_format(m, meq, n, A, Aeq)
end
#println("ia=$ia")
#println("ja=$ja")
#println("ar=$ar")
glp_load_matrix(lp, ia, ja, ar)
return (lp, n)
end
function _jl_linprog__check_A_b{T}(A::SparseOrFullMat{T}, b::Vector{T}, n::Int)
m = 0
if !isempty(A)
if size(A, 2) != n
error("invlid A size: $(size(A))")
end
m = size(A, 1)
if isempty(b)
error("b is empty but a is not")
end
if size(b, 1) != m
error("invalid b size: $(size(b))")
end
else
if !isempty(b)
error("A is empty but b is not")
end
end
return m
end
function _jl_linprog__check_lb_ub{T}(lb::Vector{T}, ub::Vector{T}, n::Int)
has_lb = false
has_ub = false
if !isempty(lb)
if size(lb, 1) != n
error("invlid lb size: $(size(lb))")
end
has_lb = true
end
if !isempty(ub)
if size(ub, 1) != n
error("invalid ub size: $(size(ub))")
end
has_ub = true
end
return (has_lb, has_ub)
end
function _jl_linprog__dense_matrices_to_glp_format(m, meq, n, A, Aeq)
l = (m + meq) * n
ia = zeros(Int32, l)
ja = zeros(Int32, l)
ar = zeros(Float64, l)
k = 0
for r = 1 : m
for c = 1 : n
k += 1
ia[k] = r
ja[k] = c
ar[k] = A[r, c]
end
end
for r = 1 : meq
for c = 1 : n
r0 = r + m
k += 1
ia[k] = r0
ja[k] = c
ar[k] = Aeq[r, c]
end
end
return (ia, ja, ar)
end
function _jl_mixintprog_set_col_kind{Ti<:Integer}(lp::GLPProb, n::Int, col_kind::Vector{Ti})
if isempty(col_kind)
for i = 1 : n
glp_set_col_kind(lp, i, GLP_IV)
end
return
end
if length(col_kind) != n
error("wrong col_kind vector size")
end
for i = 1 : n
glp_set_col_kind(lp, i, col_kind[i])
end
end
#}}}