-
Notifications
You must be signed in to change notification settings - Fork 33
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
#342 - Add caching lazy MinkowskiSum #349
Conversation
julia> using LazySets
julia> cms = CacheMinkowskiSum(5);
julia> X1 = BallInf(ones(3), 3.);
julia> X2 = BallInf(ones(3), 5.);
julia> X3 = Ball1(ones(3), 5.);
julia> d = ones(3);
julia> a = array(cms);
julia>
julia> push!(a, X1);
julia> @time σ(d, cms);
0.000027 seconds (25 allocations: 1.500 KiB)
julia> @time σ(d, cms);
0.000012 seconds (12 allocations: 608 bytes)
julia> @time σ(d, cms);
0.000011 seconds (12 allocations: 608 bytes)
julia>
julia> push!(a, X2);
julia> @time σ(d, cms);
0.000019 seconds (27 allocations: 1.656 KiB)
julia> @time σ(d, cms);
0.000012 seconds (12 allocations: 608 bytes)
julia> @time σ(d, cms);
0.000010 seconds (12 allocations: 608 bytes)
julia>
julia> for i = 1 : 1000
push!(a, X3);
end
julia> @time σ(d, cms);
0.000160 seconds (3.02 k allocations: 329.469 KiB)
julia> @time σ(d, cms);
0.000023 seconds (13 allocations: 624 bytes)
julia> @time σ(d, cms);
0.000015 seconds (13 allocations: 624 bytes)
julia>
julia> for i = 1 : 1000
push!(a, X3);
end
julia> @time σ(d, cms);
0.000159 seconds (3.03 k allocations: 329.484 KiB)
julia> @time σ(d, cms);
0.000028 seconds (13 allocations: 624 bytes)
julia> @time σ(d, cms);
0.000011 seconds (13 allocations: 624 bytes) julia> msa = MinkowskiSumArray(5);
julia> a2 = array(msa);
julia> push!(a2, X1);
julia> push!(a2, X2);
julia> for i = 1 : 1000
push!(a2, X3);
end
julia> @time σ(d, msa);
0.000162 seconds (3.02 k allocations: 329.547 KiB)
julia> @time σ(d, msa);
0.000150 seconds (3.02 k allocations: 329.547 KiB)
julia> @time σ(d, msa);
0.000152 seconds (3.02 k allocations: 329.547 KiB)
julia> for i = 1 : 1000
push!(a2, X3);
end
julia> @time σ(d, msa);
0.000274 seconds (6.02 k allocations: 657.672 KiB)
julia> @time σ(d, msa);
0.000257 seconds (6.02 k allocations: 657.672 KiB)
julia> @time σ(d, msa);
0.000989 seconds (6.02 k allocations: 657.672 KiB)
julia> @time σ(d, msa);
0.000897 seconds (6.02 k allocations: 657.672 KiB) |
Possible future work: Currently the sum stores all sets, which takes a lot of memory. In applications where we always query the same support vector(s) and then add a new set in a loop, we could also provide a variant that only stores the newest sets. |
""" | ||
struct CacheMinkowskiSum{N<:Real, S<:LazySet{N}} <: LazySet{N} | ||
array::Vector{S} | ||
cache::Dict{AbstractVector{N}, Tuple{Int, AbstractVector{N}}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
side comment: we are using floating points vectors as keys, we should be careful about tolerance matters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is true, but the directions are always constructed from the same sources at the moment, which is why we get the same numbers every time.
### Fields | ||
|
||
- `array` -- array of convex sets | ||
- `cache` -- cache of support vector query results |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we add that this is a dictionary whose keys are directions and whose values are tuples (l, s) where l is the number of elements in the array where this function was evaluated last time, and s is the value of the support vector that was obtained
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC, only the number of elements is checked, not the actual elements
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added 👍
end | ||
|
||
# ZeroSet is the neutral element for CacheMinkowskiSum | ||
@neutral(CacheMinkowskiSum, ZeroSet) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🏆
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Closes #342.