Skip to content

Commit 344b3b6

Browse files
committed
move tests of sumtree here
1 parent b1a5c22 commit 344b3b6

File tree

2 files changed

+38
-195
lines changed

2 files changed

+38
-195
lines changed

README.md

Lines changed: 12 additions & 195 deletions
Original file line numberDiff line numberDiff line change
@@ -56,209 +56,26 @@ julia> for batch in t
5656
(a = [4, 1, 2], b = Bool[1, 0, 1])
5757
```
5858

59-
### `AbstractTrace`
59+
**Traces**
6060

61-
`Trace` is the most commonly used `AbstractTrace`. It provides a sequential view on other containers.
61+
- `Traces`
62+
- `MultiplexTraces`
63+
- `CircularSARTTraces`
64+
- `Episode`
65+
- `Episodes`
6266

63-
```julia
64-
julia> t = Trace([1,2,3])
65-
3-element Trace{Vector{Int64}, SubArray{Int64, 0, Vector{Int64}, Tuple{Int64}, true}}:
66-
1
67-
2
68-
3
69-
julia> push!(t, 4)
70-
4-element Vector{Int64}:
71-
1
72-
2
73-
3
74-
4
75-
76-
julia> append!(t, 5:6)
77-
6-element Vector{Int64}:
78-
1
79-
2
80-
3
81-
4
82-
5
83-
6
84-
85-
julia> pop!(t)
86-
6
87-
88-
julia> popfirst!(t)
89-
1
90-
91-
julia> t
92-
4-element Trace{Vector{Int64}, SubArray{Int64, 0, Vector{Int64}, Tuple{Int64}, true}}:
93-
2
94-
3
95-
4
96-
5
97-
98-
julia> empty!(t)
99-
Int64[]
100-
101-
julia> t
102-
0-element Trace{Vector{Int64}, SubArray{Int64, 0, Vector{Int64}, Tuple{Int64}, true}}
103-
```
104-
105-
In most cases, it's just the same with a `Vector`.
106-
107-
When an `AbstractArray` with higher dimension provided, it is **slice**d along the last dimension to provide a sequential view.
108-
109-
```julia
110-
julia> t = Trace(rand(2,3))
111-
3-element Trace{Matrix{Float64}, SubArray{Float64, 1, Matrix{Float64}, Tuple{Base.Slice{Base.OneTo{Int64}}, Int64}, true}}:
112-
[0.276012181224494, 0.6621365818458671]
113-
[0.9937726056924112, 0.3308302850028162]
114-
[0.9856543000075456, 0.6123660950650406]
115-
116-
julia> t[1]
117-
2-element view(::Matrix{Float64}, :, 1) with eltype Float64:
118-
0.276012181224494
119-
0.6621365818458671
120-
121-
julia> t[1] = [0., 1.]
122-
2-element Vector{Float64}:
123-
0.0
124-
1.0
125-
126-
julia> t
127-
3-element Trace{Matrix{Float64}, SubArray{Float64, 1, Matrix{Float64}, Tuple{Base.Slice{Base.OneTo{Int64}}, Int64}, true}}:
128-
[0.0, 1.0]
129-
[0.9937726056924112, 0.3308302850028162]
130-
[0.9856543000075456, 0.6123660950650406]
131-
132-
julia> t[[2,3,1]]
133-
2×3 view(::Matrix{Float64}, :, [2, 3, 1]) with eltype Float64:
134-
0.993773 0.985654 0.0
135-
0.33083 0.612366 1.0
136-
```
137-
138-
**Note** that when indexing a `Trace`, a **view** is returned. As you can see above, the data is modified in-place.
139-
140-
### `AbstractTraces`
141-
142-
`Traces` is one of the common `AbstractTraces`. It is similar to a `NamedTuple` of several traces.
143-
144-
```julia
145-
julia> t = Traces(;
146-
a=[1, 2],
147-
b=Bool[0, 1]
148-
) # note that `a` and `b` are converted into `Trace` implicitly
149-
Traces with 2 traces:
150-
:a => 2-element Trace{Vector{Int64}, SubArray{Int64, 0, Vector{Int64}, Tuple{Int64}, true}}
151-
:b => 2-element Trace{Vector{Bool}, SubArray{Bool, 0, Vector{Bool}, Tuple{Int64}, true}}
152-
153-
154-
julia> push!(t, (a=3, b=false))
67+
**Samplers**
15568

156-
julia> t
157-
Traces with 2 traces:
158-
:a => 3-element Trace{Vector{Int64}, SubArray{Int64, 0, Vector{Int64}, Tuple{Int64}, true}}
159-
:b => 3-element Trace{Vector{Bool}, SubArray{Bool, 0, Vector{Bool}, Tuple{Int64}, true}}
69+
- `BatchSampler`
16070

71+
**Controllers**
16172

162-
julia> t[:a]
163-
3-element Trace{Vector{Int64}, SubArray{Int64, 0, Vector{Int64}, Tuple{Int64}, true}}:
164-
1
165-
2
166-
3
73+
- `InsertSampleRatioController`
74+
- `AsyncInsertSampleRatioController`
16775

168-
julia> t[:b]
169-
3-element Trace{Vector{Bool}, SubArray{Bool, 0, Vector{Bool}, Tuple{Int64}, true}}:
170-
false
171-
true
172-
false
17376

174-
julia> t[1]
175-
(a = 1, b = false)
77+
Please refer tests for common usage. (TODO: generate docs and add links to above data structures)
17678

177-
julia> t[1:3]
178-
(a = [1, 2, 3], b = Bool[0, 1, 0])
179-
```
180-
181-
Another commonly used traces is `MultiplexTraces`. In reinforcement learning, *states* and *next-states* share most data except for the first and last element.
182-
183-
```julia
184-
julia> t = MultiplexTraces{(:state, :next_state)}([1,2,3]);
185-
186-
julia> t[:state]
187-
2-element Trace{SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true}, SubArray{Int64, 0, SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true}, Tuple{Int64}, true}}:
188-
1
189-
2
190-
191-
julia> t[:next_state]
192-
2-element Trace{SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true}, SubArray{Int64, 0, SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true}, Tuple{Int64}, true}}:
193-
2
194-
3
195-
196-
julia> push!(t, (;state=4))
197-
4-element Vector{Int64}:
198-
1
199-
2
200-
3
201-
4
202-
203-
julia> t[:state]
204-
3-element Trace{SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true}, SubArray{Int64, 0, SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true}, Tuple{Int64}, true}}:
205-
1
206-
2
207-
3
208-
209-
julia> t[:next_state]
210-
3-element Trace{SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true}, SubArray{Int64, 0, SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true}, Tuple{Int64}, true}}:
211-
2
212-
3
213-
4
214-
215-
julia> length(t)
216-
3
217-
```
218-
219-
Note that different kinds of `AbstractTraces` can be combined to form a `MergedTraces`.
220-
221-
```
222-
ulia> t1 = Traces(a=Int[])
223-
t2 = MultiplexTraces{(:b, :c)}(Int[])
224-
t3 = t1 + t2
225-
MergedTraces with 3 traces:
226-
:a => 0-element Trace{Vector{Int64}, SubArray{Int64, 0, Vector{Int64}, Tuple{Int64}, true}}
227-
:b => 0-element Trace{SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true}, SubArray{Int64, 0, SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true}, Tuple{Int64}, true}}
228-
:c => 0-element Trace{SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true}, SubArray{Int64, 0, SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true}, Tuple{Int64}, true}}
229-
230-
231-
julia> push!(t3, (a=1,b=2,c=3))
232-
233-
julia> t3[:a]
234-
1-element Trace{Vector{Int64}, SubArray{Int64, 0, Vector{Int64}, Tuple{Int64}, true}}:
235-
1
236-
237-
julia> t3[:b]
238-
1-element Trace{SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true}, SubArray{Int64, 0, SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true}, Tuple{Int64}, true}}:
239-
2
240-
241-
julia> t3[:c]
242-
1-element Trace{SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true}, SubArray{Int64, 0, SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true}, Tuple{Int64}, true}}:
243-
3
244-
245-
julia> push!(t3, (a=-1, b=-2))
246-
247-
julia> t3[:a]
248-
2-element Trace{Vector{Int64}, SubArray{Int64, 0, Vector{Int64}, Tuple{Int64}, true}}:
249-
1
250-
-1
251-
252-
julia> t3[:b]
253-
2-element Trace{SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true}, SubArray{Int64, 0, SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true}, Tuple{Int64}, true}}:
254-
2
255-
3
256-
257-
julia> t3[:c]
258-
2-element Trace{SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true}, SubArray{Int64, 0, SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true}, Tuple{Int64}, true}}:
259-
3
260-
-2
261-
```
26279
## Acknowledgement
26380

26481
This async version is mainly inspired by [deepmind/reverb](https://github.com/deepmind/reverb).

test/common.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,29 @@
1+
@testset "sum_tree" begin
2+
t = SumTree(8)
3+
4+
for i in 1:4
5+
push!(t, i)
6+
end
7+
8+
@test length(t) == 4
9+
@test size(t) == (4,)
10+
11+
for i in 5:16
12+
push!(t, i)
13+
end
14+
15+
@test length(t) == 8
16+
@test size(t) == (8,)
17+
@test t == 9:16
18+
19+
t[:] .= 1
20+
@test t == ones(8)
21+
@test all([get(t, v)[1] == i for (i, v) in enumerate(0.5:1.0:8)])
22+
23+
empty!(t)
24+
@test length(t) == 0
25+
end
26+
127
@testset "CircularArraySARTTraces" begin
228
t = CircularArraySARTTraces(;
329
capacity=3,

0 commit comments

Comments
 (0)