From 04ce5d12986b59d9e7d01c3c32984fc2b6936261 Mon Sep 17 00:00:00 2001 From: odow Date: Thu, 20 Jun 2024 11:02:37 +1200 Subject: [PATCH 1/2] Simplify getter of ConstraintBasisStatus --- src/MOI_wrapper.jl | 49 ++++++++++++++++----------------------------- test/MOI_wrapper.jl | 2 +- 2 files changed, 18 insertions(+), 33 deletions(-) diff --git a/src/MOI_wrapper.jl b/src/MOI_wrapper.jl index 7e9518d..94341b6 100644 --- a/src/MOI_wrapper.jl +++ b/src/MOI_wrapper.jl @@ -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 +) + +_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, @@ -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( @@ -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 ### diff --git a/test/MOI_wrapper.jl b/test/MOI_wrapper.jl index bd67284..721b3cb 100644 --- a/test/MOI_wrapper.jl +++ b/test/MOI_wrapper.jl @@ -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 From ff4076d4fcabc59157fd8ce488a0b0d440d52885 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Thu, 20 Jun 2024 11:17:18 +1200 Subject: [PATCH 2/2] Update src/MOI_wrapper.jl --- src/MOI_wrapper.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MOI_wrapper.jl b/src/MOI_wrapper.jl index 94341b6..a034c31 100644 --- a/src/MOI_wrapper.jl +++ b/src/MOI_wrapper.jl @@ -2238,7 +2238,7 @@ const _BASIS_STATUS_MAP = Dict( kHighsBasisStatusBasic => MOI.BASIC, kHighsBasisStatusUpper => MOI.NONBASIC_AT_UPPER, kHighsBasisStatusZero => MOI.SUPER_BASIC, - kHighsBasisStatusNonbasic => MOI.NONBASIC + kHighsBasisStatusNonbasic => MOI.NONBASIC, ) _replace_basis_status(b::MOI.BasisStatusCode, ::Type{<:MOI.Interval}) = b