Skip to content

Commit

Permalink
Fix Interval setvrep and speed up redundancy removal (#299)
Browse files Browse the repository at this point in the history
When arguments where given, the redundancy removal
or linearity detection was using the default implementation
and not using the fact that intervals have no redundancy
by construction.
This was hitting another bug with setvrep and resetvrep which
are not fixed as well.
  • Loading branch information
blegat authored Apr 3, 2022
1 parent 83192d1 commit e97d708
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 23 deletions.
8 changes: 6 additions & 2 deletions src/defaultlibrary.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,25 +113,29 @@ end
# Fallback to avoid breaking library adding this new `red` arg.
# TODO We should remove these fallbacks in the future
sethrep!(p, h, red) = sethrep!(p, h)
setvrep!(p, v, red) = sethrep!(p, v)
setvrep!(p, v, red) = setvrep!(p, v)
resethrep!(p, h, red) = resethrep!(p, h)
resetvrep!(p, v, red) = resethrep!(p, v)
resetvrep!(p, v, red) = resetvrep!(p, v)

function sethrep!(p::DefaultPolyhedron, h::HRepresentation, redundancy = UNKNOWN_REDUNDANCY)
p.hrep = h
p.hred = redundancy
return
end
function setvrep!(p::DefaultPolyhedron, v::VRepresentation, redundancy = UNKNOWN_REDUNDANCY)
p.vrep = v
p.vred = redundancy
return
end
function resethrep!(p::DefaultPolyhedron, h::HRepresentation, redundancy = UNKNOWN_REDUNDANCY)
p.hrep = h
p.hred = redundancy
p.vrep = nothing
return
end
function resetvrep!(p::DefaultPolyhedron, v::VRepresentation, redundancy = UNKNOWN_REDUNDANCY)
p.vrep = v
p.vred = redundancy
p.hrep = nothing
return
end
29 changes: 21 additions & 8 deletions src/interval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ function _hinterval(rep::HRep{T}, ::Type{AT}, D) where {T, AT}
return Interval{T, AT, D}(_hinterval(rep, AT)...)
end

function _vinterval(v::VRep{T}, ::Type{AT}, D) where {T, AT}
function _vinterval(v::VRep{T}, ::Type{AT}) where {T, AT}
haslb = true
lb = zero(T)
hasub = true
Expand Down Expand Up @@ -171,7 +171,11 @@ function _vinterval(v::VRep{T}, ::Type{AT}, D) where {T, AT}
end
end
end
Interval{T, AT, D}(haslb, lb, hasub, ub, isempty)
return _interval(AT, haslb, lb, hasub, ub, isempty)
end

function _vinterval(rep::VRep{T}, ::Type{AT}, D) where {T, AT}
return Interval{T, AT, D}(_vinterval(rep, AT)...)
end

Interval{T, AT, D}(p::HRepresentation{T}) where {T, AT, D} = _hinterval(p, AT, D)
Expand All @@ -195,15 +199,24 @@ hrepiscomputed(::Interval) = true
vrepiscomputed(::Interval) = true

# Nothing to do
function detecthlinearity!(::Interval) end
function detectvlinearity!(::Interval) end
function removehredundancy!(::Interval) end
function removevredundancy!(::Interval) end
function detecthlinearity!(::Interval, args...; kws...) end
function detectvlinearity!(::Interval, args...; kws...) end
function removehredundancy!(::Interval, args...; kws...) end
function removevredundancy!(::Interval, args...; kws...) end

function sethrep!(p::Interval{T, AT}, h::HRep) where {T, AT}
sethrep!(p::Interval, h::HRep) = resethrep!(p, h)
function resethrep!(p::Interval{T, AT}, h::HRep) where {T, AT}
hnew, v, volume = _hinterval(h, AT)
p.hrep = hnew
p.vrep = v
p.length = volume
return p
end
end
setvrep!(p::Interval, v::VRep) = resetvrep!(p, v)
function resetvrep!(p::Interval{T, AT}, v::VRep) where {T, AT}
h, vnew, volume = _vinterval(v, AT)
p.hrep = h
p.vrep = vnew
p.length = volume
return p
end
67 changes: 54 additions & 13 deletions test/interval.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
function cartesian_product_test()
using LinearAlgebra, Test
using StaticArrays
using Polyhedra

function test_cartesian_product()
p1 = polyhedron(hrep([HalfSpace([1.0], 1.0), HalfSpace([-1.0], 0.0)]))
p2 = polyhedron(hrep([HalfSpace([1.0], 1.0), HalfSpace([-1.0], 0.0)]))
p = Polyhedra.hcartesianproduct(p1, p2)
Expand All @@ -9,26 +13,59 @@ function cartesian_product_test()
inequality_fulltest(p, expected)
end

function sethrep_test()
function _test_sethrep(fh, fv, args...)
# x_1 ≤ 2 x_1 ≥ -1 <=> -x_1 ≤ 1
h1 = HalfSpace([1], 2) HalfSpace([-1], 1)
v1 = convexhull([-1], [2])
p = polyhedron(h1)
@test volume(p) == 3
inequality_fulltest(p, h1)
generator_fulltest(p, v1)

ph = polyhedron(h1)
@test volume(ph) == 3
inequality_fulltest(ph, h1)
generator_fulltest(ph, v1)

pv = polyhedron(v1)
@test volume(pv) == 3
inequality_fulltest(pv, h1)
generator_fulltest(pv, v1)

# x_1 ≤ 1 x_1 ≥ -1 <=> -x_1 ≤ 1
h2 = HalfSpace([1], 1) HalfSpace([-1], 1)
v2 = convexhull([-1], [1])
Polyhedra.sethrep!(p, h2)

# Test that the three fields have been modified
@test volume(p) == 2
inequality_fulltest(p, h2)
generator_fulltest(p, v2)
fh(ph, h2, args...)
@test volume(ph) == 2
inequality_fulltest(ph, h2)
generator_fulltest(ph, v2)
fv(pv, v2, args...)
@test volume(pv) == 2
inequality_fulltest(pv, h2)
generator_fulltest(pv, v2)
end
function test_sethrep()
_test_sethrep(Polyhedra.sethrep!, Polyhedra.setvrep!)
_test_sethrep(Polyhedra.sethrep!, Polyhedra.setvrep!, Polyhedra.UNKNOWN_REDUNDANCY)
_test_sethrep(Polyhedra.resethrep!, Polyhedra.resetvrep!)
_test_sethrep(Polyhedra.resethrep!, Polyhedra.resetvrep!, Polyhedra.UNKNOWN_REDUNDANCY)
end

@testset "Interval tests" begin
function test_redundancy()
h = HalfSpace([1], 2) HalfSpace([-1], 1)
p = polyhedron(h)
function f()
removevredundancy!(p)
removevredundancy!(p, nothing; strongly = false, planar = false)
detectvlinearity!(p)
detectvlinearity!(p, nothing; verbose=1)
removehredundancy!(p)
removehredundancy!(p, nothing; strongly = false, planar = false)
detecthlinearity!(p)
detecthlinearity!(p, nothing; verbose=1)
end
alloc_test(f, 0)
end

@testset "Interval tests" begin
# Closed interval
h = HalfSpace([-1], 3.) HalfSpace([1.], 8)
v = convexhull([-3.], [8.])
Expand Down Expand Up @@ -222,10 +259,14 @@ end
inequality_fulltest(p, h)

@testset "Cartesian product (#132)" begin
cartesian_product_test()
test_cartesian_product()
end

@testset "sethrep! (#267)" begin
sethrep_test()
test_sethrep()
end

@testset "test_redundancy (#298)" begin
test_redundancy()
end
end
5 changes: 5 additions & 0 deletions test/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,8 @@ function generator_fulltest(p::Polyhedron, vrepargs...)
removevredundancy!(p)
generator_fulltest(vrep(p), vrepargs...)
end

function alloc_test(f, n)
f() # compile
@test n == @allocated f()
end

0 comments on commit e97d708

Please sign in to comment.