Skip to content

Commit

Permalink
Merge branch 'linprog_workarounds'
Browse files Browse the repository at this point in the history
  • Loading branch information
carlobaldassi committed May 8, 2012
2 parents 7929d4d + 6f9250a commit bbe4e97
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 100 deletions.
98 changes: 74 additions & 24 deletions extras/glpk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
### GLPK API Wrapper
###

# Note: be sure to load "sparse.jl" before this file

## Shared library interface setup
#{{{
load("glpk_h.jl")
Expand Down Expand Up @@ -128,10 +126,21 @@ end
# In this framework, optional arguments can be passed either
# as an empty vector [] or as the 'nothing' constant

typealias VecOrNothing{T} Union(AbstractVector{T}, AbstractVector{None}, Nothing)
typealias MatOrNothing{T} Union(AbstractMatrix{T}, AbstractVector{None}, Nothing)
_jl_glpk__is_empty{T}(x::Union(VecOrNothing{T}, MatOrNothing{T})) =
isa(x, Union(AbstractVector{None}, Nothing))
typealias VecOrNothing Union(Vector, Nothing)
function _jl_glpk__convert_vecornothing{T}(::Type{T}, a::VecOrNothing)
if (a == nothing) || (isa(a, Array{Any}) && isempty(a))
return T[]
elseif T <: Integer
if !(eltype(a) <: Integer)
throw(GLPError("integer-valued array required, or [] or nothing"))
end
elseif T <: Real
if !(eltype(a) <: Real)
throw(GLPError("real-valued array required, or [] or nothing"))
end
end
convert(Array{T}, a)
end

# General exception: all GLP functions
# throw this in case of errors
Expand Down Expand Up @@ -431,7 +440,7 @@ function _jl_glpk__check_vectors_size(numel::Integer, vecs...)
end
if numel > 0
for v = vecs
if _jl_glpk__is_empty(v)
if isempty(v)
throw(GLPError("Number of elements is $numel but vector is empty or nothing"))
elseif length(v) < numel
throw(GLPError("Wrong vector size: $(length(v)) (numel declared as $numel)"))
Expand Down Expand Up @@ -745,7 +754,7 @@ function glp_set_obj_coef(glp_prob::GLPProb, col::Integer, coef::Real)
@glpk_ccall set_obj_coef Void (Ptr{Void}, Int32, Float64) glp_prob.p col coef
end

function glp_set_mat_row{Ti<:Integer, Tv<:Real}(glp_prob::GLPProb, row::Integer, len::Integer, ind::VecOrNothing{Ti}, val::Vector{Tv})
function glp_set_mat_row{Ti<:Integer, Tv<:Real}(glp_prob::GLPProb, row::Integer, len::Integer, ind::Vector{Ti}, val::Vector{Tv})
_jl_glpk__check_glp_prob(glp_prob)
_jl_glpk__check_vectors_size(len, ind, val)
_jl_glpk__check_row_is_valid(glp_prob, row)
Expand All @@ -759,8 +768,10 @@ function glp_set_mat_row{Ti<:Integer, Tv<:Real}(glp_prob::GLPProb, row::Integer,

@glpk_ccall set_mat_row Void (Ptr{Void}, Int32, Int32, Ptr{Int32}, Ptr{Float64}) glp_prob row len ind32p val64p
end
glp_set_mat_row{Tv<:Real}(glp_prob::GLPProb, row::Integer, len::Integer, ::Nothing, val::Vector{Tv}) =
glp_set_mat_row(glp_prob, row, len, Int32[], val)

function glp_set_mat_col{Ti<:Integer, Tv<:Real}(glp_prob::GLPProb, col::Integer, len::Integer, ind::VecOrNothing{Ti}, val::Vector{Tv})
function glp_set_mat_col{Ti<:Integer, Tv<:Real}(glp_prob::GLPProb, col::Integer, len::Integer, ind::Vector{Ti}, val::Vector{Tv})
_jl_glpk__check_glp_prob(glp_prob)
_jl_glpk__check_vectors_size(len, ind, val)
_jl_glpk__check_col_is_valid(glp_prob, col)
Expand All @@ -774,8 +785,10 @@ function glp_set_mat_col{Ti<:Integer, Tv<:Real}(glp_prob::GLPProb, col::Integer,

@glpk_ccall set_mat_col Void (Ptr{Void}, Int32, Int32, Ptr{Int32}, Ptr{Float64}) glp_prob col len ind32p val64p
end
glp_set_mat_col{Tv<:Real}(glp_prob::GLPProb, col::Integer, len::Integer, ::Nothing, val::Vector{Tv}) =
glp_set_mat_col(glp_prob::GLPProb, col, len, Int32[], val)

function glp_load_matrix{Ti<:Integer, Tv<:Real}(glp_prob::GLPProb, numel::Integer, ia::VecOrNothing{Ti}, ja::VecOrNothing{Ti}, ar::VecOrNothing{Tv})
function glp_load_matrix{Ti<:Integer, Tv<:Real}(glp_prob::GLPProb, numel::Integer, ia::Vector{Ti}, ja::Vector{Ti}, ar::Vector{Tv})
_jl_glpk__check_glp_prob(glp_prob)
_jl_glpk__check_vectors_size(numel, ia, ja, ar)
if numel == 0
Expand All @@ -794,6 +807,12 @@ function glp_load_matrix{Ti<:Integer, Tv<:Real}(glp_prob::GLPProb, numel::Intege

@glpk_ccall load_matrix Void (Ptr{Void}, Int32, Ptr{Int32}, Ptr{Int32}, Ptr{Float64}) glp_prob.p numel ia32p ja32p ar64p
end
function glp_load_matrix{Tv<:Real}(glp_prob::GLPProb, numel::Integer, ia::VecOrNothing, ja::VecOrNothing, ar::VecOrNothing)
cia = _jl_glpk__convert_vecornothing(Int32, ia)
cja = _jl_glpk__convert_vecornothing(Int32, ja)
car = _jl_glpk__convert_vecornothing(Float64, ar)
return glp_load_matrix(glp_prob, numel, cia, cja, car)
end

glp_load_matrix{Ti<:Integer, Tv<:Real}(glp_prob::GLPProb, ia::AbstractVector{Ti}, ja::AbstractVector{Ti}, ar::AbstractVector{Tv}) =
glp_load_matrix(glp_prob, length(ar), ia, ja, ar)
Expand All @@ -803,7 +822,7 @@ function glp_load_matrix{Ti<:Integer, Tv<:Real}(glp_prob::GLPProb, a::SparseMatr
glp_load_matrix(glp_prob, ia, ja, ar)
end

function glp_check_dup{Ti<:Integer}(rows::Integer, cols::Integer, numel::Integer, ia::VecOrNothing{Ti}, ja::VecOrNothing{Ti})
function glp_check_dup{Ti<:Integer}(rows::Integer, cols::Integer, numel::Integer, ia::Vector{Ti}, ja::Vector{Ti})
_jl_glpk__check_rows_and_cols(rows, cols)
_jl_glpk__check_vectors_size(numel, ia, ja)
ia32 = int32(ia)
Expand All @@ -816,6 +835,11 @@ function glp_check_dup{Ti<:Integer}(rows::Integer, cols::Integer, numel::Integer
k = @glpk_ccall check_dup Int32 (Int32, Int32, Int32, Ptr{Int32}, Ptr{Int32}) rows cols numel ia32p ja32p
return k
end
function glp_check_dup(rows::Integer, cols::Integer, numel::Integer, ia::VecOrNothing, ja::VecOrNothing)
cia = _jl_glpk__convert_vecornothing(Int32, ia)
cja = _jl_glpk__convert_vecornothing(Int32, ja)
return glp_check_dup(rows, cols, numel, cia, cja)
end

glp_check_dup{Ti<:Integer}(rows::Integer, cols::Integer, ia::AbstractVector{Ti}, ja::AbstractVector{Ti}) =
glp_check_dup(rows, cols, length(ia), ia, ja)
Expand Down Expand Up @@ -971,33 +995,51 @@ function glp_get_num_nz(glp_prob::GLPProb)
return num_nz
end

function glp_get_mat_row(glp_prob::GLPProb, row::Integer, ind::VecOrNothing{Int32}, val::VecOrNothing{Float64})
function glp_get_mat_row(glp_prob::GLPProb, row::Integer, ind::Union(Vector{Int32},Nothing), val::Union(Vector{Float64},Nothing))
_jl_glpk__check_glp_prob(glp_prob)
_jl_glpk__check_row_is_valid(glp_prob, row)
numel = @glpk_ccall get_mat_row Int32 (Ptr{Void}, Int32, Ptr{Int32}, Ptr{Float64}) glp_prob row C_NULL C_NULL
if numel == 0
return 0
end
_jl_glpk__check_vectors_size(numel, ind, val)
off32 = sizeof(Int32)
off64 = sizeof(Float64)
ind32p = pointer(ind) - off32
val64p = pointer(val) - off64
if !isequal(ind, nothing)
_jl_glpk__check_vectors_size(numel, ind)
off32 = sizeof(Int32)
ind32p = pointer(ind) - off32
else
ind32p = C_NULL
end
if !isequal(val, nothing)
_jl_glpk__check_vectors_size(numel, val)
off64 = sizeof(Float64)
val64p = pointer(val) - off64
else
val64p = C_NULL
end
numel = @glpk_ccall get_mat_row Int32 (Ptr{Void}, Int32, Ptr{Int32}, Ptr{Float64}) glp_prob row ind32p int64p
end

function glp_get_mat_col(glp_prob::GLPProb, col::Integer, ind::VecOrNothing{Int32}, val::VecOrNothing{Float64})
function glp_get_mat_col(glp_prob::GLPProb, col::Integer, ind::Union(Vector{Int32},Nothing), val::Union(Vector{Float64},Nothing))
_jl_glpk__check_glp_prob(glp_prob)
_jl_glpk__check_col_is_valid(glp_prob, col)
numel = @glpk_ccall get_mat_col Int32 (Ptr{Void}, Int32, Ptr{Int32}, Ptr{Float64}) glp_prob col C_NULL C_NULL
if numel == 0
return 0
end
_jl_glpk__check_vectors_size(numel, ind, val)
off32 = sizeof(Int32)
off64 = sizeof(Float64)
ind32p = pointer(ind) - off32
val64p = pointer(val) - off64
if !isequal(ind, nothing)
_jl_glpk__check_vectors_size(numel, ind)
off32 = sizeof(Int32)
ind32p = pointer(ind) - off32
else
ind32p = C_NULL
end
if !isequal(val, nothing)
_jl_glpk__check_vectors_size(numel, val)
off64 = sizeof(Float64)
val64p = pointer(val) - off64
else
val64p = C_NULL
end
numel = @glpk_ccall get_mat_col Int32 (Ptr{Void}, Int32, Ptr{Int32}, Ptr{Float64}) glp_prob col ind32p int64p
end

Expand Down Expand Up @@ -1469,7 +1511,7 @@ function glp_write_mip(glp_prob::GLPProb, filename::String)
return ret
end

function glp_print_ranges(glp_prob::GLPProb, len::Integer, list::VecOrNothing{Int32}, flags::Integer, filename::String)
function glp_print_ranges{Ti<:Integer}(glp_prob::GLPProb, len::Integer, list::Vector{Ti}, flags::Integer, filename::String)
_jl_glpk__check_glp_prob(glp_prob)
_jl_glpk__check_vectors_size(len, list)
_jl_glpk__check_list_ids(glp_prob, len, list)
Expand All @@ -1482,6 +1524,14 @@ function glp_print_ranges(glp_prob::GLPProb, len::Integer, list::VecOrNothing{In

ret = @glpk_ccall print_ranges Int32 (Ptr{Void}, Int32, Ptr{Int32}, Int32, Ptr{Uint8}) glp_prob.p len ind32p cstring(filename)
end
function glp_print_ranges(glp_prob::GLPProb, len::Integer, ::Nothing, flags::Integer, filename::String)
_jl_glpk__check_glp_prob(glp_prob)
if len != 0; throw(GLPError("list passed as nothing bit len != 0 ($len)")); end
_jl_glpk_bf_exists(glp_prob.p)
_jl_glpk__check_file_is_writable(filename)

ret = @glpk_ccall print_ranges Int32 (Ptr{Void}, Int32, Ptr{Int32}, Int32, Ptr{Uint8}) glp_prob.p len C_NULL cstring(filename)
end

function glp_bf_exists(glp_prob::GLPProb)
_jl_glpk__check_glp_prob(glp_prob)
Expand Down
Loading

0 comments on commit bbe4e97

Please sign in to comment.