forked from jump-dev/Gurobi.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grb_common.jl
94 lines (72 loc) · 2.95 KB
/
grb_common.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
# Common stuff
# Floating point constant for GRB.UNDEFINED.
const GRB_UNDEFINED = 1e101
## convenient types and type conversion functions
const GChars = Union{Cchar, Char}
const IVec = Vector{Cint}
const FVec = Vector{Float64}
const CVec = Vector{Cchar}
const GCharOrVec = Union{Cchar, Char, Vector{Cchar}, Vector{Char}}
const Bounds{T<:Real} = Union{T, Vector{T}}
const CoeffMat = Union{Matrix{Float64}, SparseArrays.SparseMatrixCSC{Float64}}
cchar(c::Cchar) = c
cchar(c::Char) = convert(Cchar, c)
ivec(v::IVec) = v
fvec(v::FVec) = v
cvec(v::CVec) = v
ivec(v::AbstractVector) = convert(IVec, v)
fvec(v::AbstractVector) = convert(FVec, v)
cvec(v::AbstractVector) = convert(CVec, v)
ivec(s::Integer) = Cint[s]
# cvecx(v, n) and fvecx(v, n)
# converts v into a vector of Cchar or Float64 of length n,
# where v can be either a scalar or a vector of length n.
_chklen(v, n::Integer) = (length(v) == n || error("Inconsistent argument dimensions."))
cvecx(c::GChars, n::Integer) = fill(cchar(c), n)
cvecx(c::Vector{Cchar}, n::Integer) = (_chklen(c, n); c)
cvecx(c::Vector{Char}, n::Integer) = (_chklen(c, n); convert(Vector{Cchar}, c))
fvecx(v::Real, n::Integer) = fill(Float64(v), n)
fvecx(v::Vector{Float64}, n::Integer) = (_chklen(v, n); v)
fvecx(v::Vector{T}, n::Integer) where {T<:Real} = (_chklen(v, n); convert(Vector{Float64}, v))
# empty vector & matrix (for the purpose of supplying default arguments)
const emptyfvec = Array{Float64}(undef, 0)
const emptyfmat = Array{Float64}(undef, 0, 0)
# macro to call a Gurobi C function
macro grb_ccall(func, args...)
f = "GRB$(func)"
args = map(esc,args)
if Sys.isunix()
return quote
ccall(($f,libgurobi), $(args...))
end
elseif Sys.iswindows()
return quote
ccall(($f,libgurobi), $(esc(:stdcall)), $(args...))
end
end
error("System not recognised.s")
end
# Gurobi library version
function getlibversion()
_major = Cint[0]
_minor = Cint[0]
_tech = Cint[0]
@grb_ccall(version, Nothing, (Ptr{Cint}, Ptr{Cint}, Ptr{Cint}), _major, _minor, _tech)
return VersionNumber(_major[1], _minor[1], _tech[1])
end
# version need not be export
# one can write Gurobi.version to get the version numbers
const version = getlibversion()
const GRB_INFINITY = 1e100
const GRB_BOUNDMAX = 1e30
function checkvalue(x::Real, bound)
abs(x) > bound && abs(x) != Inf
end
checkvalue(x::Vector, bound) = any(checkvalue(c, bound) for c in x)
_objwarning(c) = @warn("Gurobi will silently silently truncate " *
"objective coefficients >$(GRB_INFINITY) or <-$(GRB_INFINITY). Current " *
"objective coefficient extrema: $(extrema(c))")
_boundwarning(lb, ub) = @warn("Gurobi has implicit variable bounds of " *
"[-1e30, 1e30]. Settings variable bounds outside this can cause " *
"infeasibility or unboundedness. Current lower bound extrema: " *
"$(extrema(lb)). Current upper bound extrema: $(extrema(ub))")