Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Apr 5, 2024
1 parent 98f8514 commit b94fdff
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
41 changes: 27 additions & 14 deletions src/lphrep.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ MOI.Utilities.@model(_MOIModel,
(), (MOI.ScalarAffineFunction,), (), ())
# We need the `VariableIndex` constraints to be bridged so we should say that
# they are not supported. We notably exclude `Integer` as we just ignore
# integrality constraints. Binary constraint should be bridged to integrality
# once https://github.com/jump-dev/MathOptInterface.jl/issues/704 is done.
# integrality constraints. Binary constraint are bridged to integrality
# with `ZeroOneBridge`.
function MOI.supports_constraint(
::_MOIModel{T}, ::Type{MOI.VariableIndex},
::Type{<:Union{MOI.EqualTo{T}, MOI.GreaterThan{T}, MOI.LessThan{T},
Expand All @@ -15,6 +15,7 @@ function MOI.supports_constraint(
end



mutable struct LPHRep{T} <: HRepresentation{T}
model::_MOIModel{T}
hyperplane_indices::Union{Nothing,
Expand All @@ -27,6 +28,15 @@ end
function LPHRep(model::_MOIModel{T}) where T
return LPHRep(model, nothing, nothing)
end

function _pass_constraints(dest, src, index_map, cis_src)
for ci_src in cis_src
f = MOI.get(src, MOI.ConstraintFunction(), ci_src)
s = MOI.get(src, MOI.ConstraintSet(), ci_src)
MOI.add_constraint(dest, MOI.Utilities.map_indices(index_map, f), s)
end
end

function LPHRep(model::MOI.ModelLike, T::Type = Float64)
_model = _MOIModel{T}()
bridged = MOI.Bridges.LazyBridgeOptimizer(_model)
Expand All @@ -42,6 +52,7 @@ function LPHRep(model::MOI.ModelLike, T::Type = Float64)
MOI.Bridges.add_bridge(bridged, MOI.Bridges.Constraint.ScalarFunctionizeBridge{T})
MOI.Bridges.add_bridge(bridged, MOI.Bridges.Constraint.VectorFunctionizeBridge{T})
MOI.Bridges.add_bridge(bridged, MOI.Bridges.Constraint.SplitIntervalBridge{T})
MOI.Bridges.add_bridge(bridged, MOI.Bridges.Constraint.ZeroOneBridge{T})
MOI.Bridges.add_bridge(bridged, MOI.Bridges.Constraint.NormInfinityBridge{T})
# This one creates variables so the user need to consider the polyhedra as the
# feasible set of the extended formulation.
Expand All @@ -61,19 +72,21 @@ function LPHRep(model::MOI.ModelLike, T::Type = Float64)
has_names = attr in MOI.get(model, MOI.ListOfVariableAttributesSet())
for (vi_src, vi_dst) in zip(vis_src, vis_dst)
index_map[vi_src] = vi_dst
if has_names
name = MOI.get(model, attr, vi_src)
if !isnothing(name)
MOI.set(bridged, attr, vi_dst, name)
end
end
if has_names
name = MOI.get(model, attr, vi_src)
if !isnothing(name)
MOI.set(bridged, attr, vi_dst, name)
end
end
end
for (F, S) in MOI.get(model, MOI.ListOfConstraintTypesPresent())
_pass_constraints(
bridged,
model,
index_map,
MOI.get(model, MOI.ListOfConstraintIndices{F,S}()),
)
end
MOI.Utilities.pass_nonvariable_constraints(
bridged,
model,
index_map,
MOI.get(model, MOI.ListOfConstraintTypesPresent()),
)
return LPHRep(_model)
end
# returns `Int64` so need to convert for 32-bit system
Expand Down
8 changes: 7 additions & 1 deletion test/model_to_polyhedron.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,11 @@ using Polyhedra
@test p isa DefaultPolyhedron{Float64}
@test nhalfspaces(p) == 3
end
# TODO add test with binary variables once https://github.com/jump-dev/MathOptInterface.jl/issues/704 is done.
@testset "Binary" begin
model = Model()
@variable(model, x, Bin)
p = polyhedron(model)
@test p isa Interval{Float64}
@test nhalfspaces(p) == 2
end
end

0 comments on commit b94fdff

Please sign in to comment.