Skip to content

refactored multi agent proposal #926

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
34 changes: 21 additions & 13 deletions src/ReinforcementLearningCore/src/policies/agent/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@

@functor Agent (policy,)

function Base.push!(agent::Agent, ::PreActStage, env::AbstractEnv)
push!(agent, state(env))
end

# !!! TODO: In async scenarios, parameters of the policy may still be updating
# (partially), which will result to incorrect action. This should be addressed
# in Oolong.jl with a wrapper
Expand All @@ -64,11 +60,8 @@
action
end

# Multiagent Version
function RLBase.plan!(agent::Agent{P,T,C}, env::E, p::Symbol) where {P,T,C,E<:AbstractEnv}
action = RLBase.plan!(agent.policy, env, p)
push!(agent.trajectory, agent.cache, action)
action
function Base.push!(agent::Agent, ::PreActStage, env::AbstractEnv)
push!(agent, state(env))

Check warning on line 64 in src/ReinforcementLearningCore/src/policies/agent/base.jl

View check run for this annotation

Codecov / codecov/patch

src/ReinforcementLearningCore/src/policies/agent/base.jl#L63-L64

Added lines #L63 - L64 were not covered by tests
end

function Base.push!(agent::Agent{P,T,C}, ::PostActStage, env::E) where {P,T,C,E<:AbstractEnv}
Expand All @@ -79,11 +72,26 @@
RLBase.reset!(agent.cache)
end

function Base.push!(agent::Agent, ::PostExperimentStage, env::E, player::Symbol) where {E<:AbstractEnv}
RLBase.reset!(agent.cache)
end

function Base.push!(agent::Agent{P,T,C}, state::S) where {P,T,C,S}
push!(agent.cache, state)
end

# Multiagent Version
function RLBase.plan!(agent::Agent{P,T,C}, env::E, p::Symbol) where {P,T,C,E<:AbstractEnv}
action = RLBase.plan!(agent.policy, env, p)
push!(agent.trajectory, agent.cache, action)
action

Check warning on line 83 in src/ReinforcementLearningCore/src/policies/agent/base.jl

View check run for this annotation

Codecov / codecov/patch

src/ReinforcementLearningCore/src/policies/agent/base.jl#L80-L83

Added lines #L80 - L83 were not covered by tests
end

# for simultaneous DynamicStyle environments, we have to define push! operations
function Base.push!(agent::Agent, ::PreActStage, env::AbstractEnv, player::Symbol)
push!(agent, state(env, player))

Check warning on line 88 in src/ReinforcementLearningCore/src/policies/agent/base.jl

View check run for this annotation

Codecov / codecov/patch

src/ReinforcementLearningCore/src/policies/agent/base.jl#L87-L88

Added lines #L87 - L88 were not covered by tests
end

function Base.push!(agent::Agent{P,T,C}, ::PostActStage, env::E, player::Symbol) where {P,T,C,E<:AbstractEnv}
push!(agent.cache, reward(env, player), is_terminated(env))

Check warning on line 92 in src/ReinforcementLearningCore/src/policies/agent/base.jl

View check run for this annotation

Codecov / codecov/patch

src/ReinforcementLearningCore/src/policies/agent/base.jl#L91-L92

Added lines #L91 - L92 were not covered by tests
end

function Base.push!(agent::Agent, ::PostExperimentStage, env::E, player::Symbol) where {E<:AbstractEnv}
RLBase.reset!(agent.cache)

Check warning on line 96 in src/ReinforcementLearningCore/src/policies/agent/base.jl

View check run for this annotation

Codecov / codecov/patch

src/ReinforcementLearningCore/src/policies/agent/base.jl#L95-L96

Added lines #L95 - L96 were not covered by tests
end
12 changes: 7 additions & 5 deletions src/ReinforcementLearningCore/src/policies/agent/multi_agent.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,12 @@

if check_stop(stop_condition, policy, env)
is_stop = true
@timeit_debug timer "push!(policy) PreActStage" push!(multiagent_policy, PreActStage(), env)
@timeit_debug timer "optimise! PreActStage" optimise!(multiagent_policy, PreActStage())
@timeit_debug timer "push!(hook) PreActStage" push!(multiagent_hook, PreActStage(), policy, env)
@timeit_debug timer "plan!" RLBase.plan!(multiagent_policy, env) # let the policy see the last observation
if !is_terminated(env)
@timeit_debug timer "push!(policy) PreActStage" push!(multiagent_policy, PreActStage(), env)
@timeit_debug timer "optimise! PreActStage" optimise!(multiagent_policy, PreActStage())
@timeit_debug timer "push!(hook) PreActStage" push!(multiagent_hook, PreActStage(), policy, env)
@timeit_debug timer "plan!" RLBase.plan!(multiagent_policy, env) # let the policy see the last observation

Check warning on line 140 in src/ReinforcementLearningCore/src/policies/agent/multi_agent.jl

View check run for this annotation

Codecov / codecov/patch

src/ReinforcementLearningCore/src/policies/agent/multi_agent.jl#L136-L140

Added lines #L136 - L140 were not covered by tests
end
break
end

Expand Down Expand Up @@ -228,7 +230,7 @@
end

function RLBase.plan!(multiagent::MultiAgentPolicy, env::E) where {E<:AbstractEnv}
return (RLBase.plan!(multiagent[player], env, player) for player in players(env))
return NamedTuple(player => RLBase.plan!(multiagent[player], env, player) for player in players(env))

Check warning on line 233 in src/ReinforcementLearningCore/src/policies/agent/multi_agent.jl

View check run for this annotation

Codecov / codecov/patch

src/ReinforcementLearningCore/src/policies/agent/multi_agent.jl#L233

Added line #L233 was not covered by tests
end

function RLBase.optimise!(multiagent::MultiAgentPolicy, stage::S) where {S<:AbstractStage}
Expand Down