Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
marl-flatland
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
manavsinghal157
marl-flatland
Commits
8d6304b3
Commit
8d6304b3
authored
4 years ago
by
Egli Adrian (IT-SCI-API-PFI)
Browse files
Options
Downloads
Patches
Plain Diff
. doc
parent
41d4b483
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
reinforcement_learning/ppo_agent.py
+21
-14
21 additions, 14 deletions
reinforcement_learning/ppo_agent.py
with
21 additions
and
14 deletions
reinforcement_learning/ppo_agent.py
+
21
−
14
View file @
8d6304b3
...
@@ -165,38 +165,45 @@ class PPOAgent(Policy):
...
@@ -165,38 +165,45 @@ class PPOAgent(Policy):
return
states
,
actions
,
rewards
,
states_next
,
dones
,
prob_actions
return
states
,
actions
,
rewards
,
states_next
,
dones
,
prob_actions
def
train_net
(
self
):
def
train_net
(
self
):
for
handle
in
range
(
len
(
self
.
memory
)):
# Optimize policy for K epochs:
agent_episode_history
=
self
.
memory
.
get_transitions
(
handle
)
for
_
in
range
(
self
.
K_epoch
):
if
len
(
agent_episode_history
)
>
0
:
# All agents have to propagate their experiences made during past episode
# convert the replay buffer to torch tensors (arrays)
for
handle
in
range
(
len
(
self
.
memory
)):
states
,
actions
,
rewards
,
states_next
,
dones
,
probs_action
=
\
# Extract agent's episode history (list of all transitions)
self
.
_convert_transitions_to_torch_tensors
(
agent_episode_history
)
agent_episode_history
=
self
.
memory
.
get_transitions
(
handle
)
if
len
(
agent_episode_history
)
>
0
:
# Optimize policy for K epochs:
# Convert the replay buffer to torch tensors (arrays)
for
_
in
range
(
self
.
K_epoch
):
states
,
actions
,
rewards
,
states_next
,
dones
,
probs_action
=
\
# evaluating actions (actor) and values (critic)
self
.
_convert_transitions_to_torch_tensors
(
agent_episode_history
)
# Evaluating actions (actor) and values (critic)
logprobs
,
state_values
,
dist_entropy
=
self
.
actor_critic_model
.
evaluate
(
states
,
actions
)
logprobs
,
state_values
,
dist_entropy
=
self
.
actor_critic_model
.
evaluate
(
states
,
actions
)
#
f
inding the ratios (pi_thetas / pi_thetas_replayed):
#
F
inding the ratios (pi_thetas / pi_thetas_replayed):
ratios
=
torch
.
exp
(
logprobs
-
probs_action
.
detach
())
ratios
=
torch
.
exp
(
logprobs
-
probs_action
.
detach
())
#
f
inding Surrogate Los
s:
#
F
inding Surrogate Lo
o
s
advantages
=
rewards
-
state_values
.
detach
()
advantages
=
rewards
-
state_values
.
detach
()
surr1
=
ratios
*
advantages
surr1
=
ratios
*
advantages
surr2
=
torch
.
clamp
(
ratios
,
1.
-
self
.
surrogate_eps_clip
,
1.
+
self
.
surrogate_eps_clip
)
*
advantages
surr2
=
torch
.
clamp
(
ratios
,
1.
-
self
.
surrogate_eps_clip
,
1.
+
self
.
surrogate_eps_clip
)
*
advantages
# The loss function is used to estimate the gardient and use the entropy function based
# heuristic to penalize the gradient function when the policy becomes deterministic this would let
# the gardient to become very flat and so the gradient is no longer useful.
loss
=
\
loss
=
\
-
torch
.
min
(
surr1
,
surr2
)
\
-
torch
.
min
(
surr1
,
surr2
)
\
+
self
.
weight_loss
*
self
.
loss_function
(
state_values
,
rewards
)
\
+
self
.
weight_loss
*
self
.
loss_function
(
state_values
,
rewards
)
\
-
self
.
weight_entropy
*
dist_entropy
-
self
.
weight_entropy
*
dist_entropy
#
m
ake a gradient step
#
M
ake a gradient step
self
.
optimizer
.
zero_grad
()
self
.
optimizer
.
zero_grad
()
loss
.
mean
().
backward
()
loss
.
mean
().
backward
()
self
.
optimizer
.
step
()
self
.
optimizer
.
step
()
#
stor
e current loss to the agent
#
Transfer th
e current loss to the agent
s loss (information) for debug purpose only
self
.
loss
=
loss
.
mean
().
detach
().
numpy
()
self
.
loss
=
loss
.
mean
().
detach
().
numpy
()
# Reset all collect transition data
self
.
memory
.
reset
()
self
.
memory
.
reset
()
def
end_episode
(
self
,
train
):
def
end_episode
(
self
,
train
):
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment