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

Simplify getter of ConstraintBasisStatus #217

Merged
merged 2 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
49 changes: 17 additions & 32 deletions src/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2233,23 +2233,22 @@ end
### MOI.ConstraintBasisStatus
###

# HiGHS only reports a single basis status for each ranged constraint. Therefore
# if the status is kHighsBasisStatusLower or kHighsBasisStatusUpper, we must
# distinguish whether or not it is refering to the given constraint.
function _nonbasic_status(code::HighsInt, ::Type{<:MOI.Interval})
if code == kHighsBasisStatusLower
return MOI.NONBASIC_AT_LOWER
else
return MOI.NONBASIC_AT_UPPER
const _BASIS_STATUS_MAP = Dict(
kHighsBasisStatusLower => MOI.NONBASIC_AT_LOWER,
kHighsBasisStatusBasic => MOI.BASIC,
kHighsBasisStatusUpper => MOI.NONBASIC_AT_UPPER,
kHighsBasisStatusZero => MOI.SUPER_BASIC,
kHighsBasisStatusNonbasic => MOI.NONBASIC
odow marked this conversation as resolved.
Show resolved Hide resolved
)

_replace_basis_status(b::MOI.BasisStatusCode, ::Type{<:MOI.Interval}) = b

function _replace_basis_status(basis::MOI.BasisStatusCode, ::Type{S}) where {S}
if basis == MOI.NONBASIC_AT_LOWER || basis == MOI.NONBASIC_AT_UPPER
return MOI.NONBASIC
end
return basis
end
function _nonbasic_status(code::HighsInt, ::Type{<:MOI.LessThan})
return code == kHighsBasisStatusLower ? MOI.BASIC : MOI.NONBASIC
end
function _nonbasic_status(code::HighsInt, ::Type{<:MOI.GreaterThan})
return code == kHighsBasisStatusLower ? MOI.NONBASIC : MOI.BASIC
end
_nonbasic_status(::HighsInt, ::Type{<:MOI.EqualTo}) = MOI.NONBASIC

function MOI.get(
model::Optimizer,
Expand All @@ -2260,15 +2259,8 @@ function MOI.get(
stat = get(model.solution.rowstatus, row(model, c) + 1, nothing)
if stat === nothing
throw(MOI.GetAttributeNotAllowed(attr, "no basis is present"))
elseif stat == kHighsBasisStatusLower
return _nonbasic_status(stat, S)
elseif stat == kHighsBasisStatusBasic
return MOI.BASIC
elseif stat == kHighsBasisStatusUpper
return _nonbasic_status(stat, S)
end
@assert stat == kHighsBasisStatusZero || stat == kHighsBasisStatusNonbasic
return MOI.NONBASIC
return _replace_basis_status(_BASIS_STATUS_MAP[stat], S)
end

function MOI.get(
Expand All @@ -2280,15 +2272,8 @@ function MOI.get(
stat = get(model.solution.colstatus, column(model, x) + 1, nothing)
if stat === nothing
throw(MOI.GetAttributeNotAllowed(attr, "no basis is present"))
elseif stat == kHighsBasisStatusLower
return MOI.NONBASIC_AT_LOWER
elseif stat == kHighsBasisStatusBasic
return MOI.BASIC
elseif stat == kHighsBasisStatusUpper
return MOI.NONBASIC_AT_UPPER
end
@assert stat == kHighsBasisStatusZero || stat == kHighsBasisStatusNonbasic
return MOI.NONBASIC
end
return _BASIS_STATUS_MAP[stat]
end

###
Expand Down
2 changes: 1 addition & 1 deletion test/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ function test_variable_basis_status_zero()
MOI.set(model, MOI.Silent(), true)
x = MOI.add_variable(model)
MOI.optimize!(model)
@test MOI.get(model, MOI.VariableBasisStatus(), x) == MOI.NONBASIC
@test MOI.get(model, MOI.VariableBasisStatus(), x) == MOI.SUPER_BASIC
return
end

Expand Down
Loading