From 2c2035c90fe40b897e6711f61ea69e46a4294a1f Mon Sep 17 00:00:00 2001 From: mforets Date: Tue, 15 Jan 2019 19:54:24 -0300 Subject: [PATCH 1/4] convert to Vector in polyhedron's LP --- src/HPolyhedron.jl | 2 +- test/unit_Polytope.jl | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/HPolyhedron.jl b/src/HPolyhedron.jl index c60f46bec1..8aaa11d27a 100644 --- a/src/HPolyhedron.jl +++ b/src/HPolyhedron.jl @@ -152,7 +152,7 @@ function σ(d::AbstractVector{N}, P::HPoly{N}) where {N<:Real} end function σ_helper(d::AbstractVector{N}, P::HPoly{N}) where {N<:Real} - c = -d + c = convert(Vector{N}, -d) (A, b) = tosimplehrep(P) if length(b) == 0 unbounded = true diff --git a/test/unit_Polytope.jl b/test/unit_Polytope.jl index 5f776a4403..fbc79e7233 100644 --- a/test/unit_Polytope.jl +++ b/test/unit_Polytope.jl @@ -274,3 +274,9 @@ if test_suite_polyhedra @test ispermutation(vl, [N[0, 0, 2], N[1, 1, 2]]) end end + +# test that one can pass a sparse vector as the direction (see #1011) +P = HPolytope([HalfSpace([1.0, 0.0], 1.0), + HalfSpace([0.0, 1.0], 1.0), + HalfSpace([-1.0, -1.0], -1.0)]) +@test an_element(P) ∈ P From 74a59f1ebf02b0dd985e50f251a3574b027845ac Mon Sep 17 00:00:00 2001 From: mforets Date: Wed, 16 Jan 2019 10:14:06 -0300 Subject: [PATCH 2/4] make conversion faster for sparse vectors --- src/HPolyhedron.jl | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/HPolyhedron.jl b/src/HPolyhedron.jl index 8aaa11d27a..73c4ff5208 100644 --- a/src/HPolyhedron.jl +++ b/src/HPolyhedron.jl @@ -151,8 +151,23 @@ function σ(d::AbstractVector{N}, P::HPoly{N}) where {N<:Real} end end +@inline function _to_minus_vector(d::SparseVector) + c = zeros(length(d)) + for (ni, i) in enumerate(d.nzind) + @inbounds c[i] = -d.nzval[ni] + end + return c +end + +@inline function _to_minus_vector(d::AbstractVector{N}) where {N} + return convert(Vector{N}, -d) +end + function σ_helper(d::AbstractVector{N}, P::HPoly{N}) where {N<:Real} - c = convert(Vector{N}, -d) + + # let c = -d as a Vector, since GLPK doesn't accept sparse vectors (see #1011) + c = _to_minus_vector(d) + (A, b) = tosimplehrep(P) if length(b) == 0 unbounded = true From 4373c379c1a1eaf37e12913aac541c351ee32041 Mon Sep 17 00:00:00 2001 From: Marcelo Forets Date: Wed, 16 Jan 2019 10:22:12 -0300 Subject: [PATCH 3/4] Update src/HPolyhedron.jl --- src/HPolyhedron.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/HPolyhedron.jl b/src/HPolyhedron.jl index 73c4ff5208..96fcf8bfef 100644 --- a/src/HPolyhedron.jl +++ b/src/HPolyhedron.jl @@ -151,7 +151,7 @@ function σ(d::AbstractVector{N}, P::HPoly{N}) where {N<:Real} end end -@inline function _to_minus_vector(d::SparseVector) +@inline function _to_minus_vector(d::SparseVector{N}) where {N} c = zeros(length(d)) for (ni, i) in enumerate(d.nzind) @inbounds c[i] = -d.nzval[ni] From 675a995e7bf2a63f64d1022e2463bb4b8dab8e26 Mon Sep 17 00:00:00 2001 From: Marcelo Forets Date: Wed, 16 Jan 2019 10:22:44 -0300 Subject: [PATCH 4/4] Update src/HPolyhedron.jl --- src/HPolyhedron.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/HPolyhedron.jl b/src/HPolyhedron.jl index 96fcf8bfef..45c75b4135 100644 --- a/src/HPolyhedron.jl +++ b/src/HPolyhedron.jl @@ -152,7 +152,7 @@ function σ(d::AbstractVector{N}, P::HPoly{N}) where {N<:Real} end @inline function _to_minus_vector(d::SparseVector{N}) where {N} - c = zeros(length(d)) + c = zeros(N, length(d)) for (ni, i) in enumerate(d.nzind) @inbounds c[i] = -d.nzval[ni] end