Skip to content

Commit

Permalink
Fix handling of model.has_int (#212)
Browse files Browse the repository at this point in the history
* Fix handling of model.has_int

* Add test
  • Loading branch information
odow authored Mar 1, 2019
1 parent cb0c922 commit bb3f45c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 54 deletions.
56 changes: 17 additions & 39 deletions src/cpx_vars.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ function add_var!(model::Model, constridx::IVec, constrcoef::FVec, l::FVec, u::F
Ptr{Cdouble},
Ptr{Ptr{Cchar}}
),
model.env.ptr, model.lp, nvars, length(constridx),
objcoef, Cint[0], constridx .- Cint(1), constrcoef,
model.env.ptr, model.lp, nvars, length(constridx),
objcoef, Cint[0], constridx .- Cint(1), constrcoef,
l, u, C_NULL)
if stat != 0
throw(CplexError(model.env, stat))
Expand Down Expand Up @@ -89,7 +89,7 @@ function c_api_getlb(model::Model, col_start::Cint, col_end::Cint)
Cint,
Cint
),
model.env.ptr, model.lp, lb,
model.env.ptr, model.lp, lb,
col_start - Cint(1), col_end - Cint(1))
if stat != 0
throw(CplexError(model.env, stat))
Expand All @@ -114,7 +114,7 @@ function get_varLB(model::Model)
return lb
end

function c_api_chgbds(model::Model, indices::IVec, lu::CVec, bd::FVec)
function c_api_chgbds(model::Model, indices::IVec, lu::CVec, bd::FVec)
cnt = length(indices)
stat = @cpx_ccall(chgbds, Cint, (
Ptr{Cvoid},
Expand Down Expand Up @@ -160,7 +160,7 @@ function c_api_getub(model::Model, col_start::Cint, col_end::Cint)
Cint,
Cint
),
model.env.ptr, model.lp, ub,
model.env.ptr, model.lp, ub,
col_start - Cint(1), col_end - Cint(1))
if stat != 0
throw(CplexError(model.env, stat))
Expand Down Expand Up @@ -206,45 +206,23 @@ function set_varUB!(model::Model, u::FVec)
end
end

function c_api_chgctype(model::Model, indices::IVec, types::CVec)
function c_api_chgctype(model::Model, indices::Vector{Cint}, types::Vector{Cchar})
nvars = length(indices)
stat = @cpx_ccall(chgctype, Cint, (
Ptr{Cvoid},
Ptr{Cvoid},
Cint,
Ptr{Cint},
Ptr{Cchar}
),
model.env.ptr, model.lp, nvars, indices .- Cint(1), types)
stat = @cpx_ccall(chgctype,
Cint,
(Ptr{Cvoid}, Ptr{Cvoid}, Cint, Ptr{Cint}, Ptr{Cchar}),
model.env.ptr, model.lp, nvars, indices .- Cint(1), types)
if any(c_type -> c_type != 'C', types)
model.has_int = true
end
if stat != 0
throw(CplexError(model.env, stat))
end
end
return stat
end

function set_vartype!(model::Model, vtype::Vector{Char})
nvars = num_var(model)
stat = @cpx_ccall(chgctype, Cint, (
Ptr{Cvoid},
Ptr{Cvoid},
Cint,
Ptr{Cint},
Ptr{Cchar}
),
model.env.ptr, model.lp, length(vtype),
Cint[0:length(vtype)-1;], convert(Vector{Cchar},vtype))
if stat != 0
throw(CplexError(model.env, stat))
end
#if !isempty(find(.!(vtype.=='C'))) # replace the line below by this one once we stop supporting Julia v0.5

if VERSION >= v"0.7.0-DEV.3382"
find_ret = findall(broadcast(!, vtype.=='C'))
else
find_ret = find(broadcast(!, vtype.=='C'))
end
if !isempty(find_ret)
model.has_int = true
end
return c_api_chgctype(model, Cint.(1:length(vtype)), Cchar.(vtype))
end

function get_vartype(model::Model)
Expand Down Expand Up @@ -302,4 +280,4 @@ function c_api_delcols(model::Model, first::Cint, last::Cint)
if stat != 0
throw(CplexError(model.env, stat))
end
end
end
24 changes: 9 additions & 15 deletions test/C_API/mip_01.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,19 @@

@testset "MIP 01" begin
env = CPLEX.Env()

model = CPLEX.Model(env, "mip_01")
CPLEX.set_sense!(model, :Max)
CPLEX.set_param!(env, "CPX_PARAM_MIPDISPLAY",1)
CPLEX.set_param!(env, "CPX_PARAM_MIPINTERVAL",1)

CPLEX.add_var!(model, 1., 0., 5.) # x
CPLEX.add_var!(model, 2., 0, 10) # y
CPLEX.add_var!(model, 5., 0, 1) # z
CPLEX.set_param!(env, "CPX_PARAM_MIPDISPLAY", 1)
CPLEX.set_param!(env, "CPX_PARAM_MIPINTERVAL", 1)
CPLEX.add_var!(model, 1.0, 0.0, 5.0) # x
CPLEX.add_var!(model, 2.0, 0.0, 10.0) # y
CPLEX.add_var!(model, 5.0, 0.0, 1.0) # z
CPLEX.set_vartype!(model, ['C', 'I', 'B'])

CPLEX.add_constr!(model, ones(3), '<', 10.)
CPLEX.add_constr!(model, [1., 2., 1.], '<', 15.)

@test model.has_int == true
CPLEX.add_constr!(model, ones(3), '<', 10.0)
CPLEX.add_constr!(model, [1.0, 2.0, 1.0], '<', 15.0)
CPLEX.optimize!(model)

sol = CPLEX.get_solution(model)
@test sol[1] 4
@test sol[2] 5
@test sol[3] 1
@test sol [4.0, 5.0, 1.0]
@test CPLEX.get_objval(model) 19
end

0 comments on commit bb3f45c

Please sign in to comment.