Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Flatland
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD 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
pranjal_dhole
Flatland
Commits
172ac2c0
Commit
172ac2c0
authored
3 years ago
by
nimishsantosh107
Browse files
Options
Downloads
Patches
Plain Diff
end reward handling - decoupled
parent
0eebf660
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
flatland/envs/rail_env.py
+31
-21
31 additions, 21 deletions
flatland/envs/rail_env.py
with
31 additions
and
21 deletions
flatland/envs/rail_env.py
+
31
−
21
View file @
172ac2c0
...
...
@@ -15,7 +15,7 @@ from flatland.core.grid.grid4 import Grid4TransitionsEnum, Grid4Transitions
from
flatland.core.grid.grid4_utils
import
get_new_position
from
flatland.core.grid.grid_utils
import
IntVector2D
from
flatland.core.transition_map
import
GridTransitionMap
from
flatland.envs.agent_utils
import
EnvAgent
,
RailAgentStatus
from
flatland.envs.agent_utils
import
Agent
,
EnvAgent
,
RailAgentStatus
from
flatland.envs.distance_map
import
DistanceMap
from
flatland.envs.rail_env_action
import
RailEnvActions
...
...
@@ -475,6 +475,34 @@ class RailEnv(Environment):
return
def
_handle_end_reward
(
self
,
agent
:
EnvAgent
)
->
int
:
'''
Handles end-of-episode reward for a particular agent.
Parameters
----------
agent : EnvAgent
'''
reward
=
None
# agent done? (arrival_time is not None)
if
agent
.
status
==
RailAgentStatus
.
DONE
or
agent
.
status
==
RailAgentStatus
.
DONE_REMOVED
:
# if agent arrived earlier or on time = 0
# if agent arrived later = -ve reward based on how late
reward
=
min
(
agent
.
latest_arrival
-
agent
.
arrival_time
,
0
)
# Agents not done (arrival_time is None)
else
:
# CANCELLED check (never departed)
if
(
agent
.
status
==
RailAgentStatus
.
READY_TO_DEPART
):
reward
=
-
1
*
self
.
cancellation_factor
*
\
(
agent
.
get_travel_time_on_shortest_path
(
self
.
distance_map
)
+
0
)
# 0 replaced with buffer
# Departed but never reached
if
(
agent
.
status
==
RailAgentStatus
.
ACTIVE
):
reward
=
agent
.
get_current_delay
(
self
.
_elapsed_steps
,
self
.
distance_map
)
return
reward
def
step
(
self
,
action_dict_
:
Dict
[
int
,
RailEnvActions
]):
"""
Updates rewards for the agents at a step.
...
...
@@ -564,26 +592,8 @@ class RailEnv(Environment):
for
i_agent
,
agent
in
enumerate
(
self
.
agents
):
# agent done? (arrival_time is not None)
if
agent
.
status
==
RailAgentStatus
.
DONE
or
agent
.
status
==
RailAgentStatus
.
DONE_REMOVED
:
# if agent arrived earlier or on time = 0
# if agent arrived later = -ve reward based on how late
reward
=
min
(
agent
.
latest_arrival
-
agent
.
arrival_time
,
0
)
self
.
rewards_dict
[
i_agent
]
+=
reward
# Agents not done (arrival_time is None)
else
:
# CANCELLED check (never departed)
if
(
agent
.
status
==
RailAgentStatus
.
READY_TO_DEPART
):
reward
=
-
1
*
self
.
cancellation_factor
*
\
(
agent
.
get_travel_time_on_shortest_path
(
self
.
distance_map
)
+
0
)
# 0 replaced with buffer
self
.
rewards_dict
[
i_agent
]
+=
reward
# Departed but never reached
if
(
agent
.
status
==
RailAgentStatus
.
ACTIVE
):
reward
=
agent
.
get_current_delay
(
self
.
_elapsed_steps
,
self
.
distance_map
)
self
.
rewards_dict
[
i_agent
]
+=
reward
reward
=
self
.
_handle_end_reward
(
agent
)
self
.
rewards_dict
[
i_agent
]
+=
reward
self
.
dones
[
i_agent
]
=
True
...
...
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