Skip to content

Commit

Permalink
[MOI] add support for MOI.Interval{Float64} (MadNLP#295)
Browse files Browse the repository at this point in the history
  • Loading branch information
frapac authored Feb 13, 2024
1 parent 7cb834f commit bd5008d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
19 changes: 17 additions & 2 deletions src/Interfaces/MOI_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,12 @@ function Optimizer(; kwargs...)
)
end

const _SETS =
Union{MOI.GreaterThan{Float64},MOI.LessThan{Float64},MOI.EqualTo{Float64}}
const _SETS = Union{
MOI.GreaterThan{Float64},
MOI.LessThan{Float64},
MOI.EqualTo{Float64},
MOI.Interval{Float64},
}

const _FUNCTIONS = Union{
MOI.ScalarAffineFunction{Float64},
Expand Down Expand Up @@ -1095,6 +1099,17 @@ function MOI.get(
return rc
end

function MOI.get(
model::Optimizer,
attr::MOI.ConstraintDual,
ci::MOI.ConstraintIndex{MOI.VariableIndex,MOI.Interval{Float64}},
)
MOI.check_result_index_bounds(model, attr)
MOI.throw_if_not_valid(model, ci)
rc = model.result.multipliers_L[ci.value] - model.result.multipliers_U[ci.value]
return rc
end

### MOI.NLPBlockDual

function MOI.get(model::Optimizer, attr::MOI.NLPBlockDual)
Expand Down
34 changes: 26 additions & 8 deletions src/Interfaces/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,24 @@ _function_info(::MOI.ScalarQuadraticFunction) = _kFunctionTypeScalarQuadratic
_kBoundTypeLessThan,
_kBoundTypeGreaterThan,
_kBoundTypeEqualTo,
_kBoundTypeInterval,
)

_set_info(s::MOI.LessThan) = _kBoundTypeLessThan, -Inf, s.upper
_set_info(s::MOI.GreaterThan) = _kBoundTypeGreaterThan, s.lower, Inf
_set_info(s::MOI.EqualTo) = _kBoundTypeEqualTo, s.value, s.value
_set_info(s::MOI.Interval) = _kBoundTypeInterval, s.lower, s.upper

function _bound_type_to_set(::Type{T}, k::_BoundType) where {T}
if k == _kBoundTypeEqualTo
return MOI.EqualTo{T}
elseif k == _kBoundTypeLessThan
return MOI.LessThan{T}
else
@assert k == _kBoundTypeGreaterThan
elseif k == _kBoundTypeGreaterThan
return MOI.GreaterThan{T}
else
@assert k == _kBoundTypeInterval
return MOI.Interval{T}
end
end

Expand Down Expand Up @@ -321,7 +325,7 @@ function MOI.is_valid(
) where {
T,
F<:Union{MOI.ScalarAffineFunction{T},MOI.ScalarQuadraticFunction{T}},
S<:Union{MOI.LessThan{T},MOI.GreaterThan{T},MOI.EqualTo{T}},
S<:Union{MOI.LessThan{T},MOI.GreaterThan{T},MOI.EqualTo{T},MOI.Interval{T}},
}
return 1 <= ci.value <= length(block)
end
Expand All @@ -332,7 +336,7 @@ function MOI.get(
) where {
T,
F<:Union{MOI.ScalarAffineFunction{T},MOI.ScalarQuadraticFunction{T}},
S<:Union{MOI.LessThan{T},MOI.GreaterThan{T},MOI.EqualTo{T}},
S<:Union{MOI.LessThan{T},MOI.GreaterThan{T},MOI.EqualTo{T},MOI.Interval{T}},
}
ret = MOI.ConstraintIndex{F,S}[]
for i in 1:length(block)
Expand All @@ -352,15 +356,15 @@ function MOI.get(
) where {
T,
F<:Union{MOI.ScalarAffineFunction{T},MOI.ScalarQuadraticFunction{T}},
S<:Union{MOI.LessThan{T},MOI.GreaterThan{T},MOI.EqualTo{T}},
S<:Union{MOI.LessThan{T},MOI.GreaterThan{T},MOI.EqualTo{T},MOI.Interval{T}},
}
return length(MOI.get(block, MOI.ListOfConstraintIndices{F,S}()))
end

function MOI.add_constraint(
block::QPBlockData{T},
f::Union{MOI.ScalarAffineFunction{T},MOI.ScalarQuadraticFunction{T}},
set::Union{MOI.LessThan{T},MOI.GreaterThan{T},MOI.EqualTo{T}},
set::Union{MOI.LessThan{T},MOI.GreaterThan{T},MOI.EqualTo{T},MOI.Interval{T}},
) where {T}
push!(block.constraints, f)
bound_type, l, u = _set_info(set)
Expand Down Expand Up @@ -390,9 +394,11 @@ function MOI.get(
return MOI.EqualTo(block.g_L[row])
elseif block.bound_type[row] == _kBoundTypeLessThan
return MOI.LessThan(block.g_U[row])
else
@assert block.bound_type[row] == _kBoundTypeGreaterThan
elseif block.bound_type[row] == _kBoundTypeGreaterThan
return MOI.GreaterThan(block.g_L[row])
else
@assert block.bound_type[row] == _kBoundTypeInterval
return MOI.Interval(block.g_L[row], block.g_U[row])
end
end

Expand Down Expand Up @@ -430,6 +436,18 @@ function MOI.set(
return
end

function MOI.set(
block::QPBlockData{T},
::MOI.ConstraintSet,
c::MOI.ConstraintIndex{F,MOI.Interval{T}},
set::MOI.Interval{T},
) where {T,F}
row = c.value
block.g_L[row] = set.lower
block.g_U[row] = set.upper
return
end

function MOI.get(
block::QPBlockData{T},
::MOI.ConstraintDualStart,
Expand Down

0 comments on commit bd5008d

Please sign in to comment.