-
Notifications
You must be signed in to change notification settings - Fork 63
/
branch_jump.jl
64 lines (56 loc) · 2.16 KB
/
branch_jump.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
using CPLEX, JuMP, MathProgBase
mod = Model(solver=CplexSolver(CPX_PARAM_PRELINEAR=0, CPX_PARAM_PREIND=0, CPX_PARAM_ADVIND=0, CPX_PARAM_MIPSEARCH=1,CPX_PARAM_MIPCBREDLP=0))
m_internal = MathProgBase.model(CplexSolver())
MathProgBase.loadproblem!(m_internal, "/Users/huchette/Applications/IBM/ILOG/CPLEX_Studio126/cplex/examples/data/location.lp")
# grab MathProgBase data
c = MathProgBase.getobj(m_internal)
A = MathProgBase.getconstrmatrix(m_internal)
m, n = size(A)
xlb = MathProgBase.getvarLB(m_internal)
xub = MathProgBase.getvarUB(m_internal)
l = MathProgBase.getconstrLB(m_internal)
u = MathProgBase.getconstrUB(m_internal)
vtypes = MathProgBase.getvartype(m_internal)
# populate JuMP model with data from internal model
@defVar(mod, x[1:n])
for i in 1:n
setLower(x[i], xlb[i])
setUpper(x[i], xub[i])
(vtypes[i] == 'I' || vtypes[i] == 'B') ? mod.colCat[x[i].col] = INTEGER : nothing # change vartype to integer when appropriate
end
At = A' # transpose to get useful row-wise sparse representation
for i in 1:At.n
@addConstraint( mod, l[i] <= sum{ At.nzval[idx]*x[At.rowval[idx]], idx = At.colptr[i]:(At.colptr[i+1]-1) } <= u[i] )
end
@setObjective(mod, Min, sum{ c[i]*x[i], i=1:n })
function callback(cb)
# println("in callback!")
xval = MathProgBase.cbgetlpsolution(cb)
objval = cbgetnodeobjval(cb)
best = MathProgBase.cbgetobj(cb)
feas = cbgetintfeas(cb)
if MathProgBase.cbgetexplorednodes(cb) < 2
maxinf = maxobj = bestj = -Inf
for j in 1:getNumVars(mod)
if feas[j] == 1
xj_inf = xval[j] - floor(xval[j])
if xj_inf > 0.5
xj_inf = 1.0 - xj_inf
end
if (xj_inf >= maxinf) && (xj_inf > maxinf || abs(c[j]) >= maxobj)
bestj = j
maxinf = xj_inf
maxobj = abs(c[j])
end
end
end
if bestj >= 1
xj_lo = floor(xval[bestj])
addBranch(cb, x[bestj] >= xj_lo + 1, objval)
addBranch(cb, x[bestj] <= xj_lo, objval)
end
end
end
solve(mod; load_model_only=true)
setBranchCallback(mod, callback)
solve(mod)