Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1384 - Add four point case for convex hull #1434

Merged
merged 14 commits into from
Jun 17, 2019
Merged

#1384 - Add four point case for convex hull #1434

merged 14 commits into from
Jun 17, 2019

Conversation

SebastianGuadalupe
Copy link
Member

Closes #1384.

@schillic
Copy link
Member

There is a problem with the sorting of vertices (hence the error):

julia> using LazySets
julia> B = Ball1(zeros(2), 1.);

# this branch:
julia> P = convert(VPolygon, B)
VPolygon{Float64,Array{Float64,1}}(Array{Float64,1}[[0.0, -1.0], [-1.0, 0.0], [0.0, 1.0], [1.0, 0.0]])

# master:
julia> P = convert(VPolygon, B)
VPolygon{Float64,Array{Float64,1}}(Array{Float64,1}[[-1.0, 0.0], [0.0, -1.0], [1.0, 0.0], [0.0, 1.0]])

Please add a test case that detects this.

src/concrete_convex_hull.jl Outdated Show resolved Hide resolved
src/concrete_convex_hull.jl Show resolved Hide resolved
src/concrete_convex_hull.jl Outdated Show resolved Hide resolved
@schillic
Copy link
Member

schillic commented Jun 14, 2019

EDIT: This was fixed in #1441. Now you need to rebase (sorry).


LazySets.ConvexHull: Test Failed at /home/travis/build/JuliaReach/LazySets.jl/test/unit_ConvexHull.jl:105
  Expression: convex_hull!(points, algorithm="")
    Expected: ErrorException
  No exception thrown

This error is a bug in our tests:

@test convex_hull!(points, algorithm="monotone_chain") == sorted
@test convex_hull!(points, algorithm="monotone_chain_sorted") == sorted
@test_throws ErrorException convex_hull!(points, algorithm="")

The tests should instead either use the non-! function or use copy(points), e.g.:

    @test ispermutation(convex_hull!(copy(points), algorithm="monotone_chain"), sorted)
    @test ispermutation(convex_hull!(copy(points), algorithm="monotone_chain_sorted"), sorted)
    @test_throws ErrorException convex_hull!(copy(points), algorithm="")

@schillic
Copy link
Member

I wonder why your tests did not catch the first error. Can you add a test for that case?

test/unit_convex_hull.jl Outdated Show resolved Hide resolved
src/helper_functions.jl Outdated Show resolved Hide resolved
src/helper_functions.jl Outdated Show resolved Hide resolved
src/helper_functions.jl Outdated Show resolved Hide resolved
test/unit_convex_hull.jl Outdated Show resolved Hide resolved
Copy link
Member

@schillic schillic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding the output type forces Julia to convert the result, which means it does not actually return the original points if the input was not a Vector.

@@ -186,6 +190,124 @@ function _three_points_2d!(points::AbstractVector{<:AbstractVector{N}}) where {N
return points
end

function _collinear_case!(points::Vector{VN}, A::VN, B::VN, C::VN, D::VN)::Vector{VN} where {N<:Real, VN<:AbstractVector{N}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function _collinear_case!(points::Vector{VN}, A::VN, B::VN, C::VN, D::VN)::Vector{VN} where {N<:Real, VN<:AbstractVector{N}}
function _collinear_case!(points::Vector{VN}, A::VN, B::VN, C::VN, D::VN) where {N<:Real, VN<:AbstractVector{N}}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the outer signature is already typed to be a Vector (in and out), what is kept arbitrary is the representation of each point:

function convex_hull!(points::Vector{VN};
                      algorithm=default_convex_hull_algorithm(points),
                      backend=nothing)::Vector{VN} where {N<:Real, VN<:AbstractVector{N}}
...

Copy link
Member

@schillic schillic Jun 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, and in full generality, each element could have a different concrete type. The most general version would thus be:

function _collinear_case!(points::AbstractVector{<:AbstractVector{N}}, A::AbstractVector{N},
    B::AbstractVector{N}, C::AbstractVector{N}, D::AbstractVector{N}) where {N<:Real}

I think if you do not use this version, the method will crash if you use, e.g., SparseVectors.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need the type annotations or can you just remove them?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After discussion on gitter, @SebastianGuadalupe would you please remove type annotations in _collinear_case!?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And then squash 👍

Copy link
Member

@mforets mforets Jun 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After discussion on gitter, @SebastianGuadalupe would you please remove type annotations in _collinear_case!?

_collinear_case!(points, A, B, C, D)

Co-Authored-By: Christian Schilling <[email protected]>
Copy link
Member

@mforets mforets left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, great job!


For reference,

julia> B = Ball1(zeros(2), 1.0);

julia> points = vertices_list(B);

# master v1.13.0
julia> @btime convex_hull!($(copy(points)))
 403.095 ns (7 allocations: 432 bytes)

# this branch
julia> @btime convex_hull!($(copy(points)))
28.775 ns (0 allocations: 0 bytes)

@mforets mforets requested a review from schillic June 17, 2019 09:48
Copy link
Member

@schillic schillic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM (after addressing the open comment and squashing).

@mforets mforets merged commit 5561482 into JuliaReach:master Jun 17, 2019
@mforets
Copy link
Member

mforets commented Jun 17, 2019

after addressing the open comment

i'll handle it, plus some other minor tweaks in the same file that i planned to do, in a follow-up PR.

squashing

done (from github).

schillic added a commit that referenced this pull request Jun 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Convex hull of four points
3 participants