Skip to content

Commit 17d4211

Browse files
committed
update README.md
1 parent 368a201 commit 17d4211

File tree

5 files changed

+155
-12
lines changed

5 files changed

+155
-12
lines changed

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ version = "0.1.0"
66
[deps]
77
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
88
IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953"
9+
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
910
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
1011
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
1112

README.md

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,64 @@
1313

1414
|Category|Style|Example|
1515
|:---|:----|:-----|
16-
|Enumerable discrete space| `DiscreteSpaceStyle{2, ()}()` | `Space((:cat, :dog))`, `Space(0:1)`, `Space(1:2)`, `Space(Bool)`|
17-
|Multi-dimensional discrete space| `DiscreteSpaceStyle{2, (3,4)}()` | `Space((:cat, :dog), 3, 4)`, `Space(0:1, 3, 4)`, `Space(1:2, 3, 4)`, `Space(Bool, 3, 4)`|
18-
|Multi-dimensional variable discrete space| `DiscreteSpaceStyle{(2,)}()` | `Space(SVector((:cat, :dog), (:litchi, longan, mango))`, `Space([-1:1, (false, true)])`|
19-
|Continuous space| `ContinuousSpaceStyle()` | `Space(-1.2..3.3)`, `Space(Float32)`|
16+
|Enumerable discrete space| `DiscreteSpaceStyle{()}()` | `Space((:cat, :dog))`, `Space(0:1)`, `Space(1:2)`, `Space(Bool)`|
17+
|Multi-dimensional discrete space| `DiscreteSpaceStyle{(3,4)}()` | `Space((:cat, :dog), 3, 4)`, `Space(0:1, 3, 4)`, `Space(1:2, 3, 4)`, `Space(Bool, 3, 4)`|
18+
|Multi-dimensional variable discrete space| `DiscreteSpaceStyle{(2,)}()` | `Space(SVector((:cat, :dog), (:litchi, :longan, :mango))`, `Space([-1:1, (false, true)])`|
19+
|Continuous space| `ContinuousSpaceStyle{()}()` | `Space(-1.2..3.3)`, `Space(Float32)`|
2020
|Multi-dimensional continuous space| `ContinuousSpaceStyle{(3,4)}()` | `Space(-1.2..3.3, 3, 4)`, `Space(Float32, 3, 4)`|
2121

2222
### API
2323

24-
### Size Info
25-
2624
```julia
27-
```
25+
julia> s = Space((:litchi, :longan, :mango))
26+
Space{Tuple{Symbol, Symbol, Symbol}}((:litchi, :longan, :mango))
2827

29-
#### Sampling
28+
julia> rand(s)
29+
:litchi
3030

31-
```julia
32-
```
31+
julia> rand(s) in s
32+
true
3333

34-
### Existence Checking
34+
julia> size(s)
35+
()
36+
```
3537

3638
```julia
39+
julia> using CommonRLSpaces
40+
41+
julia> s = Space(UInt8, 2,3)
42+
Space{Matrix{UnitRange{UInt8}}}(UnitRange{UInt8}[0x00:0xff 0x00:0xff 0x00:0xff; 0x00:0xff 0x00:0xff 0x00:0xff])
43+
44+
julia> rand(s)
45+
2×3 Matrix{UInt8}:
46+
0x7b 0x38 0xf3
47+
0x6a 0xe1 0x28
48+
49+
julia> rand(s) in s
50+
true
51+
52+
julia> SpaceStyle(s)
53+
DiscreteSpaceStyle{(2, 3)}()
54+
55+
julia> size(s)
56+
(2, 3)
3757
```
58+
59+
```julia
60+
julia> s = Space(SVector(-1..1, 0..1))
61+
Space{SVector{2, ClosedInterval{Int64}}}(ClosedInterval{Int64}[-1..1, 0..1])
62+
63+
julia> rand(s)
64+
2-element SVector{2, Float64} with indices SOneTo(2):
65+
0.5563101538643473
66+
0.9227368869418011
67+
68+
julia> rand(s) in s
69+
true
70+
71+
julia> SpaceStyle(s)
72+
ContinuousSpaceStyle{(2,)}()
73+
74+
julia> size(s)
75+
(2,)
76+
```

src/CommonRLSpaces.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ using Reexport
55
@reexport using FillArrays
66
@reexport using IntervalSets
77
@reexport using StaticArrays
8+
@reexport import Base: OneTo
89

910
include("basic.jl")
1011

src/basic.jl

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,74 @@
1+
export Space, SpaceStyle, DiscreteSpaceStyle, ContinuousSpaceStyle
2+
3+
using Random
4+
15
struct Space{T}
26
s::T
37
end
8+
9+
Space(s::Type{T}) where {T} = Space(typemin(T):typemax(T))
10+
11+
Space(x, dims::Int...) = Space(fill(x, dims))
12+
Space(x::Type{T}, dims::Int...) where {T<:Integer} = Space(fill(typemin(x):typemax(T), dims))
13+
Space(x::Type{T}, dims::Int...) where {T<:AbstractFloat} = Space(fill(typemin(x) .. typemax(T), dims))
14+
15+
const TupleSpace = Tuple{Vararg{<:Space}}
16+
const NamedSpace = NamedTuple{<:Any,<:TupleSpace}
17+
const DictSpace = Dict{<:Any,<:Space}
18+
19+
Base.size(s::Space) = size(SpaceStyle(s))
20+
21+
#####
22+
23+
abstract type AbstractSpaceStyle{S} end
24+
25+
Base.size(::AbstractSpaceStyle{S}) where {S} = S
26+
27+
struct DiscreteSpaceStyle{S} <: AbstractSpaceStyle{S} end
28+
struct ContinuousSpaceStyle{S} <: AbstractSpaceStyle{S} end
29+
30+
SpaceStyle(::Space{<:Tuple}) = DiscreteSpaceStyle{()}()
31+
SpaceStyle(::Space{<:AbstractRange}) = DiscreteSpaceStyle{()}()
32+
SpaceStyle(::Space{<:AbstractInterval}) = ContinuousSpaceStyle{()}()
33+
34+
SpaceStyle(s::Space{<:AbstractArray{<:Tuple}}) = DiscreteSpaceStyle{size(s.s)}()
35+
SpaceStyle(s::Space{<:AbstractArray{<:AbstractRange}}) = DiscreteSpaceStyle{size(s.s)}()
36+
SpaceStyle(s::Space{<:AbstractArray{<:AbstractInterval}}) = ContinuousSpaceStyle{size(s.s)}()
37+
38+
#####
39+
40+
Random.rand(rng::Random.AbstractRNG, s::Space) = rand(rng, s.s)
41+
42+
Random.rand(
43+
rng::Random.AbstractRNG,
44+
s::Union{
45+
<:Space{<:AbstractArray{<:Tuple}},
46+
<:Space{<:AbstractArray{<:AbstractRange}},
47+
<:Space{<:AbstractArray{<:AbstractInterval}}
48+
}
49+
) = map(x -> rand(rng, x), s.s)
50+
51+
Base.in(x, s::Space) = x in s.s
52+
53+
Base.in(
54+
x,
55+
s::Union{
56+
<:Space{<:AbstractArray{<:Tuple}},
57+
<:Space{<:AbstractArray{<:AbstractRange}},
58+
<:Space{<:AbstractArray{<:AbstractInterval}}
59+
}
60+
) = size(x) == size(s) && all(x -> x[1] in x[2], zip(x, s.s))
61+
62+
function Random.rand(rng::AbstractRNG, s::Interval{:closed,:closed,T}) where {T}
63+
if s == typemin(T) .. typemax(T)
64+
rand(T)
65+
else
66+
r = rand(rng)
67+
68+
if r == 0.0
69+
r = rand(Bool)
70+
end
71+
72+
r * (s.right - s.left) + s.left
73+
end
74+
end

test/runtests.jl

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,36 @@ using CommonRLSpaces
22
using Test
33

44
@testset "CommonRLSpaces.jl" begin
5-
# Write your tests here.
5+
s1 = Space((:cat, :dog))
6+
@test :cat in s1
7+
@test !(nothing in s1)
8+
9+
s2 = Space(0:1)
10+
@test 0 in s2
11+
@test !(0.5 in s2)
12+
13+
s3 = Space(Bool)
14+
@test false in s3
15+
@test true in s3
16+
17+
s4 = Space(Float64)
18+
@test rand() in s4
19+
@test 0 in s4
20+
21+
s5 = Space(Float64, 3, 4)
22+
@test rand(3, 4) in s5
23+
24+
s6 = Space(SVector((:cat, :dog), (:litchi, :longan, :mango)))
25+
@test SVector(:dog, :litchi) in s6
26+
27+
s7 = Space([-1 .. 1, 2 .. 3])
28+
@test [0, 2] in s7
29+
@test !([-5, 5] in s7)
30+
31+
# for _ in 1:100
32+
for s in [s1, s2, s3, s4, s5, s6, s7]
33+
@info s
34+
@test rand(s) in s
35+
end
36+
# end
637
end

0 commit comments

Comments
 (0)