-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathGurobiSolverInterface.jl
575 lines (506 loc) · 19 KB
/
GurobiSolverInterface.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
# GurobiMathProgInterface
# Standardized MILP interface
export GurobiSolver
type GurobiMathProgModel <: AbstractLinearQuadraticModel
inner::Model
last_op_type::Symbol # To support arbitrary order of addVar/addCon
# Two possibilities :Var :Con
changed_constr_bounds::Bool # have we updated the bounds below?
lb::Vector{Float64} # persistent bounds on constraints to maintain
ub::Vector{Float64} # abstraction for lb ≤ Ax ≤ ub
lazycb
cutcb
heuristiccb
infocb
options
end
function GurobiMathProgModel(env=nothing;options...)
finalize_env = (env == nothing)
if env == nothing
env = Env()
end
m = GurobiMathProgModel(Model(env,""; finalize_env=finalize_env), :Con, false, Float64[], Float64[], nothing, nothing, nothing, nothing, options)
setparams!(m)
return m
end
function setparams!(m::GurobiMathProgModel)
# Helper to set the parameters on the model's copy of env rather than
# modifying the global env (ref: http://www.gurobi.com/support/faqs#P)
# Set `InfUnbdInfo` to 1 by default so infeasibility rays available
setparam!(m.inner, "InfUnbdInfo", 1)
for (name,value) in m.options
setparam!(m.inner, string(name), value)
end
end
immutable GurobiSolver <: AbstractMathProgSolver
env
options
end
GurobiSolver(env=nothing; kwargs...) = GurobiSolver(env, kwargs)
LinearQuadraticModel(s::GurobiSolver) = GurobiMathProgModel(s.env; s.options...)
ConicModel(s::GurobiSolver) = LPQPtoConicBridge(LinearQuadraticModel(s))
supportedcones(::GurobiSolver) = [:Free,:Zero,:NonNeg,:NonPos,:SOC,:SOCRotated]
loadproblem!(m::GurobiMathProgModel, filename::AbstractString) = read_model(m.inner, filename)
function loadproblem!(m::GurobiMathProgModel, A, collb, colub, obj, rowlb, rowub, sense)
# throw away old model but keep env and finalize_env
env = m.inner.env
finalize_env = m.inner.finalize_env
m.inner.finalize_env = false
free_model(m.inner)
m.inner = Model(env, "", finalize_env=finalize_env)
# Re-set options on new model's copy of env
setparams!(m)
add_cvars!(m.inner, float(obj), float(collb), float(colub))
update_model!(m.inner)
neginf = typemin(eltype(rowlb))
posinf = typemax(eltype(rowub))
# check if we have any range constraints
# to properly support these, we will need to keep track of the
# slack variables automatically added by gurobi.
rangeconstrs = any((rowlb .!= rowub) & (rowlb .> neginf) & (rowub .< posinf))
if rangeconstrs
warn("Julia Gurobi interface doesn't properly support range (two-sided) constraints. See Gurobi.jl issue #14")
add_rangeconstrs!(m.inner, float(A), float(rowlb), float(rowub))
else
b = Array(Float64,length(rowlb))
senses = Array(Cchar,length(rowlb))
for i in 1:length(rowlb)
if rowlb[i] == rowub[i]
senses[i] = '='
b[i] = rowlb[i]
elseif rowlb[i] > neginf
senses[i] = '>'
b[i] = rowlb[i]
else
@assert rowub[i] < posinf
senses[i] = '<'
b[i] = rowub[i]
end
end
add_constrs!(m.inner, float(A), senses, b)
end
m.lb, m.ub = rowlb, rowub
update_model!(m.inner)
setsense!(m,sense)
end
writeproblem(m::GurobiMathProgModel, filename::AbstractString) = write_model(m.inner, filename)
getvarLB(m::GurobiMathProgModel) = get_dblattrarray( m.inner, "LB", 1, num_vars(m.inner))
setvarLB!(m::GurobiMathProgModel, l) = set_dblattrarray!(m.inner, "LB", 1, num_vars(m.inner), l)
getvarUB(m::GurobiMathProgModel) = get_dblattrarray( m.inner, "UB", 1, num_vars(m.inner))
setvarUB!(m::GurobiMathProgModel, u) = set_dblattrarray!(m.inner, "UB", 1, num_vars(m.inner), u)
function getconstrLB(m::GurobiMathProgModel)
sense = get_charattrarray(m.inner, "Sense", 1, num_constrs(m.inner))
ret = get_dblattrarray(m.inner, "RHS", 1, num_constrs(m.inner))
for i = 1:num_constrs(m.inner)
if sense[i] == '>' || sense[i] == '='
# Do nothing
else
# LEQ constraint, so LB is -Inf
ret[i] = -Inf
end
end
return ret
end
function getconstrUB(m::GurobiMathProgModel)
sense = get_charattrarray(m.inner, "Sense", 1, num_constrs(m.inner))
ret = get_dblattrarray(m.inner, "RHS", 1, num_constrs(m.inner))
for i = 1:num_constrs(m.inner)
if sense[i] == '<' || sense[i] == '='
# Do nothing
else
# GEQ constraint, so UB is +Inf
ret[i] = +Inf
end
end
return ret
end
# setconstrLB!(m::GurobiMathProgModel, lb) = (m.changed_constr_bounds = true; m.last_op_type = :Con; m.lb = copy(lb))
# setconstrUB!(m::GurobiMathProgModel, ub) = (m.changed_constr_bounds = true; m.last_op_type = :Con; m.ub = copy(ub))
setconstrLB!(m::GurobiMathProgModel, lb) = (m.changed_constr_bounds = true; m.lb = copy(lb))
setconstrUB!(m::GurobiMathProgModel, ub) = (m.changed_constr_bounds = true; m.ub = copy(ub))
getobj(m::GurobiMathProgModel) = get_dblattrarray( m.inner, "Obj", 1, num_vars(m.inner) )
setobj!(m::GurobiMathProgModel, c) = set_dblattrarray!(m.inner, "Obj", 1, num_vars(m.inner), c)
function addvar!(m::GurobiMathProgModel, constridx, constrcoef, l, u, objcoef)
if m.last_op_type == :Con
updatemodel!(m)
m.last_op_type = :Var
end
add_var!(m.inner, length(constridx), constridx, float(constrcoef), float(objcoef), float(l), float(u), GRB_CONTINUOUS)
end
function addvar!(m::GurobiMathProgModel, l, u, objcoef)
if m.last_op_type == :Con
updatemodel!(m)
m.last_op_type = :Var
end
add_var!(m.inner, 0, Integer[], Float64[], float(objcoef), float(l), float(u), GRB_CONTINUOUS)
end
function addconstr!(m::GurobiMathProgModel, varidx, coef, lb, ub)
if m.last_op_type == :Var
updatemodel!(m)
m.last_op_type = :Con
end
if lb == -Inf
# <= constraint
add_constr!(m.inner, varidx, coef, '<', ub)
elseif ub == +Inf
# >= constraint
add_constr!(m.inner, varidx, coef, '>', lb)
elseif lb == ub
# == constraint
add_constr!(m.inner, varidx, coef, '=', lb)
else
# Range constraint
error("Adding range constraints not supported yet.")
end
end
function updatemodel!(m::GurobiMathProgModel)
update_model!(m.inner)
if m.changed_constr_bounds
# update lower/upper bounds on linear constraints (if they're consistent...)
sense = get_charattrarray(m.inner, "Sense", 1, num_constrs(m.inner))
rhs = get_dblattrarray( m.inner, "RHS", 1, num_constrs(m.inner))
lb, ub = m.lb, m.ub
@assert (n_rows = num_constrs(m.inner)) == length(lb) == length(ub) ==
length(sense) == length(rhs)
for i = 1:n_rows
if -Inf < lb[i] == ub[i] < Inf
sense[i] = '='
rhs[i] = lb[i]
elseif (-Inf < lb[i] && ub[i] < Inf)
error("Gurobi.jl does not currently support range constraints")
elseif (lb[i] == -Inf && ub[i] < Inf)
sense[i] = '<'
rhs[i] = ub[i]
elseif (lb[i] > -Inf && ub[i] == Inf)
sense[i] = '>'
rhs[i] = lb[i]
else
error("Internal error.")
end
end
set_charattrarray!(m.inner, "Sense", 1, num_constrs(m.inner), sense)
set_dblattrarray!(m.inner, "RHS", 1, num_constrs(m.inner), rhs)
m.changed_constr_bounds = false
update_model!(m.inner)
end
end
getconstrmatrix(m::GurobiMathProgModel) = get_constrmatrix(m.inner)
function setsense!(m::GurobiMathProgModel, sense)
if sense == :Min
set_sense!(m.inner, :minimize)
elseif sense == :Max
set_sense!(m.inner, :maximize)
else
error("Unrecognized objective sense $sense")
end
end
function getsense(m::GurobiMathProgModel)
v = get_intattr(m.inner, "ModelSense")
if v == -1
return :Max
else
return :Min
end
end
numvar(m::GurobiMathProgModel) = (updatemodel!(m); num_vars(m.inner))
numconstr(m::GurobiMathProgModel) = num_constrs(m.inner) + num_qconstrs(m.inner)
numlinconstr(m::GurobiMathProgModel) = num_constrs(m.inner)
numquadconstr(m::GurobiMathProgModel) = num_qconstrs(m.inner)
function optimize!(m::GurobiMathProgModel)
# set callbacks if present
if m.lazycb != nothing || m.cutcb != nothing || m.heuristiccb != nothing || m.infocb != nothing
updatemodel!(m)
if !is_mip(m.inner)
Base.warn_once("Gurobi ignores branch-and-bound callbacks when no discrete elements are present in the model.")
end
setmathprogcallback!(m)
end
if m.lazycb != nothing
setparam!(m.inner, "LazyConstraints", 1)
end
updatemodel!(m)
optimize(m.inner)
end
function status(m::GurobiMathProgModel)
s = get_status(m.inner)
if s == :optimal
return :Optimal
elseif s == :infeasible
return :Infeasible
elseif s == :unbounded
return :Unbounded
elseif s == :inf_or_unbd
Base.warn_once("Gurobi reported infeasible or unbounded. Set InfUnbdInfo=1 for more specific status.")
return :InfeasibleOrUnbounded
elseif s == :iteration_limit || s == :node_limit || s == :time_limit || s == :solution_limit
return :UserLimit
elseif s == :numeric
return :Numeric
elseif s == :suboptimal
return :Suboptimal # not very useful status
elseif s == :interrupted # ended by user?
return :UserLimit
else
error("Unrecognized solution status: $s")
end
end
getobjval(m::GurobiMathProgModel) = get_objval(m.inner)
getobjbound(m::GurobiMathProgModel) = get_objbound(m.inner)
getsolution(m::GurobiMathProgModel) = get_solution(m.inner)
function getconstrsolution(m::GurobiMathProgModel)
sense = get_charattrarray(m.inner, "Sense", 1, num_constrs(m.inner))
rhs = get_dblattrarray( m.inner, "RHS", 1, num_constrs(m.inner))
slack = get_dblattrarray( m.inner, "Slack", 1, num_constrs(m.inner))
ret = zeros(num_constrs(m.inner))
for i = 1:num_constrs(m.inner)
if sense[i] == '='
# Must be equal to RHS if feasible
ret[i] = rhs[i]
else
# Slack variable is non-negative for <= constraints, and non-positive for >= constraints
ret[i] = rhs[i] - slack[i]
end
end
return ret
end
function getreducedcosts(m::GurobiMathProgModel)
if is_qcp(m.inner) && get_int_param(m.inner, "QCPDual") == 0
return fill(NaN, num_vars(m.inner))
else
return get_dblattrarray(m.inner, "RC", 1, num_vars(m.inner))
end
end
function getconstrduals(m::GurobiMathProgModel)
if is_qcp(m.inner) && get_int_param(m.inner, "QCPDual") == 0
return fill(NaN, num_constrs(m.inner))
else
return get_dblattrarray(m.inner, "Pi", 1, num_constrs(m.inner))
end
end
function getquadconstrduals(m::GurobiMathProgModel)
if is_qcp(m.inner) && get_int_param(m.inner, "QCPDual") == 0
return fill(NaN, num_qconstrs(m.inner))
else
return get_dblattrarray(m.inner, "QCPi", 1, num_qconstrs(m.inner))
end
end
getinfeasibilityray(m::GurobiMathProgModel) = -get_dblattrarray(m.inner, "FarkasDual", 1, num_constrs(m.inner)) # note sign is flipped
getunboundedray(m::GurobiMathProgModel) = get_dblattrarray(m.inner, "UnbdRay", 1, num_vars(m.inner))
getbasis(m::GurobiMathProgModel) = get_basis(m.inner)
getrawsolver(m::GurobiMathProgModel) = m.inner
const var_type_map = Dict(
'C' => :Cont,
'B' => :Bin,
'I' => :Int,
'S' => :SemiCont,
'N' => :SemiInt
)
const rev_var_type_map = Dict(
:Cont => 'C',
:Bin => 'B',
:Int => 'I',
:SemiCont => 'S',
:SemiInt => 'N'
)
function setvartype!(m::GurobiMathProgModel, vartype::Vector{Symbol})
# do this to make sure we deal with new columns
updatemodel!(m)
nvartype = map(x->rev_var_type_map[x], vartype)
set_charattrarray!(m.inner, "VType", 1, length(nvartype), nvartype)
updatemodel!(m) # otherwise getvartype! will return old values
end
function getvartype(m::GurobiMathProgModel)
ret = get_charattrarray(m.inner, "VType", 1, num_vars(m.inner))
map(x->var_type_map[x], ret)
end
function setwarmstart!(m::GurobiMathProgModel, v)
for j = 1:length(v)
if isnan(v[j])
v[j] = 1e101 # GRB_UNDEFINED
end
end
set_dblattrarray!(m.inner, "Start", 1, num_vars(m.inner), v)
end
addsos1!(m::GurobiMathProgModel, idx, weight) = add_sos!(m.inner, :SOS1, idx, weight)
addsos2!(m::GurobiMathProgModel, idx, weight) = add_sos!(m.inner, :SOS2, idx, weight)
# Callbacks
setlazycallback!(m::GurobiMathProgModel,f) = (m.lazycb = f)
setcutcallback!(m::GurobiMathProgModel,f) = (m.cutcb = f)
setheuristiccallback!(m::GurobiMathProgModel,f) = (m.heuristiccb = f)
setinfocallback!(m::GurobiMathProgModel,f) = (m.infocb = f)
type GurobiCallbackData <: MathProgCallbackData
cbdata::CallbackData
state::Symbol
where::Cint
sol::Vector{Float64} # Used for heuristic callbacks
# model::GurobiMathProgModel # not needed?
end
function cbgetmipsolution(d::GurobiCallbackData)
@assert d.state == :MIPSol
return cbget_mipsol_sol(d.cbdata, d.where)
end
function cbgetmipsolution(d::GurobiCallbackData,output)
@assert d.state == :MIPSol
return cbget_mipsol_sol(d.cbdata, d.where, output)
end
function cbgetlpsolution(d::GurobiCallbackData)
@assert d.state == :MIPNode
return cbget_mipnode_rel(d.cbdata, d.where)
end
function cbgetlpsolution(d::GurobiCallbackData, output)
@assert d.state == :MIPNode
return cbget_mipnode_rel(d.cbdata, d.where, output)
end
# TODO: macro for these getters?
function cbgetobj(d::GurobiCallbackData)
if d.state == :MIPNode
return cbget_mipnode_objbst(d.cbdata, d.where)
elseif d.state == :MIPInfo
return cbget_mip_objbst(d.cbdata, d.where)
elseif d.state == :MIPSol
error("Gurobi does not implement cbgetobj when state == MIPSol")
# https://groups.google.com/forum/#!topic/gurobi/Az_X6Ag-y6k
# https://github.com/JuliaOpt/MathProgBase.jl/issues/23
# A complex workaround is possible but hasn't been implemented.
# return cbget_mipsol_objbst(d.cbdata, d.where)
else
error("Unrecognized callback state $(d.state)")
end
end
function cbgetbestbound(d::GurobiCallbackData)
if d.state == :MIPNode
return cbget_mipnode_objbnd(d.cbdata, d.where)
elseif d.state == :MIPSol
return cbget_mipsol_objbnd(d.cbdata, d.where)
elseif d.state == :MIPInfo
return cbget_mip_objbnd(d.cbdata, d.where)
else
error("Unrecognized callback state $(d.state)")
end
end
function cbgetexplorednodes(d::GurobiCallbackData)
if d.state == :MIPNode
return cbget_mipnode_nodcnt(d.cbdata, d.where)
elseif d.state == :MIPSol
return cbget_mipsol_nodcnt(d.cbdata, d.where)
elseif d.state == :MIPInfo
return cbget_mip_nodcnt(d.cbdata, d.where)
else
error("Unrecognized callback state $(d.state)")
end
end
# returns :MIPNode :MIPSol :Other
cbgetstate(d::GurobiCallbackData) = d.state
function cbaddcut!(d::GurobiCallbackData,varidx,varcoef,sense,rhs)
@assert d.state == :MIPNode
cbcut(d.cbdata, convert(Vector{Cint}, varidx), float(varcoef), sense, float(rhs))
end
function cbaddlazy!(d::GurobiCallbackData,varidx,varcoef,sense,rhs)
@assert d.state == :MIPNode || d.state == :MIPSol
cblazy(d.cbdata, convert(Vector{Cint}, varidx), float(varcoef), sense, float(rhs))
end
function cbaddsolution!(d::GurobiCallbackData)
# Gurobi doesn't support adding solutions on MIPSol.
# TODO: support this anyway
@assert d.state == :MIPNode
cbsolution(d.cbdata, d.sol)
# "Wipe" solution back to GRB_UNDEFINIED
for i in 1:length(d.sol)
d.sol[i] = 1e101 # GRB_UNDEFINED
end
end
function cbsetsolutionvalue!(d::GurobiCallbackData,varidx,value)
d.sol[varidx] = value
end
# breaking abstraction, define our low-level callback to eliminatate
# a level of indirection
function mastercallback(ptr_model::Ptr{Void}, cbdata::Ptr{Void}, where::Cint, userdata::Ptr{Void})
model = unsafe_pointer_to_objref(userdata)::GurobiMathProgModel
grbrawcb = CallbackData(cbdata,model.inner)
if where == CB_MIPSOL
state = :MIPSol
grbcb = GurobiCallbackData(grbrawcb, state, where, [0.0])
if model.lazycb != nothing
ret = model.lazycb(grbcb)
if ret == :Exit
terminate(model.inner)
end
end
elseif where == CB_MIPNODE
state = :MIPNode
# skip callback if node is reported to be cut off or infeasible --
# nothing to do.
# TODO: users may want this information
status = cbget_mipnode_status(grbrawcb, where)
if status != 2
return convert(Cint,0)
end
grbcb = GurobiCallbackData(grbrawcb, state, where, [0.0])
if model.cutcb != nothing
ret = model.cutcb(grbcb)
if ret == :Exit
terminate(model.inner)
end
end
if model.heuristiccb != nothing
grbcb.sol = fill(1e101, numvar(model)) # GRB_UNDEFINED
ret = model.heuristiccb(grbcb)
if ret == :Exit
terminate(model.inner)
end
end
if model.lazycb != nothing
ret = model.lazycb(grbcb)
if ret == :Exit
terminate(model.inner)
end
end
elseif where == CB_MIP
state = :MIPInfo
grbcb = GurobiCallbackData(grbrawcb, state, where, [0.0])
if model.infocb != nothing
ret = model.infocb(grbcb)
if ret == :Exit
terminate(model.inner)
end
end
end
return convert(Cint,0)
end
# User callback function should be of the form:
# callback(cbdata::MathProgCallbackData)
# return :Exit to indicate an error
function setmathprogcallback!(model::GurobiMathProgModel)
is_windows() && (Sys.WORD_SIZE == 64 || error("Callbacks not currently supported on Win32. Use 64-bit Julia with 64-bit Gurobi."))
grbcallback = cfunction(mastercallback, Cint, (Ptr{Void}, Ptr{Void}, Cint, Ptr{Void}))
ret = @grb_ccall(setcallbackfunc, Cint, (Ptr{Void}, Ptr{Void}, Any), model.inner.ptr_model, grbcallback, model)
if ret != 0
throw(GurobiError(model.env, ret))
end
nothing
end
# QCQP
function setquadobj!(m::GurobiMathProgModel, rowidx, colidx, quadval)
delq!(m.inner)
update_model!(m.inner)
scaledvals = similar(quadval)
for i in 1:length(rowidx)
if rowidx[i] == colidx[i]
# rescale from matrix format to "terms" format
scaledvals[i] = quadval[i] / 2
else
scaledvals[i] = quadval[i]
end
end
add_qpterms!(m.inner, rowidx, colidx, scaledvals)
update_model!(m.inner)
end
function addquadconstr!(m::GurobiMathProgModel, linearidx, linearval, quadrowidx, quadcolidx, quadval, sense, rhs)
if m.last_op_type == :Var
updatemodel!(m)
m.last_op_type = :Con
end
add_qconstr!(m.inner, linearidx, linearval, quadrowidx, quadcolidx, quadval, sense, rhs)
end
getsolvetime(m::GurobiMathProgModel) = get_runtime(m.inner)
getnodecount(m::GurobiMathProgModel) = get_node_count(m.inner)