Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unsafe_convert method for CbcProblem. Resolve #100. #101

Merged
merged 5 commits into from
Jan 31, 2019
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 26 additions & 25 deletions src/CbcCInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,13 @@ mutable struct CbcModel
end
end

Base.unsafe_convert(::Type{Ptr{Cvoid}}, prob::CbcModel) = prob.p

function deleteModel(prob::CbcModel)
if prob.p == C_NULL
return
end
@cbc_ccall deleteModel Cvoid (Ptr{Cvoid},) prob.p
@cbc_ccall deleteModel Cvoid (Ptr{Cvoid},) prob
prob.p = C_NULL
return
end
Expand All @@ -98,7 +100,7 @@ end
macro getproperty(T, name)
@eval function ($name)(prob::CbcModel)
check_problem(prob)
@cbc_ccall $name $T (Ptr{Cvoid},) prob.p
@cbc_ccall $name $T (Ptr{Cvoid},) prob
end
end

Expand Down Expand Up @@ -138,60 +140,60 @@ function loadProblem(prob::CbcModel,

@cbc_ccall loadProblem Cvoid (Ptr{Cvoid}, Int32, Int32, Ptr{CoinBigIndex},
Ptr{Int32}, Ptr{Float64}, Ptr{Float64}, Ptr{Float64},
Ptr{Float64}, Ptr{Float64}, Ptr{Float64}) prob.p ncol nrow mat.colptr.-Int32(1) mat.rowval.-Int32(1) mat.nzval vec_or_null(Float64, col_lb, ncol) vec_or_null(Float64, col_ub, ncol) vec_or_null(Float64, obj, ncol) vec_or_null(Float64, row_lb, nrow) vec_or_null(Float64, row_ub, nrow)
Ptr{Float64}, Ptr{Float64}, Ptr{Float64}) prob ncol nrow mat.colptr.-Int32(1) mat.rowval.-Int32(1) mat.nzval vec_or_null(Float64, col_lb, ncol) vec_or_null(Float64, col_ub, ncol) vec_or_null(Float64, obj, ncol) vec_or_null(Float64, row_lb, nrow) vec_or_null(Float64, row_ub, nrow)
tkoolen marked this conversation as resolved.
Show resolved Hide resolved
end

function readMps(prob::CbcModel, filename::String)
check_problem(prob)
@assert isascii(filename)
@cbc_ccall readMps Cint (Ptr{Cvoid}, Ptr{UInt8}) prob.p filename
@cbc_ccall readMps Cint (Ptr{Cvoid}, Ptr{UInt8}) prob filename
end

function writeMps(prob::CbcModel, filename::String)
check_problem(prob)
@assert isascii(filename)
@cbc_ccall writeMps Cint (Ptr{Cvoid}, Ptr{UInt8}) prob.p filename
@cbc_ccall writeMps Cint (Ptr{Cvoid}, Ptr{UInt8}) prob filename
end

function setInitialSolution(prob::CbcModel, array::Vector{Float64})
check_problem(prob)
@cbc_ccall setInitialSolution Cvoid (Ptr{Cvoid}, Ptr{Float64}) prob.p array
@cbc_ccall setInitialSolution Cvoid (Ptr{Cvoid}, Ptr{Float64}) prob array
end

function problemName(prob::CbcModel)
check_problem(prob)
a = Array(UInt8, 100)
@cbc_ccall problemName Cvoid (Ptr{Cvoid},Cint,Ptr{UInt8}) prob.p 100 a
@cbc_ccall problemName Cvoid (Ptr{Cvoid},Cint,Ptr{UInt8}) prob 100 a
return string(a)
end

#function setProblemName(prob::CbcModel)
# check_problem(prob)
# @cbc_ccall setProblemName Cint (Ptr{Cvoid},Ptr{UInt8}) prob.p bytestring(a)
# @cbc_ccall setProblemName Cint (Ptr{Cvoid},Ptr{UInt8}) prob bytestring(a)
#end

function getNumElements(prob::CbcModel)
check_problem(prob)
@cbc_ccall getNumElements Cint (Ptr{Cvoid},) prob.p
@cbc_ccall getNumElements Cint (Ptr{Cvoid},) prob
end

function getVectorStarts(prob::CbcModel)
check_problem(prob)
p = @cbc_ccall getVectorStarts Ptr{CoinBigIndex} (Ptr{Cvoid},) prob.p
p = @cbc_ccall getVectorStarts Ptr{CoinBigIndex} (Ptr{Cvoid},) prob
num_cols = Int(getNumCols(prob))
return copy(unsafe_wrap(Array,p,(num_cols+1,)))
end

function getIndices(prob::CbcModel)
check_problem(prob)
p = @cbc_ccall getIndices Ptr{Cint} (Ptr{Cvoid},) prob.p
p = @cbc_ccall getIndices Ptr{Cint} (Ptr{Cvoid},) prob
nnz = Int(getNumElements(prob))
return copy(unsafe_wrap(Array,p,(nnz,)))
end

function getElements(prob::CbcModel)
check_problem(prob)
p = @cbc_ccall getElements Ptr{Float64} (Ptr{Cvoid},) prob.p
p = @cbc_ccall getElements Ptr{Float64} (Ptr{Cvoid},) prob
nnz = Int(getNumElements(prob))
return copy(unsafe_wrap(Array,p,(nnz,)))
end
Expand All @@ -205,7 +207,7 @@ end
# 1 : minimize, -1 : maximize
function setObjSense(prob::CbcModel, sense)
check_problem(prob)
@cbc_ccall setObjSense Cvoid (Ptr{Cvoid}, Float64) prob.p sense
@cbc_ccall setObjSense Cvoid (Ptr{Cvoid}, Float64) prob sense
end

@getproperty Float64 getObjSense
Expand All @@ -214,7 +216,7 @@ for s in (:getRowLower, :getRowUpper, :getRowActivity)
@eval function ($s)(prob::CbcModel)
check_problem(prob)
nrow = Int(getNumRows(prob))
p = @cbc_ccall $s Ptr{Float64} (Ptr{Cvoid},) prob.p
p = @cbc_ccall $s Ptr{Float64} (Ptr{Cvoid},) prob
return copy(unsafe_wrap(Array,p,(nrow,)))
end
end
Expand All @@ -223,36 +225,36 @@ for s in (:getColLower, :getColUpper, :getObjCoefficients, :getColSolution)
@eval function ($s)(prob::CbcModel)
check_problem(prob)
ncol = Int(getNumCols(prob))
p = @cbc_ccall $s Ptr{Float64} (Ptr{Cvoid},) prob.p
p = @cbc_ccall $s Ptr{Float64} (Ptr{Cvoid},) prob
return copy(unsafe_wrap(Array,p,(ncol,)))
end
end

for s in (:setRowUpper, :setRowLower, :setObjCoeff, :setColLower, :setColUpper)
@eval function($s)(prob::CbcModel, index::Integer, value::Float64)
check_problem(prob)
@cbc_ccall $s Cvoid (Ptr{Cvoid}, Cint, Float64) prob.p index value
@cbc_ccall $s Cvoid (Ptr{Cvoid}, Cint, Float64) prob index value
end
end

function isInteger(prob::CbcModel, index::Integer)
check_problem(prob)
v = @cbc_ccall isInteger Cint (Ptr{Cvoid},Cint) prob.p index
v = @cbc_ccall isInteger Cint (Ptr{Cvoid},Cint) prob index
return v == 1
end

function setContinuous(prob::CbcModel, index::Integer)
check_problem(prob)
@cbc_ccall setContinuous Cvoid (Ptr{Cvoid},Cint) prob.p index
@cbc_ccall setContinuous Cvoid (Ptr{Cvoid},Cint) prob index
end

function setInteger(prob::CbcModel, index::Integer)
check_problem(prob)
@cbc_ccall setInteger Cvoid (Ptr{Cvoid},Cint) prob.p index
@cbc_ccall setInteger Cvoid (Ptr{Cvoid},Cint) prob index
end

function Base.copy(prob::CbcModel)
p = @cbc_ccall clone Ptr{Cvoid} (Ptr{Cvoid},) prob.p
p = @cbc_ccall clone Ptr{Cvoid} (Ptr{Cvoid},) prob
prob = CbcModel(p)
@compat finalizer(deleteModel, prob)
return prob
Expand All @@ -261,13 +263,13 @@ end
function setParameter(prob::CbcModel, name::String, value::String)
@assert isascii(name)
@assert isascii(value)
@cbc_ccall setParameter Cvoid (Ptr{Cvoid},Ptr{UInt8},Ptr{UInt8}) prob.p name value
@cbc_ccall setParameter Cvoid (Ptr{Cvoid},Ptr{UInt8},Ptr{UInt8}) prob name value
end

# TODO: registerCallBack clearCallBack

function solve(prob::CbcModel)
@cbc_ccall solve Cint (Ptr{Cvoid},) prob.p
@cbc_ccall solve Cint (Ptr{Cvoid},) prob
end

@getproperty Float64 sumPrimalInfeasibilities
Expand All @@ -281,7 +283,7 @@ for s in (:isAbandoned, :isProvenOptimal, :isProvenInfeasible, :isContinuousUnbo
:isNodeLimitReached, :isSecondsLimitReached, :isSolutionLimitReached, :isInitialSolveAbandoned, :isInitialSolveProvenOptimal, :isInitialSolveProvenPrimalInfeasible)
@eval function ($s)(prob::CbcModel)
check_problem(prob)
v = @cbc_ccall $s Cint (Ptr{Cvoid},) prob.p
v = @cbc_ccall $s Cint (Ptr{Cvoid},) prob
return v != 0
end
end
Expand All @@ -296,11 +298,10 @@ function addSOS(prob::CbcModel, numRows::Integer, rowStarts::Vector{Cint},
colIndices::Vector{Cint}, weights::Vector{Float64}, typ::Integer)

@cbc_ccall(addSOS,Cvoid,(Ptr{Cvoid}, Cint, Ptr{Cint}, Ptr{Cint},
Ptr{Float64}, Cint),prob.p,numRows,
Ptr{Float64}, Cint), prob, numRows,
rowStarts .- convert(Cint,1),
colIndices .- convert(Cint,1),
weights, typ)

end

# see Cbc_C_Interface.h documentation
Expand Down