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
769f25ec
Commit
769f25ec
authored
4 years ago
by
Egli Adrian (IT-SCI-API-PFI)
Browse files
Options
Downloads
Patches
Plain Diff
small fix in object
parent
87273288
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
reinforcement_learning/multi_agent_training.py
+2
-2
2 additions, 2 deletions
reinforcement_learning/multi_agent_training.py
reinforcement_learning/ppo/ppo_agent.py
+8
-6
8 additions, 6 deletions
reinforcement_learning/ppo/ppo_agent.py
with
10 additions
and
8 deletions
reinforcement_learning/multi_agent_training.py
+
2
−
2
View file @
769f25ec
...
@@ -257,7 +257,7 @@ def train_agent(train_params, train_env_params, eval_env_params, obs_params):
...
@@ -257,7 +257,7 @@ def train_agent(train_params, train_env_params, eval_env_params, obs_params):
# Reset environment
# Reset environment
reset_timer
.
start
()
reset_timer
.
start
()
number_of_agents
=
int
(
min
(
n_agents
,
1
+
np
.
floor
(
episode_idx
/
200
)))
number_of_agents
=
int
(
min
(
n_agents
,
1
+
np
.
floor
(
episode_idx
/
200
)))
train_env_params
.
n_agents
=
1
#
episode_idx % number_of_agents + 1
train_env_params
.
n_agents
=
episode_idx
%
number_of_agents
+
1
train_env
=
create_rail_env
(
train_env_params
,
tree_observation
)
train_env
=
create_rail_env
(
train_env_params
,
tree_observation
)
obs
,
info
=
train_env
.
reset
(
regenerate_rail
=
True
,
regenerate_schedule
=
True
)
obs
,
info
=
train_env
.
reset
(
regenerate_rail
=
True
,
regenerate_schedule
=
True
)
...
@@ -314,7 +314,7 @@ def train_agent(train_params, train_env_params, eval_env_params, obs_params):
...
@@ -314,7 +314,7 @@ def train_agent(train_params, train_env_params, eval_env_params, obs_params):
next_obs
,
all_rewards
,
done
,
info
=
train_env
.
step
(
action_dict
)
next_obs
,
all_rewards
,
done
,
info
=
train_env
.
step
(
action_dict
)
# Reward shaping .Dead-lock .NotMoving .NotStarted
# Reward shaping .Dead-lock .NotMoving .NotStarted
if
Fals
e
:
if
Tru
e
:
agent_positions
=
get_agent_positions
(
train_env
)
agent_positions
=
get_agent_positions
(
train_env
)
for
agent_handle
in
train_env
.
get_agent_handles
():
for
agent_handle
in
train_env
.
get_agent_handles
():
agent
=
train_env
.
agents
[
agent_handle
]
agent
=
train_env
.
agents
[
agent_handle
]
...
...
This diff is collapsed.
Click to expand it.
reinforcement_learning/ppo/ppo_agent.py
+
8
−
6
View file @
769f25ec
...
@@ -12,11 +12,11 @@ from reinforcement_learning.policy import Policy
...
@@ -12,11 +12,11 @@ from reinforcement_learning.policy import Policy
LEARNING_RATE
=
0.1e-4
LEARNING_RATE
=
0.1e-4
GAMMA
=
0.98
GAMMA
=
0.98
LMBDA
=
0.9
L
A
MBDA
=
0.9
EPS_CLIP
=
0.1
SURROGATE_
EPS_CLIP
=
0.
0
1
K_EPOCH
=
3
K_EPOCH
=
3
device
=
torch
.
device
(
"
cuda:0
"
if
torch
.
cuda
.
is_available
()
else
"
cpu
"
)
device
=
torch
.
device
(
"
cpu
"
)
#"
cuda:0" if torch.cuda.is_available() else "cpu")
print
(
"
device:
"
,
device
)
print
(
"
device:
"
,
device
)
...
@@ -215,7 +215,7 @@ class PPOAgent(Policy):
...
@@ -215,7 +215,7 @@ class PPOAgent(Policy):
advantage_list
=
[]
advantage_list
=
[]
advantage_value
=
0.0
advantage_value
=
0.0
for
difference_to_expected_value_t
in
difference_to_expected_value_deltas
[::
-
1
]:
for
difference_to_expected_value_t
in
difference_to_expected_value_deltas
[::
-
1
]:
advantage_value
=
LMBDA
*
advantage_value
+
difference_to_expected_value_t
[
0
]
advantage_value
=
L
A
MBDA
*
advantage_value
+
difference_to_expected_value_t
[
0
]
advantage_list
.
append
([
advantage_value
])
advantage_list
.
append
([
advantage_value
])
advantage_list
.
reverse
()
advantage_list
.
reverse
()
advantages
=
torch
.
tensor
(
advantage_list
,
dtype
=
torch
.
float
)
advantages
=
torch
.
tensor
(
advantage_list
,
dtype
=
torch
.
float
)
...
@@ -227,9 +227,11 @@ class PPOAgent(Policy):
...
@@ -227,9 +227,11 @@ class PPOAgent(Policy):
# Normal Policy Gradient objective
# Normal Policy Gradient objective
surrogate_objective
=
ratios
*
advantages
surrogate_objective
=
ratios
*
advantages
# clipped version of Normal Policy Gradient objective
# clipped version of Normal Policy Gradient objective
clipped_surrogate_objective
=
torch
.
clamp
(
ratios
*
advantages
,
1
-
EPS_CLIP
,
1
+
EPS_CLIP
)
clipped_surrogate_objective
=
torch
.
clamp
(
ratios
*
advantages
,
1
-
SURROGATE_EPS_CLIP
,
1
+
SURROGATE_EPS_CLIP
)
# create value loss function
# create value loss function
value_loss
=
F
.
mse
_loss
(
self
.
value_network
(
states
),
value_loss
=
F
.
smooth_l1
_loss
(
self
.
value_network
(
states
),
estimated_target_value
.
detach
())
estimated_target_value
.
detach
())
# create final loss function
# create final loss function
loss
=
-
torch
.
min
(
surrogate_objective
,
clipped_surrogate_objective
)
+
value_loss
loss
=
-
torch
.
min
(
surrogate_objective
,
clipped_surrogate_objective
)
+
value_loss
...
...
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