-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathgrb_constrs.jl
376 lines (332 loc) · 11.9 KB
/
grb_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
## Add Linear constraints
# add single constraint
function add_constr!(model::Model, inds::IVec, coeffs::FVec, rel::Cchar, rhs::Float64)
length(inds) == length(coeffs) || error("Inconsistent argument dimensions.")
ret = @grb_ccall(addconstr, Cint, (
Ptr{Void}, # model
Cint, # numnz
Ptr{Cint}, # cind
Ptr{Float64}, # cvals
Cchar, # sense
Float64, # rhs
Ptr{UInt8} # name
),
model, length(inds), inds - Cint(1), coeffs, rel, rhs, C_NULL)
if ret != 0
throw(GurobiError(model.env, ret))
end
nothing
end
function add_constr!(model::Model, inds::Vector, coeffs::Vector, rel::GChars, rhs::Real)
add_constr!(model, ivec(inds), fvec(coeffs), cchar(rel), Float64(rhs))
end
function add_constr!(model::Model, coeffs::Vector, rel::GChars, rhs::Real)
inds = find(coeffs)
vals = coeffs[inds]
add_constr!(model, inds, vals, rel, rhs)
end
# add multiple constraints
function add_constrs!(model::Model, cbegins::IVec, inds::IVec, coeffs::FVec, rel::CVec, rhs::FVec)
m = length(cbegins)
nnz = length(inds)
(m == length(rel) == length(rhs) && nnz == length(coeffs)) || error("Inconsistent argument dimensions.")
if m > 0
ret = @grb_ccall(addconstrs, Cint, (
Ptr{Void}, # model
Cint, # num constraints
Cint, # num non-zeros
Ptr{Cint}, # cbeg
Ptr{Cint}, # cind
Ptr{Float64}, # cval
Ptr{Cchar}, # sense
Ptr{Float64}, # rhs
Ptr{UInt8} # names
),
model, m, nnz, cbegins - Cint(1), inds - Cint(1), coeffs,
rel, rhs, C_NULL)
if ret != 0
throw(GurobiError(model.env, ret))
end
end
nothing
end
function add_constrs!(model::Model, cbeg::Vector, inds::Vector, coeffs::Vector, rel::GCharOrVec, 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_vars(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::Matrix{Float64}, rel::GCharOrVec, b::Vector)
n, m = size(At)
(m == length(b) && n == num_vars(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_vars(model)) || error("Incompatible argument dimensions.")
add_constrs_t!(model, transpose(A), rel, b)
end
# add single range constraint
function add_rangeconstr!(model::Model, inds::IVec, coeffs::FVec, lb::Float64, ub::Float64)
ret = @grb_ccall(addrangeconstr, Cint, (
Ptr{Void}, # model
Cint, # numnz
Ptr{Cint}, # cind
Ptr{Float64}, # cvals
Float64, # lower
Float64, # upper
Ptr{UInt8} # name
),
model, length(inds), inds - Cint(1), coeffs, lb, ub, C_NULL)
if ret != 0
throw(GurobiError(model.env, ret))
end
nothing
end
function add_rangeconstr!(model::Model, inds::Vector, coeffs::Vector, lb::Real, ub::Real)
add_rangeconstr!(model, ivec(inds), fvec(coeffs), Float64(lb), Float64(ub))
end
function add_rangeconstr!(model::Model, coeffs::Vector, lb::Real, ub::Real)
inds = find(coeffs)
vals = coeffs[inds]
add_rangeconstr!(model, inds, vals, lb, ub)
end
# add multiple range constraints
function add_rangeconstrs!(model::Model, cbegins::IVec, inds::IVec, coeffs::FVec, lb::FVec, ub::FVec)
m = length(cbegins)
nnz = length(inds)
(m == length(lb) == length(ub) && nnz == length(coeffs)) || error("Incompatible argument dimensions.")
if m > 0
ret = @grb_ccall(addrangeconstrs, Cint, (
Ptr{Void}, # model
Cint, # num constraints
Cint, # num non-zeros
Ptr{Cint}, # cbeg
Ptr{Cint}, # cind
Ptr{Float64}, # cval
Ptr{Float64}, # lower
Ptr{Float64}, # upper
Ptr{UInt8} # names
),
model, m, nnz, cbegins - Cint(1), inds - Cint(1), coeffs, lb, ub, C_NULL)
if ret != 0
throw(GurobiError(model.env, ret))
end
end
nothing
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.n], At.rowval, 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_vars(model)) || error("Incompatible argument dimensions.")
add_rangeconstrs_t!(model, transpose(A), lb, ub)
end
function get_constrmatrix(model::Model)
get_constrs(model::Model, 1, num_constrs(model))
end
function get_constrs(model::Model, start::Integer, len::Integer)
m = num_constrs(model)
@assert start >= 1
@assert len >= 0
@assert start + len <= m + 1
n = num_vars(model)
numnzP = Array{Cint}(1)
cbeg = Array{Cint}(len+1)
ret = @grb_ccall(getconstrs, Cint, (
Ptr{Void},
Ptr{Cint},
Ptr{Cint},
Ptr{Cint},
Ptr{Cdouble},
Cint, # start
Cint #len
),
model, numnzP, cbeg, C_NULL, C_NULL, Cint(start-1), Cint(len))
nnz = numnzP[1]
cind = Array{Cint}(nnz)
cval = Array{Cdouble}(nnz)
ret = @grb_ccall(getconstrs, Cint, (
Ptr{Void},
Ptr{Cint},
Ptr{Cint},
Ptr{Cint},
Ptr{Cdouble},
Cint, # start
Cint #len
),
model, numnzP, cbeg, cind, cval, Cint(start-1), Cint(len))
if ret != 0
throw(GurobiError(model.env, ret))
end
cbeg[end] = nnz
I = Array{Int64}(nnz)
J = Array{Int64}(nnz)
V = Array{Float64}(nnz)
for i in 1:length(cbeg)-1
for j in (cbeg[i]+1):cbeg[i+1]
I[j] = i
J[j] = cind[j]+1
V[j] = cval[j]
end
end
return sparse(I, J, V, m, n)
end
const GRB_SOS_TYPE1 = convert(Cint, 1)
const GRB_SOS_TYPE2 = convert(Cint, 2)
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 = GRB_SOS_TYPE1) : ( (sostype == :SOS2) ? (typ = GRB_SOS_TYPE2) : error("Invalid SOS constraint type") )
ret = @grb_ccall(addsos, Cint, (
Ptr{Void},
Cint,
Cint,
Ptr{Cint},
Ptr{Cint},
Ptr{Cint},
Ptr{Cdouble}
),
model, convert(Cint, 1), convert(Cint, nelem), Cint[typ], Cint[0], convert(Vector{Cint}, idx-1), weight)
if ret != 0
throw(GurobiError(model.env, ret))
end
end
function del_sos!(model::Model, idx::Vector{Cint})
numdel = length(idx)
ret = @grb_ccall(delsos, Cint, (
Ptr{Void},
Cint,
Ptr{Cint}),
model, convert(Cint,numdel), ivec(idx-1) )
if ret != 0
throw(GurobiError(model.env, ret))
end
end
get_sos_matrix(model::Model) = get_sos(model::Model, 1, num_sos(model))
function get_sos(model::Model, start::Integer, len::Integer)
numnzP = Array{Cint}(1)
m = num_sos(model)
sostype = Array{Cint}(m)
@assert m > 0
@assert start <= m
@assert len <= m
n = num_vars(model)
numnzP = Array{Cint}(1)
cbeg = Array{Cint}(len+1)
ret = @grb_ccall(getsos, Cint, (
Ptr{Void},
Ptr{Cint},
Ptr{Cint},
Ptr{Cint},
Ptr{Cint},
Ptr{Cdouble},
Cint, # start
Cint #len
),
model, numnzP, sostype, cbeg, C_NULL, C_NULL, Cint(start-1), Cint(len))
nnz = numnzP[1]
cind = Array{Cint}(nnz)
cval = Array{Cdouble}(nnz)
ret = @grb_ccall(getsos, Cint, (
Ptr{Void},
Ptr{Cint},
Ptr{Cint},
Ptr{Cint},
Ptr{Cint},
Ptr{Cdouble},
Cint, # start
Cint #len
),
model, numnzP, sostype, cbeg, cind, cval, Cint(start-1), Cint(len))
if ret != 0
throw(GurobiError(model.env, ret))
end
cbeg[end] = nnz
I = Array{Int64}(nnz)
J = Array{Int64}(nnz)
V = Array{Float64}(nnz)
for i in 1:length(cbeg)-1
for j in (cbeg[i]+1):cbeg[i+1]
I[j] = i
J[j] = cind[j]+1
V[j] = cval[j]
end
end
return sparse(I, J, V, m, n), sostype#map(x-> x==Cint(1) ? :SOS1 : :SOS2 ,sostype)
end
del_constrs!(model::Model, idx::T) where {T<:Real} = del_constrs!(model, Cint[idx])
del_constrs!(model::Model, idx::Vector{T}) where {T<:Real} = del_constrs!(model, convert(Vector{Cint},idx))
function del_constrs!(model::Model, idx::Vector{Cint})
numdel = length(idx)
ret = @grb_ccall(delconstrs, Cint, (
Ptr{Void},
Cint,
Ptr{Cint}),
model, convert(Cint,numdel), idx-Cint(1))
if ret != 0
throw(GurobiError(model.env, ret))
end
end
function chg_coeffs!(model::Model, cidx::Real, vidx::Real, val::Real)
ret = @grb_ccall(chgcoeffs, Cint, (
Ptr{Void},
Cint,
Ref{Cint},
Ref{Cint},
Ref{Float64}),
model, 1, cidx - 1, vidx - 1, val)
if ret != 0
throw(GurobiError(model.env, ret))
end
end
chg_coeffs!(model::Model, cidx::AbstractVector{<:Real}, vidx::AbstractVector{<:Real}, val::AbstractVector{<:Real}) =
chg_coeffs!(model, ivec(cidx), ivec(vidx), fvec(val))
function chg_coeffs!(model::Model, cidx::IVec, vidx::IVec, val::FVec)
(length(cidx) == length(vidx) == length(val)) || error("Inconsistent argument dimensions.")
numchgs = length(cidx)
ret = @grb_ccall(chgcoeffs, Cint, (
Ptr{Void},
Cint,
Ptr{Cint},
Ptr{Cint},
Ptr{Float64}),
model, convert(Cint,numchgs), cidx-Cint(1), vidx-Cint(1), val)
if ret != 0
throw(GurobiError(model.env, ret))
end
end
function getcoeff!(val::FVec, model::Model, cidx::Integer, vidx::Integer)
Base.depwarn("getcoeff!(val, model, cidx, vidx) is deprecated. Instead you can retrieve a coefficient without allocating a vector by doing `val = getcoeff(model, cidx, vidx)`", :grb_getcoeff)
@assert length(val) == 1
ret = @grb_ccall(getcoeff, Cint, (
Ptr{Void},
Cint,
Cint,
Ptr{Float64}),
model, cidx-Cint(1), vidx-Cint(1), val)
if ret != 0
throw(GurobiError(model.env, ret))
end
end
function getcoeff(model::Model, cidx::Integer, vidx::Integer)
out = Ref{Float64}()
ret = @grb_ccall(getcoeff, Cint, (
Ptr{Void},
Cint,
Cint,
Ref{Float64}),
model, cidx - 1, vidx - 1, out)
if ret != 0
throw(GurobiError(model.env, ret))
end
out[]
end