-
Notifications
You must be signed in to change notification settings - Fork 3
Refactor to allow containers to be directly used as spaces #9
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
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ebf59fe
wrote main ideas in README
zsunberg f1ad372
fixed typo
zsunberg 54eaca2
added basic functionality
zsunberg 5ff9fb0
added Box
zsunberg 0c63bcd
added ArraySpace constructor
zsunberg 9cb43f8
ArraySpace tests mostly pass
zsunberg 8a501c8
added products of boxes and friends
zsunberg 61924cf
added some words in the README about products
zsunberg 02605b5
slight rewording
zsunberg e83a732
added tuple fallback
zsunberg 361d999
finished implementing TupleProduct
zsunberg c342b84
fixed Box docstring
zsunberg e48e17b
added docstrings, updated readme
zsunberg 9ee91b5
add minor modifications
findmyway File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
abstract type AbstractArraySpace end | ||
# Maybe AbstractArraySpace should have an eltype parameter so that you could call convert(AbstractArraySpace{Float32}, space) | ||
|
||
""" | ||
Box(lower, upper) | ||
|
||
A Box represents a space of real-valued arrays bounded element-wise above by `upper` and below by `lower`, e.g. `Box([-1, -2], [3, 4]` represents the two-dimensional vector space that is the Cartesian product of the two closed sets: ``[-1, 3] \\times [-2, 4]``. | ||
|
||
The elements of a Box are always `AbstractArray`s with `AbstractFloat` elements. `Box`es always have `ContinuousSpaceStyle`, and products of `Box`es with `Box`es or `ClosedInterval`s are `Box`es when the dimensions are compatible. | ||
""" | ||
struct Box{A<:AbstractArray{<:AbstractFloat}} <: AbstractArraySpace | ||
lower::A | ||
upper::A | ||
|
||
Box{A}(lower, upper) where {A<:AbstractArray} = new(lower, upper) | ||
end | ||
|
||
function Box(lower, upper; convert_to_static::Bool=false) | ||
@assert size(lower) == size(upper) | ||
sz = size(lower) | ||
continuous_lower = convert(AbstractArray{float(eltype(lower))}, lower) | ||
continuous_upper = convert(AbstractArray{float(eltype(upper))}, upper) | ||
if convert_to_static | ||
final_lower = SArray{Tuple{sz...}}(continuous_lower) | ||
final_upper = SArray{Tuple{sz...}}(continuous_upper) | ||
else | ||
final_lower, final_upper = promote(continuous_lower, continuous_upper) | ||
end | ||
return Box{typeof(final_lower)}(final_lower, final_upper) | ||
end | ||
|
||
# By default, convert builtin arrays to static | ||
Box(lower::Array, upper::Array) = Box(lower, upper; convert_to_static=true) | ||
|
||
SpaceStyle(::Box) = ContinuousSpaceStyle() | ||
|
||
function Base.rand(rng::AbstractRNG, sp::Random.SamplerTrivial{<:Box}) | ||
box = sp[] | ||
return box.lower + rand_similar(rng, box.lower) .* (box.upper-box.lower) | ||
end | ||
|
||
rand_similar(rng::AbstractRNG, a::StaticArray) = rand(rng, typeof(a)) | ||
rand_similar(rng::AbstractRNG, a::AbstractArray) = rand(rng, eltype(a), size(a)...) | ||
|
||
Base.in(x::AbstractArray, b::Box) = all(b.lower .<= x .<= b.upper) | ||
|
||
Base.eltype(::Box{A}) where A = A | ||
elsize(b::Box) = size(b.lower) | ||
|
||
bounds(b::Box) = (b.lower, b.upper) | ||
Base.clamp(x::AbstractArray, b::Box) = clamp.(x, b.lower, b.upper) | ||
|
||
Base.convert(t::Type{<:Box}, i::ClosedInterval) = t(SA[minimum(i)], SA[maximum(i)]) | ||
|
||
struct RepeatedSpace{B, S<:Tuple} <: AbstractArraySpace | ||
base_space::B | ||
elsize::S | ||
end | ||
|
||
""" | ||
ArraySpace(base_space, size...) | ||
|
||
Create a space of Arrays with shape `size`, where each element of the array is drawn from `base_space`. | ||
""" | ||
ArraySpace(base_space, size...) = RepeatedSpace(base_space, size) | ||
|
||
SpaceStyle(s::RepeatedSpace) = SpaceStyle(s.base_space) | ||
|
||
Base.rand(rng::AbstractRNG, sp::Random.SamplerTrivial{<:RepeatedSpace}) = rand(rng, sp[].base_space, sp[].elsize...) | ||
|
||
Base.in(x::AbstractArray, s::RepeatedSpace) = all(entry in s.base_space for entry in x) | ||
Base.eltype(s::RepeatedSpace) = AbstractArray{eltype(s.base_space), length(s.elsize)} | ||
Base.eltype(s::RepeatedSpace{<:AbstractInterval}) = AbstractArray{Random.gentype(s.base_space), length(s.elsize)} | ||
elsize(s::RepeatedSpace) = s.elsize | ||
|
||
function bounds(s::RepeatedSpace) | ||
bs = bounds(s.base_space) | ||
return (Fill(first(bs), s.elsize...), Fill(last(bs), s.elsize...)) | ||
end | ||
|
||
Base.clamp(x::AbstractArray, s::RepeatedSpace) = map(entry -> clamp(entry, s.base_space), x) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
we may also have
eltype
implemented