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

Fix is_valid for ZeroOne and Integer constraints in MOI wrapper #277

Merged
merged 1 commit into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 3 additions & 13 deletions src/MOI_wrapper/variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,15 @@ function MOI.set(
end

# TODO: is actually wrong for unbounded variables?
function MOI.is_valid(o::Optimizer, ci::CI{VI,S}) where {S<:BOUNDS}
function MOI.is_valid(o::Optimizer, ci::CI{VI,S}) where {S<:Union{<:BOUNDS,MOI.ZeroOne,MOI.Integer}}
if !haskey(o.inner.vars, VarRef(ci.value))
return false
end
cons_set = get(o.constypes, (VI, S), nothing)
if cons_set === nothing
return false
end
return ConsRef(ci.value) in o.constypes[VI, S]
return ConsRef(ci.value) in cons_set
end

function MOI.get(
Expand Down Expand Up @@ -306,16 +306,6 @@ function MOI.get(
return from_bounds(S, lb, ub)
end

function MOI.is_valid(
o::Optimizer,
ci::CI{VI,S},
) where {S<:Union{MOI.ZeroOne,MOI.Integer}}
if !haskey(o.inner.vars, VarRef(ci.value))
return false
end
return ConsRef(ci.value) in o.constypes[VI, S]
end

function MOI.get(o::Optimizer, ::MOI.ConstraintSet, ci::CI{VI,MOI.ZeroOne})
vi = VI(ci.value)
v = var(o, vi)
Expand Down Expand Up @@ -396,7 +386,7 @@ function get_original_variables(vars::Array{Ptr{SCIP_VAR}}, nvars::Int)
orig_vars = map(1:nvars) do i
var = Ref(vars[i])
@SCIP_CALL SCIPvarGetOrigvarSum(var, scalar, constant)
@assert scalar[] == 1.0
@assert scalar[] == 1.0
@assert constant[] == 0.0
var[]
end
Expand Down
22 changes: 22 additions & 0 deletions test/MOI_additional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -557,3 +557,25 @@ end
MOI.set(optimizer, SCIP.Presolving(), presolving)
@test MOI.get(optimizer, SCIP.Presolving()) == presolving
end

@testset "is_valid_zeroone" begin
model = SCIP.Optimizer()
x = MOI.add_variable(model)
c = MOI.ConstraintIndex{MOI.VariableIndex,MOI.ZeroOne}(x.value)
@test !MOI.is_valid(model, c)
MOI.add_constraint(model, x, MOI.ZeroOne())
@test MOI.is_valid(model, c)
MOI.delete(model, c)
@test !MOI.is_valid(model, c)
end

@testset "is_valid_integer" begin
model = SCIP.Optimizer()
x = MOI.add_variable(model)
c = MOI.ConstraintIndex{MOI.VariableIndex,MOI.Integer}(x.value)
@test !MOI.is_valid(model, c)
MOI.add_constraint(model, x, MOI.Integer())
@test MOI.is_valid(model, c)
MOI.delete(model, c)
@test !MOI.is_valid(model, c)
end
Loading