|
| 1 | +export REMDQNLearner |
| 2 | + |
| 3 | +mutable struct REMDQNLearner{ |
| 4 | + Tq<:AbstractApproximator, |
| 5 | + Tt<:AbstractApproximator, |
| 6 | + Tf, |
| 7 | + R<:AbstractRNG, |
| 8 | +} <: AbstractLearner |
| 9 | + approximator::Tq |
| 10 | + target_approximator::Tt |
| 11 | + loss_func::Tf |
| 12 | + min_replay_history::Int |
| 13 | + update_freq::Int |
| 14 | + update_step::Int |
| 15 | + target_update_freq::Int |
| 16 | + sampler::NStepBatchSampler |
| 17 | + ensemble_num::Int |
| 18 | + ensemble_method::Symbol |
| 19 | + rng::R |
| 20 | + # for logging |
| 21 | + loss::Float32 |
| 22 | +end |
| 23 | + |
| 24 | +""" |
| 25 | + REMDQNLearner(;kwargs...) |
| 26 | +
|
| 27 | +See paper: [An Optimistic Perspective on Offline Reinforcement Learning](https://arxiv.org/abs/1907.04543) |
| 28 | +
|
| 29 | +# Keywords |
| 30 | +
|
| 31 | +- `approximator`::[`AbstractApproximator`](@ref): used to get Q-values of a state. |
| 32 | +- `target_approximator`::[`AbstractApproximator`](@ref): similar to `approximator`, but used to estimate the target (the next state). |
| 33 | +- `loss_func`: the loss function. |
| 34 | +- `γ::Float32=0.99f0`: discount rate. |
| 35 | +- `batch_size::Int=32` |
| 36 | +- `update_horizon::Int=1`: length of update ('n' in n-step update). |
| 37 | +- `min_replay_history::Int=32`: number of transitions that should be experienced before updating the `approximator`. |
| 38 | +- `update_freq::Int=4`: the frequency of updating the `approximator`. |
| 39 | +- `ensemble_num::Int=1`: the number of ensemble approximators. |
| 40 | +- `ensemble_method::Symbol=:rand`: the method of combining Q values. ':rand' represents random ensemble mixture, and ':mean' is the average. |
| 41 | +- `target_update_freq::Int=100`: the frequency of syncing `target_approximator`. |
| 42 | +- `stack_size::Union{Int, Nothing}=4`: use the recent `stack_size` frames to form a stacked state. |
| 43 | +- `traces = SARTS`, set to `SLARTSL` if you are to apply to an environment of `FULL_ACTION_SET`. |
| 44 | +- `rng = Random.GLOBAL_RNG` |
| 45 | +""" |
| 46 | +function REMDQNLearner(; |
| 47 | + approximator::Tq, |
| 48 | + target_approximator::Tt, |
| 49 | + loss_func::Tf, |
| 50 | + stack_size::Union{Int,Nothing} = nothing, |
| 51 | + γ::Float32 = 0.99f0, |
| 52 | + batch_size::Int = 32, |
| 53 | + update_horizon::Int = 1, |
| 54 | + min_replay_history::Int = 32, |
| 55 | + update_freq::Int = 1, |
| 56 | + ensemble_num::Int = 1, |
| 57 | + ensemble_method::Symbol = :rand, |
| 58 | + target_update_freq::Int = 100, |
| 59 | + traces = SARTS, |
| 60 | + update_step = 0, |
| 61 | + rng = Random.GLOBAL_RNG, |
| 62 | +) where {Tq,Tt,Tf} |
| 63 | + copyto!(approximator, target_approximator) |
| 64 | + sampler = NStepBatchSampler{traces}(; |
| 65 | + γ = γ, |
| 66 | + n = update_horizon, |
| 67 | + stack_size = stack_size, |
| 68 | + batch_size = batch_size, |
| 69 | + ) |
| 70 | + REMDQNLearner( |
| 71 | + approximator, |
| 72 | + target_approximator, |
| 73 | + loss_func, |
| 74 | + min_replay_history, |
| 75 | + update_freq, |
| 76 | + update_step, |
| 77 | + target_update_freq, |
| 78 | + sampler, |
| 79 | + ensemble_num, |
| 80 | + ensemble_method, |
| 81 | + rng, |
| 82 | + 0.0f0, |
| 83 | + ) |
| 84 | +end |
| 85 | + |
| 86 | +Flux.functor(x::REMDQNLearner) = (Q = x.approximator, Qₜ = x.target_approximator), |
| 87 | +y -> begin |
| 88 | + x = @set x.approximator = y.Q |
| 89 | + x = @set x.target_approximator = y.Qₜ |
| 90 | + x |
| 91 | +end |
| 92 | + |
| 93 | +function (learner::REMDQNLearner)(env) |
| 94 | + s = send_to_device(device(learner.approximator), state(env)) |
| 95 | + s = Flux.unsqueeze(s, ndims(s) + 1) |
| 96 | + q = reshape(learner.approximator(s), :, learner.ensemble_num) |
| 97 | + vec(mean(q, dims = 2)) |> send_to_host |
| 98 | +end |
| 99 | + |
| 100 | +function RLBase.update!(learner::REMDQNLearner, batch::NamedTuple) |
| 101 | + Q = learner.approximator |
| 102 | + Qₜ = learner.target_approximator |
| 103 | + γ = learner.sampler.γ |
| 104 | + loss_func = learner.loss_func |
| 105 | + n = learner.sampler.n |
| 106 | + batch_size = learner.sampler.batch_size |
| 107 | + ensemble_num = learner.ensemble_num |
| 108 | + D = device(Q) |
| 109 | + # Build a convex polygon to make a combination of multiple Q-value estimates as a Q-value estimate. |
| 110 | + if learner.ensemble_method == :rand |
| 111 | + convex_polygon = rand(Float32, (1, ensemble_num)) |
| 112 | + else |
| 113 | + convex_polygon = ones(Float32, (1, ensemble_num)) |
| 114 | + end |
| 115 | + convex_polygon ./= sum(convex_polygon) |
| 116 | + convex_polygon = send_to_device(D, convex_polygon) |
| 117 | + |
| 118 | + s, a, r, t, s′ = (send_to_device(D, batch[x]) for x in SARTS) |
| 119 | + a = CartesianIndex.(a, 1:batch_size) |
| 120 | + |
| 121 | + target_q = Qₜ(s′) |
| 122 | + target_q = convex_polygon .* reshape(target_q, :, ensemble_num, batch_size) |
| 123 | + target_q = dropdims(sum(target_q, dims=2), dims=2) |
| 124 | + |
| 125 | + if haskey(batch, :next_legal_actions_mask) |
| 126 | + l′ = send_to_device(D, batch[:next_legal_actions_mask]) |
| 127 | + target_q .+= ifelse.(l′, 0.0f0, typemin(Float32)) |
| 128 | + end |
| 129 | + |
| 130 | + q′ = dropdims(maximum(target_q; dims = 1), dims = 1) |
| 131 | + G = r .+ γ^n .* (1 .- t) .* q′ |
| 132 | + |
| 133 | + gs = gradient(params(Q)) do |
| 134 | + q = Q(s) |
| 135 | + q = convex_polygon .* reshape(q, :, ensemble_num, batch_size) |
| 136 | + q = dropdims(sum(q, dims=2), dims=2)[a] |
| 137 | + |
| 138 | + loss = loss_func(G, q) |
| 139 | + ignore() do |
| 140 | + learner.loss = loss |
| 141 | + end |
| 142 | + loss |
| 143 | + end |
| 144 | + |
| 145 | + update!(Q, gs) |
| 146 | +end |
| 147 | + |
0 commit comments