1
0
Fork 0
mirror of https://gitlab.rlp.net/mobitar/ReCo.jl.git synced 2024-12-21 00:51:21 +00:00
ReCo.jl/src/RL/Hooks.jl

87 lines
2.2 KiB
Julia
Raw Normal View History

2022-01-14 12:01:14 +00:00
using ..ReCo: Particle
2022-01-18 01:17:52 +00:00
function pre_integration_hook!(::EnvHelper)
2022-01-14 12:01:14 +00:00
return ReCo.method_not_implemented()
2022-01-11 18:00:41 +00:00
end
2022-01-18 01:17:52 +00:00
function state_update_helper_hook!(
2022-01-31 00:47:48 +00:00
::EnvHelper, id1::Int64, id2::Int64, r⃗₁₂::SVector{2,Float64}, distance²::Float64
2022-01-11 18:00:41 +00:00
)
2022-01-14 12:01:14 +00:00
return ReCo.method_not_implemented()
2022-01-11 18:00:41 +00:00
end
2022-01-18 01:17:52 +00:00
function state_update_hook!(::EnvHelper, particles::Vector{Particle})
2022-01-14 12:01:14 +00:00
return ReCo.method_not_implemented()
2022-01-11 18:00:41 +00:00
end
function update_reward!(::Env, ::EnvHelper, particle::Particle)
2022-01-14 12:01:14 +00:00
return ReCo.method_not_implemented()
2022-01-11 18:00:41 +00:00
end
2022-01-18 01:17:52 +00:00
function update_table_and_actions_hook!(
2022-01-11 18:00:41 +00:00
env_helper::EnvHelper, particle::Particle, first_integration_step::Bool
)
env, agent, hook = get_env_agent_hook(env_helper)
if !first_integration_step
# Old state
2022-01-29 15:45:36 +00:00
env.shared.state_id = env_helper.shared.old_states_id[particle.id]
2022-01-11 18:00:41 +00:00
2022-01-29 15:45:36 +00:00
action_id = env_helper.shared.actions_id[particle.id]
2022-01-11 18:00:41 +00:00
# Pre act
2022-01-15 20:27:15 +00:00
agent(PRE_ACT_STAGE, env, action_id)
hook(PRE_ACT_STAGE, agent, env, action_id)
2022-01-11 18:00:41 +00:00
# Update to current state
2022-01-29 15:45:36 +00:00
env.shared.state_id = env_helper.shared.states_id[particle.id]
2022-01-11 18:00:41 +00:00
# Update reward
update_reward!(env, env_helper, particle)
# Post act
agent(POST_ACT_STAGE, env)
hook(POST_ACT_STAGE, agent, env)
end
# Update action
2022-01-15 20:27:15 +00:00
action_id = agent(env)
action = env.shared.action_space[action_id]
2022-01-11 18:00:41 +00:00
2022-01-29 15:45:36 +00:00
env_helper.shared.actions[particle.id] = action
env_helper.shared.actions_id[particle.id] = action_id
2022-01-11 18:00:41 +00:00
return nothing
end
2022-01-18 01:17:52 +00:00
function act_hook!(::Particle, ::Nothing, args...)
return nothing
end
2022-01-11 18:00:41 +00:00
2022-01-18 01:17:52 +00:00
function act_hook!(
particle::Particle, env_helper::EnvHelper, δt::Float64, si::Float64, co::Float64
2022-01-11 18:00:41 +00:00
)
# Apply action
action = env_helper.shared.actions[particle.id]
vδt = action[1] * δt
particle.tmp_c += SVector(vδt * co, vδt * si)
particle.φ += action[2] * δt
2022-01-29 15:45:36 +00:00
return nothing
end
function copy_states_to_old_states_hook!(env_helper::EnvHelper)
n_particles = env_helper.shared.n_particles
2022-04-05 01:12:25 +00:00
@simd for particle_id in 1:n_particles
2022-01-29 15:45:36 +00:00
env_helper.shared.old_states_id[particle_id] = env_helper.shared.states_id[particle_id]
end
return nothing
end
function copy_states_to_old_states_hook!(::Nothing)
2022-01-11 18:00:41 +00:00
return nothing
2022-03-19 22:11:03 +00:00
end