/
cpx_callbacks.jl
116 lines (104 loc) · 4.41 KB
/
cpx_callbacks.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
function get_best_bound(m::Model)
objval_p = Vector{Cdouble}(undef, 1)
stat = @cpx_ccall(getbestobjval, Cint, (Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cdouble}), m.env.ptr, m.lp, objval_p)
if stat != 0
throw(CplexError(m.env.ptr, stat))
end
return objval_p[1]
end
mutable struct CallbackData
cbdata::Ptr{Cvoid}
model::Model
end
export CallbackData
function setcallbackcut(cbdata::CallbackData, where::Cint, ind::Vector{Cint}, val::Vector{Cdouble}, sense::Char, rhs::Cdouble, purgeable::Cint)
len = length(ind)
@assert length(val) == len
sns = convert(Cint, sense)
stat = @cpx_ccall(cutcallbackadd, Cint, (
Ptr{Cvoid},
Ptr{Cvoid},
Cint,
Cint,
Cdouble,
Cint,
Ptr{Cint},
Ptr{Cdouble},
Cint
),
cbdata.model.env.ptr, cbdata.cbdata, where, len, rhs, sns, ind .- Cint(1), val, purgeable)
if stat != 0
throw(CplexError(cbdata.model.env.ptr, stat))
end
end
function setcallbackcutlocal(cbdata::CallbackData, where::Cint, ind::Vector{Cint}, val::Vector{Cdouble}, sense::Char, rhs::Cdouble, purgeable::Cint)
len = length(ind)
@assert length(val) == len
sns = convert(Cint, sense)
stat = @cpx_ccall(cutcallbackaddlocal, Cint, (
Ptr{Cvoid},
Ptr{Cvoid},
Cint,
Cint,
Cdouble,
Cint,
Ptr{Cint},
Ptr{Cdouble},
Cint
),
cbdata.model.env.ptr, cbdata.cbdata, where, len, rhs, sns, ind .- Cint(1), val, purgeable)
if stat != 0
throw(CplexError(cbdata.model.env.ptr, stat))
end
end
cbcut(cbdata::CallbackData, where::Cint, ind::Vector{Cint}, val::Vector{Cdouble}, sense::Char, rhs::Cdouble) =
setcallbackcut(cbdata, where, ind, val, sense, rhs, convert(Cint,CPX_USECUT_PURGE))
cbcutlocal(cbdata::CallbackData, where::Cint, ind::Vector{Cint}, val::Vector{Cdouble}, sense::Char, rhs::Cdouble) =
setcallbackcutlocal(cbdata, where, ind, val, sense, rhs, convert(Cint,CPX_USECUT_PURGE))
cblazy(cbdata::CallbackData, where::Cint, ind::Vector{Cint}, val::Vector{Cdouble}, sense::Char, rhs::Cdouble) =
setcallbackcut(cbdata, where, ind, val, sense, rhs, convert(Cint,CPX_USECUT_FORCE))
cblazylocal(cbdata::CallbackData, where::Cint, ind::Vector{Cint}, val::Vector{Cdouble}, sense::Char, rhs::Cdouble) =
setcallbackcutlocal(cbdata, where, ind, val, sense, rhs, convert(Cint,CPX_USECUT_FORCE))
function cbbranch(cbdata::CallbackData, where::Cint, idx::Cint, LU::Cchar, bd::Cdouble, nodeest::Cdouble)
seqnum = Vector{Cint}(1)
stat = @cpx_ccall(branchcallbackbranchbds, Cint, (Ptr{Cvoid},Ptr{Cvoid},Cint,Cint,Ptr{Cint},Ptr{Cchar},Ptr{Cdouble},Cdouble,Ptr{Cvoid},Ptr{Cint}),
cbdata.model.env.ptr,cbdata.cbdata,where,convert(Cint,1),[idx],[LU],[bd],nodeest,C_NULL,seqnum)
if stat != 0
throw(CplexError(cbdata.model.env.ptr, stat))
end
return seqnum[1]
end
function cbbranchconstr(cbdata::CallbackData, where::Cint, indices::Vector{Cint}, coeffs::Vector{Cdouble}, rhs::Cdouble, sense::Cchar, nodeest::Cdouble)
seqnum = Vector{Cint}(1)
stat = @cpx_ccall(branchcallbackbranchconstraints, Cint,
(Ptr{Cvoid},
Ptr{Cvoid},
Cint,
Cint,
Cint,
Ptr{Cdouble},
Ptr{Cchar},
Ptr{Cint},
Ptr{Cint},
Ptr{Cdouble},
Cdouble,
Ptr{Cvoid},
Ptr{Cint}),
cbdata.model.env.ptr,
cbdata.cbdata,
where,
convert(Cint,1),
convert(Cint,length(indices)),
[rhs],
[sense],
Cint[0],
indices,
coeffs,
nodeest,
C_NULL,
seqnum)
if stat != 0
throw(CplexError(cbdata.model.env.ptr, stat))
end
return seqnum[1]
end