|
| 1 | +function RLCore.Experiment( |
| 2 | + ::Val{:JuliaRL}, |
| 3 | + ::Val{:BasicDQN}, |
| 4 | + ::Val{:EmptyRoom}, |
| 5 | + ::Nothing; |
| 6 | + seed = 123, |
| 7 | + save_dir = nothing, |
| 8 | +) |
| 9 | + if isnothing(save_dir) |
| 10 | + t = Dates.format(now(), "yyyy_mm_dd_HH_MM_SS") |
| 11 | + save_dir = joinpath(pwd(), "checkpoints", "JuliaRL_BasicDQN_EmptyRoom$(t)") |
| 12 | + end |
| 13 | + log_dir = joinpath(save_dir, "tb_log") |
| 14 | + lg = TBLogger(log_dir, min_level = Logging.Info) |
| 15 | + rng = StableRNG(seed) |
| 16 | + |
| 17 | + inner_env = GridWorlds.EmptyRoom(rng = rng) |
| 18 | + action_space_mapping = x -> Base.OneTo(length(RLBase.action_space(inner_env))) |
| 19 | + action_mapping = i -> RLBase.action_space(inner_env)[i] |
| 20 | + env = RLEnvs.ActionTransformedEnv(inner_env, action_space_mapping = action_space_mapping, action_mapping = action_mapping) |
| 21 | + env = RLEnvs.StateOverriddenEnv(env, x -> vec(Float32.(x))) |
| 22 | + env = RewardOverriddenEnv(env, x -> x - convert(typeof(x), 0.01)) |
| 23 | + env = MaxTimeoutEnv(env, 240) |
| 24 | + |
| 25 | + ns, na = length(state(env)), length(action_space(env)) |
| 26 | + agent = Agent( |
| 27 | + policy = QBasedPolicy( |
| 28 | + learner = BasicDQNLearner( |
| 29 | + approximator = NeuralNetworkApproximator( |
| 30 | + model = Chain( |
| 31 | + Dense(ns, 128, relu; initW = glorot_uniform(rng)), |
| 32 | + Dense(128, 128, relu; initW = glorot_uniform(rng)), |
| 33 | + Dense(128, na; initW = glorot_uniform(rng)), |
| 34 | + ) |> cpu, |
| 35 | + optimizer = ADAM(), |
| 36 | + ), |
| 37 | + batch_size = 32, |
| 38 | + min_replay_history = 100, |
| 39 | + loss_func = huber_loss, |
| 40 | + rng = rng, |
| 41 | + ), |
| 42 | + explorer = EpsilonGreedyExplorer( |
| 43 | + kind = :exp, |
| 44 | + ϵ_stable = 0.01, |
| 45 | + decay_steps = 500, |
| 46 | + rng = rng, |
| 47 | + ), |
| 48 | + ), |
| 49 | + trajectory = CircularArraySARTTrajectory( |
| 50 | + capacity = 1000, |
| 51 | + state = Vector{Float32} => (ns,), |
| 52 | + ), |
| 53 | + ) |
| 54 | + |
| 55 | + stop_condition = StopAfterStep(10_000) |
| 56 | + |
| 57 | + total_reward_per_episode = TotalRewardPerEpisode() |
| 58 | + time_per_step = TimePerStep() |
| 59 | + hook = ComposedHook( |
| 60 | + total_reward_per_episode, |
| 61 | + time_per_step, |
| 62 | + DoEveryNStep() do t, agent, env |
| 63 | + with_logger(lg) do |
| 64 | + @info "training" loss = agent.policy.learner.loss |
| 65 | + end |
| 66 | + end, |
| 67 | + DoEveryNEpisode() do t, agent, env |
| 68 | + with_logger(lg) do |
| 69 | + @info "training" reward = total_reward_per_episode.rewards[end] log_step_increment = |
| 70 | + 0 |
| 71 | + end |
| 72 | + end, |
| 73 | + ) |
| 74 | + |
| 75 | + description = """ |
| 76 | + This experiment uses three dense layers to approximate the Q value. |
| 77 | + The testing environment is EmptyRoom. |
| 78 | +
|
| 79 | + You can view the runtime logs with `tensorboard --logdir $log_dir`. |
| 80 | + Some useful statistics are stored in the `hook` field of this experiment. |
| 81 | + """ |
| 82 | + |
| 83 | + Experiment(agent, env, stop_condition, hook, description) |
| 84 | +end |
0 commit comments