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

Faster low/high for Zonotope #2941

Merged
merged 1 commit into from
Mar 1, 2022
Merged

Faster low/high for Zonotope #2941

merged 1 commit into from
Mar 1, 2022

Conversation

schillic
Copy link
Member

The master version computes the support function, which is already quite fast. The first version I tried is faster. The final version is even slightly faster than the other version because it avoids the @view.

function low2(Z::AbstractZonotope, i::Int)
    return center(Z, i) - sum(abs, @view genmat(Z)[i, :])
end

function low3(Z::AbstractZonotope, i::Int)
    G = genmat(Z)
    v = center(Z, i)
    for j in 1:ngens(Z)
        v -= abs(G[i, j])
    end
    return v
end

julia> Z = rand(Zonotope, dim=50, seed=0);

julia> @btime LazySets.low($Z, 1);  # master
  120.834 ns (0 allocations: 0 bytes)

julia> @btime LazySets.low2($Z, 1);
  61.188 ns (0 allocations: 0 bytes)

julia> @btime LazySets.low3($Z, 1);  # this branch
  55.798 ns (0 allocations: 0 bytes)

@schillic schillic requested a review from mforets February 24, 2022 19:30
src/Interfaces/AbstractZonotope.jl Outdated Show resolved Hide resolved
src/Interfaces/AbstractZonotope.jl Outdated Show resolved Hide resolved
@mforets
Copy link
Member

mforets commented Feb 28, 2022

The master version computes the support function, which is already quite fast. The first version I tried is faster.

I think that, in general it depends on the cost of computing genmat. It is for free if the set is a Zonotope, but could otherwise be cheaper to compute support function instead.

@mforets
Copy link
Member

mforets commented Feb 28, 2022

For the general (AbstractZonotope) case, I'm tempted to prefer leaving the default fallback, but we can keep the faster version in this branch for the Zonotope case.

@schillic
Copy link
Member Author

Good point 👍

julia> LazySets.subtypes(AbstractZonotope)
5-element Vector{Any}:
 AbstractHyperrectangle
 HParallelotope
 LineSegment
 RotatedHyperrectangle
 Zonotope


julia> L = rand(LineSegment, dim=2, seed=0);

julia> @btime LazySets.low($L, 1);  # master
  338.658 ns (7 allocations: 544 bytes)

julia> @btime LazySets.low2($L, 1);  # this branch
  453.561 ns (10 allocations: 768 bytes)


julia> H = rand(HParallelotope, dim=20, seed=0);

julia> @btime LazySets.low($H, 1);  # master
  767.961 μs (171 allocations: 172.12 KiB)

julia> @btime LazySets.low2($H, 1);  # this branch
  1.168 ms (305 allocations: 261.94 KiB)

@schillic schillic changed the title Faster low/high for AbstractZonotope Faster low/high for Zonotope Feb 28, 2022
@schillic schillic merged commit 783ec73 into master Mar 1, 2022
@schillic schillic deleted the schillic/lowhigh branch March 1, 2022 22:53
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.

2 participants