Skip to content

Fix interaction between CircularPrioritizedTraces and NStepBatchSampler #44

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
merged 8 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ReinforcementLearningTrajectories"
uuid = "6486599b-a3cd-4e92-a99a-2cea90cc8c3c"
version = "0.2"
version = "0.2.1"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
4 changes: 3 additions & 1 deletion src/common/CircularPrioritizedTraces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,6 @@ function Base.getindex(ts::CircularPrioritizedTraces, s::Symbol)
end
end

Base.getindex(t::CircularPrioritizedTraces{<:Any,names}, i) where {names} = NamedTuple{names}(map(k -> t[k][i], names))
Base.getindex(t::CircularPrioritizedTraces{<:Any,names}, i) where {names} = NamedTuple{names}(map(k -> t[k][i], names))

capacity(t::CircularPrioritizedTraces) = ReinforcementLearningTrajectories.capacity(t.traces)
15 changes: 14 additions & 1 deletion src/episodes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,21 @@ struct PartialNamedTuple{T}
namedtuple::T
end

# Capacity of an EpisodesBuffer is the capacity of the underlying traces + 1 for certain cases
function is_capacity_plus_one(traces::AbstractTraces)
if any(t->t isa MultiplexTraces, traces.traces)
# MultiplexTraces buffer next_state and next_action, so we need to add one to the capacity
return true
elseif traces isa CircularPrioritizedTraces
# CircularPrioritizedTraces buffer next_state and next_action, so we need to add one to the capacity
return true
else
false
end
end

function EpisodesBuffer(traces::AbstractTraces)
cap = any(t->t isa MultiplexTraces, traces.traces) ? capacity(traces) + 1 : capacity(traces)
cap = is_capacity_plus_one(traces) ? capacity(traces) + 1 : capacity(traces)
@assert isempty(traces) "EpisodesBuffer must be initialized with empty traces."
if !isinf(cap)
legalinds = CircularBuffer{Bool}(cap)
Expand Down
8 changes: 6 additions & 2 deletions src/samplers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,12 @@ function StatsBase.sample(s::NStepBatchSampler, ts, ::Val{SS′L′ART}, inds)
NamedTuple{SSLART}(map(collect, (s, s′, l, a, r, t)))
end

function StatsBase.sample(s::NStepBatchSampler{names}, t::CircularPrioritizedTraces) where {names}
inds, priorities = rand(s.rng, t.priorities, s.batch_size)
function StatsBase.sample(s::NStepBatchSampler{names}, e::EpisodesBuffer{<:Any, <:Any, <:CircularPrioritizedTraces}) where {names}
t = e.traces
st = deepcopy(t.priorities)
st .*= e.sampleable_inds[1:end-1] #temporary sumtree that puts 0 priority to non sampleable indices.
inds, priorities = rand(s.rng, st, s.batch_size)

merge(
(key=t.keys[inds], priority=priorities),
StatsBase.sample(s, t.traces, Val(names), inds)
Expand Down
34 changes: 33 additions & 1 deletion test/samplers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,36 @@ end
@test xs3.reward[2] ≈ 5 + γ * (6 + γ * 7)
@test xs3.reward[3] ≈ 7 + γ * (8 + γ * 9)
end
#! format: on
#! format: on

@testset "Trajectory with CircularArraySARTSTraces and NStepBatchSampler" begin
n=1
γ=0.99f0

t = Trajectory(
container=CircularPrioritizedTraces(
CircularArraySARTSTraces(
capacity=5,
state=Float32 => (4,),
);
default_priority=100.0f0
),
sampler=NStepBatchSampler{SS′ART}(
n=n,
γ=γ,
batch_size=32,
),
controller=InsertSampleRatioController(
threshold=100,
n_inserted=-1
)
)

push!(t, (state = 1, action = true))
for i = 1:9
push!(t, (state = i+1, action = true, reward = i, terminal = false))
end

b = RLTrajectories.StatsBase.sample(t)
@test haskey(b, :priority)
end