Skip to content

Commit e48e17b

Browse files
committed
added docstrings, updated readme
1 parent c342b84 commit e48e17b

File tree

5 files changed

+64
-13
lines changed

5 files changed

+64
-13
lines changed

README.md

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,37 +91,47 @@ julia> length(s)
9191
```
9292

9393
```julia
94-
julia> s = ArraySpace(UInt8, 2,3)
94+
julia> s = ArraySpace(1:5, 2,3)
95+
CommonRLSpaces.RepeatedSpace{UnitRange{Int64}, Tuple{Int64, Int64}}(1:5, (2, 3))
9596

9697
julia> rand(s)
97-
2×3 Matrix{UInt8}:
98-
0x7b 0x38 0xf3
99-
0x6a 0xe1 0x28
98+
2×3 Matrix{Int64}:
99+
4 1 1
100+
3 2 2
100101

101102
julia> rand(s) in s
102103
true
103104

104105
julia> SpaceStyle(s)
105-
FiniteSpaceStyle{(2, 3)}()
106+
FiniteSpaceStyle()
106107

107108
julia> elsize(s)
108109
(2, 3)
109110
```
110111

111112
```julia
112113
julia> s = product(-1..1, 0..1)
114+
Box{StaticArraysCore.SVector{2, Float64}}([-1.0, 0.0], [1.0, 1.0])
113115

114116
julia> rand(s)
115-
2-element SVector{2, Float64} with indices SOneTo(2):
116-
0.5563101538643473
117-
0.9227368869418011
117+
2-element StaticArraysCore.SVector{2, Float64} with indices SOneTo(2):
118+
0.03049072910834738
119+
0.6295234114874269
118120

119121
julia> rand(s) in s
120122
true
121123

122124
julia> SpaceStyle(s)
123-
ContinuousSpaceStyle{(2,)}()
125+
ContinuousSpaceStyle()
124126

125127
julia> elsize(s)
126128
(2,)
129+
130+
julia> bounds(s)
131+
([-1.0, 0.0], [1.0, 1.0])
132+
133+
julia> clamp([5, 5], s)
134+
2-element StaticArraysCore.SizedVector{2, Float64, Vector{Float64}} with indices SOneTo(2):
135+
1.0
136+
1.0
127137
```

src/CommonRLSpaces.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ using Reexport
66

77
using StaticArrays
88
using FillArrays
9+
using Random
10+
import Base: clamp
911

1012
export
1113
SpaceStyle,

src/array.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ struct RepeatedSpace{B, S<:Tuple} <: AbstractArraySpace
5757
elsize::S
5858
end
5959

60+
"""
61+
ArraySpace(base_space, size...)
62+
63+
Create a space of Arrays with shape `size`, where each element of the array is drawn from `base_space`.
64+
"""
6065
ArraySpace(base_space, size...) = RepeatedSpace(base_space, size)
6166

6267
SpaceStyle(s::RepeatedSpace) = SpaceStyle(s.base_space)

src/basic.jl

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
using Random
2-
3-
#####
4-
51
abstract type AbstractSpaceStyle end
62

73
struct FiniteSpaceStyle <: AbstractSpaceStyle end
84
struct ContinuousSpaceStyle <: AbstractSpaceStyle end
95
struct UnknownSpaceStyle <: AbstractSpaceStyle end
106

7+
"""
8+
SpaceStyle(space)
9+
10+
Holy-style trait that describes whether the space is continuous, finite discrete, or an unknown type. See CommonRLInterface for a more detailed description of the styles.
11+
"""
1112
SpaceStyle(space::Any) = UnknownSpaceStyle()
1213

1314
SpaceStyle(::Tuple) = FiniteSpaceStyle()
@@ -30,9 +31,34 @@ promote_spacestyle(_, _) = UnknownSpaceStyle()
3031
# handle case of 3 or more
3132
promote_spacestyle(s1, s2, s3, others...) = foldl(promote_spacestyle, (s1, s2, s3, args...))
3233

34+
"Return the size of the objects in a space. This is guaranteed to be defined if the objects in the space are arrays, but otherwise it may not be defined."
3335
function elsize end # note: different than Base.elsize
3436

37+
"""
38+
bounds(space)
39+
40+
Return a `Tuple` containing lower and upper bounds for the elements in a space.
41+
42+
For example, if `space` is a unit circle, `bounds(space)` will return `([-1.0, -1.0], [1.0, 1.0])`. This allows agents to choose policies that appropriately cover the space e.g. a normal distribution with a mean of `mean(bounds(space))` and a standard deviation of half the distance between the bounds.
43+
44+
`bounds` should be defined for ContinuousSpaceStyle spaces.
45+
46+
# Example
47+
```juliadoctest
48+
julia> bounds(1..2)
49+
(1, 2)
50+
```
51+
"""
3552
function bounds end
3653

54+
"""
55+
clamp(x, space)
56+
57+
Return an element of `space` that is near `x`.
58+
59+
For example, if `space` is a unit circle, `clamp([2.0, 0.0], space)` might return `[1.0, 0.0]`. This allows for a convenient way for an agent to find a valid action if they sample actions from a distribution that doesn't match the space exactly (e.g. a normal distribution).
60+
"""
61+
function clamp end
62+
3763
bounds(i::AbstractInterval) = (infimum(i), supremum(i))
3864
Base.clamp(x, i::AbstractInterval) = IntevalSets.clamp(x, i)

src/product.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,16 @@ struct TupleProduct{T<:Tuple}
1818
ss::T
1919
end
2020

21+
"""
22+
TupleProduct(space1, space2, ...)
23+
24+
Create a space representing the Cartesian product of the argument. Each element is a `Tuple` containing one element from each of the constituent spaces.
25+
26+
Use `subspaces` to access a `Tuple` containing the constituent spaces.
27+
"""
2128
TupleProduct(s1, s2, others...) = TupleProduct((s1, s2, others...))
2229

30+
"Return a `Tuple` containing the spaces used to create a `TupleProduct`"
2331
subspaces(s::TupleProduct) = s.ss
2432

2533
product(s1::TupleProduct, s2::TupleProduct) = TupleProduct(subspaces(s1)..., subspaces(s2)...)

0 commit comments

Comments
 (0)