-
Notifications
You must be signed in to change notification settings - Fork 63
/
cpx_constrs.jl
537 lines (491 loc) · 18.9 KB
/
cpx_constrs.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
function c_api_addrows(model::Model, cbegins::IVec, inds::IVec, coeffs::FVec,
rel::CVec, rhs::FVec)
nnz = length(inds)
ncons = length(rhs)
(nnz == length(coeffs)) || error("Incompatible constraint arg dimensions.")
if ncons > 0
stat = @cpx_ccall(addrows, Cint, (
Ptr{Cvoid}, # environment
Ptr{Cvoid}, # problem
Cint, # num new cols
Cint, # num new rows
Cint, # num non-zeros
Ptr{Cdouble}, # rhs
Ptr{Cchar}, # sense
Ptr{Cint}, # matrix start
Ptr{Cint}, # matrix index
Ptr{Cdouble}, # matrix values
Ptr{Ptr{Cchar}}, # col names
Ptr{Ptr{Cchar}} # row names
),
model.env.ptr, model.lp, 0, ncons, nnz, rhs, rel,
cbegins .- Cint(1), inds .- Cint(1), coeffs,
C_NULL, C_NULL)
if stat != 0
throw(CplexError(model.env, stat))
end
end
end
# same as add_rows but does conversion of sense chars
function add_constrs!(model::Model, cbegins::IVec, inds::IVec, coeffs::FVec,
rel::CVec, rhs::FVec)
for k in 1:length(rel)
if rel[k] == UInt8('>')
rel[k] = convert(Cchar, 'G')
elseif rel[k] == UInt8('<')
rel[k] = convert(Cchar, 'L')
elseif rel[k] == UInt8('=')
rel[k] = convert(Cchar, 'E')
end
end
c_api_addrows(model, cbegins, inds, coeffs, rel, rhs)
end
add_constr!(model::Model, coef::Vector, sense::Char, rhs) = add_constrs!(model, [1], collect(1:length(coef)), coef, [sense], [rhs])
function add_constrs!(model::Model, cbeg::Vector, inds::Vector, coeffs::Vector, rel::Vector, rhs::Vector)
add_constrs!(model, ivec(cbeg), ivec(inds), fvec(coeffs), cvecx(rel, length(cbeg)), fvec(rhs))
end
function add_constrs_t!(model::Model, At::SparseMatrixCSC{Float64}, rel::GCharOrVec, b::Vector)
n, m = size(At)
(m == length(b) && n == num_var(model)) || error("Incompatible argument dimensions.")
add_constrs!(model, At.colptr[1:At.n], At.rowval, At.nzval, rel, b)
end
function add_constrs_t!(model::Model, At::AbstractArray{Float64, 2}, rel::GCharOrVec, b::Vector)
n, m = size(At)
(m == length(b) && n == num_var(model)) || error("Incompatible argument dimensions.")
add_constrs_t!(model, sparse(At), rel, b)
end
function add_constrs!(model::Model, A::CoeffMat, rel::GCharOrVec, b::Vector{Float64})
m, n = size(A)
(m == length(b) && n == num_var(model)) || error("Incompatible argument dimensions.")
add_constrs_t!(model, transpose(A), rel, b)
end
add_constrs!(model::Model, cbeg::Vector, inds::Vector, coeffs::Vector, rel::Char, rhs::Vector) = add_constrs!(model, cbeg, inds, coeffs, cvecx(rel, length(cbeg)), rhs)
function add_rangeconstrs!(model::Model, cbegins::IVec, inds::IVec, coeffs::FVec, lb::FVec, ub::FVec)
nnz = length(inds)
ncons = length(lb)
(ncons == length(ub) && nnz == length(coeffs)) || error("Incompatible constraint argument dimensions.")
sense = fill!(Vector{Cchar}(undef, ncons), 'R')
for i in 1:ncons
if lb[i] == -Inf
lb[i] = -ub[i]
ub[i] = Inf
coeffs[cbegins[i]+1:cbegins[i+1]] = -coeffs[cbegins[i]+1:cbegins[i+1]]
end
end
if ncons > 0
stat = @cpx_ccall(addrows, Cint, (
Ptr{Cvoid}, # environment
Ptr{Cvoid}, # problem
Cint, # num new cols
Cint, # num new rows
Cint, # num non-zeros
Ptr{Cdouble}, # rhs
Ptr{Cchar}, # sense
Ptr{Cint}, # matrix start
Ptr{Cint}, # matrix index
Ptr{Cdouble}, # matrix values
Ptr{Cvoid}, # col names
Ptr{Cvoid} # row names
),
model.env.ptr, model.lp, 0, ncons, nnz, lb, sense, cbegins[1:end-1], inds, coeffs, C_NULL, C_NULL)
if stat != 0
throw(CplexError(model.env, stat))
end
for i = 1:ncons
stat = @cpx_ccall(chgrngval, Cint, (
Ptr{Cvoid},
Ptr{Cvoid},
Cint,
Ptr{Cint},
Ptr{Cdouble}
),
model.env.ptr, model.lp, 1, Cint[i-1], [ub[i]-lb[i]])
if stat != 0
throw(CplexError(model.env, stat))
end
end
end
end
function add_rangeconstrs!(model::Model, cbeg::Vector, inds::Vector, coeffs::Vector, lb::Vector, ub::Vector)
add_rangeconstrs!(model, ivec(cbeg), ivec(inds), fvec(coeffs), fvec(lb), fvec(ub))
end
function add_rangeconstrs_t!(model::Model, At::SparseMatrixCSC{Float64}, lb::Vector, ub::Vector)
add_rangeconstrs!(model, At.colptr-1, At.rowval-1, At.nzval, lb, ub)
end
function add_rangeconstrs_t!(model::Model, At::Matrix{Float64}, lb::Vector, ub::Vector)
add_rangeconstrs_t!(model, sparse(At), lb, ub)
end
function add_rangeconstrs!(model::Model, A::CoeffMat, lb::Vector, ub::Vector)
m, n = size(A)
(m == length(lb) == length(ub) && n == num_var(model)) || error("Incompatible constraint argument dimensions.")
add_rangeconstrs_t!(model, transpose(A), lb, ub)
end
function c_api_getnumrows(model::Model)
ncons = @cpx_ccall(getnumrows, Cint, (
Ptr{Cvoid},
Ptr{Cvoid}
),
model.env.ptr, model.lp)
return ncons
end
# Needed by MathProgBase Interface
num_constr(model::Model) = c_api_getnumrows(model)
function get_constr_senses(model::Model)
ncons = num_constr(model)
senses = Vector{Cchar}(undef, ncons)
stat = @cpx_ccall(getsense, Cint, (
Ptr{Cvoid},
Ptr{Cvoid},
Ptr{Cchar},
Cint,
Cint
),
model.env.ptr, model.lp, senses, 0, ncons-1)
if stat != 0
throw(CplexError(model.env, stat))
end
return senses
end
function c_api_chgsense(model::Model, indices::IVec, senses::CVec)
ncons = length(IVec)
stat = @cpx_ccall(chgsense, Cint, (
Ptr{Cvoid},
Ptr{Cvoid},
Cint,
Ptr{Cint},
Ptr{Cchar}
),
model.env.ptr, model.lp, ncons,
indices .- Cint(1), senses)
if stat != 0
throw(CplexError(model.env, stat))
end
end
function set_constr_senses!(model::Model, senses::Vector)
ncons = num_constr(model)
stat = @cpx_ccall(chgsense, Cint, (
Ptr{Cvoid},
Ptr{Cvoid},
Cint,
Ptr{Cint},
Ptr{Cchar}
),
model.env.ptr, model.lp, ncons, Cint[0:ncons-1;],
convert(Vector{Cchar},senses))
if stat != 0
throw(CplexError(model.env, stat))
end
end
function c_api_getrhs(model::Model, rhs::Vector{Cdouble},
row_start::Cint, row_end::Cint)
stat = @cpx_ccall(getrhs, Cint, (
Ptr{Cvoid},
Ptr{Cvoid},
Ptr{Cdouble},
Cint,
Cint
),
model.env.ptr, model.lp, rhs,
row_start - Cint(1), row_end - Cint(1))
if stat != 0
throw(CplexError(model.env, stat))
end
end
function get_rhs(model::Model)
ncons = num_constr(model)
rhs = Vector{Cdouble}(undef, ncons)
stat = @cpx_ccall(getrhs, Cint, (
Ptr{Cvoid},
Ptr{Cvoid},
Ptr{Cdouble},
Cint,
Cint
),
model.env.ptr, model.lp, rhs, 0, ncons-1)
if stat != 0
throw(CplexError(model.env, stat))
end
return rhs
end
function c_api_chgrhs(model::Model, indices::IVec, rhs::FVec)
ncons = length(indices)
@assert ncons == length(rhs)
stat = @cpx_ccall(chgrhs, Cint, (
Ptr{Cvoid},
Ptr{Cvoid},
Cint,
Ptr{Cint},
Ptr{Cdouble}
),
model.env.ptr, model.lp, ncons, indices .- Cint(1), rhs)
if stat != 0
throw(CplexError(model.env, stat))
end
end
function set_rhs!(model::Model, rhs::Vector)
ncons = num_constr(model)
@assert ncons == length(rhs)
stat = @cpx_ccall(chgrhs, Cint, (
Ptr{Cvoid},
Ptr{Cvoid},
Cint,
Ptr{Cint},
Ptr{Cdouble}
),
model.env.ptr, model.lp, ncons, Cint[0:ncons-1;], float(rhs))
if stat != 0
throw(CplexError(model.env, stat))
end
end
function get_constrLB(model::Model)
senses = get_constr_senses(model)
ret = get_rhs(model)
for i = 1:num_constr(model)
if senses[i] == UInt8('G') || senses[i] == UInt8('E')
# Do nothing
else
# LEQ constraint so LB is -Inf
ret[i] = -Inf
end
end
return ret
end
function get_constrUB(model::Model)
senses = get_constr_senses(model)
ret = get_rhs(model)
for i = 1:num_constr(model)
if senses[i] == UInt8('L') || senses[i] == UInt8('E')
# Do nothing
else
# GEQ constraint so UB is Inf
ret[i] = +Inf
end
end
return ret
end
function set_constrLB!(model::Model, lb)
senses = get_constr_senses(model)
rhs = get_rhs(model)
sense_changed = false
for i = 1:num_constr(model)
if senses[i] == UInt8('G') || senses[i] == UInt8('E')
# Do nothing
elseif senses[i] == UInt8('L')
if lb[i] != -Inf
# LEQ constraint with non-NegInf LB implies a range
if isapprox(lb[i], rhs[i])
# seems to be an equality
senses[i] = 'E'
sense_changed = true
else
error("Tried to set LB != -Inf on a LEQ constraint (index $i)")
end
else
lb[i] = rhs[i]
end
end
end
if sense_changed
set_constr_senses!(model, senses)
end
set_rhs!(model, lb)
end
function set_constrUB!(model::Model, ub)
senses = get_constr_senses(model)
rhs = get_rhs(model)
sense_changed = false
for i = 1:num_constr(model)
if senses[i] == UInt8('L') || senses[i] == UInt8('E')
# Do nothing
elseif senses[i] == UInt8('G')
if ub[i] != Inf
# GEQ constraint with non-PosInf UB implies a range
if isapprox(ub[i], rhs[i])
# seems to be an equality
senses[i] = 'E'
sense_changed = true
else
error("Tried to set UB != +Inf on a GEQ constraint (index $i)")
end
else
ub[i] = rhs[i]
end
end
end
if sense_changed
set_constr_senses!(model, senses)
end
set_rhs!(model, ub)
end
function get_nnz(model::Model)
ret = @cpx_ccall(getnumnz, Cint, (Ptr{Cvoid}, Ptr{Cvoid}), model.env.ptr, model.lp)
ret == 0 && throw(error("Could not query number of nonzeros in model"))
return ret
end
function c_api_getrows(model::Model, mbegin::Cint, mend::Cint)
nzcnt_p = Vector{Cint}(undef, 1)
m = mend - mbegin + 1
nnz = get_nnz(model)
rmatbeg = Vector{Cint}(undef, m+1)
rmatind = Vector{Cint}(undef, nnz)
rmatval = Vector{Cdouble}(undef, nnz)
surplus_p = Vector{Cint}(undef, 1)
stat = @cpx_ccall(getrows, Cint, (
Ptr{Cvoid},
Ptr{Cvoid},
Ptr{Cint},
Ptr{Cint},
Ptr{Cint},
Ptr{Cdouble},
Cint,
Ptr{Cint},
Cint,
Cint
),
model.env.ptr, model.lp, nzcnt_p,
rmatbeg, rmatind, rmatval,
nnz, surplus_p, mbegin-Cint(1), mend-Cint(1))
if stat != 0 || surplus_p[1] < 0
throw(CplexError(model.env, stat))
end
rmatbeg[end] = nnz # add the last entry that Julia wants
return (nzcnt_p[1], rmatbeg, rmatind .+ Cint(1), rmatval)
end
function get_constr_matrix(model::Model)
nzcnt_p = Vector{Cint}(undef, 1)
m = num_constr(model)
n = num_var(model)
nnz = get_nnz(model)
cmatbeg = Vector{Cint}(undef, n+1)
cmatind = Vector{Cint}(undef, nnz)
cmatval = Vector{Cdouble}(undef, nnz)
surplus_p = Vector{Cint}(undef, 1)
stat = @cpx_ccall(getcols, Cint, (
Ptr{Cvoid},
Ptr{Cvoid},
Ptr{Cint},
Ptr{Cint},
Ptr{Cint},
Ptr{Cdouble},
Cint,
Ptr{Cint},
Cint,
Cint
),
model.env.ptr, model.lp, nzcnt_p, cmatbeg, cmatind, cmatval, nnz, surplus_p, 0, n-1)
if stat != 0 || surplus_p[1] < 0
throw(CplexError(model.env, stat))
end
cmatbeg[end] = nnz # add the last entry that Julia wants
return SparseMatrixCSC(m, n, convert(Vector{Int64}, cmatbeg.+1), convert(Vector{Int64}, cmatind.+1), cmatval)
end
get_num_sos(model::Model) = @cpx_ccall(getnumsos, Cint, (Ptr{Cvoid}, Ptr{Cvoid}), model.env.ptr, model.lp)
# int CPXaddsos( CPXCENVptr env, CPXLPptr lp, int numsos, int numsosnz, char const * sostype, int const * sosbeg, int const * sosind, double const * soswt, char ** sosname )
function add_sos!(model::Model, sostype::Symbol, idx::Vector{Int}, weight::Vector{Cdouble})
((nelem = length(idx)) == length(weight)) || error("Index and weight vectors of unequal length")
(sostype == :SOS1) ? (typ = CPX_TYPE_SOS1) : ((sostype == :SOS2) ? (typ = CPX_TYPE_SOS2) : error("Invalid SOS constraint type"))
stat = @cpx_ccall(addsos, Cint, (
Ptr{Cvoid},
Ptr{Cvoid},
Cint,
Cint,
Ptr{Cchar},
Ptr{Cint},
Ptr{Cint},
Ptr{Cdouble},
Ptr{Ptr{Cchar}}
),
model.env.ptr, model.lp, convert(Cint, 1), convert(Cint, nelem), [convert(Cchar, typ)], Cint[0], convert(Vector{Cint}, idx.-1), weight, C_NULL)
if stat != 0
throw(CplexError(model.env, stat))
end
model.has_int = true
model.has_sos = true
return nothing
end
# int CPXgetsos( CPXCENVptr env, CPXCLPptr lp, int * numsosnz_p, char * sostype,
# int * sosbeg, int * sosind, double * soswt, int sosspace, int * surplus_p, int
# begin, int end )
function c_api_getsos(model::Model, index::Int)
numsosnz_p = Ref{Cint}()
surplus_p = Ref{Cint}()
sos_type = Ref{Cchar}()
sos_beg = Ref{Cint}()
stat = @cpx_ccall(getsos, Cint, (
Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cint}, Ptr{Cchar}, Ptr{Cint}, Ptr{Cint},
Ptr{Cdouble}, Cint, Ptr{Cint}, Cint, Cint),
model.env.ptr, model.lp, numsosnz_p, sos_type, sos_beg, C_NULL, C_NULL,
0, surplus_p, index, index)
num_terms = -surplus_p[]
columns = fill(Cint(0), num_terms)
weights = fill(Cdouble(0.0), num_terms)
stat = @cpx_ccall(getsos, Cint, (
Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cint}, Ptr{Cchar}, Ptr{Cint}, Ptr{Cint},
Ptr{Cdouble}, Cint, Ptr{Cint}, Cint, Cint),
model.env.ptr, model.lp, numsosnz_p, sos_type, sos_beg, columns, weights,
num_terms, surplus_p, index, index)
if stat != 0
throw(CplexError(model.env, stat))
end
return (columns, weights, sos_type[])
end
# int CPXdelsos( CPXCENVptr env, CPXLPptr lp, int begin, int end )
function c_api_delsos(model::Model, begin_index::Int, end_index::Int)
stat = @cpx_ccall(delsos, Cint, (Ptr{Cvoid}, Ptr{Cvoid}, Cint, Cint),
model.env.ptr, model.lp, begin_index, end_index)
if stat != 0
throw(CplexError(model.env, stat))
end
return
end
add_indicator_constraint(model::Model, idx, coeff, sense, rhs, indicator) =
add_indicator_constraint(model::Model, idx, coeff, sense, rhs, indicator, 0)
add_indicator_constraint(model::Model, idx, coeff, sense, rhs, indicator, comp) =
add_indicator_constraint(model, convert(Vector{Cint},idx), convert(Vector{Cdouble},coeff),
convert(Cchar,sense), convert(Cdouble,rhs), convert(Cint,indicator), convert(Cint,comp))
function add_indicator_constraint(model::Model, idx::Vector{Cint}, coeff::Vector{Cdouble}, sense::Cchar, rhs::Cdouble, indicator::Cint, comp::Cint)
(nzcnt = length(idx)) == length(coeff) || error("Incompatible lengths in constraint specification")
stat = @cpx_ccall(addindconstr, Cint, (
Ptr{Cvoid},
Ptr{Cvoid},
Cint,
Cint,
Cint,
Cdouble,
Cchar,
Ptr{Cint},
Ptr{Cdouble},
Ptr{Cchar}),
model.env.ptr, model.lp, indicator, comp,
nzcnt, rhs, sense, idx-Cint(1), coeff, C_NULL)
if stat != 0
throw(CplexError(model.env, stat))
end
return nothing
end
function c_api_chgcoef(model::Model, row::Cint, col::Cint, coef::Cdouble)
stat = @cpx_ccall(chgcoef, Cint, (
Ptr{Cvoid}, # environment
Ptr{Cvoid}, # problem
Cint, # row
Cint, # col
Cdouble, # coef
),
model.env.ptr, model.lp,
row - Cint(1), col - Cint(1), coef)
if stat != 0
throw(CplexError(model.env, stat))
end
end
function c_api_delrows(model::Model, first::Cint, last::Cint)
stat = @cpx_ccall(delrows, Cint, (
Ptr{Cvoid},
Ptr{Cvoid},
Cint,
Cint,
),
model.env.ptr, model.lp, first-1, last-1)
if stat != 0
throw(CplexError(model.env, stat))
end
end