Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Flatland
Flatland
Commits
29ba0119
Commit
29ba0119
authored
Sep 18, 2019
by
u214892
Browse files
#174
#33
#149
cleanup sphinx
parent
6ba4670c
Pipeline
#2055
passed with stages
in 30 minutes and 39 seconds
Changes
14
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
benchmarks/benchmark_utils.py
View file @
29ba0119
...
...
@@ -12,7 +12,7 @@ def swap_attr(obj, attr, new_val):
...
This will set obj.attr to 5 for the duration of the with: block,
restoring the old value at the end of the block. If `attr` doesn
'
t
restoring the old value at the end of the block. If `attr` doesn
`
t
exist on `obj`, it will be created and then deleted at the end of the
block.
...
...
flatland/core/env_observation_builder.py
View file @
29ba0119
...
...
@@ -14,7 +14,7 @@ class ObservationBuilder:
"""
ObservationBuilder base class.
Derived objects must implement and `observation_space
'
attribute as a tuple with the dimensions of the returned
Derived objects must implement and `observation_space
`
attribute as a tuple with the dimensions of the returned
observations.
"""
...
...
@@ -32,8 +32,8 @@ class ObservationBuilder:
def
get_many
(
self
,
handles
=
[]):
"""
Called whenever an observation has to be computed for the `env
'
environment, for each agent with handle
in the `handles
'
list.
Called whenever an observation has to be computed for the `env
`
environment, for each agent with handle
in the `handles
`
list.
Parameters
----------
...
...
@@ -44,7 +44,7 @@ class ObservationBuilder:
-------
function
A dictionary of observation structures, specific to the corresponding environment, with handles from
`handles
'
as keys.
`handles
`
as keys.
"""
observations
=
{}
for
h
in
handles
:
...
...
@@ -53,8 +53,8 @@ class ObservationBuilder:
def
get
(
self
,
handle
=
0
):
"""
Called whenever an observation has to be computed for the `env
'
environment, possibly
for each agent independently (agent id `handle
'
).
Called whenever an observation has to be computed for the `env
`
environment, possibly
for each agent independently (agent id `handle
`
).
Parameters
----------
...
...
flatland/core/grid/grid4.py
View file @
29ba0119
...
...
@@ -24,9 +24,9 @@ class Grid4Transitions(Transitions):
"""
Grid4Transitions class derived from Transitions.
Special case of `Transitions
'
over a 2D-grid (FlatLand).
Special case of `Transitions
`
over a 2D-grid (FlatLand).
Transitions are possible to neighboring cells on the grid if allowed.
GridTransitions keeps track of valid transitions supplied as `transitions
'
GridTransitions keeps track of valid transitions supplied as `transitions
`
list, each represented as a bitmap of 16 bits.
Whether a transition is allowed or not depends on which direction an agent
...
...
@@ -67,8 +67,8 @@ class Grid4Transitions(Transitions):
"""
Get the 4 possible transitions ((N,E,S,W), 4 elements tuple
if no diagonal transitions allowed) available for an agent oriented
in direction `orientation
'
and inside a cell with
transitions `cell_transition
'
.
in direction `orientation
`
and inside a cell with
transitions `cell_transition
`
.
Parameters
----------
...
...
@@ -90,9 +90,9 @@ class Grid4Transitions(Transitions):
"""
Set the possible transitions (e.g., (N,E,S,W), 4 elements tuple
if no diagonal transitions allowed) available for an agent
oriented in direction `orientation
'
and inside a cell with transitions
`cell_transition'. A new `cell_transition
'
is returned with
the specified bits replaced by `new_transitions
'
.
oriented in direction `orientation
`
and inside a cell with transitions
`cell_transition'. A new `cell_transition
`
is returned with
the specified bits replaced by `new_transitions
`
.
Parameters
----------
...
...
@@ -107,8 +107,8 @@ class Grid4Transitions(Transitions):
-------
int
An updated bitmap that replaces the original transitions validity
of `cell_transition' with `new_transitions
'
, for the appropriate
`orientation
'
.
of `cell_transition' with `new_transitions
`
, for the appropriate
`orientation
`
.
"""
mask
=
(
1
<<
((
4
-
orientation
)
*
4
))
-
(
1
<<
((
3
-
orientation
)
*
4
))
...
...
@@ -127,8 +127,8 @@ class Grid4Transitions(Transitions):
def
get_transition
(
self
,
cell_transition
,
orientation
,
direction
):
"""
Get the transition bit (1 value) that determines whether an agent
oriented in direction `orientation
'
and inside a cell with transitions
`cell_transition' can move to the cell in direction `direction
'
oriented in direction `orientation
`
and inside a cell with transitions
`cell_transition' can move to the cell in direction `direction
`
relative to the current cell.
Parameters
...
...
@@ -151,8 +151,8 @@ class Grid4Transitions(Transitions):
def
set_transition
(
self
,
cell_transition
,
orientation
,
direction
,
new_transition
,
remove_deadends
=
False
):
"""
Set the transition bit (1 value) that determines whether an agent
oriented in direction `orientation
'
and inside a cell with transitions
`cell_transition' can move to the cell in direction `direction
'
oriented in direction `orientation
`
and inside a cell with transitions
`cell_transition' can move to the cell in direction `direction
`
relative to the current cell.
Parameters
...
...
@@ -171,8 +171,8 @@ class Grid4Transitions(Transitions):
-------
int
An updated bitmap that replaces the original transitions validity
of `cell_transition' with `new_transitions
'
, for the appropriate
`orientation
'
.
of `cell_transition' with `new_transitions
`
, for the appropriate
`orientation
`
.
"""
if
new_transition
:
...
...
@@ -196,7 +196,7 @@ class Grid4Transitions(Transitions):
16 bits used to encode the valid transitions for a cell.
rotation : int
Angle by which to clock-wise rotate the transition bits in
`cell_transition
'
by. I.e., rotation={0, 90, 180, 270} degrees.
`cell_transition
`
by. I.e., rotation={0, 90, 180, 270} degrees.
Returns
-------
...
...
flatland/core/grid/grid8.py
View file @
29ba0119
...
...
@@ -20,9 +20,9 @@ class Grid8Transitions(Transitions):
"""
Grid8Transitions class derived from Transitions.
Special case of `Transitions
'
over a 2D-grid (FlatLand).
Special case of `Transitions
`
over a 2D-grid (FlatLand).
Transitions are possible to neighboring cells on the grid if allowed.
GridTransitions keeps track of valid transitions supplied as `transitions
'
GridTransitions keeps track of valid transitions supplied as `transitions
`
list, each represented as a bitmap of 64 bits.
0=North, 1=North-East, etc.
...
...
@@ -82,8 +82,8 @@ class Grid8Transitions(Transitions):
-------
int
An updated bitmap that replaces the original transitions validity
of `cell_transition' with `new_transitions
'
, for the appropriate
`orientation
'
.
of `cell_transition' with `new_transitions
`
, for the appropriate
`orientation
`
.
"""
mask
=
(
1
<<
((
8
-
orientation
)
*
8
))
-
(
1
<<
((
7
-
orientation
)
*
8
))
...
...
@@ -106,8 +106,8 @@ class Grid8Transitions(Transitions):
def
get_transition
(
self
,
cell_transition
,
orientation
,
direction
):
"""
Get the transition bit (1 value) that determines whether an agent
oriented in direction `orientation
'
and inside a cell with transitions
`cell_transition' can move to the cell in direction `direction
'
oriented in direction `orientation
`
and inside a cell with transitions
`cell_transition' can move to the cell in direction `direction
`
relative to the current cell.
Parameters
...
...
@@ -131,8 +131,8 @@ class Grid8Transitions(Transitions):
"""
Set the transition bit (1 value) that determines whether an agent
oriented in direction `orientation
'
and inside a cell with transitions
`cell_transition' can move to the cell in direction `direction
'
oriented in direction `orientation
`
and inside a cell with transitions
`cell_transition' can move to the cell in direction `direction
`
relative to the current cell.
Parameters
...
...
@@ -150,8 +150,8 @@ class Grid8Transitions(Transitions):
-------
int
An updated bitmap that replaces the original transitions validity
of `cell_transition' with `new_transitions
'
, for the appropriate
`orientation
'
.
of `cell_transition' with `new_transitions
`
, for the appropriate
`orientation
`
.
"""
if
new_transition
:
...
...
@@ -172,7 +172,7 @@ class Grid8Transitions(Transitions):
64 bits used to encode the valid transitions for a cell.
rotation : int
Angle by which to clock-wise rotate the transition bits in
`cell_transition
'
by. I.e., rotation={0, 45, 90, 135, 180,
`cell_transition
`
by. I.e., rotation={0, 45, 90, 135, 180,
225, 270, 315} degrees.
Returns
...
...
flatland/core/grid/rail_env_grid.py
View file @
29ba0119
...
...
@@ -3,7 +3,7 @@ from flatland.core.grid.grid4 import Grid4Transitions
class
RailEnvTransitions
(
Grid4Transitions
):
"""
Special case of `GridTransitions
'
over a 2D-grid, with a pre-defined set
Special case of `GridTransitions
`
over a 2D-grid, with a pre-defined set
of transitions mimicking the types of real Swiss rail connections.
--------------------------------------------------------------------------
...
...
flatland/core/transition_map.py
View file @
29ba0119
...
...
@@ -23,7 +23,7 @@ class TransitionMap:
def
get_transitions
(
self
,
cell_id
):
"""
Return a tuple of transitions available in a cell specified by
`cell_id
'
(e.g., a tuple of size of the maximum number of transitions,
`cell_id
`
(e.g., a tuple of size of the maximum number of transitions,
with values 0 or 1, or potentially in between,
for stochastic transitions).
...
...
@@ -43,8 +43,8 @@ class TransitionMap:
def
set_transitions
(
self
,
cell_id
,
new_transitions
):
"""
Replaces the available transitions in cell `cell_id
'
with the tuple
`new_transitions'. `new_transitions
'
must have
Replaces the available transitions in cell `cell_id
`
with the tuple
`new_transitions'. `new_transitions
`
must have
one element for each possible transition.
Parameters
...
...
@@ -60,7 +60,7 @@ class TransitionMap:
def
get_transition
(
self
,
cell_id
,
transition_index
):
"""
Return the status of whether an agent in cell `cell_id
'
can perform a
Return the status of whether an agent in cell `cell_id
`
can perform a
movement along transition `transition_index (e.g., the NESW direction
of movement, for agents on a grid).
...
...
@@ -85,8 +85,8 @@ class TransitionMap:
def
set_transition
(
self
,
cell_id
,
transition_index
,
new_transition
):
"""
Replaces the validity of transition to `transition_index
'
in cell
`cell_id' with the new `new_transition
'
.
Replaces the validity of transition to `transition_index
`
in cell
`cell_id' with the new `new_transition
`
.
Parameters
...
...
@@ -156,7 +156,7 @@ class GridTransitionMap(TransitionMap):
def
get_transitions
(
self
,
row
,
column
,
orientation
):
"""
Return a tuple of transitions available in a cell specified by
`cell_id
'
(e.g., a tuple of size of the maximum number of transitions,
`cell_id
`
(e.g., a tuple of size of the maximum number of transitions,
with values 0 or 1, or potentially in between,
for stochastic transitions).
...
...
@@ -178,8 +178,8 @@ class GridTransitionMap(TransitionMap):
def
set_transitions
(
self
,
cell_id
,
new_transitions
):
"""
Replaces the available transitions in cell `cell_id
'
with the tuple
`new_transitions'. `new_transitions
'
must have
Replaces the available transitions in cell `cell_id
`
with the tuple
`new_transitions'. `new_transitions
`
must have
one element for each possible transition.
Parameters
...
...
@@ -204,7 +204,7 @@ class GridTransitionMap(TransitionMap):
def
get_transition
(
self
,
cell_id
,
transition_index
):
"""
Return the status of whether an agent in cell `cell_id
'
can perform a
Return the status of whether an agent in cell `cell_id
`
can perform a
movement along transition `transition_index (e.g., the NESW direction
of movement, for agents on a grid).
...
...
@@ -232,8 +232,8 @@ class GridTransitionMap(TransitionMap):
def
set_transition
(
self
,
cell_id
,
transition_index
,
new_transition
,
remove_deadends
=
False
):
"""
Replaces the validity of transition to `transition_index
'
in cell
`cell_id' with the new `new_transition
'
.
Replaces the validity of transition to `transition_index
`
in cell
`cell_id' with the new `new_transition
`
.
Parameters
...
...
@@ -261,7 +261,7 @@ class GridTransitionMap(TransitionMap):
def
save_transition_map
(
self
,
filename
):
"""
Save the transitions grid as `filename
'
, in npy format.
Save the transitions grid as `filename
`
, in npy format.
Parameters
----------
...
...
@@ -273,9 +273,9 @@ class GridTransitionMap(TransitionMap):
def
load_transition_map
(
self
,
package
,
resource
):
"""
Load the transitions grid from `filename
'
(npy format).
Load the transitions grid from `filename
`
(npy format).
The load function only updates the transitions grid, and possibly width and height, but the object has to be
initialized with the correct `transitions
'
object anyway.
initialized with the correct `transitions
`
object anyway.
Parameters
----------
...
...
@@ -285,7 +285,7 @@ class GridTransitionMap(TransitionMap):
Name of the file from which to load the transitions grid within the package.
override_gridsize : bool
If override_gridsize=True, the width and height of the GridTransitionMap object are replaced with the size
of the map loaded from `filename
'
. If override_gridsize=False, the transitions grid is either cropped (if
of the map loaded from `filename
`
. If override_gridsize=False, the transitions grid is either cropped (if
the grid size is larger than (height,width) ) or padded with zeros (if the grid size is smaller than
(height,width) )
...
...
flatland/core/transitions.py
View file @
29ba0119
...
...
@@ -12,7 +12,7 @@ class Transitions:
Generic class that implements checks to control whether a
certain transition is allowed (agent facing a direction
`orientation' and moving into direction `orientation
'
)
`orientation' and moving into direction `orientation
`
)
"""
def
get_type
(
self
):
...
...
@@ -21,7 +21,7 @@ class Transitions:
def
get_transitions
(
self
,
cell_transition
,
orientation
):
"""
Return a tuple of transitions available in a cell specified by
`cell_transition' for an agent facing direction `orientation
'
`cell_transition' for an agent facing direction `orientation
`
(e.g., a tuple of size of the maximum number of transitions,
with values 0 or 1, or potentially in between,
for stochastic transitions).
...
...
@@ -45,9 +45,9 @@ class Transitions:
def
set_transitions
(
self
,
cell_transition
,
orientation
,
new_transitions
):
"""
Return a `cell_transition
'
specification where the transitions
available for an agent facing direction `orientation
'
are replaced
with the tuple `new_transitions'. `new_orientations
'
must have
Return a `cell_transition
`
specification where the transitions
available for an agent facing direction `orientation
`
are replaced
with the tuple `new_transitions'. `new_orientations
`
must have
one element for each possible transition.
Parameters
...
...
@@ -65,8 +65,8 @@ class Transitions:
-------
[cell-content]
An updated class-specific object that replaces the original
transitions validity of `cell_transition' with `new_transitions
'
,
for the appropriate `orientation
'
.
transitions validity of `cell_transition' with `new_transitions
`
,
for the appropriate `orientation
`
.
"""
raise
NotImplementedError
()
...
...
@@ -74,8 +74,8 @@ class Transitions:
def
get_transition
(
self
,
cell_transition
,
orientation
,
direction
):
"""
Return the status of whether an agent oriented in directions
`orientation' and inside a cell with transitions `cell_transition
'
can move to the cell in direction `direction
'
relative
`orientation' and inside a cell with transitions `cell_transition
`
can move to the cell in direction `direction
`
relative
to the current cell.
Parameters
...
...
@@ -101,11 +101,11 @@ class Transitions:
def
set_transition
(
self
,
cell_transition
,
orientation
,
direction
,
new_transition
):
"""
Return a `cell_transition
'
specification where the status of
whether an agent oriented in direction `orientation
'
and inside
a cell with transitions `cell_transition
'
can move to the cell
in direction `direction
'
relative to the current cell is set
to `new_transition
'
.
Return a `cell_transition
`
specification where the status of
whether an agent oriented in direction `orientation
`
and inside
a cell with transitions `cell_transition
`
can move to the cell
in direction `direction
`
relative to the current cell is set
to `new_transition
`
.
Parameters
----------
...
...
@@ -125,8 +125,8 @@ class Transitions:
-------
[cell-content]
An updated class-specific object that replaces the original
transitions validity of `cell_transition' with `new_transitions
'
,
for the appropriate `orientation' to `direction
'
.
transitions validity of `cell_transition' with `new_transitions
`
,
for the appropriate `orientation' to `direction
`
.
"""
raise
NotImplementedError
()
...
...
flatland/envs/observations.py
View file @
29ba0119
...
...
@@ -85,10 +85,10 @@ class TreeObsForRailEnv(ObservationBuilder):
# Fill in the (up to) 4 neighboring nodes
# direction is the direction of movement, meaning that at least a possible orientation of an agent
# in cell (row,col) allows a movement in direction `direction
'
# in cell (row,col) allows a movement in direction `direction
`
nodes_queue
=
deque
(
self
.
_get_and_update_neighbors
(
position
,
target_nr
,
0
,
enforce_target_direction
=-
1
))
# BFS from target `position
'
to all the reachable nodes in the grid
# BFS from target `position
`
to all the reachable nodes in the grid
# Stop the search if the target position is re-visited, in any direction
visited
=
{(
position
[
0
],
position
[
1
],
0
),
(
position
[
0
],
position
[
1
],
1
),
(
position
[
0
],
position
[
1
],
2
),
(
position
[
0
],
position
[
1
],
3
)}
...
...
@@ -124,7 +124,7 @@ class TreeObsForRailEnv(ObservationBuilder):
possible_directions
=
[
0
,
1
,
2
,
3
]
if
enforce_target_direction
>=
0
:
# The agent must land into the current cell with orientation `enforce_target_direction
'
.
# The agent must land into the current cell with orientation `enforce_target_direction
`
.
# This is only possible if the agent has arrived from the cell in the opposite direction!
possible_directions
=
[(
enforce_target_direction
+
2
)
%
4
]
...
...
@@ -137,7 +137,7 @@ class TreeObsForRailEnv(ObservationBuilder):
# Check all possible transitions in new_cell
for
agent_orientation
in
range
(
4
):
# Is a transition along movement `desired_movement_from_new_cell
'
to the current cell possible?
# Is a transition along movement `desired_movement_from_new_cell
`
to the current cell possible?
is_valid
=
self
.
env
.
rail
.
get_transition
((
new_cell
[
0
],
new_cell
[
1
],
agent_orientation
),
desired_movement_from_new_cell
)
...
...
@@ -170,8 +170,8 @@ class TreeObsForRailEnv(ObservationBuilder):
def
get_many
(
self
,
handles
=
None
):
"""
Called whenever an observation has to be computed for the `env
'
environment, for each agent with handle
in the `handles
'
list.
Called whenever an observation has to be computed for the `env
`
environment, for each agent with handle
in the `handles
`
list.
"""
if
handles
is
None
:
...
...
@@ -199,7 +199,7 @@ class TreeObsForRailEnv(ObservationBuilder):
def
get
(
self
,
handle
):
"""
Computes the current observation for agent `handle
'
in env
Computes the current observation for agent `handle
`
in env
The observation vector is composed of 4 sequential parts, corresponding to data from the up to 4 possible
movements in a RailEnv (up to because only a subset of possible transitions are allowed in RailEnv).
...
...
@@ -460,7 +460,7 @@ class TreeObsForRailEnv(ObservationBuilder):
last_is_dead_end
=
True
if
not
last_is_dead_end
:
# Keep walking through the tree along `direction
'
# Keep walking through the tree along `direction
`
exploring
=
True
# convert one-hot encoding to 0,1,2,3
direction
=
np
.
argmax
(
cell_transitions
)
...
...
@@ -479,7 +479,7 @@ class TreeObsForRailEnv(ObservationBuilder):
last_is_terminal
=
True
break
# `position
'
is either a terminal node or a switch
# `position
`
is either a terminal node or a switch
# #############################
# #############################
...
...
@@ -749,8 +749,8 @@ class LocalObsForRailEnv(ObservationBuilder):
def
get_many
(
self
,
handles
=
None
):
"""
Called whenever an observation has to be computed for the `env
'
environment, for each agent with handle
in the `handles
'
list.
Called whenever an observation has to be computed for the `env
`
environment, for each agent with handle
in the `handles
`
list.
"""
observations
=
{}
...
...
flatland/envs/rail_generators.py
View file @
29ba0119
...
...
@@ -250,7 +250,7 @@ def rail_from_grid_transition_map(rail_map) -> RailGenerator:
Returns
-------
function
Generator function that always returns the given `rail_map
'
object.
Generator function that always returns the given `rail_map
`
object.
"""
def
generator
(
width
:
int
,
height
:
int
,
num_agents
:
int
,
num_resets
:
int
=
0
)
->
RailGeneratorProduct
:
...
...
flatland/envs/schedule_generators.py
View file @
29ba0119
...
...
@@ -117,7 +117,7 @@ def sparse_schedule_generator(speed_ratio_map: Mapping[float, float] = None) ->
def
random_schedule_generator
(
speed_ratio_map
:
Mapping
[
float
,
float
]
=
None
)
->
ScheduleGenerator
:
"""
Given a `rail
'
GridTransitionMap, return a random placement of agents (initial position, direction and target).
Given a `rail
`
GridTransitionMap, return a random placement of agents (initial position, direction and target).
Parameters
----------
...
...
flatland/utils/rendertools.py
View file @
29ba0119
...
...
@@ -560,7 +560,7 @@ class RenderTool(object):
position
=
agent
.
position
direction
=
agent
.
direction
for
possible_directions
in
range
(
4
):
# Is a transition along movement `desired_movement_from_new_cell
'
to the current cell possible?
# Is a transition along movement `desired_movement_from_new_cell
`
to the current cell possible?
isValid
=
env
.
rail
.
get_transition
((
*
agent
.
position
,
agent
.
direction
),
possible_directions
)
if
isValid
:
direction
=
possible_directions
...
...
make_docs.py
View file @
29ba0119
...
...
@@ -19,7 +19,9 @@ def remove_exists(filename):
# clean docs config and html files, and rebuild everything
remove_exists
(
'docs/flatland*.rst'
)
# wildcards do not work under Windows
for
file
in
glob
.
glob
(
r
'./docs/flatland*.rst'
):
remove_exists
(
file
)
remove_exists
(
'docs/modules.rst'
)
# copy md files from root folder into docs folder
...
...
tests/test_flatland_envs_observations.py
View file @
29ba0119
...
...
@@ -58,7 +58,7 @@ def _step_along_shortest_path(env, obs_builder, rail):
# Check all possible transitions in new_cell
for
agent_orientation
in
range
(
4
):
# Is a transition along movement `entry_direction
'
to the neighbour possible?
# Is a transition along movement `entry_direction
`
to the neighbour possible?
is_valid
=
obs_builder
.
env
.
rail
.
get_transition
((
neighbour
[
0
],
neighbour
[
1
],
agent_orientation
),
desired_movement_from_new_cell
)
if
is_valid
:
...
...
tox.ini
View file @
29ba0119
...
...
@@ -21,8 +21,6 @@ commands =
flake8
flatland
tests
examples
benchmarks
[testenv:docs]
; TODO https://gitlab.aicrowd.com/flatland/flatland/issues/33 docs requires make installed, therefore exclude Windows
platform
=
linux|linux2|darwin
; use python3.6 because of incompatibility under Windows of the pycairo installed through conda for py37
basepython
=
python3.6
whitelist_externals
=
make
...
...
@@ -46,8 +44,6 @@ commands =
make
docs
[testenv:coverage]
; TODO https://gitlab.aicrowd.com/flatland/flatland/issues/33 coverage requires make, therefore exclude Windows
platform
=
linux|linux2|darwin
; use python3.6 because of incompatibility under Windows of the pycairo installed through conda for py37
basepython
=
python3.6
whitelist_externals
=
make
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment